/
rotel.go
106 lines (95 loc) · 2.94 KB
/
rotel.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
// Copyright JAMF Software, LLC
package rotel
import (
"context"
"fmt"
"unicode/utf8"
client "github.com/jamf/regatta-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
)
const instrumentationName = "github.com/jamf/regatta-go/plugin/rotel"
// NewTracing returns a new Tracing that adds opentelemetry tracing to the client.
func NewTracing(opts ...Opt) *Tracing {
t := &Tracing{}
for _, opt := range opts {
opt.apply(t)
}
if t.tracerProvider == nil {
t.tracerProvider = otel.GetTracerProvider()
}
t.tracer = t.tracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(semVersion()),
trace.WithSchemaURL(semconv.SchemaURL),
)
return t
}
type Tracing struct {
tracerProvider trace.TracerProvider
tracer trace.Tracer
keyFormatter func([]byte) (string, error)
}
func (t *Tracing) OnKVCall(ctx context.Context, table string, op client.Op, fn client.KvDo) (client.OpResponse, error) {
// Set up the span options.
attrs := []attribute.KeyValue{
semconv.DBSystemKey.String("regatta"),
semconv.DBName(table),
}
spanName, attrs := t.opAttrs(attrs, op)
opts := []trace.SpanStartOption{
trace.WithAttributes(attrs...),
trace.WithSpanKind(trace.SpanKindClient),
}
ctx, span := t.tracer.Start(ctx, spanName, opts...)
defer span.End()
response, err := fn(ctx, table, op)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
return response, err
}
func (t *Tracing) opAttrs(attrs []attribute.KeyValue, op client.Op) (string, []attribute.KeyValue) {
switch {
case op.IsGet():
attrs = append(attrs, semconv.DBOperation("get"))
attrs = append(attrs, semconv.DBStatement(fmt.Sprintf("regatta.v1.kv/Range key: %s rangeEnd: %s", t.formatKey(op.KeyBytes()), t.formatKey(op.RangeBytes()))))
return "regatta/get", attrs
case op.IsPut():
attrs = append(attrs, semconv.DBOperation("put"))
attrs = append(attrs, semconv.DBStatement(fmt.Sprintf("regatta.v1.kv/Put key: %s", t.formatKey(op.KeyBytes()))))
return "regatta/put", attrs
case op.IsDelete():
attrs = append(attrs, semconv.DBOperation("delete"))
attrs = append(attrs, semconv.DBStatement(fmt.Sprintf("regatta.v1.kv/DeleteRange key: %s rangeEnd: %s", t.formatKey(op.KeyBytes()), t.formatKey(op.RangeBytes()))))
return "regatta/delete", attrs
case op.IsTxn():
attrs = append(attrs, semconv.DBOperation("txn"))
attrs = append(attrs, semconv.DBStatement(fmt.Sprintf("regatta.v1.kv/Txn ops")))
return "regatta/txn", attrs
}
return "regatta/unknown", attrs
}
func (t *Tracing) formatKey(bytes []byte) string {
if bytes == nil {
return ""
}
var keykey string
if t.keyFormatter != nil {
k, err := t.keyFormatter(bytes)
if err != nil || !utf8.ValidString(k) {
return ""
}
keykey = k
} else {
if !utf8.Valid(bytes) {
return ""
}
keykey = string(bytes)
}
return keykey
}