noop.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package trace // import "go.opentelemetry.io/otel/trace"
  4. import (
  5. "context"
  6. "go.opentelemetry.io/otel/attribute"
  7. "go.opentelemetry.io/otel/codes"
  8. "go.opentelemetry.io/otel/trace/embedded"
  9. )
  10. // NewNoopTracerProvider returns an implementation of TracerProvider that
  11. // performs no operations. The Tracer and Spans created from the returned
  12. // TracerProvider also perform no operations.
  13. //
  14. // Deprecated: Use [go.opentelemetry.io/otel/trace/noop.NewTracerProvider]
  15. // instead.
  16. func NewNoopTracerProvider() TracerProvider {
  17. return noopTracerProvider{}
  18. }
  19. type noopTracerProvider struct{ embedded.TracerProvider }
  20. var _ TracerProvider = noopTracerProvider{}
  21. // Tracer returns noop implementation of Tracer.
  22. func (noopTracerProvider) Tracer(string, ...TracerOption) Tracer {
  23. return noopTracer{}
  24. }
  25. // noopTracer is an implementation of Tracer that performs no operations.
  26. type noopTracer struct{ embedded.Tracer }
  27. var _ Tracer = noopTracer{}
  28. // Start carries forward a non-recording Span, if one is present in the context, otherwise it
  29. // creates a no-op Span.
  30. func (noopTracer) Start(ctx context.Context, _ string, _ ...SpanStartOption) (context.Context, Span) {
  31. span := SpanFromContext(ctx)
  32. if _, ok := span.(nonRecordingSpan); !ok {
  33. // span is likely already a noopSpan, but let's be sure
  34. span = noopSpanInstance
  35. }
  36. return ContextWithSpan(ctx, span), span
  37. }
  38. // noopSpan is an implementation of Span that performs no operations.
  39. type noopSpan struct{ embedded.Span }
  40. var noopSpanInstance Span = noopSpan{}
  41. // SpanContext returns an empty span context.
  42. func (noopSpan) SpanContext() SpanContext { return SpanContext{} }
  43. // IsRecording always returns false.
  44. func (noopSpan) IsRecording() bool { return false }
  45. // SetStatus does nothing.
  46. func (noopSpan) SetStatus(codes.Code, string) {}
  47. // SetError does nothing.
  48. func (noopSpan) SetError(bool) {}
  49. // SetAttributes does nothing.
  50. func (noopSpan) SetAttributes(...attribute.KeyValue) {}
  51. // End does nothing.
  52. func (noopSpan) End(...SpanEndOption) {}
  53. // RecordError does nothing.
  54. func (noopSpan) RecordError(error, ...EventOption) {}
  55. // AddEvent does nothing.
  56. func (noopSpan) AddEvent(string, ...EventOption) {}
  57. // AddLink does nothing.
  58. func (noopSpan) AddLink(Link) {}
  59. // SetName does nothing.
  60. func (noopSpan) SetName(string) {}
  61. // TracerProvider returns a no-op TracerProvider.
  62. func (s noopSpan) TracerProvider() TracerProvider {
  63. return s.tracerProvider(autoInstEnabled)
  64. }
  65. // autoInstEnabled defines if the auto-instrumentation SDK is enabled.
  66. //
  67. // The auto-instrumentation is expected to overwrite this value to true when it
  68. // attaches to the process.
  69. var autoInstEnabled = new(bool)
  70. // tracerProvider return a noopTracerProvider if autoEnabled is false,
  71. // otherwise it will return a TracerProvider from the sdk package used in
  72. // auto-instrumentation.
  73. //
  74. //go:noinline
  75. func (noopSpan) tracerProvider(autoEnabled *bool) TracerProvider {
  76. if *autoEnabled {
  77. return newAutoTracerProvider()
  78. }
  79. return noopTracerProvider{}
  80. }