-
Notifications
You must be signed in to change notification settings - Fork 3
/
tracer.go
179 lines (144 loc) · 4.95 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
package tracer
import (
"context"
"database/sql/driver"
"github.com/alexfalkowski/go-service/meta"
"github.com/ngrok/sqlmw"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// NewInterceptor for tracer.
func NewInterceptor(driver string, tracer trace.Tracer, interceptor sqlmw.Interceptor) *Interceptor {
return &Interceptor{driver: driver, tracer: tracer, interceptor: interceptor}
}
// Interceptor for tracer.
type Interceptor struct {
driver string
tracer trace.Tracer
interceptor sqlmw.Interceptor
}
func (i *Interceptor) ConnBeginTx(ctx context.Context, conn driver.ConnBeginTx, txOpts driver.TxOptions) (context.Context, driver.Tx, error) {
return i.interceptor.ConnBeginTx(ctx, conn, txOpts)
}
func (i *Interceptor) ConnPrepareContext(ctx context.Context, conn driver.ConnPrepareContext, query string) (context.Context, driver.Stmt, error) {
return i.interceptor.ConnPrepareContext(ctx, conn, query)
}
func (i *Interceptor) ConnPing(ctx context.Context, conn driver.Pinger) error {
return i.interceptor.ConnPing(ctx, conn)
}
func (i *Interceptor) ConnExecContext(ctx context.Context, conn driver.ExecerContext, query string, args []driver.NamedValue) (driver.Result, error) {
operationName := "connection exec"
attrs := []attribute.KeyValue{
attribute.Key("db.sql.query").String(query),
}
ctx, span := i.tracer.Start(
ctx,
operationName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
defer span.End()
res, err := i.interceptor.ConnExecContext(ctx, conn, query, args)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
return res, err
}
//nolint:dupl
func (i *Interceptor) ConnQueryContext(ctx context.Context, conn driver.QueryerContext, query string, args []driver.NamedValue) (context.Context, driver.Rows, error) {
operationName := "connection query"
attrs := []attribute.KeyValue{
attribute.Key("db.sql.query").String(query),
}
ctx, span := i.tracer.Start(
ctx,
operationName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
defer span.End()
ctx, res, err := i.interceptor.ConnQueryContext(ctx, conn, query, args)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
return ctx, res, err
}
func (i *Interceptor) ConnectorConnect(ctx context.Context, connect driver.Connector) (driver.Conn, error) {
return i.interceptor.ConnectorConnect(ctx, connect)
}
//nolint:revive,stylecheck
func (i *Interceptor) ResultLastInsertId(res driver.Result) (int64, error) {
return i.interceptor.ResultLastInsertId(res)
}
func (i *Interceptor) ResultRowsAffected(res driver.Result) (int64, error) {
return i.interceptor.ResultRowsAffected(res)
}
func (i *Interceptor) RowsNext(ctx context.Context, rows driver.Rows, dest []driver.Value) error {
return i.interceptor.RowsNext(ctx, rows, dest)
}
func (i *Interceptor) RowsClose(ctx context.Context, rows driver.Rows) error {
return i.interceptor.RowsClose(ctx, rows)
}
func (i *Interceptor) StmtExecContext(ctx context.Context, stmt driver.StmtExecContext, query string, args []driver.NamedValue) (driver.Result, error) {
operationName := "statement exec"
attrs := []attribute.KeyValue{
attribute.Key("db.sql.query").String(query),
}
ctx, span := i.tracer.Start(
ctx,
operationName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
defer span.End()
res, err := i.interceptor.StmtExecContext(ctx, stmt, query, args)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
return res, err
}
//nolint:dupl
func (i *Interceptor) StmtQueryContext(ctx context.Context, stmt driver.StmtQueryContext, query string, args []driver.NamedValue) (context.Context, driver.Rows, error) {
operationName := "statement query"
attrs := []attribute.KeyValue{
attribute.Key("db.sql.query").String(query),
}
ctx, span := i.tracer.Start(
ctx,
operationName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...),
)
defer span.End()
ctx, res, err := i.interceptor.StmtQueryContext(ctx, stmt, query, args)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
}
for k, v := range meta.Attributes(ctx) {
span.SetAttributes(attribute.Key(k).String(v))
}
return ctx, res, err
}
func (i *Interceptor) StmtClose(ctx context.Context, stmt driver.Stmt) error {
return i.interceptor.StmtClose(ctx, stmt)
}
func (i *Interceptor) TxCommit(ctx context.Context, tx driver.Tx) error {
return i.interceptor.TxCommit(ctx, tx)
}
func (i *Interceptor) TxRollback(ctx context.Context, tx driver.Tx) error {
return i.interceptor.TxRollback(ctx, tx)
}