forked from raystack/optimus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracer.go
201 lines (160 loc) · 5.77 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package postgres
import (
"context"
"github.com/jackc/pgx/v5"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
)
// tracer is a wrapper around the pgx tracer interfaces which instrument
// queries.
type tracer struct {
tracer trace.Tracer
attrs []attribute.KeyValue
}
// NewTracer returns a new Tracer.
func newTracer() *tracer {
return &tracer{
tracer: otel.Tracer("store/postgres"),
attrs: []attribute.KeyValue{
semconv.DBSystemPostgreSQL,
},
}
}
func recordError(span trace.Span, err error) {
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}
// TraceQueryStart is called at the beginning of Query, QueryRow, and Exec calls.
// The returned context is used for the rest of the call and will be passed to TraceQueryEnd.
func (t *tracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
if !trace.SpanFromContext(ctx).IsRecording() {
return ctx
}
opts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.attrs...),
trace.WithAttributes(semconv.DBStatementKey.String(data.SQL)),
}
spanName := "query " + data.SQL
ctx, _ = t.tracer.Start(ctx, spanName, opts...)
return ctx
}
// TraceQueryEnd is called at the end of Query, QueryRow, and Exec calls.
func (*tracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
span := trace.SpanFromContext(ctx)
recordError(span, data.Err)
span.SetAttributes(
attribute.String("result", data.CommandTag.String()),
)
span.End()
}
// TraceCopyFromStart is called at the beginning of CopyFrom calls. The
// returned context is used for the rest of the call and will be passed to
// TraceCopyFromEnd.
func (t *tracer) TraceCopyFromStart(ctx context.Context, _ *pgx.Conn, data pgx.TraceCopyFromStartData) context.Context {
if !trace.SpanFromContext(ctx).IsRecording() {
return ctx
}
opts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.attrs...),
trace.WithAttributes(attribute.String("db.table", data.TableName.Sanitize())),
}
ctx, _ = t.tracer.Start(ctx, "copy_from "+data.TableName.Sanitize(), opts...)
return ctx
}
// TraceCopyFromEnd is called at the end of CopyFrom calls.
func (*tracer) TraceCopyFromEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceCopyFromEndData) {
span := trace.SpanFromContext(ctx)
recordError(span, data.Err)
span.End()
}
// TraceBatchStart is called at the beginning of SendBatch calls. The returned
// context is used for the rest of the call and will be passed to
// TraceBatchQuery and TraceBatchEnd.
func (t *tracer) TraceBatchStart(ctx context.Context, _ *pgx.Conn, data pgx.TraceBatchStartData) context.Context {
if !trace.SpanFromContext(ctx).IsRecording() {
return ctx
}
var size int
if b := data.Batch; b != nil {
size = b.Len()
}
opts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.attrs...),
trace.WithAttributes(attribute.Int("pgx.batch.size", size)),
}
ctx, _ = t.tracer.Start(ctx, "batch start", opts...)
return ctx
}
// TraceBatchQuery is called at the after each query in a batch.
func (t *tracer) TraceBatchQuery(ctx context.Context, _ *pgx.Conn, data pgx.TraceBatchQueryData) {
opts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.attrs...),
trace.WithAttributes(semconv.DBStatementKey.String(data.SQL)),
}
spanName := "batch query " + data.SQL
_, span := t.tracer.Start(ctx, spanName, opts...)
recordError(span, data.Err)
span.End()
}
// TraceBatchEnd is called at the end of SendBatch calls.
func (*tracer) TraceBatchEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceBatchEndData) {
span := trace.SpanFromContext(ctx)
recordError(span, data.Err)
span.End()
}
// TraceConnectStart is called at the beginning of Connect and ConnectConfig
// calls. The returned context is used for the rest of the call and will be
// passed to TraceConnectEnd.
func (t *tracer) TraceConnectStart(ctx context.Context, data pgx.TraceConnectStartData) context.Context {
if !trace.SpanFromContext(ctx).IsRecording() {
return ctx
}
opts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.attrs...),
}
if data.ConnConfig != nil {
opts = append(opts,
trace.WithAttributes(attribute.String("database.host", data.ConnConfig.Host)),
trace.WithAttributes(attribute.Int("database.port", int(data.ConnConfig.Port))),
trace.WithAttributes(attribute.String("database.user", data.ConnConfig.User)))
}
ctx, _ = t.tracer.Start(ctx, "connect", opts...)
return ctx
}
// TraceConnectEnd is called at the end of Connect and ConnectConfig calls.
func (*tracer) TraceConnectEnd(ctx context.Context, data pgx.TraceConnectEndData) {
span := trace.SpanFromContext(ctx)
recordError(span, data.Err)
span.End()
}
// TracePrepareStart is called at the beginning of Prepare calls. The returned
// context is used for the rest of the call and will be passed to
// TracePrepareEnd.
func (t *tracer) TracePrepareStart(ctx context.Context, _ *pgx.Conn, data pgx.TracePrepareStartData) context.Context {
if !trace.SpanFromContext(ctx).IsRecording() {
return ctx
}
opts := []trace.SpanStartOption{
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(semconv.DBStatementKey.String(data.SQL)),
}
spanName := "prepare " + data.Name
ctx, _ = t.tracer.Start(ctx, spanName, opts...)
return ctx
}
// TracePrepareEnd is called at the end of Prepare calls.
func (*tracer) TracePrepareEnd(ctx context.Context, _ *pgx.Conn, data pgx.TracePrepareEndData) {
span := trace.SpanFromContext(ctx)
recordError(span, data.Err)
span.End()
}