-
Notifications
You must be signed in to change notification settings - Fork 486
/
starttimemetricadjuster.go
128 lines (111 loc) · 3.96 KB
/
starttimemetricadjuster.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
// 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 internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
import (
"errors"
"regexp"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
)
var (
errNoStartTimeMetrics = errors.New("start_time metric is missing")
errNoDataPointsStartTimeMetric = errors.New("start time metric with no data points")
errUnsupportedTypeStartTimeMetric = errors.New("unsupported data type for start time metric")
)
type startTimeMetricAdjuster struct {
startTimeMetricRegex *regexp.Regexp
logger *zap.Logger
}
// NewStartTimeMetricAdjuster returns a new MetricsAdjuster that adjust metrics' start times based on a start time metric.
func NewStartTimeMetricAdjuster(logger *zap.Logger, startTimeMetricRegex *regexp.Regexp) MetricsAdjuster {
return &startTimeMetricAdjuster{
startTimeMetricRegex: startTimeMetricRegex,
logger: logger,
}
}
func (stma *startTimeMetricAdjuster) AdjustMetrics(metrics pmetric.Metrics) error {
startTime, err := stma.getStartTime(metrics)
if err != nil {
return err
}
startTimeTs := timestampFromFloat64(startTime)
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
rm := metrics.ResourceMetrics().At(i)
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
ilm := rm.ScopeMetrics().At(j)
for k := 0; k < ilm.Metrics().Len(); k++ {
metric := ilm.Metrics().At(k)
switch metric.Type() {
case pmetric.MetricTypeGauge:
continue
case pmetric.MetricTypeSum:
dataPoints := metric.Sum().DataPoints()
for l := 0; l < dataPoints.Len(); l++ {
dp := dataPoints.At(l)
dp.SetStartTimestamp(startTimeTs)
}
case pmetric.MetricTypeSummary:
dataPoints := metric.Summary().DataPoints()
for l := 0; l < dataPoints.Len(); l++ {
dp := dataPoints.At(l)
dp.SetStartTimestamp(startTimeTs)
}
case pmetric.MetricTypeHistogram:
dataPoints := metric.Histogram().DataPoints()
for l := 0; l < dataPoints.Len(); l++ {
dp := dataPoints.At(l)
dp.SetStartTimestamp(startTimeTs)
}
default:
stma.logger.Warn("Unknown metric type", zap.String("type", metric.Type().String()))
}
}
}
}
return nil
}
func (stma *startTimeMetricAdjuster) getStartTime(metrics pmetric.Metrics) (float64, error) {
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
rm := metrics.ResourceMetrics().At(i)
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
ilm := rm.ScopeMetrics().At(j)
for k := 0; k < ilm.Metrics().Len(); k++ {
metric := ilm.Metrics().At(k)
if stma.matchStartTimeMetric(metric.Name()) {
switch metric.Type() {
case pmetric.MetricTypeGauge:
if metric.Gauge().DataPoints().Len() == 0 {
return 0.0, errNoDataPointsStartTimeMetric
}
return metric.Gauge().DataPoints().At(0).DoubleValue(), nil
case pmetric.MetricTypeSum:
if metric.Sum().DataPoints().Len() == 0 {
return 0.0, errNoDataPointsStartTimeMetric
}
return metric.Sum().DataPoints().At(0).DoubleValue(), nil
default:
return 0, errUnsupportedTypeStartTimeMetric
}
}
}
}
}
return 0.0, errNoStartTimeMetrics
}
func (stma *startTimeMetricAdjuster) matchStartTimeMetric(metricName string) bool {
if stma.startTimeMetricRegex != nil {
return stma.startTimeMetricRegex.MatchString(metricName)
}
return metricName == startTimeMetricName
}