-
Notifications
You must be signed in to change notification settings - Fork 6
/
contract_telemeter.go
83 lines (67 loc) · 1.99 KB
/
contract_telemeter.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
package telemetry
import (
"context"
"github.com/hyperledger/fabric-chaincode-go/shim"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
type TraceContext struct {
ctx context.Context
remote bool
remoteCtx context.Context
}
type TracingHandler struct {
Tracer trace.Tracer
Propagators propagation.TextMapPropagator
isInit bool
}
// TracingIsInit checks if telemetry was initialized
func (th *TracingHandler) TracingIsInit() bool {
return th.isInit
}
// TracingInit sets tracing telemetry init param as true
func (th *TracingHandler) TracingInit() {
th.isInit = true
}
// StartNewSpan starts new span
func (th *TracingHandler) StartNewSpan(traceCtx TraceContext, spanName string, opts ...trace.SpanStartOption) (TraceContext, trace.Span) {
if traceCtx.ctx == nil {
traceCtx.ctx = context.Background()
}
ctx, span := th.Tracer.Start(traceCtx.ctx, spanName, opts...)
return TraceContext{
ctx: ctx,
remote: traceCtx.remote,
remoteCtx: traceCtx.remoteCtx,
}, span
}
func (th *TracingHandler) ContextFromStub(stub shim.ChaincodeStubInterface) TraceContext {
traceCtx := TraceContext{
ctx: th.Propagators.Extract(context.Background(), propagation.MapCarrier{}),
}
transientMap, err := stub.GetTransient()
if err != nil {
return traceCtx
}
carrier, err := UnpackTransientMap(transientMap)
if err != nil {
return traceCtx
}
traceCtx.ctx = th.Propagators.Extract(context.Background(), carrier)
traceCtx.remote = trace.SpanContextFromContext(traceCtx.ctx).IsRemote()
traceCtx.remoteCtx = traceCtx.ctx
return traceCtx
}
func (th *TracingHandler) RemoteCarrier(traceCtx TraceContext) propagation.MapCarrier {
carrier := propagation.MapCarrier{}
if !traceCtx.remote {
return carrier
}
th.Propagators.Inject(traceCtx.remoteCtx, carrier)
return carrier
}
func (th *TracingHandler) ExtractContext(carrier propagation.MapCarrier) TraceContext {
return TraceContext{
ctx: th.Propagators.Extract(context.Background(), carrier),
}
}