noop.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. // Package noop provides an implementation of the OpenTelemetry trace API that
  4. // produces no telemetry and minimizes used computation resources.
  5. //
  6. // Using this package to implement the OpenTelemetry trace API will effectively
  7. // disable OpenTelemetry.
  8. //
  9. // This implementation can be embedded in other implementations of the
  10. // OpenTelemetry trace API. Doing so will mean the implementation defaults to
  11. // no operation for methods it does not implement.
  12. package noop // import "go.opentelemetry.io/otel/trace/noop"
  13. import (
  14. "context"
  15. "go.opentelemetry.io/otel/attribute"
  16. "go.opentelemetry.io/otel/codes"
  17. "go.opentelemetry.io/otel/trace"
  18. "go.opentelemetry.io/otel/trace/embedded"
  19. )
  20. var (
  21. // Compile-time check this implements the OpenTelemetry API.
  22. _ trace.TracerProvider = TracerProvider{}
  23. _ trace.Tracer = Tracer{}
  24. _ trace.Span = Span{}
  25. )
  26. // TracerProvider is an OpenTelemetry No-Op TracerProvider.
  27. type TracerProvider struct{ embedded.TracerProvider }
  28. // NewTracerProvider returns a TracerProvider that does not record any telemetry.
  29. func NewTracerProvider() TracerProvider {
  30. return TracerProvider{}
  31. }
  32. // Tracer returns an OpenTelemetry Tracer that does not record any telemetry.
  33. func (TracerProvider) Tracer(string, ...trace.TracerOption) trace.Tracer {
  34. return Tracer{}
  35. }
  36. // Tracer is an OpenTelemetry No-Op Tracer.
  37. type Tracer struct{ embedded.Tracer }
  38. // Start creates a span. The created span will be set in a child context of ctx
  39. // and returned with the span.
  40. //
  41. // If ctx contains a span context, the returned span will also contain that
  42. // span context. If the span context in ctx is for a non-recording span, that
  43. // span instance will be returned directly.
  44. func (Tracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
  45. span := trace.SpanFromContext(ctx)
  46. // If the parent context contains a non-zero span context, that span
  47. // context needs to be returned as a non-recording span
  48. // (https://github.com/open-telemetry/opentelemetry-specification/blob/3a1dde966a4ce87cce5adf464359fe369741bbea/specification/trace/api.md#behavior-of-the-api-in-the-absence-of-an-installed-sdk).
  49. var zeroSC trace.SpanContext
  50. if sc := span.SpanContext(); !sc.Equal(zeroSC) {
  51. if !span.IsRecording() {
  52. // If the span is not recording return it directly.
  53. return ctx, span
  54. }
  55. // Otherwise, return the span context needs in a non-recording span.
  56. span = Span{sc: sc}
  57. } else {
  58. // No parent, return a No-Op span with an empty span context.
  59. span = noopSpanInstance
  60. }
  61. return trace.ContextWithSpan(ctx, span), span
  62. }
  63. var noopSpanInstance trace.Span = Span{}
  64. // Span is an OpenTelemetry No-Op Span.
  65. type Span struct {
  66. embedded.Span
  67. sc trace.SpanContext
  68. }
  69. // SpanContext returns an empty span context.
  70. func (s Span) SpanContext() trace.SpanContext { return s.sc }
  71. // IsRecording always returns false.
  72. func (Span) IsRecording() bool { return false }
  73. // SetStatus does nothing.
  74. func (Span) SetStatus(codes.Code, string) {}
  75. // SetAttributes does nothing.
  76. func (Span) SetAttributes(...attribute.KeyValue) {}
  77. // End does nothing.
  78. func (Span) End(...trace.SpanEndOption) {}
  79. // RecordError does nothing.
  80. func (Span) RecordError(error, ...trace.EventOption) {}
  81. // AddEvent does nothing.
  82. func (Span) AddEvent(string, ...trace.EventOption) {}
  83. // AddLink does nothing.
  84. func (Span) AddLink(trace.Link) {}
  85. // SetName does nothing.
  86. func (Span) SetName(string) {}
  87. // TracerProvider returns a No-Op TracerProvider.
  88. func (Span) TracerProvider() trace.TracerProvider { return TracerProvider{} }