-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
metrics.go
358 lines (320 loc) · 13.6 KB
/
metrics.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"strings"
"sync"
"github.com/jaegertracing/jaeger/cmd/collector/app/processor"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/pkg/normalizer"
)
const (
// TODO this needs to be configurable via CLI.
maxServiceNames = 4000
// otherServices is the catch-all label when number of services exceeds maxServiceNames
otherServices = "other-services"
// samplerTypeKey is the name of the metric tag showing sampler type
samplerTypeKey = "sampler_type"
// // types of samplers: const, probabilistic, ratelimiting, lowerbound
// numOfSamplerTypes = 4
concatenation = "$_$"
)
var otherServicesSamplers map[model.SamplerType]string = initOtherServicesSamplers()
func initOtherServicesSamplers() map[model.SamplerType]string {
samplers := []model.SamplerType{
model.SamplerTypeUnrecognized,
model.SamplerTypeProbabilistic,
model.SamplerTypeLowerBound,
model.SamplerTypeRateLimiting,
model.SamplerTypeConst,
}
m := make(map[model.SamplerType]string)
for _, s := range samplers {
m[s] = otherServices + concatenation + s.String()
}
return m
}
// SpanProcessorMetrics contains all the necessary metrics for the SpanProcessor
type SpanProcessorMetrics struct {
// TODO - initialize metrics in the traditional factory way. Initialize map afterward.
// SaveLatency measures how long the actual save to storage takes
SaveLatency metrics.Timer
// InQueueLatency measures how long the span spends in the queue
InQueueLatency metrics.Timer
// SpansDropped measures the number of spans we discarded because the queue was full
SpansDropped metrics.Counter
// SpansBytes records how many bytes were processed
SpansBytes metrics.Gauge
// BatchSize measures the span batch size
BatchSize metrics.Gauge // size of span batch
// QueueCapacity measures the capacity of the internal span queue
QueueCapacity metrics.Gauge
// QueueLength measures the current number of elements in the internal span queue
QueueLength metrics.Gauge
// SavedOkBySvc contains span and trace counts by service
SavedOkBySvc metricsBySvc // spans actually saved
SavedErrBySvc metricsBySvc // spans failed to save
serviceNames metrics.Gauge // total number of unique service name metrics reported by this collector
spanCounts SpanCountsByFormat
}
type countsBySvc struct {
counts map[string]metrics.Counter // counters per service
debugCounts map[string]metrics.Counter // debug counters per service
factory metrics.Factory
lock *sync.Mutex
maxServiceNames int
category string
}
type spanCountsBySvc struct {
countsBySvc
}
type traceCountsBySvc struct {
countsBySvc
stringBuilderPool *sync.Pool
}
type metricsBySvc struct {
spans spanCountsBySvc // number of spans received per service
traces traceCountsBySvc // number of traces originated per service
}
// SpanCountsByFormat groups metrics by different span formats (thrift, proto, etc.)
type SpanCountsByFormat map[processor.SpanFormat]SpanCountsByTransport
// SpanCountsByTransport groups metrics by inbound transport (e.g http, grpc, tchannel)
type SpanCountsByTransport map[processor.InboundTransport]SpanCounts
// SpanCounts contains counts for received and rejected spans.
type SpanCounts struct {
// ReceivedBySvc maintain by-service metrics.
ReceivedBySvc metricsBySvc
// RejectedBySvc is the number of spans we rejected (usually due to blacklisting) by-service.
RejectedBySvc metricsBySvc
}
// NewSpanProcessorMetrics returns a SpanProcessorMetrics
func NewSpanProcessorMetrics(serviceMetrics metrics.Factory, hostMetrics metrics.Factory, otherFormatTypes []processor.SpanFormat) *SpanProcessorMetrics {
spanCounts := SpanCountsByFormat{
processor.ZipkinSpanFormat: newCountsByTransport(serviceMetrics, processor.ZipkinSpanFormat),
processor.JaegerSpanFormat: newCountsByTransport(serviceMetrics, processor.JaegerSpanFormat),
processor.ProtoSpanFormat: newCountsByTransport(serviceMetrics, processor.ProtoSpanFormat),
processor.UnknownSpanFormat: newCountsByTransport(serviceMetrics, processor.UnknownSpanFormat),
}
for _, otherFormatType := range otherFormatTypes {
spanCounts[otherFormatType] = newCountsByTransport(serviceMetrics, otherFormatType)
}
m := &SpanProcessorMetrics{
SaveLatency: hostMetrics.Timer(metrics.TimerOptions{Name: "save-latency", Tags: nil}),
InQueueLatency: hostMetrics.Timer(metrics.TimerOptions{Name: "in-queue-latency", Tags: nil}),
SpansDropped: hostMetrics.Counter(metrics.Options{Name: "spans.dropped", Tags: nil}),
BatchSize: hostMetrics.Gauge(metrics.Options{Name: "batch-size", Tags: nil}),
QueueCapacity: hostMetrics.Gauge(metrics.Options{Name: "queue-capacity", Tags: nil}),
QueueLength: hostMetrics.Gauge(metrics.Options{Name: "queue-length", Tags: nil}),
SpansBytes: hostMetrics.Gauge(metrics.Options{Name: "spans.bytes", Tags: nil}),
SavedOkBySvc: newMetricsBySvc(serviceMetrics.Namespace(metrics.NSOptions{Name: "", Tags: map[string]string{"result": "ok"}}), "saved-by-svc"),
SavedErrBySvc: newMetricsBySvc(serviceMetrics.Namespace(metrics.NSOptions{Name: "", Tags: map[string]string{"result": "err"}}), "saved-by-svc"),
spanCounts: spanCounts,
serviceNames: hostMetrics.Gauge(metrics.Options{Name: "spans.serviceNames", Tags: nil}),
}
return m
}
func newMetricsBySvc(factory metrics.Factory, category string) metricsBySvc {
spansFactory := factory.Namespace(metrics.NSOptions{Name: "spans", Tags: nil})
tracesFactory := factory.Namespace(metrics.NSOptions{Name: "traces", Tags: nil})
return metricsBySvc{
spans: newSpanCountsBySvc(spansFactory, category, maxServiceNames),
traces: newTraceCountsBySvc(tracesFactory, category, maxServiceNames),
}
}
func newTraceCountsBySvc(factory metrics.Factory, category string, maxServices int) traceCountsBySvc {
extraSlotsForOtherServicesSamples := len(otherServicesSamplers) - 1 // excluding UnrecognizedSampler
return traceCountsBySvc{
countsBySvc: countsBySvc{
counts: newTraceCountsOtherServices(factory, category, "false"),
debugCounts: newTraceCountsOtherServices(factory, category, "true"),
factory: factory,
lock: &sync.Mutex{},
maxServiceNames: maxServices + extraSlotsForOtherServicesSamples,
category: category,
},
// use sync.Pool to reduce allocation of stringBuilder
stringBuilderPool: &sync.Pool{
New: func() interface{} {
return new(strings.Builder)
},
},
}
}
func newTraceCountsOtherServices(factory metrics.Factory, category string, isDebug string) map[string]metrics.Counter {
m := make(map[string]metrics.Counter)
for kSampler, vString := range otherServicesSamplers {
m[vString] = factory.Counter(
metrics.Options{
Name: category,
Tags: map[string]string{
"svc": otherServices,
"debug": isDebug,
samplerTypeKey: kSampler.String(),
},
})
}
return m
}
func newSpanCountsBySvc(factory metrics.Factory, category string, maxServiceNames int) spanCountsBySvc {
return spanCountsBySvc{
countsBySvc: countsBySvc{
counts: map[string]metrics.Counter{otherServices: factory.Counter(metrics.Options{Name: category, Tags: map[string]string{"svc": otherServices, "debug": "false"}})},
debugCounts: map[string]metrics.Counter{otherServices: factory.Counter(metrics.Options{Name: category, Tags: map[string]string{"svc": otherServices, "debug": "true"}})},
factory: factory,
lock: &sync.Mutex{},
maxServiceNames: maxServiceNames,
category: category,
},
}
}
func newCountsByTransport(factory metrics.Factory, format processor.SpanFormat) SpanCountsByTransport {
factory = factory.Namespace(metrics.NSOptions{Tags: map[string]string{"format": string(format)}})
return SpanCountsByTransport{
processor.HTTPTransport: newCounts(factory, processor.HTTPTransport),
processor.GRPCTransport: newCounts(factory, processor.GRPCTransport),
processor.UnknownTransport: newCounts(factory, processor.UnknownTransport),
}
}
func newCounts(factory metrics.Factory, transport processor.InboundTransport) SpanCounts {
factory = factory.Namespace(metrics.NSOptions{Tags: map[string]string{"transport": string(transport)}})
return SpanCounts{
RejectedBySvc: newMetricsBySvc(factory, "rejected"),
ReceivedBySvc: newMetricsBySvc(factory, "received"),
}
}
// GetCountsForFormat gets the SpanCounts for a given format and transport. If none exists, we use the Unknown format.
func (m *SpanProcessorMetrics) GetCountsForFormat(spanFormat processor.SpanFormat, transport processor.InboundTransport) SpanCounts {
c, ok := m.spanCounts[spanFormat]
if !ok {
c = m.spanCounts[processor.UnknownSpanFormat]
}
t, ok := c[transport]
if !ok {
t = c[processor.UnknownTransport]
}
return t
}
// reportServiceNameForSpan determines the name of the service that emitted
// the span and reports a counter stat.
func (m metricsBySvc) ReportServiceNameForSpan(span *model.Span) {
var serviceName string
if nil == span.Process || len(span.Process.ServiceName) == 0 {
serviceName = "__unknown"
} else {
serviceName = span.Process.ServiceName
}
m.countSpansByServiceName(serviceName, span.Flags.IsDebug())
if span.ParentSpanID() == 0 {
m.countTracesByServiceName(serviceName, span.Flags.IsDebug(), span.
GetSamplerType())
}
}
// countSpansByServiceName counts how many spans are received per service.
func (m metricsBySvc) countSpansByServiceName(serviceName string, isDebug bool) {
m.spans.countByServiceName(serviceName, isDebug)
}
// countTracesByServiceName counts how many traces are received per service,
// i.e. the counter is only incremented for the root spans.
func (m metricsBySvc) countTracesByServiceName(serviceName string, isDebug bool, samplerType model.SamplerType) {
m.traces.countByServiceName(serviceName, isDebug, samplerType)
}
// traceCountsBySvc.countByServiceName maintains a map of counters for each service name it's
// given and increments the respective counter when called. The service name
// are first normalized to safe-for-metrics format. If the number of counters
// exceeds maxServiceNames, new service names are ignored to avoid polluting
// the metrics namespace and overloading M3.
//
// The reportServiceNameCount() function runs on a timer and will report the
// total number of stored counters, so if it exceeds say the 90% threshold
// an alert should be raised to investigate what's causing so many unique
// service names.
func (m *traceCountsBySvc) countByServiceName(serviceName string, isDebug bool, samplerType model.SamplerType) {
serviceName = normalizer.ServiceName(serviceName)
counts := m.counts
if isDebug {
counts = m.debugCounts
}
var counter metrics.Counter
m.lock.Lock()
// trace counter key is combination of serviceName and samplerType.
key := m.buildKey(serviceName, samplerType.String())
if c, ok := counts[key]; ok {
counter = c
} else if len(counts) < m.maxServiceNames {
debugStr := "false"
if isDebug {
debugStr = "true"
}
// Only trace metrics have samplerType tag
tags := map[string]string{"svc": serviceName, "debug": debugStr, samplerTypeKey: samplerType.String()}
c := m.factory.Counter(metrics.Options{Name: m.category, Tags: tags})
counts[key] = c
counter = c
} else {
otherServicesSampler, ok := otherServicesSamplers[samplerType]
if !ok {
otherServicesSampler = otherServicesSamplers[model.SamplerTypeUnrecognized]
}
counter = counts[otherServicesSampler]
}
m.lock.Unlock()
counter.Inc(1)
}
// spanCountsBySvc.countByServiceName maintains a map of counters for each service name it's
// given and increments the respective counter when called. The service name
// are first normalized to safe-for-metrics format. If the number of counters
// exceeds maxServiceNames, new service names are ignored to avoid polluting
// the metrics namespace and overloading M3.
//
// The reportServiceNameCount() function runs on a timer and will report the
// total number of stored counters, so if it exceeds say the 90% threshold
// an alert should be raised to investigate what's causing so many unique
// service names.
func (m *spanCountsBySvc) countByServiceName(serviceName string, isDebug bool) {
serviceName = normalizer.ServiceName(serviceName)
counts := m.counts
if isDebug {
counts = m.debugCounts
}
var counter metrics.Counter
m.lock.Lock()
if c, ok := counts[serviceName]; ok {
counter = c
} else if len(counts) < m.maxServiceNames {
debugStr := "false"
if isDebug {
debugStr = "true"
}
tags := map[string]string{"svc": serviceName, "debug": debugStr}
c := m.factory.Counter(metrics.Options{Name: m.category, Tags: tags})
counts[serviceName] = c
counter = c
} else {
counter = counts[otherServices]
}
m.lock.Unlock()
counter.Inc(1)
}
func (m *traceCountsBySvc) buildKey(serviceName, samplerType string) string {
keyBuilder := m.stringBuilderPool.Get().(*strings.Builder)
keyBuilder.Reset()
keyBuilder.WriteString(serviceName)
keyBuilder.WriteString(concatenation)
keyBuilder.WriteString(samplerType)
key := keyBuilder.String()
m.stringBuilderPool.Put(keyBuilder)
return key
}