forked from nhatthm/otelsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.go
106 lines (88 loc) · 2.9 KB
/
prepare.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
package otelsql
import (
"context"
"database/sql/driver"
)
const (
metricMethodPrepare = "go.sql.prepare"
traceMethodPrepare = "prepare"
)
type prepareContextFuncMiddleware = middleware[prepareContextFunc]
type prepareContextFunc func(ctx context.Context, query string) (driver.Stmt, error)
// nopPrepareContext prepares nothing.
func nopPrepareContext(_ context.Context, _ string) (driver.Stmt, error) {
return nil, nil
}
func ensurePrepareContext(conn driver.Conn) prepareContextFunc {
if p, ok := conn.(driver.ConnPrepareContext); ok {
return p.PrepareContext
}
return func(_ context.Context, query string) (driver.Stmt, error) {
return conn.Prepare(query)
}
}
// prepareStats records metrics for prepare.
func prepareStats(r methodRecorder) prepareContextFuncMiddleware {
return func(next prepareContextFunc) prepareContextFunc {
return func(ctx context.Context, query string) (stmt driver.Stmt, err error) {
end := r.Record(ctx, metricMethodPrepare)
defer func() {
end(err)
}()
return next(ctx, query)
}
}
}
// prepareTrace creates a span for prepare.
func prepareTrace(t methodTracer, traceQuery queryTracer) prepareContextFuncMiddleware {
return func(next prepareContextFunc) prepareContextFunc {
return func(ctx context.Context, query string) (stmt driver.Stmt, err error) {
ctx, end := t.Trace(ctx, traceMethodPrepare)
defer func() {
end(err, traceQuery(ctx, query, nil)...)
}()
return next(ctx, query)
}
}
}
func prepareWrapResult(
execFuncMiddlewares []execContextFuncMiddleware,
execContextFuncMiddlewares []execContextFuncMiddleware,
queryFuncMiddlewares []queryContextFuncMiddleware,
queryContextFuncMiddlewares []queryContextFuncMiddleware,
) prepareContextFuncMiddleware {
return func(next prepareContextFunc) prepareContextFunc {
return func(ctx context.Context, query string) (driver.Stmt, error) {
stmt, err := next(ctx, query)
if err != nil {
return nil, err
}
return wrapStmt(stmt, stmtConfig{
query: query,
execFuncMiddlewares: execFuncMiddlewares,
queryContextFuncMiddlewares: queryContextFuncMiddlewares,
execContextFuncMiddlewares: execContextFuncMiddlewares,
queryFuncMiddlewares: queryFuncMiddlewares,
}), nil
}
}
}
type prepareConfig struct {
traceQuery queryTracer
execFuncMiddlewares []execContextFuncMiddleware
execContextFuncMiddlewares []execContextFuncMiddleware
queryFuncMiddlewares []queryContextFuncMiddleware
queryContextFuncMiddlewares []queryContextFuncMiddleware
}
func makePrepareContextFuncMiddlewares(r methodRecorder, t methodTracer, cfg prepareConfig) []prepareContextFuncMiddleware {
return []prepareContextFuncMiddleware{
prepareStats(r),
prepareTrace(t, cfg.traceQuery),
prepareWrapResult(
cfg.execFuncMiddlewares,
cfg.execContextFuncMiddlewares,
cfg.queryFuncMiddlewares,
cfg.queryContextFuncMiddlewares,
),
}
}