-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcallbacks.go
225 lines (177 loc) · 6.06 KB
/
callbacks.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package otgorm
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"github.com/jinzhu/gorm"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
)
//Attributes that may or may not be added to a span based on Options used
const (
TableKey = core.Key("gorm.table") //The table the GORM query is acting upon
QueryKey = core.Key("gorm.query") //The GORM query itself
)
type callbacks struct {
//Allow otgorm to create root spans in the absence of a parent span.
//Default is to not allow root spans.
allowRoot bool
//Record the DB query as a KeyValue onto the span where the DB is called
query bool
//Record the table that the sql query is acting on
table bool
//List of default attributes to include onto the span for DB calls
defaultAttributes []core.KeyValue
//tracer creates spans. This is required
tracer trace.Tracer
//List of default options spans will start with
spanStartOptions []trace.StartOption
}
//Gorm scope keys for passing around context and span within the DB scope
var (
contextScopeKey = "_otContext"
spanScopeKey = "_otSpan"
)
// Option allows for managing otgorm configuration using functional options.
type Option interface {
apply(c *callbacks)
}
// OptionFunc converts a regular function to an Option if it's definition is compatible.
type OptionFunc func(c *callbacks)
func (fn OptionFunc) apply(c *callbacks) {
fn(c)
}
//WithSpanOptions configures the db callback functions with an additional set of
//trace.StartOptions which will be applied to each new span
func WithSpanOptions(opts ...trace.StartOption) OptionFunc {
return func(c *callbacks) {
c.spanStartOptions = opts
}
}
//WithTracer configures the tracer to use when starting spans. Otherwise
//the global tarcer is used with a default name
func WithTracer(tracer trace.Tracer) OptionFunc {
return func(c *callbacks) {
c.tracer = tracer
}
}
// AllowRoot allows creating root spans in the absence of existing spans.
type AllowRoot bool
func (a AllowRoot) apply(c *callbacks) {
c.allowRoot = bool(a)
}
// Query allows recording the sql queries in spans.
type Query bool
func (q Query) apply(c *callbacks) {
c.query = bool(q)
}
//Table allows for recording the table affected by sql queries in spans
type Table bool
func (t Table) apply(c *callbacks) {
c.table = bool(t)
}
// DefaultAttributes sets attributes to each span.
type DefaultAttributes []core.KeyValue
func (d DefaultAttributes) apply(c *callbacks) {
c.defaultAttributes = []core.KeyValue(d)
}
// RegisterCallbacks registers the necessary callbacks in Gorm's hook system for instrumentation with OpenTelemetry Spans.
func RegisterCallbacks(db *gorm.DB, opts ...Option) {
c := &callbacks{
defaultAttributes: []core.KeyValue{},
}
defaultOpts := []Option{
WithTracer(global.TraceProvider().Tracer("otgorm")),
WithSpanOptions(trace.WithSpanKind(trace.SpanKindInternal)),
}
for _, opt := range append(defaultOpts, opts...) {
opt.apply(c)
}
db.Callback().Create().Before("gorm:create").Register("before_create", c.beforeCreate)
db.Callback().Create().After("gorm:create").Register("after_create", c.afterCreate)
db.Callback().Query().Before("gorm:query").Register("before_query", c.beforeQuery)
db.Callback().Query().After("gorm:query").Register("after_query", c.afterQuery)
db.Callback().Update().Before("gorm:update").Register("before_update", c.beforeUpdate)
db.Callback().Update().After("gorm:update").Register("after_update", c.afterUpdate)
db.Callback().Delete().Before("gorm:delete").Register("before_delete", c.beforeDelete)
db.Callback().Delete().After("gorm:delete").Register("after_delete", c.afterDelete)
}
func (c *callbacks) before(scope *gorm.Scope, operation string) {
rctx, _ := scope.Get(contextScopeKey)
ctx, ok := rctx.(context.Context)
if !ok || ctx == nil {
ctx = context.Background()
}
ctx = c.startTrace(ctx, scope, operation)
scope.Set(contextScopeKey, ctx)
}
func (c *callbacks) after(scope *gorm.Scope) {
c.endTrace(scope)
}
func (c *callbacks) startTrace(ctx context.Context, scope *gorm.Scope, operation string) context.Context {
//Start with configured span options
opts := append([]trace.StartOption{}, c.spanStartOptions...)
// There's no context but we are ok with root spans
if ctx == nil {
ctx = context.Background()
}
//If there's no parent span and we don't allow root spans, return context
parentSpan := trace.SpanFromContext(ctx)
if parentSpan == nil && !c.allowRoot {
return ctx
}
var span trace.Span
if parentSpan == nil {
ctx, span = c.tracer.Start(
context.Background(),
fmt.Sprintf("gorm:%s", operation),
opts...,
)
} else {
opts = append(opts, trace.ChildOf(parentSpan.SpanContext()))
ctx, span = c.tracer.Start(ctx, fmt.Sprintf("gorm:%s", operation), opts...)
}
scope.Set(spanScopeKey, span)
return ctx
}
func (c *callbacks) endTrace(scope *gorm.Scope) {
rspan, ok := scope.Get(spanScopeKey)
if !ok {
return
}
span, ok := rspan.(trace.Span)
if !ok {
return
}
//Apply span attributes
attributes := c.defaultAttributes
if c.table {
attributes = append(attributes, TableKey.String(scope.TableName()))
}
if c.query {
attributes = append(attributes, QueryKey.String(scope.SQL))
}
span.SetAttributes(attributes...)
//Set StatusCode if there are any issues
var code codes.Code
if scope.HasError() {
err := scope.DB().Error
if gorm.IsRecordNotFoundError(err) {
code = codes.NotFound
} else {
code = codes.Unknown
}
}
span.SetStatus(code)
//End Span
span.End()
}
func (c *callbacks) beforeCreate(scope *gorm.Scope) { c.before(scope, "create") }
func (c *callbacks) afterCreate(scope *gorm.Scope) { c.after(scope) }
func (c *callbacks) beforeQuery(scope *gorm.Scope) { c.before(scope, "query") }
func (c *callbacks) afterQuery(scope *gorm.Scope) { c.after(scope) }
func (c *callbacks) beforeUpdate(scope *gorm.Scope) { c.before(scope, "update") }
func (c *callbacks) afterUpdate(scope *gorm.Scope) { c.after(scope) }
func (c *callbacks) beforeDelete(scope *gorm.Scope) { c.before(scope, "delete") }
func (c *callbacks) afterDelete(scope *gorm.Scope) { c.after(scope) }