forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocessor.go
141 lines (129 loc) · 4.23 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
// 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 (
"context"
"math"
"go.opentelemetry.io/collector/model/pdata"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor/internal/tracking"
)
type cumulativeToDeltaProcessor struct {
metrics map[string]struct{}
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.metrics = make(map[string]struct{}, len(config.Metrics))
for _, m := range config.Metrics {
p.metrics[m] = struct{}{}
}
}
return p
}
// processMetrics implements the ProcessMetricsFunc type.
func (ctdp *cumulativeToDeltaProcessor) processMetrics(_ context.Context, md pdata.Metrics) (pdata.Metrics, error) {
resourceMetricsSlice := md.ResourceMetrics()
resourceMetricsSlice.RemoveIf(func(rm pdata.ResourceMetrics) bool {
ilms := rm.InstrumentationLibraryMetrics()
ilms.RemoveIf(func(ilm pdata.InstrumentationLibraryMetrics) bool {
ms := ilm.Metrics()
ms.RemoveIf(func(m pdata.Metric) bool {
if _, ok := ctdp.metrics[m.Name()]; !ok {
return false
}
switch m.DataType() {
case pdata.MetricDataTypeSum:
ms := m.Sum()
if ms.AggregationTemporality() != pdata.MetricAggregationTemporalityCumulative {
return false
}
// Ignore any metrics that aren't monotonic
if !ms.IsMonotonic() {
return false
}
baseIdentity := tracking.MetricIdentity{
Resource: rm.Resource(),
InstrumentationLibrary: ilm.InstrumentationLibrary(),
MetricDataType: m.DataType(),
MetricName: m.Name(),
MetricUnit: m.Unit(),
MetricIsMonotonic: ms.IsMonotonic(),
}
ctdp.convertDataPoints(ms.DataPoints(), baseIdentity)
ms.SetAggregationTemporality(pdata.MetricAggregationTemporalityDelta)
return ms.DataPoints().Len() == 0
default:
return false
}
})
return ilm.Metrics().Len() == 0
})
return rm.InstrumentationLibraryMetrics().Len() == 0
})
return md, nil
}
func (ctdp *cumulativeToDeltaProcessor) shutdown(context.Context) error {
ctdp.cancelFunc()
return nil
}
func (ctdp *cumulativeToDeltaProcessor) convertDataPoints(in interface{}, baseIdentity tracking.MetricIdentity) {
switch dps := in.(type) {
case pdata.NumberDataPointSlice:
dps.RemoveIf(func(dp pdata.NumberDataPoint) bool {
id := baseIdentity
id.StartTimestamp = dp.StartTimestamp()
id.Attributes = dp.Attributes()
id.MetricValueType = dp.Type()
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
})
}
}