1package gocb
2
3import (
4	"github.com/couchbase/gocbcore/v9"
5)
6
7func tracerAddRef(tracer requestTracer) {
8	if tracer == nil {
9		return
10	}
11	if refTracer, ok := tracer.(interface {
12		AddRef() int32
13	}); ok {
14		refTracer.AddRef()
15	}
16}
17
18func tracerDecRef(tracer requestTracer) {
19	if tracer == nil {
20		return
21	}
22	if refTracer, ok := tracer.(interface {
23		DecRef() int32
24	}); ok {
25		refTracer.DecRef()
26	}
27}
28
29// requestTracer describes the tracing abstraction in the SDK.
30type requestTracer interface {
31	StartSpan(operationName string, parentContext requestSpanContext) requestSpan
32}
33
34// requestSpan is the interface for spans that are created by a requestTracer.
35type requestSpan interface {
36	Finish()
37	Context() requestSpanContext
38	SetTag(key string, value interface{}) requestSpan
39}
40
41// requestSpanContext is the interface for for external span contexts that can be passed in into the SDK option blocks.
42type requestSpanContext interface {
43}
44
45type requestTracerWrapper struct {
46	tracer requestTracer
47}
48
49func (tracer *requestTracerWrapper) StartSpan(operationName string, parentContext gocbcore.RequestSpanContext) gocbcore.RequestSpan {
50	return requestSpanWrapper{
51		span: tracer.tracer.StartSpan(operationName, parentContext),
52	}
53}
54
55type requestSpanWrapper struct {
56	span requestSpan
57}
58
59func (span requestSpanWrapper) Finish() {
60	span.span.Finish()
61}
62
63func (span requestSpanWrapper) Context() gocbcore.RequestSpanContext {
64	return span.span.Context()
65}
66
67func (span requestSpanWrapper) SetTag(key string, value interface{}) gocbcore.RequestSpan {
68	span.span = span.span.SetTag(key, value)
69	return span
70}
71
72type noopSpan struct{}
73type noopSpanContext struct{}
74
75var (
76	defaultNoopSpanContext = noopSpanContext{}
77	defaultNoopSpan        = noopSpan{}
78)
79
80// noopTracer will have a future use so we tell the linter not to flag it.
81type noopTracer struct { // nolint: unused
82}
83
84func (tracer *noopTracer) StartSpan(operationName string, parentContext requestSpanContext) requestSpan {
85	return defaultNoopSpan
86}
87
88func (span noopSpan) Finish() {
89}
90
91func (span noopSpan) Context() requestSpanContext {
92	return defaultNoopSpanContext
93}
94
95func (span noopSpan) SetTag(key string, value interface{}) requestSpan {
96	return defaultNoopSpan
97}
98