exporter.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package tracetest is a testing helper package for the SDK. User can
  15. // configure no-op or in-memory exporters to verify different SDK behaviors or
  16. // custom instrumentation.
  17. package tracetest // import "go.opentelemetry.io/otel/sdk/trace/tracetest"
  18. import (
  19. "context"
  20. "sync"
  21. "go.opentelemetry.io/otel/sdk/trace"
  22. )
  23. var _ trace.SpanExporter = (*NoopExporter)(nil)
  24. // NewNoopExporter returns a new no-op exporter.
  25. func NewNoopExporter() *NoopExporter {
  26. return new(NoopExporter)
  27. }
  28. // NoopExporter is an exporter that drops all received spans and performs no
  29. // action.
  30. type NoopExporter struct{}
  31. // ExportSpans handles export of spans by dropping them.
  32. func (nsb *NoopExporter) ExportSpans(context.Context, []trace.ReadOnlySpan) error { return nil }
  33. // Shutdown stops the exporter by doing nothing.
  34. func (nsb *NoopExporter) Shutdown(context.Context) error { return nil }
  35. var _ trace.SpanExporter = (*InMemoryExporter)(nil)
  36. // NewInMemoryExporter returns a new InMemoryExporter.
  37. func NewInMemoryExporter() *InMemoryExporter {
  38. return new(InMemoryExporter)
  39. }
  40. // InMemoryExporter is an exporter that stores all received spans in-memory.
  41. type InMemoryExporter struct {
  42. mu sync.Mutex
  43. ss SpanStubs
  44. }
  45. // ExportSpans handles export of spans by storing them in memory.
  46. func (imsb *InMemoryExporter) ExportSpans(_ context.Context, spans []trace.ReadOnlySpan) error {
  47. imsb.mu.Lock()
  48. defer imsb.mu.Unlock()
  49. imsb.ss = append(imsb.ss, SpanStubsFromReadOnlySpans(spans)...)
  50. return nil
  51. }
  52. // Shutdown stops the exporter by clearing spans held in memory.
  53. func (imsb *InMemoryExporter) Shutdown(context.Context) error {
  54. imsb.Reset()
  55. return nil
  56. }
  57. // Reset the current in-memory storage.
  58. func (imsb *InMemoryExporter) Reset() {
  59. imsb.mu.Lock()
  60. defer imsb.mu.Unlock()
  61. imsb.ss = nil
  62. }
  63. // GetSpans returns the current in-memory stored spans.
  64. func (imsb *InMemoryExporter) GetSpans() SpanStubs {
  65. imsb.mu.Lock()
  66. defer imsb.mu.Unlock()
  67. ret := make(SpanStubs, len(imsb.ss))
  68. copy(ret, imsb.ss)
  69. return ret
  70. }