-
Notifications
You must be signed in to change notification settings - Fork 5
/
traceinfo.go
48 lines (41 loc) · 1.34 KB
/
traceinfo.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
package trace
import (
"context"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
// TraceInfo carries the trace informations
type TraceInfo struct {
ID string
IsSampled bool
}
// GetTraceInfoFromContext returns a TraceInfo from the context @ctx or logs
// if there is no TraceInfo in context
func GetTraceInfoFromContext(ctx context.Context) TraceInfo {
sc := trace.SpanContextFromContext(ctx)
t := TraceInfo{
ID: sc.TraceID().String(),
IsSampled: sc.IsSampled(),
}
if t.ID != "" {
return t
}
log.Warn().
Str("method", "trace.GetTraceInfoFromContext").
Msg("[FoundationKit] There is no Trace Info in context.")
return TraceInfo{}
}
// ToMap extracts the current trace from the context and put it on a map.
// This can be used to serialize the trace context on json messages.
func ToMap(ctx context.Context) map[string]string {
m := map[string]string{}
otel.GetTextMapPropagator().Inject(ctx, propagation.MapCarrier(m))
return m
}
// FromMap injects the trace context from the map into the context. This is the
// inverse of ToMap and could be used to extract trace context form json messages.
func FromMap(ctx context.Context, m map[string]string) context.Context {
return otel.GetTextMapPropagator().Extract(ctx, propagation.MapCarrier(m))
}