-
Notifications
You must be signed in to change notification settings - Fork 450
/
metrics_appender.go
628 lines (550 loc) · 19.1 KB
/
metrics_appender.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package downsample
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sort"
"time"
"github.com/m3db/m3/src/aggregator/aggregator"
"github.com/m3db/m3/src/aggregator/client"
"github.com/m3db/m3/src/metrics/aggregation"
"github.com/m3db/m3/src/metrics/generated/proto/metricpb"
"github.com/m3db/m3/src/metrics/matcher"
"github.com/m3db/m3/src/metrics/metadata"
"github.com/m3db/m3/src/metrics/metric"
"github.com/m3db/m3/src/metrics/policy"
"github.com/m3db/m3/src/query/graphite/graphite"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/ts"
"github.com/m3db/m3/src/x/clock"
"github.com/m3db/m3/src/x/pool"
"github.com/m3db/m3/src/x/serialize"
"github.com/gogo/protobuf/jsonpb"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var errNoTags = errors.New("no tags provided")
type metricsAppenderPool struct {
pool pool.ObjectPool
}
func newMetricsAppenderPool(opts pool.ObjectPoolOptions) *metricsAppenderPool {
p := &metricsAppenderPool{
pool: pool.NewObjectPool(opts),
}
p.pool.Init(func() interface{} {
return newMetricsAppender(p)
})
return p
}
func (p *metricsAppenderPool) Get() *metricsAppender {
appender := p.pool.Get().(*metricsAppender)
// NB: reset appender.
appender.NextMetric()
return appender
}
func (p *metricsAppenderPool) Put(v *metricsAppender) {
p.pool.Put(v)
}
type metricsAppender struct {
metricsAppenderOptions
pool *metricsAppenderPool
multiSamplesAppender *multiSamplesAppender
curr metadata.StagedMetadata
defaultStagedMetadatasCopies []metadata.StagedMetadatas
mappingRuleStoragePolicies []policy.StoragePolicy
cachedEncoders []serialize.TagEncoder
inuseEncoders []serialize.TagEncoder
originalTags *tags
cachedTags []*tags
inuseTags []*tags
}
// metricsAppenderOptions will have one of agg or clientRemote set.
type metricsAppenderOptions struct {
agg aggregator.Aggregator
clientRemote client.Client
defaultStagedMetadatasProtos []metricpb.StagedMetadatas
matcher matcher.Matcher
tagEncoderPool serialize.TagEncoderPool
metricTagsIteratorPool serialize.MetricTagsIteratorPool
augmentM3Tags bool
clockOpts clock.Options
debugLogging bool
logger *zap.Logger
}
func newMetricsAppender(pool *metricsAppenderPool) *metricsAppender {
return &metricsAppender{
pool: pool,
multiSamplesAppender: newMultiSamplesAppender(),
}
}
// reset is called when pulled from the pool.
func (a *metricsAppender) reset(opts metricsAppenderOptions) {
a.metricsAppenderOptions = opts
// Copy over any previous inuse encoders to the cached encoders list.
a.resetEncoders()
// Make sure a.defaultStagedMetadatasCopies is right length.
capRequired := len(opts.defaultStagedMetadatasProtos)
if cap(a.defaultStagedMetadatasCopies) < capRequired {
// Too short, reallocate.
slice := make([]metadata.StagedMetadatas, capRequired)
a.defaultStagedMetadatasCopies = slice
} else {
// Has enough capacity, take subslice.
slice := a.defaultStagedMetadatasCopies[:capRequired]
a.defaultStagedMetadatasCopies = slice
}
}
func (a *metricsAppender) AddTag(name, value []byte) {
if a.originalTags == nil {
a.originalTags = a.tags()
}
a.originalTags.append(name, value)
}
func (a *metricsAppender) SamplesAppender(opts SampleAppenderOptions) (SamplesAppenderResult, error) {
if a.originalTags == nil {
return SamplesAppenderResult{}, errNoTags
}
tags := a.originalTags
// Augment tags if necessary.
if a.augmentM3Tags {
// NB (@shreyas): Add the metric type tag. The tag has the prefix
// __m3_. All tags with that prefix are only used for the purpose of
// filter match and then stripped off before we actually send to the aggregator.
switch opts.MetricType {
case ts.M3MetricTypeCounter:
tags.append(metric.M3TypeTag, metric.M3CounterValue)
case ts.M3MetricTypeGauge:
tags.append(metric.M3TypeTag, metric.M3GaugeValue)
case ts.M3MetricTypeTimer:
tags.append(metric.M3TypeTag, metric.M3TimerValue)
}
}
// Sort tags
sort.Sort(tags)
// Encode tags and compute a temporary (unowned) ID
tagEncoder := a.tagEncoder()
if err := tagEncoder.Encode(tags); err != nil {
return SamplesAppenderResult{}, err
}
data, ok := tagEncoder.Data()
if !ok {
return SamplesAppenderResult{}, fmt.Errorf("unable to encode tags: names=%v, values=%v",
tags.names, tags.values)
}
a.multiSamplesAppender.reset()
unownedID := data.Bytes()
// Match policies and rollups and build samples appender
id := a.metricTagsIteratorPool.Get()
id.Reset(unownedID)
now := time.Now()
nowNanos := now.UnixNano()
fromNanos := nowNanos
toNanos := nowNanos + 1
matchResult := a.matcher.ForwardMatch(id, fromNanos, toNanos)
id.Close()
// If we augmented metrics tags before running the forward match, then
// filter them out.
if a.augmentM3Tags {
tags.filterPrefix(metric.M3MetricsPrefix)
}
var dropApplyResult metadata.ApplyOrRemoveDropPoliciesResult
if opts.Override {
// Reuse a slice to keep the current staged metadatas we will apply.
a.curr.Pipelines = a.curr.Pipelines[:0]
for _, rule := range opts.OverrideRules.MappingRules {
stagedMetadatas, err := rule.StagedMetadatas()
if err != nil {
return SamplesAppenderResult{}, err
}
a.debugLogMatch("downsampler applying override mapping rule",
debugLogMatchOptions{Meta: stagedMetadatas})
pipelines := stagedMetadatas[len(stagedMetadatas)-1]
a.curr.Pipelines =
append(a.curr.Pipelines, pipelines.Pipelines...)
}
if err := a.addSamplesAppenders(tags, a.curr, unownedID); err != nil {
return SamplesAppenderResult{}, err
}
} else {
// Reuse a slice to keep the current staged metadatas we will apply.
a.curr.Pipelines = a.curr.Pipelines[:0]
// NB(r): First apply mapping rules to see which storage policies
// have been applied, any that have been applied as part of
// mapping rules that exact match a default storage policy will be
// skipped when applying default rules, so as to avoid storing
// the same metrics in the same namespace with the same metric
// name and tags (i.e. overwriting each other).
a.mappingRuleStoragePolicies = a.mappingRuleStoragePolicies[:0]
mappingRuleStagedMetadatas := matchResult.ForExistingIDAt(nowNanos)
if !mappingRuleStagedMetadatas.IsDefault() && len(mappingRuleStagedMetadatas) != 0 {
a.debugLogMatch("downsampler applying matched mapping rule",
debugLogMatchOptions{Meta: mappingRuleStagedMetadatas})
// Collect all the current active mapping rules
for _, stagedMetadata := range mappingRuleStagedMetadatas {
for _, pipe := range stagedMetadata.Pipelines {
for _, sp := range pipe.StoragePolicies {
a.mappingRuleStoragePolicies =
append(a.mappingRuleStoragePolicies, sp)
}
}
}
// Only sample if going to actually aggregate
pipelines := mappingRuleStagedMetadatas[len(mappingRuleStagedMetadatas)-1]
a.curr.Pipelines =
append(a.curr.Pipelines, pipelines.Pipelines...)
}
// Always aggregate any default staged metadatas (unless
// mapping rule has provided an override for a storage policy,
// if so then skip aggregating for that storage policy).
for idx, stagedMetadatasProto := range a.defaultStagedMetadatasProtos {
// NB(r): Need to take copy of default staged metadatas as we
// sometimes mutate it.
stagedMetadatas := a.defaultStagedMetadatasCopies[idx]
err := stagedMetadatas.FromProto(stagedMetadatasProto)
if err != nil {
return SamplesAppenderResult{},
fmt.Errorf("unable to copy default staged metadatas: %v", err)
}
// Save the staged metadatas back to the idx so all slices can be reused.
a.defaultStagedMetadatasCopies[idx] = stagedMetadatas
stagedMetadataBeforeFilter := stagedMetadatas[:]
if len(a.mappingRuleStoragePolicies) != 0 {
// If mapping rules have applied aggregations for
// storage policies then de-dupe so we don't have two
// active aggregations for the same storage policy.
stagedMetadatasAfterFilter := stagedMetadatas[:0]
for _, stagedMetadata := range stagedMetadatas {
pipesAfterFilter := stagedMetadata.Pipelines[:0]
for _, pipe := range stagedMetadata.Pipelines {
storagePoliciesAfterFilter := pipe.StoragePolicies[:0]
for _, sp := range pipe.StoragePolicies {
// Check aggregation for storage policy not already
// set by a mapping rule.
matchedByMappingRule := false
for _, existing := range a.mappingRuleStoragePolicies {
if sp.Equivalent(existing) {
matchedByMappingRule = true
a.debugLogMatch("downsampler skipping default mapping rule storage policy",
debugLogMatchOptions{Meta: stagedMetadataBeforeFilter})
break
}
}
if !matchedByMappingRule {
// Keep storage policy if not matched by mapping rule.
storagePoliciesAfterFilter =
append(storagePoliciesAfterFilter, sp)
}
}
// Update storage policies slice after filtering.
pipe.StoragePolicies = storagePoliciesAfterFilter
if len(pipe.StoragePolicies) != 0 {
// Keep storage policy if still has some storage policies.
pipesAfterFilter = append(pipesAfterFilter, pipe)
}
}
// Update pipelnes after filtering.
stagedMetadata.Pipelines = pipesAfterFilter
if len(stagedMetadata.Pipelines) != 0 {
// Keep staged metadata if still has some pipelines.
stagedMetadatasAfterFilter =
append(stagedMetadatasAfterFilter, stagedMetadata)
}
}
// Finally set the staged metadatas we're keeping
// as those that were kept after filtering.
stagedMetadatas = stagedMetadatasAfterFilter
}
// Now skip appending if after filtering there's no staged metadatas
// after any filtering that's applied.
if len(stagedMetadatas) == 0 {
a.debugLogMatch("downsampler skipping default mapping rule completely",
debugLogMatchOptions{Meta: stagedMetadataBeforeFilter})
continue
}
a.debugLogMatch("downsampler applying default mapping rule",
debugLogMatchOptions{Meta: stagedMetadatas})
pipelines := stagedMetadatas[len(stagedMetadatas)-1]
a.curr.Pipelines =
append(a.curr.Pipelines, pipelines.Pipelines...)
}
// Apply drop policies results
a.curr.Pipelines, dropApplyResult = a.curr.Pipelines.ApplyOrRemoveDropPolicies()
if len(a.curr.Pipelines) > 0 && !a.curr.IsDropPolicyApplied() {
// Send to downsampler if we have something in the pipeline.
a.debugLogMatch("downsampler using built mapping staged metadatas",
debugLogMatchOptions{Meta: []metadata.StagedMetadata{a.curr}})
if err := a.addSamplesAppenders(tags, a.curr, unownedID); err != nil {
return SamplesAppenderResult{}, err
}
}
numRollups := matchResult.NumNewRollupIDs()
for i := 0; i < numRollups; i++ {
rollup := matchResult.ForNewRollupIDsAt(i, nowNanos)
a.debugLogMatch("downsampler applying matched rollup rule",
debugLogMatchOptions{Meta: rollup.Metadatas, RollupID: rollup.ID})
a.multiSamplesAppender.addSamplesAppender(samplesAppender{
agg: a.agg,
clientRemote: a.clientRemote,
unownedID: rollup.ID,
stagedMetadatas: rollup.Metadatas,
})
}
}
dropPolicyApplied := dropApplyResult != metadata.NoDropPolicyPresentResult
return SamplesAppenderResult{
SamplesAppender: a.multiSamplesAppender,
IsDropPolicyApplied: dropPolicyApplied,
}, nil
}
type debugLogMatchOptions struct {
Meta metadata.StagedMetadatas
StoragePolicy policy.StoragePolicy
RollupID []byte
}
func (a *metricsAppender) debugLogMatch(str string, opts debugLogMatchOptions) {
if !a.debugLogging {
return
}
fields := []zapcore.Field{
zap.String("tags", a.originalTags.String()),
}
if v := opts.RollupID; v != nil {
fields = append(fields, zap.ByteString("rollupID", v))
}
if v := opts.Meta; v != nil {
fields = append(fields, stagedMetadatasLogField(v))
}
if v := opts.StoragePolicy; v != policy.EmptyStoragePolicy {
fields = append(fields, zap.Stringer("storagePolicy", v))
}
a.logger.Debug(str, fields...)
}
func (a *metricsAppender) NextMetric() {
// Move the inuse encoders to cached as we should be done with using them.
a.resetEncoders()
a.resetTags()
}
func (a *metricsAppender) Finalize() {
// Return to pool.
a.pool.Put(a)
}
func (a *metricsAppender) tagEncoder() serialize.TagEncoder {
// Take an encoder from the cached encoder list, if not present get one
// from the pool. Add the returned encoder to the used list.
var tagEncoder serialize.TagEncoder
if len(a.cachedEncoders) == 0 {
tagEncoder = a.tagEncoderPool.Get()
} else {
l := len(a.cachedEncoders)
tagEncoder = a.cachedEncoders[l-1]
a.cachedEncoders = a.cachedEncoders[:l-1]
}
a.inuseEncoders = append(a.inuseEncoders, tagEncoder)
tagEncoder.Reset()
return tagEncoder
}
func (a *metricsAppender) tags() *tags {
// Take an encoder from the cached encoder list, if not present get one
// from the pool. Add the returned encoder to the used list.
var t *tags
if len(a.cachedTags) == 0 {
t = newTags()
} else {
l := len(a.cachedTags)
t = a.cachedTags[l-1]
a.cachedTags = a.cachedTags[:l-1]
}
a.inuseTags = append(a.inuseTags, t)
t.names = t.names[:0]
t.values = t.values[:0]
t.reset()
return t
}
func (a *metricsAppender) resetEncoders() {
a.cachedEncoders = append(a.cachedEncoders, a.inuseEncoders...)
for i := range a.inuseEncoders {
a.inuseEncoders[i] = nil
}
a.inuseEncoders = a.inuseEncoders[:0]
}
func (a *metricsAppender) resetTags() {
a.cachedTags = append(a.cachedTags, a.inuseTags...)
for i := range a.inuseTags {
a.inuseTags[i] = nil
}
a.inuseTags = a.inuseTags[:0]
a.originalTags = nil
}
func (a *metricsAppender) addSamplesAppenders(
originalTags *tags,
stagedMetadata metadata.StagedMetadata,
unownedID []byte,
) error {
// Check if any of the pipelines have tags or a graphite prefix to set.
var tagsExist bool
for _, pipeline := range stagedMetadata.Pipelines {
if len(pipeline.Tags) > 0 || len(pipeline.GraphitePrefix) > 0 {
tagsExist = true
break
}
}
// If we do not need to do any tag augmentation then just return.
if !a.augmentM3Tags && !tagsExist {
a.multiSamplesAppender.addSamplesAppender(samplesAppender{
agg: a.agg,
clientRemote: a.clientRemote,
unownedID: unownedID,
stagedMetadatas: []metadata.StagedMetadata{stagedMetadata},
})
return nil
}
var (
pipelines []metadata.PipelineMetadata
)
for _, pipeline := range stagedMetadata.Pipelines {
// For pipeline which have tags to augment we generate and send
// separate IDs. Other pipelines return the same.
pipeline := pipeline
if len(pipeline.Tags) == 0 && len(pipeline.GraphitePrefix) == 0 {
pipelines = append(pipelines, pipeline)
continue
}
tags := a.augmentTags(originalTags, pipeline.GraphitePrefix, pipeline.Tags, pipeline.AggregationID)
sm := stagedMetadata
sm.Pipelines = []metadata.PipelineMetadata{pipeline}
appender, err := a.newSamplesAppender(tags, sm)
if err != nil {
return err
}
a.multiSamplesAppender.addSamplesAppender(appender)
}
if len(pipelines) == 0 {
return nil
}
sm := stagedMetadata
sm.Pipelines = pipelines
appender, err := a.newSamplesAppender(originalTags, sm)
if err != nil {
return err
}
a.multiSamplesAppender.addSamplesAppender(appender)
return nil
}
func (a *metricsAppender) newSamplesAppender(
tags *tags,
sm metadata.StagedMetadata,
) (samplesAppender, error) {
tagEncoder := a.tagEncoder()
if err := tagEncoder.Encode(tags); err != nil {
return samplesAppender{}, err
}
data, ok := tagEncoder.Data()
if !ok {
return samplesAppender{}, fmt.Errorf("unable to encode tags: names=%v, values=%v", tags.names, tags.values)
}
return samplesAppender{
agg: a.agg,
clientRemote: a.clientRemote,
unownedID: data.Bytes(),
stagedMetadatas: []metadata.StagedMetadata{sm},
}, nil
}
func (a *metricsAppender) augmentTags(
originalTags *tags,
graphitePrefix [][]byte,
t []models.Tag,
id aggregation.ID,
) *tags {
// Create the prefix tags if any.
tags := a.tags()
for i, path := range graphitePrefix {
// Add the graphite prefix as the initial graphite tags.
tags.append(graphite.TagName(i), path)
}
// Make a copy of the tags to augment.
prefixes := len(graphitePrefix)
for i := range originalTags.names {
// If we applied prefixes then we need to parse and modify the original
// tags. Check if the original tag was graphite type, if so add the
// number of prefixes to the tag index and update.
var (
name = originalTags.names[i]
value = originalTags.values[i]
)
if prefixes > 0 {
// If the tag seen is a graphite tag then offset it based on number
// of prefixes we have seen.
if index, ok := graphite.TagIndex(name); ok {
name = graphite.TagName(index + prefixes)
}
}
tags.append(name, value)
}
// Add any additional tags we need to.
for _, tag := range t {
// If the tag is not special tag, then just add it.
if !bytes.HasPrefix(tag.Name, metric.M3MetricsPrefix) {
if len(tag.Name) > 0 && len(tag.Value) > 0 {
tags.append(tag.Name, tag.Value)
}
continue
}
// Handle m3 special tags.
if bytes.Equal(tag.Name, metric.M3MetricsGraphiteAggregation) {
// Add the aggregation tag as the last graphite tag.
types, err := id.Types()
if err != nil || len(types) == 0 {
continue
}
var (
count = tags.countPrefix(graphite.Prefix)
name = graphite.TagName(count)
value = types[0].Bytes()
)
tags.append(name, value)
}
}
return tags
}
func stagedMetadatasLogField(sm metadata.StagedMetadatas) zapcore.Field {
json, err := stagedMetadatasJSON(sm)
if err != nil {
return zap.String("stagedMetadatasDebugErr", err.Error())
}
return zap.Any("stagedMetadatas", json)
}
func stagedMetadatasJSON(sm metadata.StagedMetadatas) (interface{}, error) {
var pb metricpb.StagedMetadatas
if err := sm.ToProto(&pb); err != nil {
return nil, err
}
var buff bytes.Buffer
if err := (&jsonpb.Marshaler{}).Marshal(&buff, &pb); err != nil {
return nil, err
}
var result map[string]interface{}
if err := json.Unmarshal(buff.Bytes(), &result); err != nil {
return nil, err
}
return result, nil
}