-
Notifications
You must be signed in to change notification settings - Fork 178
/
log_tracer.go
202 lines (174 loc) · 5.46 KB
/
log_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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package trace
import (
"context"
"math/rand"
"time"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"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
// TODO(rbtz): make private
type LogTracer struct {
log zerolog.Logger
provider trace.TracerProvider
}
// NewLogTracer creates a new zerolog-based tracer.
// TODO: Consider switching to go.opentelemetry.io/otel/exporters/stdout/stdouttrace
func NewLogTracer(log zerolog.Logger) *LogTracer {
t := &LogTracer{
log: log,
}
t.provider = &logTracerProvider{tracer: t}
return t
}
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
}
// Start implements trace.Tracer interface.
func (t *LogTracer) Start(ctx context.Context, spanName string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
sp := newLogSpan(t, spanName)
ctx = context.WithValue(ctx, activeSpan, sp.spanID)
return ctx, sp
}
func (t *LogTracer) StartBlockSpan(
ctx context.Context,
blockID flow.Identifier,
spanName SpanName,
opts ...trace.SpanStartOption,
) (trace.Span, context.Context, bool) {
ctx, sp := t.Start(ctx, string(spanName), opts...)
return sp, ctx, true
}
func (t *LogTracer) StartCollectionSpan(
ctx context.Context,
collectionID flow.Identifier,
spanName SpanName,
opts ...trace.SpanStartOption,
) (trace.Span, context.Context, bool) {
ctx, sp := t.Start(ctx, string(spanName), opts...)
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 ...trace.SpanStartOption,
) (trace.Span, context.Context, bool) {
ctx, sp := t.Start(ctx, string(spanName), opts...)
return sp, ctx, true
}
func (t *LogTracer) StartSpanFromContext(
ctx context.Context,
operationName SpanName,
opts ...trace.SpanStartOption,
) (trace.Span, context.Context) {
parentSpanID := ctx.Value(activeSpan).(uint64)
sp := newLogSpanWithParent(t, operationName, parentSpanID)
ctx = context.WithValue(ctx, activeSpan, sp.spanID)
return sp, trace.ContextWithSpan(ctx, sp)
}
func (t *LogTracer) StartSpanFromParent(
span trace.Span,
operationName SpanName,
opts ...trace.SpanStartOption,
) trace.Span {
parentSpan := span.(*logSpan)
return newLogSpanWithParent(t, operationName, parentSpan.spanID)
}
func (t *LogTracer) RecordSpanFromParent(
span trace.Span,
operationName SpanName,
duration time.Duration,
attrs []attribute.KeyValue,
opts ...trace.SpanStartOption,
) {
parentSpan := span.(*logSpan)
sp := newLogSpanWithParent(t, operationName, parentSpan.spanID)
sp.start = time.Now().Add(-duration)
span.End()
}
// 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 ...trace.SpanStartOption,
) {
span, _ := t.StartSpanFromContext(ctx, operationName, opts...)
defer span.End()
f()
}
var _ trace.Span = &logSpan{}
type logSpan struct {
tracer *LogTracer
spanID uint64
parentID uint64
operationName string
start time.Time
end time.Time
attrs map[attribute.Key]attribute.Value
}
func newLogSpan(tracer *LogTracer, operationName string) *logSpan {
return &logSpan{
tracer: tracer,
spanID: rand.Uint64(),
operationName: operationName,
start: time.Now(),
attrs: make(map[attribute.Key]attribute.Value),
}
}
func newLogSpanWithParent(tracer *LogTracer, operationName SpanName, parentSpanID uint64) *logSpan {
sp := newLogSpan(tracer, string(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).
TimeDiff("duration", s.end, s.start).
Str("operationName", string(s.operationName)).
Interface("attrs", s.attrs).
Msg("span")
}
func (s *logSpan) End(...trace.SpanEndOption) {
s.end = time.Now()
s.produceLog()
}
func (s *logSpan) SpanContext() trace.SpanContext { return trace.SpanContext{} }
func (s *logSpan) IsRecording() bool { return s.end == time.Time{} }
func (s *logSpan) SetStatus(codes.Code, string) {}
func (s *logSpan) SetError(bool) {}
func (s *logSpan) SetAttributes(attrs ...attribute.KeyValue) {
for _, f := range attrs {
s.attrs[f.Key] = f.Value
}
}
func (s *logSpan) RecordError(error, ...trace.EventOption) {}
func (s *logSpan) AddEvent(string, ...trace.EventOption) {}
func (s *logSpan) SetName(string) {}
func (s *logSpan) TracerProvider() trace.TracerProvider { return s.tracer.provider }
type logTracerProvider struct {
tracer *LogTracer
}
func (ltp *logTracerProvider) Tracer(instrumentationName string, _ ...trace.TracerOption) trace.Tracer {
return ltp.tracer
}