-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracing.go
110 lines (88 loc) · 2.26 KB
/
tracing.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
package ssormotel
import (
"context"
"runtime"
"go.opentelemetry.io/otel/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/codes"
)
type (
Read func(ctx context.Context) error
Write func(ctx context.Context) (int64, error)
)
type Tracing interface {
StartForRead(context.Context, Read) error
StartForWrite(context.Context, Write) (int64, error)
SetStatement(string)
UnsetStatement()
}
type tracing struct {
conf *config
spanOpts []trace.SpanStartOption
}
func NewTracing(opts ...Option) *tracing {
conf := newConfig(opts...)
return &tracing{
conf: conf,
spanOpts: []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(conf.attrs...),
},
}
}
func (t *tracing) SetStatement(statement string) {
t.conf.statement = statement
}
func (t *tracing) UnsetStatement() {
t.conf.statement = ""
}
func (t *tracing) StartForRead(ctx context.Context, f Read) error {
if !trace.SpanFromContext(ctx).IsRecording() {
return f(ctx)
}
spanOpts := t.spanOpts
if t.isEnableStatement() {
spanOpts = append(spanOpts, trace.WithAttributes(semconv.DBStatementKey.String(t.conf.statement)))
}
spanCtx, span := t.conf.tracer.Start(ctx, t.makeSpanName(), spanOpts...)
defer func() {
t.UnsetStatement()
span.End()
}()
if err := f(ctx); err != nil {
recordError(spanCtx, span, err)
return err
}
return nil
}
func (t *tracing) StartForWrite(ctx context.Context, f Write) (int64, error) {
if !trace.SpanFromContext(ctx).IsRecording() {
return f(ctx)
}
spanOpts := t.spanOpts
if t.isEnableStatement() {
spanOpts = append(spanOpts, trace.WithAttributes(semconv.DBStatementKey.String(t.conf.statement)))
}
ctx, span := t.conf.tracer.Start(ctx, t.makeSpanName(), spanOpts...)
defer func() {
t.UnsetStatement()
span.End()
}()
row, err := f(ctx)
if err != nil {
recordError(ctx, span, err)
return row, err
}
return row, nil
}
func (t *tracing) isEnableStatement() bool {
return t.conf.enableQueryStatement && t.conf.statement != ""
}
func (t *tracing) makeSpanName() string {
pc, _, _, _ := runtime.Caller(3)
return runtime.FuncForPC(pc).Name()
}
func recordError(ctx context.Context, span trace.Span, err error) {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}