forked from nhatthm/otelsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
67 lines (49 loc) · 1.38 KB
/
result.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
package otelsql
import (
"context"
"database/sql/driver"
"go.opentelemetry.io/otel/trace"
)
const (
traceMethodLastInsertID = "last_insert_id"
traceMethodRowsAffected = "rows_affected"
)
type resultFunc func() (int64, error)
var _ driver.Result = (*result)(nil)
type result struct {
lastInsertIDFunc resultFunc
rowsAffectedFunc resultFunc
}
func (r result) LastInsertId() (int64, error) {
return r.lastInsertIDFunc()
}
func (r result) RowsAffected() (int64, error) {
return r.rowsAffectedFunc()
}
func wrapResult(ctx context.Context, parent driver.Result, t methodTracer, traceLastInsertID bool, traceRowsAffected bool) driver.Result {
if !traceLastInsertID && !traceRowsAffected {
return parent
}
ctx = trace.ContextWithSpanContext(context.Background(), trace.SpanContextFromContext(ctx))
r := &result{
lastInsertIDFunc: parent.LastInsertId,
rowsAffectedFunc: parent.RowsAffected,
}
if traceLastInsertID {
r.lastInsertIDFunc = resultTrace(ctx, t, traceMethodLastInsertID, parent.LastInsertId)
}
if traceRowsAffected {
r.rowsAffectedFunc = resultTrace(ctx, t, traceMethodRowsAffected, parent.RowsAffected)
}
return r
}
func resultTrace(ctx context.Context, t methodTracer, method string, f resultFunc) resultFunc {
return func() (result int64, err error) {
_, end := t.MustTrace(ctx, method)
defer func() {
end(err)
}()
result, err = f()
return
}
}