-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
recorder.go
65 lines (51 loc) · 1.72 KB
/
recorder.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
package otelsql
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
)
// float64Recorder adds a new value to the list of Histogram's records.
type float64Recorder = func(ctx context.Context, value float64, opts ...metric.RecordOption)
// int64Counter adds the value to the counter's sum.
type int64Counter = func(ctx context.Context, value int64, opts ...metric.AddOption)
// methodRecorder records metrics about a sql method.
type methodRecorder interface {
Record(ctx context.Context, method string, labels ...attribute.KeyValue) func(err error)
}
type methodRecorderImpl struct {
recordLatency float64Recorder
countCalls int64Counter
attributes []attribute.KeyValue
}
func (r methodRecorderImpl) Record(ctx context.Context, method string, labels ...attribute.KeyValue) func(err error) {
startTime := time.Now()
attrs := make([]attribute.KeyValue, 0, len(r.attributes)+len(labels)+2)
attrs = append(attrs, r.attributes...)
attrs = append(attrs, labels...)
attrs = append(attrs, semconv.DBOperationKey.String(method))
return func(err error) {
elapsedTime := millisecondsSince(startTime)
if err == nil {
attrs = append(attrs, dbSQLStatusOK)
} else {
attrs = append(attrs, dbSQLStatusERROR,
dbSQLError.String(err.Error()),
)
}
r.countCalls(ctx, 1, metric.WithAttributes(attrs...))
r.recordLatency(ctx, elapsedTime, metric.WithAttributes(attrs...))
}
}
func newMethodRecorder(
latencyRecorder float64Recorder,
callsCounter int64Counter,
attrs ...attribute.KeyValue,
) methodRecorderImpl {
return methodRecorderImpl{
recordLatency: latencyRecorder,
countCalls: callsCounter,
attributes: attrs,
}
}