-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
exec.go
107 lines (84 loc) · 3.01 KB
/
exec.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
package otelsql
import (
"context"
"database/sql/driver"
)
const (
metricMethodExec = "go.sql.exec"
traceMethodExec = "exec"
)
type execContextFuncMiddleware = middleware[execContextFunc]
type execContextFunc func(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
// nopExecContext executes nothing.
func nopExecContext(_ context.Context, _ string, _ []driver.NamedValue) (driver.Result, error) {
return nil, nil //nolint: nilnil
}
// skippedExecContext always returns driver.ErrSkip.
func skippedExecContext(_ context.Context, _ string, _ []driver.NamedValue) (driver.Result, error) {
return nil, driver.ErrSkip
}
// execStats records metrics for exec.
func execStats(r methodRecorder, method string) execContextFuncMiddleware {
return func(next execContextFunc) execContextFunc {
return func(ctx context.Context, query string, args []driver.NamedValue) (result driver.Result, err error) {
end := r.Record(ctx, method)
defer func() {
end(err)
}()
return next(ctx, query, args)
}
}
}
// execTrace creates a span for exec.
func execTrace(t methodTracer, traceQuery queryTracer, method string) execContextFuncMiddleware {
return func(next execContextFunc) execContextFunc {
return func(ctx context.Context, query string, args []driver.NamedValue) (result driver.Result, err error) {
ctx = ContextWithQuery(ctx, query)
ctx, end := t.Trace(ctx, method)
defer func() {
end(err, traceQuery(ctx, query, args)...)
}()
return next(ctx, query, args)
}
}
}
func execWrapResult(t methodTracer, traceLastInsertID bool, traceRowsAffected bool) execContextFuncMiddleware {
return func(next execContextFunc) execContextFunc {
return func(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
result, err := next(ctx, query, args)
if err != nil {
return nil, err
}
shouldTrace, _ := t.ShouldTrace(ctx)
return wrapResult(ctx, result, t, shouldTrace && traceLastInsertID, shouldTrace && traceRowsAffected), nil
}
}
}
func makeExecContextFuncMiddlewares(r methodRecorder, t methodTracer, cfg execConfig) []execContextFuncMiddleware {
middlewares := make([]middleware[execContextFunc], 0, 3)
middlewares = append(middlewares, execStats(r, cfg.metricMethod))
if t == nil {
return middlewares
}
middlewares = append(middlewares, execTrace(t, cfg.traceQuery, cfg.traceMethod))
if cfg.traceLastInsertID || cfg.traceRowsAffected {
middlewares = append(middlewares, execWrapResult(t, cfg.traceLastInsertID, cfg.traceRowsAffected))
}
return middlewares
}
type execConfig struct {
metricMethod string
traceMethod string
traceQuery queryTracer
traceLastInsertID bool
traceRowsAffected bool
}
func newExecConfig(opts driverOptions, metricMethod, traceMethod string) execConfig {
return execConfig{
metricMethod: metricMethod,
traceMethod: traceMethod,
traceQuery: opts.trace.queryTracer,
traceLastInsertID: opts.trace.LastInsertID,
traceRowsAffected: opts.trace.RowsAffected,
}
}