-
Notifications
You must be signed in to change notification settings - Fork 535
Expand file tree
/
Copy pathstats.go
More file actions
310 lines (278 loc) · 10.2 KB
/
Copy pathstats.go
File metadata and controls
310 lines (278 loc) · 10.2 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package tracer
import (
"sync"
"sync/atomic"
"time"
"github.com/DataDog/datadog-agent/pkg/obfuscate"
"github.com/DataDog/datadog-agent/pkg/trace/stats"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/internal"
"github.com/DataDog/dd-trace-go/v2/internal/civisibility/constants"
"github.com/DataDog/dd-trace-go/v2/internal/civisibility/utils"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/processtags"
"github.com/DataDog/datadog-go/v5/statsd"
)
// tracerObfuscationVersion indicates which version of stats obfuscation logic we implement
// In the future this can be pulled directly from our obfuscation import.
var tracerObfuscationVersion = 1
// defaultStatsBucketSize specifies the default span of time that will be
// covered in one stats bucket.
var defaultStatsBucketSize = (10 * time.Second).Nanoseconds()
// statsConcentrator abstracts the stats-computation lifecycle so that callers
// don't need nil checks when stats are disabled (e.g. OTLP export mode).
type statsConcentrator interface {
Start()
Stop()
flushAndSend(now time.Time, includeCurrent bool)
newTracerStatSpan(s *Span, obfuscator *obfuscate.Obfuscator) (*tracerStatSpan, bool)
trySendSpan(s *tracerStatSpan)
}
// concentrator aggregates and stores statistics on incoming spans in time buckets,
// flushing them occasionally to the underlying transport located in the given
// tracer config.
type concentrator struct {
// In specifies the channel to be used for feeding data to the concentrator.
// In order for In to have a consumer, the concentrator must be started using
// a call to Start.
In chan *tracerStatSpan
// stopped reports whether the concentrator is stopped (when non-zero)
stopped uint32 // +checkatomic
spanConcentrator *stats.SpanConcentrator
aggregationKey stats.PayloadAggregationKey
wg sync.WaitGroup // waits for any active goroutines
bucketSize int64 // the size of a bucket in nanoseconds
stop chan struct{} // closing this channel triggers shutdown
cfg *config // tracer startup configuration
statsdClient internal.StatsdClient // statsd client for sending metrics.
}
type tracerStatSpan struct {
statSpan *stats.StatSpan
origin string
version string // per-span version tag; "" means use global aggKey version
}
// newConcentrator creates a new concentrator using the given tracer
// configuration c. It creates buckets of bucketSize nanoseconds duration.
func newConcentrator(c *config, bucketSize int64, statsdClient internal.StatsdClient) *concentrator {
sCfg := &stats.SpanConcentratorConfig{
ComputeStatsBySpanKind: true,
BucketInterval: defaultStatsBucketSize,
}
env := c.agent.load().defaultEnv
if c.internalConfig.Env() != "" {
env = c.internalConfig.Env()
}
if env == "" {
// We do this to avoid a panic in the stats calculation logic when env is empty
// This should never actually happen as the agent MUST have an env configured to start-up
// That panic will be removed in a future release at which point we can remove this
env = "unknown-env"
log.Debug("No DD Env found, normally the agent should have one")
}
gitCommitSha := ""
if c.internalConfig.CIVisibilityEnabled() {
// We only have this data if we're in CI Visibility
gitCommitSha = utils.GetCITags()[constants.GitCommitSHA]
}
aggKey := stats.PayloadAggregationKey{
Hostname: c.internalConfig.Hostname(),
Env: env,
Version: c.internalConfig.Version(),
ContainerID: "", // This intentionally left empty as the Agent will attach the container ID only in certain situations.
GitCommitSha: gitCommitSha,
ImageTag: "",
}
spanConcentrator := stats.NewSpanConcentrator(sCfg, time.Now())
return &concentrator{
In: make(chan *tracerStatSpan, 10000),
bucketSize: bucketSize,
stopped: 1,
cfg: c,
aggregationKey: aggKey,
spanConcentrator: spanConcentrator,
statsdClient: statsdClient,
}
}
// alignTs returns the provided timestamp truncated to the bucket size.
// It gives us the start time of the time bucket in which such timestamp falls.
func alignTs(ts, bucketSize int64) int64 { return ts - ts%bucketSize }
// Start starts the concentrator. A started concentrator needs to be stopped
// in order to gracefully shut down, using Stop.
func (c *concentrator) Start() {
if atomic.SwapUint32(&c.stopped, 0) == 0 {
// already running
log.Warn("(*concentrator).Start called more than once. This is likely a programming error.")
return
}
c.stop = make(chan struct{})
c.wg.Go(func() {
tick := time.NewTicker(time.Duration(c.bucketSize) * time.Nanosecond)
defer tick.Stop()
c.runFlusher(tick.C)
})
c.wg.Go(func() {
c.runIngester()
})
}
// runFlusher runs the flushing loop which sends stats to the underlying transport.
func (c *concentrator) runFlusher(tick <-chan time.Time) {
for {
select {
case now := <-tick:
c.flushAndSend(now, withoutCurrentBucket)
case <-c.stop:
return
}
}
}
// statsd returns any tracer configured statsd client, or a no-op.
func (c *concentrator) statsd() internal.StatsdClient {
if c.statsdClient == nil {
return &statsd.NoOpClientDirect{}
}
return c.statsdClient
}
// runIngester runs the loop which accepts incoming data on the concentrator's In
// channel.
func (c *concentrator) runIngester() {
for {
select {
case s := <-c.In:
c.statsd().Incr("datadog.tracer.stats.spans_in", nil, 1)
c.add(s)
case <-c.stop:
return
}
}
}
// +checklocksignore — Post-finish: reads finished span fields during stats computation.
func (c *concentrator) newTracerStatSpan(s *Span, obfuscator *obfuscate.Obfuscator) (*tracerStatSpan, bool) {
resource := s.resource
if c.shouldObfuscate() {
resource = obfuscatedResource(obfuscator, s.spanType, s.resource)
}
httpMethod, _ := s.meta.Get(ext.HTTPMethod)
httpEndpoint, _ := s.meta.Get(ext.HTTPEndpoint)
statSpan, ok := c.spanConcentrator.NewStatSpanWithConfig(stats.StatSpanConfig{
Service: s.service,
Resource: resource,
Name: s.name,
Type: s.spanType,
ParentID: s.parentID,
Start: s.start,
Duration: s.duration,
Error: s.error,
Meta: s.meta.Map(false), // stats reads span.kind, _dd.svc_src, status codes, peer tags — no promoted keys needed
Metrics: s.metrics,
PeerTags: c.cfg.agent.load().peerTags,
HTTPMethod: httpMethod,
HTTPEndpoint: httpEndpoint,
})
if !ok {
return nil, false
}
origin, _ := s.meta.Get(keyOrigin)
version, _ := s.meta.Version()
return &tracerStatSpan{
statSpan: statSpan,
origin: origin,
version: version,
}, true
}
func (c *concentrator) shouldObfuscate() bool {
// Obfuscate if agent reports an obfuscation version AND our version is at least as new
agentObfVersion := c.cfg.agent.load().obfuscationVersion
return agentObfVersion > 0 && agentObfVersion <= tracerObfuscationVersion
}
// add s into the concentrator's internal stats buckets.
func (c *concentrator) add(s *tracerStatSpan) {
aggKey := c.aggregationKey
if s.version != "" {
aggKey.Version = s.version
}
c.spanConcentrator.AddSpan(s.statSpan, aggKey, "", nil, s.origin)
}
// Stop stops the concentrator and blocks until the operation completes.
func (c *concentrator) Stop() {
if atomic.SwapUint32(&c.stopped, 1) > 0 {
return
}
close(c.stop)
c.wg.Wait()
drain:
for {
select {
case s := <-c.In:
c.statsd().Incr("datadog.tracer.stats.spans_in", nil, 1)
c.add(s)
default:
break drain
}
}
c.flushAndSend(time.Now(), withCurrentBucket)
}
const (
withCurrentBucket = true
withoutCurrentBucket = false
)
// flushAndSend flushes all the stats buckets with the given timestamp and sends them using the transport specified in
// the concentrator config. The current bucket is only included if includeCurrent is true, such as during shutdown.
func (c *concentrator) flushAndSend(timenow time.Time, includeCurrent bool) {
csps := c.spanConcentrator.Flush(timenow.UnixNano(), includeCurrent)
obfVersion := 0
if c.shouldObfuscate() {
obfVersion = tracerObfuscationVersion
} else {
log.Debug("Stats Obfuscation was skipped, agent will obfuscate (tracer %d, agent %d)", tracerObfuscationVersion, c.cfg.agent.load().obfuscationVersion)
}
if len(csps) == 0 {
// nothing to flush
return
}
c.statsd().Incr("datadog.tracer.stats.flush_payloads", nil, float64(len(csps)))
flushedBuckets := 0
// Given we use a constant PayloadAggregationKey there should only ever be 1 of these, but to be forward
// compatible in case this ever changes we can just iterate through all of them.
for _, csp := range csps {
csp.ProcessTags = processtags.GlobalTags().String()
flushedBuckets += len(csp.Stats)
var err error
for attempt := 0; attempt <= c.cfg.sendRetries; attempt++ {
err = c.cfg.ddTransport.sendStats(csp, obfVersion)
if err == nil {
break
}
if attempt < c.cfg.sendRetries {
time.Sleep(c.cfg.internalConfig.RetryInterval())
}
}
if err != nil {
c.statsd().Incr("datadog.tracer.stats.flush_errors", nil, 1)
log.Error("Error sending stats payload: %s", err.Error())
}
}
c.statsd().Incr("datadog.tracer.stats.flush_buckets", nil, float64(flushedBuckets))
}
// trySendSpan attempts a non-blocking send of the stat span to the
// concentrator's input channel.
func (c *concentrator) trySendSpan(s *tracerStatSpan) {
select {
case c.In <- s:
default:
log.Error("Stats channel full, disregarding span.")
}
}
// noopConcentrator is a no-op implementation of statsConcentrator used when
// client-side stats are disabled (e.g. OTLP export mode).
type noopConcentrator struct{}
func (c *noopConcentrator) Start() {}
func (c *noopConcentrator) Stop() {}
func (c *noopConcentrator) flushAndSend(_ time.Time, _ bool) {}
func (c *noopConcentrator) newTracerStatSpan(_ *Span, _ *obfuscate.Obfuscator) (*tracerStatSpan, bool) {
return nil, false
}
func (c *noopConcentrator) trySendSpan(_ *tracerStatSpan) {}