-
Notifications
You must be signed in to change notification settings - Fork 194
/
context.go
132 lines (119 loc) · 3.18 KB
/
context.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
// +build go1.9
package apmgorm
import (
"context"
"fmt"
"github.com/jinzhu/gorm"
"go.elastic.co/apm"
"go.elastic.co/apm/internal/sqlutil"
"go.elastic.co/apm/module/apmsql"
)
const (
apmContextKey = "elasticapm:context"
)
// WithContext returns a copy of db with ctx recorded for use by
// the callbacks registered via RegisterCallbacks.
func WithContext(ctx context.Context, db *gorm.DB) *gorm.DB {
return db.Set(apmContextKey, ctx)
}
func scopeContext(scope *gorm.Scope) (context.Context, bool) {
value, ok := scope.Get(apmContextKey)
if !ok {
return nil, false
}
ctx, _ := value.(context.Context)
return ctx, ctx != nil
}
// RegisterCallbacks registers callbacks on db for reporting spans
// to Elastic APM. This is called automatically by apmgorm.Open;
// it is provided for cases where a *gorm.DB is acquired by other
// means.
func RegisterCallbacks(db *gorm.DB) {
registerCallbacks(db, apmsql.DSNInfo{})
}
func registerCallbacks(db *gorm.DB, dsnInfo apmsql.DSNInfo) {
driverName := db.Dialect().GetName()
switch driverName {
case "postgres":
driverName = "postgresql"
}
spanTypePrefix := fmt.Sprintf("db.%s.", driverName)
querySpanType := spanTypePrefix + "query"
execSpanType := spanTypePrefix + "exec"
type params struct {
spanType string
processor func() *gorm.CallbackProcessor
}
callbacks := map[string]params{
"gorm:create": {
spanType: execSpanType,
processor: func() *gorm.CallbackProcessor { return db.Callback().Create() },
},
"gorm:delete": {
spanType: execSpanType,
processor: func() *gorm.CallbackProcessor { return db.Callback().Delete() },
},
"gorm:query": {
spanType: querySpanType,
processor: func() *gorm.CallbackProcessor { return db.Callback().Query() },
},
"gorm:update": {
spanType: execSpanType,
processor: func() *gorm.CallbackProcessor { return db.Callback().Update() },
},
}
for name, params := range callbacks {
const callbackPrefix = "elasticapm"
params.processor().Before(name).Register(
fmt.Sprintf("%s:before:%s", callbackPrefix, name),
newBeforeCallback(params.spanType),
)
params.processor().After(name).Register(
fmt.Sprintf("%s:after:%s", callbackPrefix, name),
newAfterCallback(dsnInfo),
)
}
}
func newBeforeCallback(spanType string) func(*gorm.Scope) {
return func(scope *gorm.Scope) {
ctx, ok := scopeContext(scope)
if !ok {
return
}
span, ctx := apm.StartSpan(ctx, "", spanType)
if span.Dropped() {
span.End()
ctx = nil
}
scope.Set(apmContextKey, ctx)
}
}
func newAfterCallback(dsnInfo apmsql.DSNInfo) func(*gorm.Scope) {
return func(scope *gorm.Scope) {
ctx, ok := scopeContext(scope)
if !ok {
return
}
span := apm.SpanFromContext(ctx)
if span == nil {
return
}
span.Name = sqlutil.QuerySignature(scope.SQL)
span.Context.SetDatabase(apm.DatabaseSpanContext{
Instance: dsnInfo.Database,
Statement: scope.SQL,
Type: "sql",
User: dsnInfo.User,
})
defer span.End()
// Capture errors, except for "record not found", which may be expected.
for _, err := range scope.DB().GetErrors() {
if gorm.IsRecordNotFoundError(err) {
continue
}
if e := apm.CaptureError(ctx, err); e != nil {
e.Send()
}
}
}
}