forked from lightstep/lightstep-tracer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
330 lines (266 loc) · 7.54 KB
/
events.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package lightstep
import (
"errors"
"fmt"
"reflect"
"time"
"github.com/opentracing/opentracing-go"
)
// An Event is emitted by the LightStep tracer as a reporting mechanism. They are
// handled by registering an EventHandler callback via SetGlobalEventHandler. The
// emitted events may be cast to specific event types in order access additional
// information.
//
// NOTE: To ensure that events can be accurately identified, each event type contains
// a sentinel method matching the name of the type. This method is a no-op, it is
// only used for type coersion.
type Event interface {
Event()
String() string
}
// The ErrorEvent type can be used to filter events. The `Err` method
// retuns the underlying error.
type ErrorEvent interface {
Event
error
Err() error
}
// EventStartError occurs if the Options passed to NewTracer are invalid, and
// the Tracer has failed to start.
type EventStartError interface {
ErrorEvent
EventStartError()
}
type eventStartError struct {
err error
}
func newEventStartError(err error) *eventStartError {
return &eventStartError{err: err}
}
func (*eventStartError) Event() {}
func (*eventStartError) EventStartError() {}
func (e *eventStartError) String() string {
return e.err.Error()
}
func (e *eventStartError) Error() string {
return e.err.Error()
}
func (e *eventStartError) Err() error {
return e.err
}
// EventFlushErrorState lists the possible causes for a flush to fail.
type EventFlushErrorState string
// Constant strings corresponding to flush errors
const (
FlushErrorTracerClosed EventFlushErrorState = "flush failed, the tracer is closed."
FlushErrorTracerDisabled EventFlushErrorState = "flush failed, the tracer is disabled."
FlushErrorTransport EventFlushErrorState = "flush failed, could not send report to Collector"
FlushErrorReport EventFlushErrorState = "flush failed, report contained errors"
FlushErrorTranslate EventFlushErrorState = "flush failed, could not translate report"
)
var (
errFlushFailedTracerClosed = errors.New(string(FlushErrorTracerClosed))
)
// EventFlushError occurs when a flush fails to send. Call the `State` method to
// determine the type of error.
type EventFlushError interface {
ErrorEvent
EventFlushError()
State() EventFlushErrorState
}
type eventFlushError struct {
err error
state EventFlushErrorState
}
func newEventFlushError(err error, state EventFlushErrorState) *eventFlushError {
return &eventFlushError{err: err, state: state}
}
func (*eventFlushError) Event() {}
func (*eventFlushError) EventFlushError() {}
func (e *eventFlushError) State() EventFlushErrorState {
return e.state
}
func (e *eventFlushError) String() string {
return e.err.Error()
}
func (e *eventFlushError) Error() string {
return e.err.Error()
}
func (e *eventFlushError) Err() error {
return e.err
}
// EventConnectionError occurs when the tracer fails to maintain it's connection
// with the Collector.
type EventConnectionError interface {
ErrorEvent
EventConnectionError()
}
type eventConnectionError struct {
err error
}
func newEventConnectionError(err error) *eventConnectionError {
return &eventConnectionError{err: err}
}
func (*eventConnectionError) Event() {}
func (*eventConnectionError) EventConnectionError() {}
func (e *eventConnectionError) String() string {
return e.err.Error()
}
func (e *eventConnectionError) Error() string {
return e.err.Error()
}
func (e *eventConnectionError) Err() error {
return e.err
}
// EventStatusReport occurs on every successful flush. It contains all metrics
// collected since the previous succesful flush.
type EventStatusReport interface {
Event
EventStatusReport()
StartTime() time.Time
FinishTime() time.Time
Duration() time.Duration
SentSpans() int
DroppedSpans() int
EncodingErrors() int
}
type eventStatusReport struct {
startTime time.Time
finishTime time.Time
sentSpans int
droppedSpans int
encodingErrors int
}
func newEventStatusReport(
startTime, finishTime time.Time,
sentSpans, droppedSpans, encodingErrors int,
) *eventStatusReport {
return &eventStatusReport{
startTime: startTime,
finishTime: finishTime,
sentSpans: sentSpans,
droppedSpans: droppedSpans,
encodingErrors: encodingErrors,
}
}
func (*eventStatusReport) Event() {}
func (*eventStatusReport) EventStatusReport() {}
func (s *eventStatusReport) SetSentSpans(sent int) {
s.sentSpans = sent
}
func (s *eventStatusReport) StartTime() time.Time {
return s.startTime
}
func (s *eventStatusReport) FinishTime() time.Time {
return s.finishTime
}
func (s *eventStatusReport) Duration() time.Duration {
return s.finishTime.Sub(s.startTime)
}
func (s *eventStatusReport) SentSpans() int {
return s.sentSpans
}
func (s *eventStatusReport) DroppedSpans() int {
return s.droppedSpans
}
func (s *eventStatusReport) EncodingErrors() int {
return s.encodingErrors
}
func (s *eventStatusReport) String() string {
return fmt.Sprint(
"STATUS REPORT start: ", s.startTime,
", end: ", s.finishTime,
", dropped spans: ", s.droppedSpans,
", encoding errors: ", s.encodingErrors,
)
}
// EventUnsupportedTracer occurs when a tracer being passed to a helper function
// fails to typecast as a LightStep tracer.
type EventUnsupportedTracer interface {
ErrorEvent
EventUnsupportedTracer()
Tracer() opentracing.Tracer
}
type eventUnsupportedTracer struct {
tracer opentracing.Tracer
err error
}
func newEventUnsupportedTracer(tracer opentracing.Tracer) EventUnsupportedTracer {
return &eventUnsupportedTracer{
tracer: tracer,
err: fmt.Errorf("unsupported tracer type: %v", reflect.TypeOf(tracer)),
}
}
func (e *eventUnsupportedTracer) Event() {}
func (e *eventUnsupportedTracer) EventUnsupportedTracer() {}
func (e *eventUnsupportedTracer) Tracer() opentracing.Tracer {
return e.tracer
}
func (e *eventUnsupportedTracer) String() string {
return e.err.Error()
}
func (e *eventUnsupportedTracer) Error() string {
return e.err.Error()
}
func (e *eventUnsupportedTracer) Err() error {
return e.err
}
// EventUnsupportedValue occurs when a tracer encounters an unserializable tag
// or log field.
type EventUnsupportedValue interface {
ErrorEvent
EventUnsupportedValue()
Key() string
Value() interface{}
}
type eventUnsupportedValue struct {
key string
value interface{}
err error
}
func newEventUnsupportedValue(key string, value interface{}, err error) EventUnsupportedValue {
if err == nil {
err = fmt.Errorf(
"value `%v` of type `%T` for key `%s` is an unsupported type",
value, value, key,
)
}
return &eventUnsupportedValue{
key: key,
value: value,
err: err,
}
}
func (e *eventUnsupportedValue) Event() {}
func (e *eventUnsupportedValue) EventUnsupportedValue() {}
func (e *eventUnsupportedValue) Key() string {
return e.key
}
func (e *eventUnsupportedValue) Value() interface{} {
return e.value
}
func (e *eventUnsupportedValue) String() string {
return e.err.Error()
}
func (e *eventUnsupportedValue) Error() string {
return e.err.Error()
}
func (e *eventUnsupportedValue) Err() error {
return e.err
}
const tracerDisabled = "the tracer has been disabled"
// EventTracerDisabled occurs when a tracer is disabled by either the user or
// the collector.
type EventTracerDisabled interface {
Event
EventTracerDisabled()
}
type eventTracerDisabled struct{}
func newEventTracerDisabled() EventTracerDisabled {
return eventTracerDisabled{}
}
func (eventTracerDisabled) Event() {}
func (eventTracerDisabled) EventTracerDisabled() {}
func (eventTracerDisabled) String() string {
return tracerDisabled
}