Path: blob/main/component/otelcol/receiver/prometheus/internal/starttimemetricadjuster_test.go
5443 views
// Copyright The OpenTelemetry Authors1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package internal1516import (17"regexp"18"testing"1920"github.com/stretchr/testify/assert"21"go.opentelemetry.io/collector/pdata/pcommon"22"go.opentelemetry.io/collector/pdata/pmetric"23"go.uber.org/zap"24)2526func TestStartTimeMetricMatch(t *testing.T) {27const startTime = pcommon.Timestamp(123 * 1e9)28const currentTime = pcommon.Timestamp(126 * 1e9)29const matchBuilderStartTime = 1243031tests := []struct {32name string33inputs pmetric.Metrics34startTimeMetricRegex *regexp.Regexp35expectedStartTime pcommon.Timestamp36expectedErr error37}{38{39name: "regexp_match_sum_metric",40inputs: metrics(41sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),42histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),43summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),44sumMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),45sumMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),46),47startTimeMetricRegex: regexp.MustCompile("^.*_process_start_time_seconds$"),48expectedStartTime: timestampFromFloat64(matchBuilderStartTime),49},50{51name: "match_default_sum_start_time_metric",52inputs: metrics(53sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),54histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),55summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),56sumMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),57sumMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),58),59expectedStartTime: timestampFromFloat64(matchBuilderStartTime + 1),60},61{62name: "regexp_match_gauge_metric",63inputs: metrics(64sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),65histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),66summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),67gaugeMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),68gaugeMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),69),70startTimeMetricRegex: regexp.MustCompile("^.*_process_start_time_seconds$"),71expectedStartTime: timestampFromFloat64(matchBuilderStartTime),72},73{74name: "match_default_gauge_start_time_metric",75inputs: metrics(76sumMetric("test_sum_metric", doublePoint(nil, startTime, currentTime, 16)),77histogramMetric("test_histogram_metric", histogramPoint(nil, startTime, currentTime, []float64{1, 2}, []uint64{2, 3, 4})),78summaryMetric("test_summary_metric", summaryPoint(nil, startTime, currentTime, 10, 100, []float64{10, 50, 90}, []float64{9, 15, 48})),79gaugeMetric("example_process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),80gaugeMetric("process_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime+1)),81),82expectedStartTime: timestampFromFloat64(matchBuilderStartTime + 1),83},84{85name: "empty gauge start time metrics",86inputs: metrics(87gaugeMetric("process_start_time_seconds"),88),89expectedErr: errNoDataPointsStartTimeMetric,90},91{92name: "empty sum start time metrics",93inputs: metrics(94sumMetric("process_start_time_seconds"),95),96expectedErr: errNoDataPointsStartTimeMetric,97},98{99name: "unsupported type start time metric",100inputs: metrics(101histogramMetric("process_start_time_seconds"),102),103expectedErr: errUnsupportedTypeStartTimeMetric,104},105{106name: "regexp_nomatch",107inputs: metrics(108sumMetric("subprocess_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),109),110startTimeMetricRegex: regexp.MustCompile("^.+_process_start_time_seconds$"),111expectedErr: errNoStartTimeMetrics,112},113{114name: "nomatch_default_start_time_metric",115inputs: metrics(116gaugeMetric("subprocess_start_time_seconds", doublePoint(nil, startTime, currentTime, matchBuilderStartTime)),117),118expectedErr: errNoStartTimeMetrics,119},120}121122for _, tt := range tests {123t.Run(tt.name, func(t *testing.T) {124stma := NewStartTimeMetricAdjuster(zap.NewNop(), tt.startTimeMetricRegex)125if tt.expectedErr != nil {126assert.ErrorIs(t, stma.AdjustMetrics(tt.inputs), tt.expectedErr)127return128}129assert.NoError(t, stma.AdjustMetrics(tt.inputs))130for i := 0; i < tt.inputs.ResourceMetrics().Len(); i++ {131rm := tt.inputs.ResourceMetrics().At(i)132for j := 0; j < rm.ScopeMetrics().Len(); j++ {133ilm := rm.ScopeMetrics().At(j)134for k := 0; k < ilm.Metrics().Len(); k++ {135metric := ilm.Metrics().At(k)136switch metric.Type() {137case pmetric.MetricTypeSum:138dps := metric.Sum().DataPoints()139for l := 0; l < dps.Len(); l++ {140assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())141}142case pmetric.MetricTypeSummary:143dps := metric.Summary().DataPoints()144for l := 0; l < dps.Len(); l++ {145assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())146}147case pmetric.MetricTypeHistogram:148dps := metric.Histogram().DataPoints()149for l := 0; l < dps.Len(); l++ {150assert.Equal(t, tt.expectedStartTime, dps.At(l).StartTimestamp())151}152}153}154}155}156})157}158}159160161