-
Notifications
You must be signed in to change notification settings - Fork 322
/
gorm.go
184 lines (150 loc) · 4.48 KB
/
gorm.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
package htrace
import (
"database/sql"
"database/sql/driver"
"fmt"
"io"
"github.com/highlight/highlight/sdk/highlight-go"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
)
var dbRowsAffected = attribute.Key("db.rows_affected")
type otelPlugin struct {
attrs []attribute.KeyValue
excludeQueryVars bool
}
type PluginOption func(p *otelPlugin)
func NewGormPlugin(opts ...PluginOption) gorm.Plugin {
p := &otelPlugin{}
for _, opt := range opts {
opt(p)
}
return p
}
func (p otelPlugin) Name() string {
return "otelgorm"
}
type gormHookFunc func(tx *gorm.DB)
type gormRegister interface {
Register(name string, fn func(*gorm.DB)) error
}
func (p otelPlugin) Initialize(db *gorm.DB) (err error) {
cb := db.Callback()
hooks := []struct {
callback gormRegister
hook gormHookFunc
name string
}{
{cb.Create().Before("gorm:create"), p.before("gorm.Create"), "before:create"},
{cb.Create().After("gorm:create"), p.after(), "after:create"},
{cb.Query().Before("gorm:query"), p.before("gorm.Query"), "before:select"},
{cb.Query().After("gorm:query"), p.after(), "after:select"},
{cb.Delete().Before("gorm:delete"), p.before("gorm.Delete"), "before:delete"},
{cb.Delete().After("gorm:delete"), p.after(), "after:delete"},
{cb.Update().Before("gorm:update"), p.before("gorm.Update"), "before:update"},
{cb.Update().After("gorm:update"), p.after(), "after:update"},
{cb.Row().Before("gorm:row"), p.before("gorm.Row"), "before:row"},
{cb.Row().After("gorm:row"), p.after(), "after:row"},
{cb.Raw().Before("gorm:raw"), p.before("gorm.Raw"), "before:raw"},
{cb.Raw().After("gorm:raw"), p.after(), "after:raw"},
}
var firstErr error
for _, h := range hooks {
if err := h.callback.Register("otel:"+h.name, h.hook); err != nil && firstErr == nil {
firstErr = fmt.Errorf("callback register %s failed: %w", h.name, err)
}
}
return firstErr
}
func (p *otelPlugin) before(spanName string) gormHookFunc {
return func(tx *gorm.DB) {
_, tx.Statement.Context = highlight.StartTrace(tx.Statement.Context, spanName)
}
}
func (p *otelPlugin) after() gormHookFunc {
return func(tx *gorm.DB) {
span := trace.SpanFromContext(tx.Statement.Context)
defer highlight.EndTrace(span)
attrs := make([]attribute.KeyValue, 0, len(p.attrs)+4)
attrs = append(attrs, p.attrs...)
if sys := dbSystem(tx); sys.Valid() {
attrs = append(attrs, sys)
}
vars := tx.Statement.Vars
var query string
if p.excludeQueryVars {
query = tx.Statement.SQL.String()
} else {
query = tx.Dialector.Explain(tx.Statement.SQL.String(), vars...)
}
attrs = append(attrs, semconv.DBStatementKey.String(query))
if tx.Statement.Table != "" {
attrs = append(attrs, semconv.DBSQLTableKey.String(tx.Statement.Table))
}
if tx.Statement.RowsAffected != -1 {
attrs = append(attrs, dbRowsAffected.Int64(tx.Statement.RowsAffected))
}
span.SetAttributes(attrs...)
switch tx.Error {
case nil,
gorm.ErrRecordNotFound,
driver.ErrSkip,
io.EOF, // end of rows iterator
sql.ErrNoRows:
// ignore
default:
span.RecordError(tx.Error)
span.SetStatus(codes.Error, tx.Error.Error())
}
}
}
func dbSystem(tx *gorm.DB) attribute.KeyValue {
switch tx.Dialector.Name() {
case "mysql":
return semconv.DBSystemMySQL
case "mssql":
return semconv.DBSystemMSSQL
case "postgres", "postgresql":
return semconv.DBSystemPostgreSQL
case "sqlite":
return semconv.DBSystemSqlite
case "sqlserver":
return semconv.DBSystemKey.String("sqlserver")
case "clickhouse":
return semconv.DBSystemKey.String("clickhouse")
default:
return attribute.KeyValue{}
}
}
// WithGormAttributes configures attributes that are used to create a span.
func WithGormAttributes(attrs ...attribute.KeyValue) PluginOption {
return func(p *otelPlugin) {
p.attrs = append(p.attrs, attrs...)
}
}
// WithGormDBName configures a db.name attribute.
func WithGormDBName(name string) PluginOption {
return func(p *otelPlugin) {
p.attrs = append(p.attrs, semconv.DBNameKey.String(name))
}
}
// WithoutGormQueryVariables configures the db.statement attribute to exclude query variables
func WithoutGormQueryVariables() PluginOption {
return func(p *otelPlugin) {
p.excludeQueryVars = true
}
}
func SetupGORMTracing(db *gorm.DB, attrs ...attribute.KeyValue) error {
err := db.Use(
NewGormPlugin(
WithGormAttributes(attrs...),
),
)
if err != nil {
return err
}
return nil
}