blob: 0d32f692c4104649aa3f5b9b40b0d59974fb2a95 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001package opentracing
2
3import "github.com/opentracing/opentracing-go/log"
4
5// A NoopTracer is a trivial, minimum overhead implementation of Tracer
6// for which all operations are no-ops.
7//
8// The primary use of this implementation is in libraries, such as RPC
9// frameworks, that make tracing an optional feature controlled by the
10// end user. A no-op implementation allows said libraries to use it
11// as the default Tracer and to write instrumentation that does
12// not need to keep checking if the tracer instance is nil.
13//
14// For the same reason, the NoopTracer is the default "global" tracer
15// (see GlobalTracer and SetGlobalTracer functions).
16//
17// WARNING: NoopTracer does not support baggage propagation.
18type NoopTracer struct{}
19
20type noopSpan struct{}
21type noopSpanContext struct{}
22
23var (
24 defaultNoopSpanContext = noopSpanContext{}
25 defaultNoopSpan = noopSpan{}
26 defaultNoopTracer = NoopTracer{}
27)
28
29const (
30 emptyString = ""
31)
32
33// noopSpanContext:
34func (n noopSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {}
35
36// noopSpan:
37func (n noopSpan) Context() SpanContext { return defaultNoopSpanContext }
38func (n noopSpan) SetBaggageItem(key, val string) Span { return defaultNoopSpan }
39func (n noopSpan) BaggageItem(key string) string { return emptyString }
40func (n noopSpan) SetTag(key string, value interface{}) Span { return n }
41func (n noopSpan) LogFields(fields ...log.Field) {}
42func (n noopSpan) LogKV(keyVals ...interface{}) {}
43func (n noopSpan) Finish() {}
44func (n noopSpan) FinishWithOptions(opts FinishOptions) {}
45func (n noopSpan) SetOperationName(operationName string) Span { return n }
46func (n noopSpan) Tracer() Tracer { return defaultNoopTracer }
47func (n noopSpan) LogEvent(event string) {}
48func (n noopSpan) LogEventWithPayload(event string, payload interface{}) {}
49func (n noopSpan) Log(data LogData) {}
50
51// StartSpan belongs to the Tracer interface.
52func (n NoopTracer) StartSpan(operationName string, opts ...StartSpanOption) Span {
53 return defaultNoopSpan
54}
55
56// Inject belongs to the Tracer interface.
57func (n NoopTracer) Inject(sp SpanContext, format interface{}, carrier interface{}) error {
58 return nil
59}
60
61// Extract belongs to the Tracer interface.
62func (n NoopTracer) Extract(format interface{}, carrier interface{}) (SpanContext, error) {
63 return nil, ErrSpanContextNotFound
64}