-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
processor.go
161 lines (148 loc) · 5.17 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
// Copyright The OpenTelemetry Authors
//
// 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 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/coreinternal/processor/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor/internal/tracking"
)
type cumulativeToDeltaProcessor struct {
metrics map[string]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),
cancelFunc: cancel,
}
if len(config.Metrics) > 0 {
p.logger.Warn("The 'metrics' configuration is deprecated. Use 'include'/'exclude' instead.")
p.metrics = make(map[string]struct{}, len(config.Metrics))
for _, m := range config.Metrics {
p.metrics[m] = struct{}{}
}
}
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) {
resourceMetricsSlice := md.ResourceMetrics()
resourceMetricsSlice.RemoveIf(func(rm pmetric.ResourceMetrics) bool {
ilms := rm.ScopeMetrics()
ilms.RemoveIf(func(ilm pmetric.ScopeMetrics) bool {
ms := ilm.Metrics()
ms.RemoveIf(func(m pmetric.Metric) bool {
if !ctdp.shouldConvertMetric(m.Name()) {
return false
}
switch m.DataType() {
case pmetric.MetricDataTypeSum:
ms := m.Sum()
if ms.AggregationTemporality() != pmetric.MetricAggregationTemporalityCumulative {
return false
}
// Ignore any metrics that aren't monotonic
if !ms.IsMonotonic() {
return false
}
baseIdentity := tracking.MetricIdentity{
Resource: rm.Resource(),
InstrumentationLibrary: ilm.Scope(),
MetricDataType: m.DataType(),
MetricName: m.Name(),
MetricUnit: m.Unit(),
MetricIsMonotonic: ms.IsMonotonic(),
}
ctdp.convertDataPoints(ms.DataPoints(), baseIdentity)
ms.SetAggregationTemporality(pmetric.MetricAggregationTemporalityDelta)
return ms.DataPoints().Len() == 0
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 {
// Legacy support for deprecated Metrics config
if len(ctdp.metrics) > 0 {
_, ok := ctdp.metrics[metricName]
return ok
}
return (ctdp.includeFS == nil || ctdp.includeFS.Matches(metricName)) &&
(ctdp.excludeFS == nil || !ctdp.excludeFS.Matches(metricName))
}
func (ctdp *cumulativeToDeltaProcessor) convertDataPoints(in interface{}, baseIdentity tracking.MetricIdentity) {
switch dps := in.(type) {
case pmetric.NumberDataPointSlice:
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 id.IsFloatVal() {
// Do not attempt to transform NaN values
if math.IsNaN(dp.DoubleVal()) {
return false
}
point.FloatValue = dp.DoubleVal()
} else {
point.IntValue = dp.IntVal()
}
trackingPoint := tracking.MetricPoint{
Identity: id,
Value: point,
}
delta, valid := ctdp.deltaCalculator.Convert(trackingPoint)
// When converting non-monotonic cumulative counters,
// the first data point is omitted since the initial
// reference is not assumed to be zero
if !valid {
return true
}
dp.SetStartTimestamp(delta.StartTimestamp)
if id.IsFloatVal() {
dp.SetDoubleVal(delta.FloatValue)
} else {
dp.SetIntVal(delta.IntValue)
}
return false
})
}
}