-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathmetrics_appender.go
137 lines (120 loc) · 4.11 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
// 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 (
"fmt"
"sort"
"time"
"github.com/m3db/m3/src/aggregator/aggregator"
"github.com/m3db/m3/src/metrics/matcher"
"github.com/m3db/m3/src/metrics/metadata"
"github.com/m3db/m3/src/x/serialize"
"github.com/m3db/m3x/clock"
)
type metricsAppender struct {
metricsAppenderOptions
tags *tags
multiSamplesAppender *multiSamplesAppender
}
type metricsAppenderOptions struct {
agg aggregator.Aggregator
defaultStagedMetadatas []metadata.StagedMetadatas
clockOpts clock.Options
tagEncoder serialize.TagEncoder
matcher matcher.Matcher
metricTagsIteratorPool serialize.MetricTagsIteratorPool
}
func (a *metricsAppender) AddTag(name, value []byte) {
a.tags.append(name, value)
}
func (a *metricsAppender) SamplesAppender(opts SampleAppenderOptions) (SamplesAppender, error) {
// Sort tags
sort.Sort(a.tags)
// Encode tags and compute a temporary (unowned) ID
a.tagEncoder.Reset()
if err := a.tagEncoder.Encode(a.tags); err != nil {
return nil, err
}
data, ok := a.tagEncoder.Data()
if !ok {
return nil, fmt.Errorf("unable to encode tags: names=%v, values=%v",
a.tags.names, a.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 opts.Override {
for _, rule := range opts.OverrideRules.MappingRules {
stagedMetadatas, err := rule.StagedMetadatas()
if err != nil {
return nil, err
}
a.multiSamplesAppender.addSamplesAppender(samplesAppender{
agg: a.agg,
unownedID: unownedID,
stagedMetadatas: stagedMetadatas,
})
}
} else {
// Always aggregate any default staged metadats
for _, stagedMetadatas := range a.defaultStagedMetadatas {
a.multiSamplesAppender.addSamplesAppender(samplesAppender{
agg: a.agg,
unownedID: unownedID,
stagedMetadatas: stagedMetadatas,
})
}
stagedMetadatas := matchResult.ForExistingIDAt(nowNanos)
if !stagedMetadatas.IsDefault() && len(stagedMetadatas) != 0 {
// Only sample if going to actually aggregate
a.multiSamplesAppender.addSamplesAppender(samplesAppender{
agg: a.agg,
unownedID: unownedID,
stagedMetadatas: stagedMetadatas,
})
}
numRollups := matchResult.NumNewRollupIDs()
for i := 0; i < numRollups; i++ {
rollup := matchResult.ForNewRollupIDsAt(i, nowNanos)
a.multiSamplesAppender.addSamplesAppender(samplesAppender{
agg: a.agg,
unownedID: rollup.ID,
stagedMetadatas: rollup.Metadatas,
})
}
}
return a.multiSamplesAppender, nil
}
func (a *metricsAppender) Reset() {
a.tags.names = a.tags.names[:0]
a.tags.values = a.tags.values[:0]
}
func (a *metricsAppender) Finalize() {
a.tagEncoder.Finalize()
a.tagEncoder = nil
}