-
Notifications
You must be signed in to change notification settings - Fork 71
/
otel.go
252 lines (213 loc) · 6.74 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
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
package otelsql
import (
"context"
"database/sql"
"database/sql/driver"
"io"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
)
const instrumName = "github.com/uptrace/opentelemetry-go-extra/otelsql"
var dbRowsAffected = attribute.Key("db.rows_affected")
type config struct {
tracerProvider trace.TracerProvider
tracer trace.Tracer //nolint:structcheck
meterProvider metric.MeterProvider
meter metric.Meter
attrs []attribute.KeyValue
queryFormatter func(query string) string
}
func newConfig(opts []Option) *config {
c := &config{
tracerProvider: otel.GetTracerProvider(),
meterProvider: otel.GetMeterProvider(),
}
for _, opt := range opts {
opt(c)
}
return c
}
func (c *config) formatQuery(query string) string {
if c.queryFormatter != nil {
return c.queryFormatter(query)
}
return query
}
type dbInstrum struct {
*config
queryHistogram metric.Int64Histogram
}
func newDBInstrum(opts []Option) *dbInstrum {
t := &dbInstrum{
config: newConfig(opts),
}
if t.tracer == nil {
t.tracer = t.tracerProvider.Tracer(instrumName)
}
if t.meter == nil {
t.meter = t.meterProvider.Meter(instrumName)
}
var err error
t.queryHistogram, err = t.meter.Int64Histogram(
"go.sql.query_timing",
metric.WithDescription("Timing of processed queries"),
metric.WithUnit("milliseconds"),
)
if err != nil {
panic(err)
}
return t
}
func (t *dbInstrum) withSpan(
ctx context.Context,
spanName string,
query string,
fn func(ctx context.Context, span trace.Span) error,
) error {
var startTime time.Time
if query != "" {
startTime = time.Now()
}
attrs := make([]attribute.KeyValue, 0, len(t.attrs)+1)
attrs = append(attrs, t.attrs...)
if query != "" {
attrs = append(attrs, semconv.DBStatementKey.String(t.formatQuery(query)))
}
ctx, span := t.tracer.Start(ctx, spanName,
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(attrs...))
err := fn(ctx, span)
defer span.End()
if query != "" {
t.queryHistogram.Record(ctx, time.Since(startTime).Milliseconds(), metric.WithAttributes(t.attrs...))
}
if !span.IsRecording() {
return err
}
switch err {
case nil,
driver.ErrSkip,
io.EOF, // end of rows iterator
sql.ErrNoRows:
// ignore
default:
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
return err
}
type Option func(c *config)
// WithTracerProvider configures a tracer provider that is used to create a tracer.
func WithTracerProvider(tracerProvider trace.TracerProvider) Option {
return func(c *config) {
c.tracerProvider = tracerProvider
}
}
// WithAttributes configures attributes that are used to create a span.
func WithAttributes(attrs ...attribute.KeyValue) Option {
return func(c *config) {
c.attrs = append(c.attrs, attrs...)
}
}
// WithDBSystem configures a db.system attribute. You should prefer using
// WithAttributes and semconv, for example, `otelsql.WithAttributes(semconv.DBSystemSqlite)`.
func WithDBSystem(system string) Option {
return func(c *config) {
c.attrs = append(c.attrs, semconv.DBSystemKey.String(system))
}
}
// WithDBName configures a db.name attribute.
func WithDBName(name string) Option {
return func(c *config) {
c.attrs = append(c.attrs, semconv.DBNameKey.String(name))
}
}
// WithMeterProvider configures a metric.Meter used to create instruments.
func WithMeterProvider(meterProvider metric.MeterProvider) Option {
return func(c *config) {
c.meterProvider = meterProvider
}
}
// WithQueryFormatter configures a query formatter
func WithQueryFormatter(queryFormatter func(query string) string) Option {
return func(c *config) {
c.queryFormatter = queryFormatter
}
}
// ReportDBStatsMetrics reports DBStats metrics using OpenTelemetry Metrics API.
func ReportDBStatsMetrics(db *sql.DB, opts ...Option) {
cfg := newConfig(opts)
if cfg.meter == nil {
cfg.meter = cfg.meterProvider.Meter(instrumName)
}
meter := cfg.meter
labels := cfg.attrs
maxOpenConns, _ := meter.Int64ObservableGauge(
"go.sql.connections_max_open",
metric.WithDescription("Maximum number of open connections to the database"),
)
openConns, _ := meter.Int64ObservableGauge(
"go.sql.connections_open",
metric.WithDescription("The number of established connections both in use and idle"),
)
inUseConns, _ := meter.Int64ObservableGauge(
"go.sql.connections_in_use",
metric.WithDescription("The number of connections currently in use"),
)
idleConns, _ := meter.Int64ObservableGauge(
"go.sql.connections_idle",
metric.WithDescription("The number of idle connections"),
)
connsWaitCount, _ := meter.Int64ObservableCounter(
"go.sql.connections_wait_count",
metric.WithDescription("The total number of connections waited for"),
)
connsWaitDuration, _ := meter.Int64ObservableCounter(
"go.sql.connections_wait_duration",
metric.WithDescription("The total time blocked waiting for a new connection"),
metric.WithUnit("nanoseconds"),
)
connsClosedMaxIdle, _ := meter.Int64ObservableCounter(
"go.sql.connections_closed_max_idle",
metric.WithDescription("The total number of connections closed due to SetMaxIdleConns"),
)
connsClosedMaxIdleTime, _ := meter.Int64ObservableCounter(
"go.sql.connections_closed_max_idle_time",
metric.WithDescription("The total number of connections closed due to SetConnMaxIdleTime"),
)
connsClosedMaxLifetime, _ := meter.Int64ObservableCounter(
"go.sql.connections_closed_max_lifetime",
metric.WithDescription("The total number of connections closed due to SetConnMaxLifetime"),
)
if _, err := meter.RegisterCallback(
func(ctx context.Context, o metric.Observer) error {
stats := db.Stats()
o.ObserveInt64(maxOpenConns, int64(stats.MaxOpenConnections), metric.WithAttributes(labels...))
o.ObserveInt64(openConns, int64(stats.OpenConnections), metric.WithAttributes(labels...))
o.ObserveInt64(inUseConns, int64(stats.InUse), metric.WithAttributes(labels...))
o.ObserveInt64(idleConns, int64(stats.Idle), metric.WithAttributes(labels...))
o.ObserveInt64(connsWaitCount, stats.WaitCount, metric.WithAttributes(labels...))
o.ObserveInt64(connsWaitDuration, int64(stats.WaitDuration), metric.WithAttributes(labels...))
o.ObserveInt64(connsClosedMaxIdle, stats.MaxIdleClosed, metric.WithAttributes(labels...))
o.ObserveInt64(connsClosedMaxIdleTime, stats.MaxIdleTimeClosed, metric.WithAttributes(labels...))
o.ObserveInt64(connsClosedMaxLifetime, stats.MaxLifetimeClosed, metric.WithAttributes(labels...))
return nil
},
maxOpenConns,
openConns,
inUseConns,
idleConns,
connsWaitCount,
connsWaitDuration,
connsClosedMaxIdle,
connsClosedMaxIdleTime,
connsClosedMaxLifetime,
); err != nil {
panic(err)
}
}