-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
155 lines (141 loc) · 4.22 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//go:build go1.9
// +build go1.9
package atgorm // import "go.atatus.com/agent/module/atgorm"
import (
"context"
"database/sql"
"fmt"
"github.com/jinzhu/gorm"
atatus "go.atatus.com/agent"
"go.atatus.com/agent/module/atsql"
)
const (
apmContextKey = "atatus: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 Atatus. This is called automatically by atgorm.Open;
// it is provided for cases where a *gorm.DB is acquired by other
// means.
func RegisterCallbacks(db *gorm.DB) {
registerCallbacks(db, atsql.DSNInfo{})
}
func registerCallbacks(db *gorm.DB, dsnInfo atsql.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() },
},
"gorm:row_query": {
spanType: querySpanType,
processor: func() *gorm.CallbackProcessor { return db.Callback().RowQuery() },
},
}
for name, params := range callbacks {
const callbackPrefix = "atatus"
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 := atatus.StartSpan(ctx, "", spanType)
if span.Dropped() {
span.End()
ctx = nil
}
scope.Set(apmContextKey, ctx)
}
}
func newAfterCallback(dsnInfo atsql.DSNInfo) func(*gorm.Scope) {
return func(scope *gorm.Scope) {
ctx, ok := scopeContext(scope)
if !ok {
return
}
span := atatus.SpanFromContext(ctx)
if span == nil {
return
}
span.Name = atsql.QuerySignature(scope.SQL)
span.Context.SetDestinationAddress(dsnInfo.Address, dsnInfo.Port)
span.Context.SetDatabase(atatus.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) || err == sql.ErrNoRows {
continue
}
if e := atatus.CaptureError(ctx, err); e != nil {
e.Send()
}
}
}
}