forked from elastic/apm-agent-go
/
conn.go
168 lines (150 loc) · 4.44 KB
/
conn.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
package apmsql
import (
"context"
"database/sql/driver"
"errors"
"github.com/elastic/apm-agent-go"
)
func newConn(in driver.Conn, d *tracingDriver, dsnInfo DSNInfo) driver.Conn {
conn := &conn{Conn: in, driver: d}
conn.dsnInfo = dsnInfo
conn.pinger, _ = in.(driver.Pinger)
conn.queryer, _ = in.(driver.Queryer)
conn.queryerContext, _ = in.(driver.QueryerContext)
conn.connPrepareContext, _ = in.(driver.ConnPrepareContext)
conn.execer, _ = in.(driver.Execer)
conn.execerContext, _ = in.(driver.ExecerContext)
conn.connBeginTx, _ = in.(driver.ConnBeginTx)
conn.connGo110.init(in)
if in, ok := in.(driver.ConnBeginTx); ok {
return &connBeginTx{conn, in}
}
return conn
}
type conn struct {
driver.Conn
connGo110
driver *tracingDriver
dsnInfo DSNInfo
pinger driver.Pinger
queryer driver.Queryer
queryerContext driver.QueryerContext
connPrepareContext driver.ConnPrepareContext
execer driver.Execer
execerContext driver.ExecerContext
connBeginTx driver.ConnBeginTx
}
func (c *conn) startStmtSpan(ctx context.Context, stmt, spanType string) (*elasticapm.Span, context.Context) {
return c.startSpan(ctx, c.driver.querySignature(stmt), spanType, stmt)
}
func (c *conn) startSpan(ctx context.Context, name, spanType, stmt string) (*elasticapm.Span, context.Context) {
span, ctx := elasticapm.StartSpan(ctx, name, spanType)
if !span.Dropped() {
span.Context.SetDatabase(elasticapm.DatabaseSpanContext{
Instance: c.dsnInfo.Database,
Statement: stmt,
Type: "sql",
User: c.dsnInfo.User,
})
}
return span, ctx
}
func (c *conn) finishSpan(ctx context.Context, span *elasticapm.Span, resultError error) {
if resultError == driver.ErrSkip {
// TODO(axw) mark span as abandoned,
// so it's not sent and not counted
// in the span limit. Ideally remove
// from the slice so memory is kept
// in check.
return
}
span.End()
if e := elasticapm.CaptureError(ctx, resultError); e != nil {
e.Send()
}
}
func (c *conn) Ping(ctx context.Context) (resultError error) {
if c.pinger == nil {
return nil
}
span, ctx := c.startSpan(ctx, "ping", c.driver.pingSpanType, "")
defer c.finishSpan(ctx, span, resultError)
return c.pinger.Ping(ctx)
}
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (_ driver.Rows, resultError error) {
if c.queryerContext == nil && c.queryer == nil {
return nil, driver.ErrSkip
}
span, ctx := c.startStmtSpan(ctx, query, c.driver.querySpanType)
defer c.finishSpan(ctx, span, resultError)
if c.queryerContext != nil {
return c.queryerContext.QueryContext(ctx, query, args)
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return c.queryer.Query(query, dargs)
}
func (*conn) Query(query string, args []driver.Value) (driver.Rows, error) {
return nil, errors.New("Query should never be called")
}
func (c *conn) PrepareContext(ctx context.Context, query string) (_ driver.Stmt, resultError error) {
span, ctx := c.startStmtSpan(ctx, query, c.driver.prepareSpanType)
defer c.finishSpan(ctx, span, resultError)
var stmt driver.Stmt
var err error
if c.connPrepareContext != nil {
stmt, err = c.connPrepareContext.PrepareContext(ctx, query)
} else {
stmt, err = c.Prepare(query)
if err == nil {
select {
default:
case <-ctx.Done():
stmt.Close()
return nil, ctx.Err()
}
}
}
if stmt != nil {
stmt = newStmt(stmt, c, query)
}
return stmt, err
}
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (_ driver.Result, resultError error) {
if c.execerContext == nil && c.execer == nil {
return nil, driver.ErrSkip
}
span, ctx := c.startStmtSpan(ctx, query, c.driver.execSpanType)
defer c.finishSpan(ctx, span, resultError)
if c.execerContext != nil {
return c.execerContext.ExecContext(ctx, query, args)
}
dargs, err := namedValueToValue(args)
if err != nil {
return nil, err
}
select {
default:
case <-ctx.Done():
return nil, ctx.Err()
}
return c.execer.Exec(query, dargs)
}
func (*conn) Exec(query string, args []driver.Value) (driver.Result, error) {
return nil, errors.New("Exec should never be called")
}
type connBeginTx struct {
*conn
connBeginTx driver.ConnBeginTx
}
func (c *connBeginTx) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
// TODO(axw) instrument commit/rollback?
return c.connBeginTx.BeginTx(ctx, opts)
}