forked from jdefrank/otgorm
/
callbacks.go
321 lines (268 loc) · 9.12 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package otgorm
import (
"context"
"database/sql/driver"
"fmt"
"github.com/jinzhu/gorm"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"reflect"
"regexp"
"runtime"
"time"
"unicode"
)
//Attributes that may or may not be added to a span based on Options used
const (
TableKey = attribute.Key("gorm.table") //The table the GORM query is acting upon
QueryKey = attribute.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 []attribute.KeyValue
//tracer creates spans. This is required
tracer trace.Tracer
//List of default options spans will start with
spanOptions []trace.SpanOption
}
//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.SpanOption) OptionFunc {
return func(c *callbacks) {
c.spanOptions = opts
}
}
//WithTracer configures the tracer to use when starting spans. Otherwise
//the global tracer 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 []attribute.KeyValue
func (d DefaultAttributes) apply(c *callbacks) {
c.defaultAttributes = []attribute.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: []attribute.KeyValue{},
}
defaultOpts := []Option{
// Default to the global tracer if not configured
WithTracer(otel.GetTracerProvider().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)
db.Callback().RowQuery().Before("gorm:row_query").Register("before_row_query", c.beforeRowQuery)
db.Callback().RowQuery().After("gorm:row_query").Register("after_row_query", c.afterRowQuery)
}
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.SpanOption{}, c.spanOptions...)
// 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 {
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(LogFormatter(scope.SQL, scope.SQLVars)))
}
attributes = append(attributes, attribute.String("path", fileWithLineNum()))
span.SetAttributes(attributes...)
//Set StatusCode if there are any issues
code := codes.Ok
msg := ""
if scope.HasError() {
err := scope.DB().Error
code = codes.Error
if gorm.IsRecordNotFoundError(err) {
msg = "gorm:NotFound"
} else {
msg = "gorm:Unknown"
}
}
span.SetStatus(code, msg)
//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) }
func (c *callbacks) beforeRowQuery(scope *gorm.Scope) { c.before(scope, "row_query") }
func (c *callbacks) afterRowQuery(scope *gorm.Scope) { c.after(scope) }
func fileWithLineNum() string {
_, file, line, ok := runtime.Caller(6)
if ok {
return fmt.Sprintf("%v:%v", file, line)
}
return ""
}
var (
sqlRegexp = regexp.MustCompile(`\?`)
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
goSrcRegexp = regexp.MustCompile(`golang/ocgorm(@.*)?/.*.go`)
goTestRegexp = regexp.MustCompile(`jinzhu/gorm(@.*)?/.*test.go`)
)
func isPrintable(s string) bool {
for _, r := range s {
if !unicode.IsPrint(r) {
return false
}
}
return true
}
var LogFormatter = func(values ...interface{}) string {
var (
sql string
formattedValues []string
)
for _, value := range values[1].([]interface{}) {
indirectValue := reflect.Indirect(reflect.ValueOf(value))
if indirectValue.IsValid() {
value = indirectValue.Interface()
if t, ok := value.(time.Time); ok {
if t.IsZero() {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00"))
} else {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05")))
}
} else if b, ok := value.([]byte); ok {
if str := string(b); isPrintable(str) {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str))
} else {
formattedValues = append(formattedValues, "'<binary>'")
}
} else if r, ok := value.(driver.Valuer); ok {
if value, err := r.Value(); err == nil && value != nil {
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
} else {
formattedValues = append(formattedValues, "NULL")
}
} else {
switch value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool:
formattedValues = append(formattedValues, fmt.Sprintf("%v", value))
default:
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value))
}
}
} else {
formattedValues = append(formattedValues, "NULL")
}
}
// differentiate between $n placeholders or else treat like ?
if numericPlaceHolderRegexp.MatchString(values[0].(string)) {
sql = values[0].(string)
for index, value := range formattedValues {
placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1)
sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1")
}
} else {
formattedValuesLength := len(formattedValues)
for index, value := range sqlRegexp.Split(values[0].(string), -1) {
sql += value
if index < formattedValuesLength {
sql += formattedValues[index]
}
}
}
return sql
}