-
Notifications
You must be signed in to change notification settings - Fork 0
/
opentracing.go
99 lines (79 loc) · 2.66 KB
/
opentracing.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
package gormopentracing
import (
"strings"
"gorm.io/gorm"
)
type opentracingPlugin struct {
// opt includes options those opentracingPlugin support.
opt *options
}
// New constructs a new plugin based opentracing. It supports to trace all operations in gorm,
// so if you have already traced your servers, now this plugin will perfect your tracing job.
func New(opts ...ApplyOption) gorm.Plugin {
dst := defaultOption()
for _, apply := range opts {
apply(dst)
}
return opentracingPlugin{
//logResult: dst.logResult,
opt: dst,
}
}
func (p opentracingPlugin) Name() string {
return "opentracing"
}
// Initialize registers all needed callbacks
func (p opentracingPlugin) Initialize(db *gorm.DB) (err error) {
e := myError{
errs: make([]string, 0, 12),
}
// create
err = db.Callback().Create().Before("gorm:create").Register(_stageBeforeCreate.Name(), p.beforeCreate)
e.add(_stageBeforeCreate, err)
err = db.Callback().Create().After("gorm:create").Register(_stageAfterCreate.Name(), p.after)
e.add(_stageAfterCreate, err)
// update
err = db.Callback().Update().Before("gorm:update").Register(_stageBeforeUpdate.Name(), p.beforeUpdate)
e.add(_stageBeforeUpdate, err)
err = db.Callback().Update().After("gorm:update").Register(_stageAfterUpdate.Name(), p.after)
e.add(_stageAfterUpdate, err)
// query
err = db.Callback().Query().Before("gorm:query").Register(_stageBeforeQuery.Name(), p.beforeQuery)
e.add(_stageBeforeQuery, err)
err = db.Callback().Query().After("gorm:query").Register(_stageAfterQuery.Name(), p.after)
e.add(_stageAfterQuery, err)
// delete
err = db.Callback().Delete().Before("gorm:delete").Register(_stageBeforeDelete.Name(), p.beforeDelete)
e.add(_stageBeforeDelete, err)
err = db.Callback().Delete().After("gorm:delete").Register(_stageAfterDelete.Name(), p.after)
e.add(_stageAfterDelete, err)
// row
err = db.Callback().Row().Before("gorm:row").Register(_stageBeforeRow.Name(), p.beforeRow)
e.add(_stageBeforeRow, err)
err = db.Callback().Row().After("gorm:row").Register(_stageAfterRow.Name(), p.after)
e.add(_stageAfterRow, err)
// raw
err = db.Callback().Raw().Before("gorm:raw").Register(_stageBeforeRaw.Name(), p.beforeRaw)
e.add(_stageBeforeRaw, err)
err = db.Callback().Raw().After("gorm:raw").Register(_stageAfterRaw.Name(), p.after)
e.add(_stageAfterRaw, err)
return e.toError()
}
type myError struct {
errs []string
}
func (e *myError) add(stage operationStage, err error) {
if err == nil {
return
}
e.errs = append(e.errs, "stage="+stage.Name()+":"+err.Error())
}
func (e myError) toError() error {
if len(e.errs) == 0 {
return nil
}
return e
}
func (e myError) Error() string {
return strings.Join(e.errs, ";")
}