-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoc.go
36 lines (30 loc) · 844 Bytes
/
oc.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
package sqlcomment
import (
"context"
"go.opencensus.io/plugin/ochttp/propagation/tracecontext"
"go.opencensus.io/trace"
)
const (
traceparentHeader = "traceparent"
tracestateHeader = "tracestate"
)
// OCTagger is a Tagger that adds `traceparent` and `tracestate` tags to the SQL comment.
type OCTagger struct {
format *tracecontext.HTTPFormat
}
// NewOCTagger adds OC trace information as SQL tags.
func NewOCTagger() OCTagger {
return OCTagger{&tracecontext.HTTPFormat{}}
}
// Tag finds trace information on the given context and returns SQL tags with trace information.
func (ot OCTagger) Tag(ctx context.Context) Tags {
spanCtx := trace.FromContext(ctx).SpanContext()
tp, ts := ot.format.SpanContextToHeaders(spanCtx)
tags := Tags{
traceparentHeader: tp,
}
if ts != "" {
tags[tracestateHeader] = ts
}
return tags
}