This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
forked from instana/go-sensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recorder.go
176 lines (148 loc) · 3.93 KB
/
recorder.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
package instana
import (
"sync"
"time"
)
// A SpanRecorder handles all of the `RawSpan` data generated via an
// associated `Tracer` (see `NewStandardTracer`) instance. It also names
// the containing process and provides access to a straightforward tag map.
type SpanRecorder interface {
// Implementations must determine whether and where to store `span`.
RecordSpan(span *spanS)
}
// Recorder accepts spans, processes and queues them
// for delivery to the backend.
type Recorder struct {
sync.RWMutex
spans []jsonSpan
testMode bool
}
// NewRecorder Establish a Recorder span recorder
func NewRecorder() *Recorder {
r := new(Recorder)
r.init()
return r
}
// NewTestRecorder Establish a new span recorder used for testing
func NewTestRecorder() *Recorder {
r := new(Recorder)
r.testMode = true
r.init()
return r
}
func (r *Recorder) init() {
r.clearQueuedSpans()
if r.testMode {
return
}
ticker := time.NewTicker(1 * time.Second)
go func() {
for range ticker.C {
if sensor.agent.canSend() {
r.send()
}
}
}()
}
// RecordSpan accepts spans to be recorded and and added to the span queue
// for eventual reporting to the host agent.
func (r *Recorder) RecordSpan(span *spanS) {
// If we're not announced and not in test mode then just
// return
if !r.testMode && !sensor.agent.canSend() {
return
}
var data = &jsonData{}
kindTag := span.getSpanKindTag()
data.SDK = &jsonSDKData{
Name: span.Operation,
Type: kindTag,
Custom: &jsonCustomData{Tags: span.Tags, Logs: span.collectLogs()}}
baggage := make(map[string]string)
span.context.ForeachBaggageItem(func(k string, v string) bool {
baggage[k] = v
return true
})
if len(baggage) > 0 {
data.SDK.Custom.Baggage = baggage
}
data.Service = sensor.serviceName
var parentID *int64
if span.ParentSpanID == 0 {
parentID = nil
} else {
parentID = &span.ParentSpanID
}
r.Lock()
defer r.Unlock()
if len(r.spans) == sensor.options.MaxBufferedSpans {
r.spans = r.spans[1:]
}
r.spans = append(r.spans, jsonSpan{
TraceID: span.context.TraceID,
ParentID: parentID,
SpanID: span.context.SpanID,
Timestamp: uint64(span.Start.UnixNano()) / uint64(time.Millisecond),
Duration: uint64(span.Duration) / uint64(time.Millisecond),
Name: "sdk",
Error: span.Error,
Ec: span.Ec,
Lang: "go",
From: sensor.agent.from,
Kind: span.getSpanKindInt(),
Data: data})
if r.testMode || !sensor.agent.canSend() {
return
}
if len(r.spans) >= sensor.options.ForceTransmissionStartingAt {
log.debug("Forcing spans to agent. Count:", len(r.spans))
go r.send()
}
}
// QueuedSpansCount returns the number of queued spans
// Used only in tests currently.
func (r *Recorder) QueuedSpansCount() int {
r.RLock()
defer r.RUnlock()
return len(r.spans)
}
// GetQueuedSpans returns a copy of the queued spans and clears the queue.
func (r *Recorder) GetQueuedSpans() []jsonSpan {
r.Lock()
defer r.Unlock()
// Copy queued spans
queuedSpans := make([]jsonSpan, len(r.spans))
copy(queuedSpans, r.spans)
// and clear out the source
r.clearQueuedSpans()
return queuedSpans
}
// clearQueuedSpans brings the span queue to empty/0/nada
// This function doesn't take the Lock so make sure to have
// the write lock before calling.
// This is meant to be called from GetQueuedSpans which handles
// locking.
func (r *Recorder) clearQueuedSpans() {
var mbs int
if len(r.spans) > 0 {
if sensor != nil {
mbs = sensor.options.MaxBufferedSpans
} else {
mbs = DefaultMaxBufferedSpans
}
r.spans = make([]jsonSpan, 0, mbs)
}
}
// Retrieve the queued spans and post them to the host agent asynchronously.
func (r *Recorder) send() {
spansToSend := r.GetQueuedSpans()
if len(spansToSend) > 0 {
go func() {
_, err := sensor.agent.request(sensor.agent.makeURL(agentTracesURL), "POST", spansToSend)
if err != nil {
log.debug("Posting traces failed in send(): ", err)
sensor.agent.reset()
}
}()
}
}