forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocessor.go
204 lines (181 loc) · 6.11 KB
/
processor.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package cumulativetodeltaprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor"
import (
"context"
"math"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor/internal/tracking"
)
type cumulativeToDeltaProcessor struct {
includeFS filterset.FilterSet
excludeFS filterset.FilterSet
logger *zap.Logger
deltaCalculator *tracking.MetricTracker
cancelFunc context.CancelFunc
}
func newCumulativeToDeltaProcessor(config *Config, logger *zap.Logger) *cumulativeToDeltaProcessor {
ctx, cancel := context.WithCancel(context.Background())
p := &cumulativeToDeltaProcessor{
logger: logger,
deltaCalculator: tracking.NewMetricTracker(ctx, logger, config.MaxStaleness, config.InitialValue),
cancelFunc: cancel,
}
if len(config.Include.Metrics) > 0 {
p.includeFS, _ = filterset.CreateFilterSet(config.Include.Metrics, &config.Include.Config)
}
if len(config.Exclude.Metrics) > 0 {
p.excludeFS, _ = filterset.CreateFilterSet(config.Exclude.Metrics, &config.Exclude.Config)
}
return p
}
// processMetrics implements the ProcessMetricsFunc type.
func (ctdp *cumulativeToDeltaProcessor) processMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
md.ResourceMetrics().RemoveIf(func(rm pmetric.ResourceMetrics) bool {
rm.ScopeMetrics().RemoveIf(func(ilm pmetric.ScopeMetrics) bool {
ilm.Metrics().RemoveIf(func(m pmetric.Metric) bool {
if !ctdp.shouldConvertMetric(m.Name()) {
return false
}
switch m.Type() {
case pmetric.MetricTypeSum:
ms := m.Sum()
if ms.AggregationTemporality() != pmetric.AggregationTemporalityCumulative {
return false
}
// Ignore any metrics that aren't monotonic
if !ms.IsMonotonic() {
return false
}
baseIdentity := tracking.MetricIdentity{
Resource: rm.Resource(),
InstrumentationLibrary: ilm.Scope(),
MetricType: m.Type(),
MetricName: m.Name(),
MetricUnit: m.Unit(),
MetricIsMonotonic: ms.IsMonotonic(),
}
ctdp.convertDataPoints(ms.DataPoints(), baseIdentity)
ms.SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
return ms.DataPoints().Len() == 0
case pmetric.MetricTypeHistogram:
ms := m.Histogram()
if ms.AggregationTemporality() != pmetric.AggregationTemporalityCumulative {
return false
}
if ms.DataPoints().Len() == 0 {
return false
}
baseIdentity := tracking.MetricIdentity{
Resource: rm.Resource(),
InstrumentationLibrary: ilm.Scope(),
MetricType: m.Type(),
MetricName: m.Name(),
MetricUnit: m.Unit(),
MetricIsMonotonic: true,
MetricValueType: pmetric.NumberDataPointValueTypeInt,
}
ctdp.convertHistogramDataPoints(ms.DataPoints(), baseIdentity)
ms.SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
return ms.DataPoints().Len() == 0
case pmetric.MetricTypeEmpty, pmetric.MetricTypeGauge, pmetric.MetricTypeExponentialHistogram, pmetric.MetricTypeSummary:
fallthrough
default:
return false
}
})
return ilm.Metrics().Len() == 0
})
return rm.ScopeMetrics().Len() == 0
})
return md, nil
}
func (ctdp *cumulativeToDeltaProcessor) shutdown(context.Context) error {
ctdp.cancelFunc()
return nil
}
func (ctdp *cumulativeToDeltaProcessor) shouldConvertMetric(metricName string) bool {
return (ctdp.includeFS == nil || ctdp.includeFS.Matches(metricName)) &&
(ctdp.excludeFS == nil || !ctdp.excludeFS.Matches(metricName))
}
func (ctdp *cumulativeToDeltaProcessor) convertDataPoints(in any, baseIdentity tracking.MetricIdentity) {
if dps, ok := in.(pmetric.NumberDataPointSlice); ok {
dps.RemoveIf(func(dp pmetric.NumberDataPoint) bool {
id := baseIdentity
id.StartTimestamp = dp.StartTimestamp()
id.Attributes = dp.Attributes()
id.MetricValueType = dp.ValueType()
point := tracking.ValuePoint{
ObservedTimestamp: dp.Timestamp(),
}
if dp.Flags().NoRecordedValue() {
// drop points with no value
return true
}
if id.IsFloatVal() {
// Do not attempt to transform NaN values
if math.IsNaN(dp.DoubleValue()) {
return false
}
point.FloatValue = dp.DoubleValue()
} else {
point.IntValue = dp.IntValue()
}
trackingPoint := tracking.MetricPoint{
Identity: id,
Value: point,
}
delta, valid := ctdp.deltaCalculator.Convert(trackingPoint)
if !valid {
return true
}
dp.SetStartTimestamp(delta.StartTimestamp)
if id.IsFloatVal() {
dp.SetDoubleValue(delta.FloatValue)
} else {
dp.SetIntValue(delta.IntValue)
}
return false
})
}
}
func (ctdp *cumulativeToDeltaProcessor) convertHistogramDataPoints(in any, baseIdentity tracking.MetricIdentity) {
if dps, ok := in.(pmetric.HistogramDataPointSlice); ok {
dps.RemoveIf(func(dp pmetric.HistogramDataPoint) bool {
id := baseIdentity
id.StartTimestamp = dp.StartTimestamp()
id.Attributes = dp.Attributes()
if dp.Flags().NoRecordedValue() {
// drop points with no value
return true
}
point := tracking.ValuePoint{
ObservedTimestamp: dp.Timestamp(),
HistogramValue: &tracking.HistogramPoint{
Count: dp.Count(),
Sum: dp.Sum(),
Buckets: dp.BucketCounts().AsRaw(),
},
}
trackingPoint := tracking.MetricPoint{
Identity: id,
Value: point,
}
delta, valid := ctdp.deltaCalculator.Convert(trackingPoint)
if valid {
dp.SetStartTimestamp(delta.StartTimestamp)
dp.SetCount(delta.HistogramValue.Count)
if dp.HasSum() && !math.IsNaN(dp.Sum()) {
dp.SetSum(delta.HistogramValue.Sum)
}
dp.BucketCounts().FromRaw(delta.HistogramValue.Buckets)
dp.RemoveMin()
dp.RemoveMax()
return false
}
return !valid
})
}
}