forked from nhatthm/otelsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.go
184 lines (139 loc) · 4.62 KB
/
tracer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package otelsql
import (
"context"
"database/sql/driver"
"errors"
"strings"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
"go.opentelemetry.io/otel/trace"
xattr "go.nhat.io/otelsql/attribute"
)
type spanNameFormatter func(ctx context.Context, op string) string
type errorToSpanStatus func(err error) (codes.Code, string)
type queryTracer func(ctx context.Context, query string, args []driver.NamedValue) []attribute.KeyValue
// methodTracer traces a sql method.
type methodTracer interface {
// ShouldTrace checks whether it should trace a method and the given context has a parent span.
ShouldTrace(ctx context.Context) (bool, bool)
MustTrace(ctx context.Context, method string, labels ...attribute.KeyValue) (context.Context, func(err error, attrs ...attribute.KeyValue))
Trace(ctx context.Context, method string, labels ...attribute.KeyValue) (context.Context, func(err error, attrs ...attribute.KeyValue))
}
type methodTracerImpl struct {
tracer trace.Tracer
formatSpanName spanNameFormatter
errorToStatus func(err error) (codes.Code, string)
allowRoot bool
attributes []attribute.KeyValue
}
func (t *methodTracerImpl) ShouldTrace(ctx context.Context) (bool, bool) {
hasSpan := trace.SpanContextFromContext(ctx).IsValid()
return t.allowRoot || hasSpan, hasSpan
}
func (t *methodTracerImpl) Trace(ctx context.Context, method string, labels ...attribute.KeyValue) (context.Context, func(err error, attrs ...attribute.KeyValue)) {
shouldTrace, hasParentSpan := t.ShouldTrace(ctx)
if !shouldTrace {
return ctx, func(_ error, _ ...attribute.KeyValue) {}
}
newCtx, end := t.MustTrace(ctx, method, labels...)
if !hasParentSpan {
ctx = newCtx
}
return ctx, end
}
func (t *methodTracerImpl) MustTrace(ctx context.Context, method string, labels ...attribute.KeyValue) (context.Context, func(err error, attrs ...attribute.KeyValue)) {
ctx, span := t.tracer.Start(ctx, t.formatSpanName(ctx, method),
trace.WithTimestamp(time.Now()),
trace.WithSpanKind(trace.SpanKindClient),
)
attrs := make([]attribute.KeyValue, 0, len(t.attributes)+len(labels)+1)
attrs = append(attrs, t.attributes...)
attrs = append(attrs, labels...)
attrs = append(attrs, semconv.DBOperationKey.String(method))
return ctx, func(err error, labels ...attribute.KeyValue) {
code, desc := t.errorToStatus(err)
attrs = append(attrs, labels...)
span.SetAttributes(attrs...)
span.SetStatus(code, desc)
if code == codes.Error {
span.RecordError(err)
}
span.End(
trace.WithTimestamp(time.Now()),
)
}
}
func newMethodTracer(tracer trace.Tracer, opts ...func(t *methodTracerImpl)) *methodTracerImpl {
t := &methodTracerImpl{
tracer: tracer,
formatSpanName: formatSpanName,
errorToStatus: spanStatusFromError,
}
for _, o := range opts {
o(t)
}
return t
}
func tracerOrNil(t methodTracer, shouldTrace bool) methodTracer {
if shouldTrace {
return t
}
return nil
}
func traceWithAllowRoot(allow bool) func(t *methodTracerImpl) {
return func(t *methodTracerImpl) {
t.allowRoot = allow
}
}
func traceWithDefaultAttributes(attrs ...attribute.KeyValue) func(t *methodTracerImpl) {
return func(t *methodTracerImpl) {
t.attributes = append(t.attributes, attrs...)
}
}
func traceWithSpanNameFormatter(f spanNameFormatter) func(t *methodTracerImpl) {
return func(t *methodTracerImpl) {
t.formatSpanName = f
}
}
func traceWithErrorToSpanStatus(f errorToSpanStatus) func(t *methodTracerImpl) {
return func(t *methodTracerImpl) {
t.errorToStatus = f
}
}
func formatSpanName(_ context.Context, method string) string {
var sb strings.Builder
sb.Grow(len(method) + 4)
sb.WriteString("sql:")
sb.WriteString(method)
return sb.String()
}
func spanStatusFromError(err error) (codes.Code, string) {
if err == nil {
return codes.Ok, ""
}
return codes.Error, err.Error()
}
func spanStatusFromErrorIgnoreErrSkip(err error) (codes.Code, string) {
if err == nil || errors.Is(err, driver.ErrSkip) {
return codes.Ok, ""
}
return codes.Error, err.Error()
}
func traceNoQuery(context.Context, string, []driver.NamedValue) []attribute.KeyValue {
return nil
}
func traceQueryWithoutArgs(_ context.Context, sql string, _ []driver.NamedValue) []attribute.KeyValue {
return []attribute.KeyValue{
semconv.DBStatementKey.String(sql),
}
}
func traceQueryWithArgs(_ context.Context, sql string, args []driver.NamedValue) []attribute.KeyValue {
attrs := make([]attribute.KeyValue, 0, 1+len(args))
attrs = append(attrs, semconv.DBStatementKey.String(sql))
for _, arg := range args {
attrs = append(attrs, xattr.FromNamedValue(arg))
}
return attrs
}