-
Notifications
You must be signed in to change notification settings - Fork 462
/
Copy pathstats.go
218 lines (195 loc) · 6.9 KB
/
stats.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
// 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"
"gopkg.in/DataDog/dd-trace-go.v1/internal"
"gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/constants"
"gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/utils"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"github.com/DataDog/datadog-go/v5/statsd"
)
// defaultStatsBucketSize specifies the default span of time that will be
// covered in one stats bucket.
var defaultStatsBucketSize = (10 * time.Second).Nanoseconds()
// 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
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
}
// 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: false,
BucketInterval: defaultStatsBucketSize,
}
env := c.agent.defaultEnv
if c.env != "" {
env = c.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")
}
aggKey := stats.PayloadAggregationKey{
Hostname: c.hostname,
Env: env,
Version: c.version,
ContainerID: "", // This intentionally left empty as the Agent will attach the container ID only in certain situations.
GitCommitSha: utils.GetCITags()[constants.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.Add(1)
go func() {
defer c.wg.Done()
tick := time.NewTicker(time.Duration(c.bucketSize) * time.Nanosecond)
defer tick.Stop()
c.runFlusher(tick.C)
}()
c.wg.Add(1)
go func() {
defer c.wg.Done()
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.NoOpClient{}
}
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
}
}
}
func (c *concentrator) newTracerStatSpan(s *span, obfuscator *obfuscate.Obfuscator) (*tracerStatSpan, bool) {
statSpan, ok := c.spanConcentrator.NewStatSpan(s.Service, obfuscatedResource(obfuscator, s.Type, s.Resource),
s.Name, s.Type, s.ParentID, s.Start, s.Duration, s.Error, s.Meta, s.Metrics, c.cfg.agent.peerTags)
if !ok {
return nil, false
}
origin := s.Meta[keyOrigin]
return &tracerStatSpan{
statSpan: statSpan,
origin: origin,
}, true
}
// add s into the concentrator's internal stats buckets.
func (c *concentrator) add(s *tracerStatSpan) {
c.spanConcentrator.AddSpan(s.statSpan, c.aggregationKey, "", 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)
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 {
flushedBuckets += len(csp.Stats)
if err := c.cfg.transport.sendStats(csp); err != nil {
c.statsd().Incr("datadog.tracer.stats.flush_errors", nil, 1)
log.Error("Error sending stats payload: %v", err)
}
}
c.statsd().Incr("datadog.tracer.stats.flush_buckets", nil, float64(flushedBuckets))
}