noop.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 trace // import "go.opentelemetry.io/otel/trace"
  15. import (
  16. "context"
  17. "go.opentelemetry.io/otel/attribute"
  18. "go.opentelemetry.io/otel/codes"
  19. "go.opentelemetry.io/otel/trace/embedded"
  20. )
  21. // NewNoopTracerProvider returns an implementation of TracerProvider that
  22. // performs no operations. The Tracer and Spans created from the returned
  23. // TracerProvider also perform no operations.
  24. //
  25. // Deprecated: Use [go.opentelemetry.io/otel/trace/noop.NewTracerProvider]
  26. // instead.
  27. func NewNoopTracerProvider() TracerProvider {
  28. return noopTracerProvider{}
  29. }
  30. type noopTracerProvider struct{ embedded.TracerProvider }
  31. var _ TracerProvider = noopTracerProvider{}
  32. // Tracer returns noop implementation of Tracer.
  33. func (p noopTracerProvider) Tracer(string, ...TracerOption) Tracer {
  34. return noopTracer{}
  35. }
  36. // noopTracer is an implementation of Tracer that performs no operations.
  37. type noopTracer struct{ embedded.Tracer }
  38. var _ Tracer = noopTracer{}
  39. // Start carries forward a non-recording Span, if one is present in the context, otherwise it
  40. // creates a no-op Span.
  41. func (t noopTracer) Start(ctx context.Context, name string, _ ...SpanStartOption) (context.Context, Span) {
  42. span := SpanFromContext(ctx)
  43. if _, ok := span.(nonRecordingSpan); !ok {
  44. // span is likely already a noopSpan, but let's be sure
  45. span = noopSpan{}
  46. }
  47. return ContextWithSpan(ctx, span), span
  48. }
  49. // noopSpan is an implementation of Span that performs no operations.
  50. type noopSpan struct{ embedded.Span }
  51. var _ Span = noopSpan{}
  52. // SpanContext returns an empty span context.
  53. func (noopSpan) SpanContext() SpanContext { return SpanContext{} }
  54. // IsRecording always returns false.
  55. func (noopSpan) IsRecording() bool { return false }
  56. // SetStatus does nothing.
  57. func (noopSpan) SetStatus(codes.Code, string) {}
  58. // SetError does nothing.
  59. func (noopSpan) SetError(bool) {}
  60. // SetAttributes does nothing.
  61. func (noopSpan) SetAttributes(...attribute.KeyValue) {}
  62. // End does nothing.
  63. func (noopSpan) End(...SpanEndOption) {}
  64. // RecordError does nothing.
  65. func (noopSpan) RecordError(error, ...EventOption) {}
  66. // AddEvent does nothing.
  67. func (noopSpan) AddEvent(string, ...EventOption) {}
  68. // SetName does nothing.
  69. func (noopSpan) SetName(string) {}
  70. // TracerProvider returns a no-op TracerProvider.
  71. func (noopSpan) TracerProvider() TracerProvider { return noopTracerProvider{} }