blob: f9b680a213de908ccc3a3a67d69eb09ca44ead1a [file] [log] [blame]
Rohan Agrawalc32d9932020-06-15 11:01:47 +00001package 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 (
Maninder12b909f2020-10-23 14:23:36 +053024 defaultNoopSpanContext SpanContext = noopSpanContext{}
25 defaultNoopSpan Span = noopSpan{}
26 defaultNoopTracer Tracer = NoopTracer{}
Rohan Agrawalc32d9932020-06-15 11:01:47 +000027)
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 }
Maninder12b909f2020-10-23 14:23:36 +053038func (n noopSpan) SetBaggageItem(key, val string) Span { return n }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000039func (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}