Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 1 | package opentracing |
| 2 | |
| 3 | import "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. |
| 18 | type NoopTracer struct{} |
| 19 | |
| 20 | type noopSpan struct{} |
| 21 | type noopSpanContext struct{} |
| 22 | |
| 23 | var ( |
| 24 | defaultNoopSpanContext = noopSpanContext{} |
| 25 | defaultNoopSpan = noopSpan{} |
| 26 | defaultNoopTracer = NoopTracer{} |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | emptyString = "" |
| 31 | ) |
| 32 | |
| 33 | // noopSpanContext: |
| 34 | func (n noopSpanContext) ForeachBaggageItem(handler func(k, v string) bool) {} |
| 35 | |
| 36 | // noopSpan: |
| 37 | func (n noopSpan) Context() SpanContext { return defaultNoopSpanContext } |
| 38 | func (n noopSpan) SetBaggageItem(key, val string) Span { return defaultNoopSpan } |
| 39 | func (n noopSpan) BaggageItem(key string) string { return emptyString } |
| 40 | func (n noopSpan) SetTag(key string, value interface{}) Span { return n } |
| 41 | func (n noopSpan) LogFields(fields ...log.Field) {} |
| 42 | func (n noopSpan) LogKV(keyVals ...interface{}) {} |
| 43 | func (n noopSpan) Finish() {} |
| 44 | func (n noopSpan) FinishWithOptions(opts FinishOptions) {} |
| 45 | func (n noopSpan) SetOperationName(operationName string) Span { return n } |
| 46 | func (n noopSpan) Tracer() Tracer { return defaultNoopTracer } |
| 47 | func (n noopSpan) LogEvent(event string) {} |
| 48 | func (n noopSpan) LogEventWithPayload(event string, payload interface{}) {} |
| 49 | func (n noopSpan) Log(data LogData) {} |
| 50 | |
| 51 | // StartSpan belongs to the Tracer interface. |
| 52 | func (n NoopTracer) StartSpan(operationName string, opts ...StartSpanOption) Span { |
| 53 | return defaultNoopSpan |
| 54 | } |
| 55 | |
| 56 | // Inject belongs to the Tracer interface. |
| 57 | func (n NoopTracer) Inject(sp SpanContext, format interface{}, carrier interface{}) error { |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | // Extract belongs to the Tracer interface. |
| 62 | func (n NoopTracer) Extract(format interface{}, carrier interface{}) (SpanContext, error) { |
| 63 | return nil, ErrSpanContextNotFound |
| 64 | } |