/
trace_context.go
43 lines (35 loc) · 1.33 KB
/
trace_context.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
package grpc
import (
"context"
"encoding/json"
"log"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"go.opentelemetry.io/otel/propagation"
)
func CreateCarrierFromContext(ctx context.Context) *proto.TraceContext {
// Inject trace context information from context onto the carrier
carrier := propagation.MapCarrier{}
propagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
propagator.Inject(ctx, carrier)
// Transform carrier data to be sent back as a string value
carrierData, err := json.Marshal(carrier)
if err != nil {
return &proto.TraceContext{Value: ""}
}
return &proto.TraceContext{Value: string(carrierData)}
}
func ExtractContextFromCarrier(ctx context.Context, traceCtx *proto.TraceContext) context.Context {
if traceCtx == nil || len(traceCtx.Value) == 0 {
return ctx
}
propagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
carrier := propagation.MapCarrier{}
// Convert raw trace context data into MapCarrier
err := json.Unmarshal([]byte(traceCtx.Value), &carrier)
if err != nil {
log.Printf("[WARN] ExtractContextFromCarrier unmarshal error: %s", err.Error())
return ctx
}
// Frame a new context with extracted trace context information from carrier
return propagator.Extract(ctx, carrier)
}