-
Notifications
You must be signed in to change notification settings - Fork 67
/
traceing.go
74 lines (64 loc) · 1.91 KB
/
traceing.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
package drpc
import (
"encoding/json"
"go.opentelemetry.io/otel/trace"
)
/*
TraceCarrier is required to Marshal and Unmarshall trace.SpanContext across RPC boundaries since the
OpenTelemetry spec dictates trace.SpanContext must be read only, and therefore does not implement a
JSON unmarshaller.
Context: https://github.com/open-telemetry/opentelemetry-go/issues/1927#issuecomment-842663910
*/
// NewTraceCarrier accepts a trace.SpanContext and returns a TraceCarrier.
func NewTraceCarrier(sc trace.SpanContext) *TraceCarrier {
if sc.IsValid() {
return &TraceCarrier{
TraceID: sc.TraceID(),
SpanID: sc.SpanID(),
Remote: sc.IsRemote(),
}
}
return nil
}
// TraceCarrier is a wrapper that allows trace.SpanContext's to be round-tripped through JSON.
type TraceCarrier struct {
TraceID trace.TraceID `json:"traceID"`
SpanID trace.SpanID `json:"spanID"`
Remote bool `json:"remote"`
}
//MarshalJSON converts TraceCarrier to a trace.SpanContext and marshals it to JSON.
func (c *TraceCarrier) MarshalJSON() ([]byte, error) {
return c.AsSpanContext().MarshalJSON()
}
// UnmarshalJSON unmarshalls a serialized trace.SpanContext to a TraceCarrier.
func (c *TraceCarrier) UnmarshalJSON(b []byte) error {
var data traceCarrierInfo
if err := json.Unmarshal(b, &data); err != nil {
return err
}
var err error
c.TraceID, err = trace.TraceIDFromHex(data.TraceID)
if err != nil {
return err
}
c.SpanID, err = trace.SpanIDFromHex(data.SpanID)
if err != nil {
return err
}
c.Remote = data.Remote
return nil
}
// AsSpanContext converts TraceCarrier to a trace.SpanContext.
func (c *TraceCarrier) AsSpanContext() trace.SpanContext {
return trace.NewSpanContext(trace.SpanContextConfig{
TraceID: c.TraceID,
SpanID: c.SpanID,
Remote: c.Remote,
})
}
// carrierInfo is a helper used to deserialize a SpanContext from JSON.
type traceCarrierInfo struct {
TraceID string
SpanID string
Remote bool
}