-
Notifications
You must be signed in to change notification settings - Fork 178
/
logTracer.go
185 lines (163 loc) · 5.33 KB
/
logTracer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package trace
import (
"context"
"math/rand"
"time"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/model/flow"
)
type spanKey string
const activeSpan spanKey = "activeSpan"
// LogTracer is the implementation of the Tracer interface which passes
// all the traces back to the passed logger and print them
// this is mostly useful for debugging and testing
type LogTracer struct {
log zerolog.Logger
}
// LogTracer creates a new tracer.
func NewLogTracer(log zerolog.Logger) *LogTracer {
return &LogTracer{log: log}
}
func (t *LogTracer) Ready() <-chan struct{} {
ready := make(chan struct{})
close(ready)
return ready
}
func (t *LogTracer) Done() <-chan struct{} {
done := make(chan struct{})
close(done)
return done
}
func (t *LogTracer) StartBlockSpan(
ctx context.Context,
blockID flow.Identifier,
spanName SpanName,
opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context, bool) {
sp := NewLogSpan(t, spanName)
ctx = context.WithValue(ctx, activeSpan, sp.spanID)
return sp, ctx, true
}
func (t *LogTracer) StartCollectionSpan(
ctx context.Context,
collectionID flow.Identifier,
spanName SpanName,
opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context, bool) {
sp := NewLogSpan(t, spanName)
ctx = context.WithValue(ctx, activeSpan, sp.spanID)
return sp, ctx, true
}
// StartTransactionSpan starts a span that will be aggregated under the given transaction.
// All spans for the same transaction will be aggregated under a root span
func (t *LogTracer) StartTransactionSpan(
ctx context.Context,
transactionID flow.Identifier,
spanName SpanName,
opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context, bool) {
sp := NewLogSpan(t, spanName)
ctx = context.WithValue(ctx, activeSpan, sp.spanID)
return sp, ctx, true
}
func (t *LogTracer) StartSpanFromContext(
ctx context.Context,
operationName SpanName,
opts ...opentracing.StartSpanOption,
) (opentracing.Span, context.Context) {
parentSpanID := ctx.Value(activeSpan).(uint64)
sp := NewLogSpanWithParent(t, operationName, parentSpanID)
ctx = context.WithValue(ctx, activeSpan, sp.spanID)
return sp, opentracing.ContextWithSpan(ctx, sp)
}
func (t *LogTracer) StartSpanFromParent(
span opentracing.Span,
operationName SpanName,
opts ...opentracing.StartSpanOption,
) opentracing.Span {
parentSpan := span.(*LogSpan)
return NewLogSpanWithParent(t, operationName, parentSpan.spanID)
}
func (t *LogTracer) RecordSpanFromParent(
span opentracing.Span,
operationName SpanName,
duration time.Duration,
logs []opentracing.LogRecord,
opts ...opentracing.StartSpanOption,
) {
parentSpan := span.(*LogSpan)
sp := NewLogSpanWithParent(t, operationName, parentSpan.spanID)
sp.start = time.Now().Add(-duration)
sp.Finish()
}
// WithSpanFromContext encapsulates executing a function within an span, i.e., it starts a span with the specified SpanName from the context,
// executes the function f, and finishes the span once the function returns.
func (t *LogTracer) WithSpanFromContext(ctx context.Context,
operationName SpanName,
f func(),
opts ...opentracing.StartSpanOption) {
span, _ := t.StartSpanFromContext(ctx, operationName, opts...)
defer span.Finish()
f()
}
type LogSpan struct {
tracer *LogTracer
spanID uint64
parentID uint64
operationName SpanName
start time.Time
end time.Time
tags map[string]interface{}
}
func NewLogSpan(tracer *LogTracer, operationName SpanName) *LogSpan {
return &LogSpan{
tracer: tracer,
spanID: rand.Uint64(),
operationName: operationName,
start: time.Now(),
tags: make(map[string]interface{}),
}
}
func NewLogSpanWithParent(tracer *LogTracer, operationName SpanName, parentSpanID uint64) *LogSpan {
sp := NewLogSpan(tracer, operationName)
sp.parentID = parentSpanID
return sp
}
func (s *LogSpan) ProduceLog() {
s.tracer.log.Info().
Uint64("spanID", s.spanID).
Uint64("parent", s.parentID).
Time("start", s.start).
Time("end", s.end).
Msgf("Span %s (duration %d ms)", s.operationName, s.end.Sub(s.start).Milliseconds())
}
func (s *LogSpan) Finish() {
s.end = time.Now()
s.ProduceLog()
}
func (s *LogSpan) FinishWithOptions(opts opentracing.FinishOptions) {
// TODO support finish options
s.Finish()
}
func (s *LogSpan) Context() opentracing.SpanContext {
return &NoopSpanContext{}
}
func (s *LogSpan) SetOperationName(operationName string) opentracing.Span {
s.operationName = SpanName(operationName)
return s
}
func (s *LogSpan) SetTag(key string, value interface{}) opentracing.Span {
s.tags[key] = value
return s
}
func (s *LogSpan) LogFields(fields ...log.Field) {
for _, f := range fields {
s.tags[f.Key()] = f.Value()
}
}
func (s *LogSpan) LogKV(alternatingKeyValues ...interface{}) {}
func (s *LogSpan) SetBaggageItem(restrictedKey, value string) opentracing.Span { return s }
func (s *LogSpan) BaggageItem(restrictedKey string) string { return "" }
func (s *LogSpan) Tracer() opentracing.Tracer { return nil }
func (s *LogSpan) LogEvent(event string) {}
func (s *LogSpan) LogEventWithPayload(event string, payload interface{}) {}
func (s *LogSpan) Log(data opentracing.LogData) {}