This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathotel.go
147 lines (122 loc) · 2.97 KB
/
otel.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
package pgext
import (
"context"
"runtime"
"strings"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
)
var tracer = global.Tracer("github.com/go-pg/pg")
type queryOperation interface {
Operation() orm.QueryOp
}
// OpenTelemetryHook is a pg.QueryHook that adds OpenTelemetry instrumentation.
type OpenTelemetryHook struct{}
var _ pg.QueryHook = (*OpenTelemetryHook)(nil)
func (h OpenTelemetryHook) BeforeQuery(ctx context.Context, _ *pg.QueryEvent) (context.Context, error) {
if !trace.SpanFromContext(ctx).IsRecording() {
return ctx, nil
}
ctx, _ = tracer.Start(ctx, "")
return ctx, nil
}
func (h OpenTelemetryHook) AfterQuery(ctx context.Context, evt *pg.QueryEvent) error {
span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
return nil
}
defer span.End()
var operation orm.QueryOp
if v, ok := evt.Query.(queryOperation); ok {
operation = v.Operation()
}
var query string
if operation == orm.InsertOp {
b, err := evt.UnformattedQuery()
if err != nil {
return err
}
query = string(b)
} else {
b, err := evt.FormattedQuery()
if err != nil {
return err
}
query = string(b)
}
if operation != "" {
span.SetName(string(operation))
} else {
name := query
if idx := strings.IndexByte(name, ' '); idx > 0 {
name = name[:idx]
}
if len(name) > 20 {
name = name[:20]
}
span.SetName(strings.TrimSpace(name))
}
const queryLimit = 5000
if len(query) > queryLimit {
query = query[:queryLimit]
}
fn, file, line := funcFileLine("github.com/go-pg/pg")
attrs := make([]label.KeyValue, 0, 10)
attrs = append(attrs,
label.String("db.system", "postgres"),
label.String("db.statement", query),
label.String("code.function", fn),
label.String("code.filepath", file),
label.Int("code.lineno", line),
)
if db, ok := evt.DB.(*pg.DB); ok {
opt := db.Options()
attrs = append(attrs,
label.String("db.connection_string", opt.Addr),
label.String("db.user", opt.User),
label.String("db.name", opt.Database),
)
}
if evt.Err != nil {
switch evt.Err {
case pg.ErrNoRows, pg.ErrMultiRows:
// nothing
default:
span.RecordError(ctx, evt.Err, trace.WithErrorStatus(codes.Error))
}
} else if evt.Result != nil {
numRow := evt.Result.RowsAffected()
if numRow == 0 {
numRow = evt.Result.RowsReturned()
}
attrs = append(attrs, label.Int("db.rows_affected", numRow))
}
span.SetAttributes(attrs...)
return nil
}
func funcFileLine(pkg string) (string, string, int) {
const depth = 16
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
ff := runtime.CallersFrames(pcs[:n])
var fn, file string
var line int
for {
f, ok := ff.Next()
if !ok {
break
}
fn, file, line = f.Function, f.File, f.Line
if !strings.Contains(fn, pkg) {
break
}
}
if ind := strings.LastIndexByte(fn, '/'); ind != -1 {
fn = fn[ind+1:]
}
return fn, file, line
}