-
Notifications
You must be signed in to change notification settings - Fork 175
/
tracer.go
71 lines (57 loc) · 1.37 KB
/
tracer.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
package environment
import (
"time"
"go.opentelemetry.io/otel/attribute"
otelTrace "go.opentelemetry.io/otel/trace"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/trace"
)
type TracerParams struct {
module.Tracer
ExtensiveTracing bool
RootSpan otelTrace.Span
}
func DefaultTracerParams() TracerParams {
// NOTE: RootSpan is set by NewTransactionEnv rather by Context
return TracerParams{
Tracer: nil,
ExtensiveTracing: false,
}
}
type Tracer struct {
TracerParams
}
func NewTracer(params TracerParams) *Tracer {
return &Tracer{params}
}
func (tracer *Tracer) isTraceable() bool {
return tracer.Tracer != nil && tracer.RootSpan != nil
}
func (tracer *Tracer) StartSpanFromRoot(name trace.SpanName) otelTrace.Span {
if tracer.isTraceable() {
return tracer.Tracer.StartSpanFromParent(tracer.RootSpan, name)
}
return trace.NoopSpan
}
func (tracer *Tracer) StartExtensiveTracingSpanFromRoot(
name trace.SpanName,
) otelTrace.Span {
if tracer.isTraceable() && tracer.ExtensiveTracing {
return tracer.Tracer.StartSpanFromParent(tracer.RootSpan, name)
}
return trace.NoopSpan
}
func (tracer *Tracer) RecordSpanFromRoot(
spanName trace.SpanName,
duration time.Duration,
attrs []attribute.KeyValue,
) {
if !tracer.isTraceable() {
return
}
tracer.Tracer.RecordSpanFromParent(
tracer.RootSpan,
spanName,
duration,
attrs)
}