noop.go 2.7 KB

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