forked from luna-duclos/instrumentedsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
executable file
·177 lines (151 loc) · 4.02 KB
/
stmt.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
package instrumentedsql
import (
"context"
"database/sql/driver"
"time"
)
type wrappedStmt struct {
opts
ctx context.Context
query string
parent driver.Stmt
}
// Compile time validation that our types implement the expected interfaces
var (
_ driver.Stmt = wrappedStmt{}
_ driver.StmtExecContext = wrappedStmt{}
_ driver.StmtQueryContext = wrappedStmt{}
)
func (s wrappedStmt) Close() (err error) {
if !s.hasOpExcluded(OpSQLStmtClose) {
span := s.GetSpan(s.ctx).NewChild(OpSQLStmtClose)
span.SetLabel("component", "database/sql")
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
s.Log(s.ctx, OpSQLStmtClose, "err", err, "duration", time.Since(start))
}()
}
return s.parent.Close()
}
func (s wrappedStmt) NumInput() int {
return s.parent.NumInput()
}
func (s wrappedStmt) Exec(args []driver.Value) (res driver.Result, err error) {
if !s.hasOpExcluded(OpSQLStmtExec) {
span := s.GetSpan(s.ctx).NewChild(OpSQLStmtExec)
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
if !s.OmitArgs {
span.SetLabel("args", formatArgs(args))
}
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
logQuery(s.ctx, s.opts, OpSQLStmtExec, s.query, err, args, start)
}()
}
res, err = s.parent.Exec(args)
if err != nil {
return nil, err
}
return wrappedResult{opts: s.opts, ctx: s.ctx, parent: res}, nil
}
func (s wrappedStmt) Query(args []driver.Value) (rows driver.Rows, err error) {
if !s.hasOpExcluded(OpSQLStmtQuery) {
span := s.GetSpan(s.ctx).NewChild(OpSQLStmtQuery)
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
if !s.OmitArgs {
span.SetLabel("args", formatArgs(args))
}
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
logQuery(s.ctx, s.opts, OpSQLStmtQuery, s.query, err, args, start)
}()
}
rows, err = s.parent.Query(args)
if err != nil {
return nil, err
}
return wrappedRows{opts: s.opts, ctx: s.ctx, parent: rows}, nil
}
func (s wrappedStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (res driver.Result, err error) {
if !s.hasOpExcluded(OpSQLStmtExec) {
span := s.GetSpan(ctx).NewChild(OpSQLStmtExec)
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
if !s.OmitArgs {
span.SetLabel("args", formatArgs(args))
}
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
logQuery(ctx, s.opts, OpSQLStmtExec, s.query, err, args, start)
}()
}
if stmtExecContext, ok := s.parent.(driver.StmtExecContext); ok {
res, err := stmtExecContext.ExecContext(ctx, args)
if err != nil {
return nil, err
}
return wrappedResult{opts: s.opts, ctx: ctx, parent: res}, nil
}
// Fallback implementation
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
res, err = s.parent.Exec(dargs)
if err != nil {
return nil, err
}
return wrappedResult{opts: s.opts, ctx: ctx, parent: res}, nil
}
func (s wrappedStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (rows driver.Rows, err error) {
if !s.hasOpExcluded(OpSQLStmtQuery) {
span := s.GetSpan(ctx).NewChild(OpSQLStmtQuery)
span.SetLabel("component", "database/sql")
span.SetLabel("query", s.query)
if !s.OmitArgs {
span.SetLabel("args", formatArgs(args))
}
start := time.Now()
defer func() {
span.SetError(err)
span.Finish()
logQuery(ctx, s.opts, OpSQLStmtQuery, s.query, err, args, start)
}()
}
if stmtQueryContext, ok := s.parent.(driver.StmtQueryContext); ok {
rows, err := stmtQueryContext.QueryContext(ctx, args)
if err != nil {
return nil, err
}
return wrappedRows{opts: s.opts, ctx: ctx, parent: rows}, nil
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
rows, err = s.parent.Query(dargs)
if err != nil {
return nil, err
}
return wrappedRows{opts: s.opts, ctx: ctx, parent: rows}, nil
}