Path: blob/main/component/otelcol/receiver/prometheus/internal/util_test.go
5395 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 internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"1516import (17"testing"18"time"1920"github.com/prometheus/common/model"21"github.com/prometheus/prometheus/model/labels"22"github.com/prometheus/prometheus/model/textparse"23"github.com/prometheus/prometheus/scrape"24"github.com/stretchr/testify/assert"25"github.com/stretchr/testify/require"26"go.opentelemetry.io/collector/pdata/pcommon"27"go.opentelemetry.io/collector/pdata/pmetric"28)2930var testMetadata = map[string]scrape.MetricMetadata{31"counter_test": {Metric: "counter_test", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},32"counter_test2": {Metric: "counter_test2", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},33"gauge_test": {Metric: "gauge_test", Type: textparse.MetricTypeGauge, Help: "", Unit: ""},34"gauge_test2": {Metric: "gauge_test2", Type: textparse.MetricTypeGauge, Help: "", Unit: ""},35"hist_test": {Metric: "hist_test", Type: textparse.MetricTypeHistogram, Help: "", Unit: ""},36"hist_test2": {Metric: "hist_test2", Type: textparse.MetricTypeHistogram, Help: "", Unit: ""},37"ghist_test": {Metric: "ghist_test", Type: textparse.MetricTypeGaugeHistogram, Help: "", Unit: ""},38"summary_test": {Metric: "summary_test", Type: textparse.MetricTypeSummary, Help: "", Unit: ""},39"summary_test2": {Metric: "summary_test2", Type: textparse.MetricTypeSummary, Help: "", Unit: ""},40"unknown_test": {Metric: "unknown_test", Type: textparse.MetricTypeUnknown, Help: "", Unit: ""},41"poor_name": {Metric: "poor_name", Type: textparse.MetricTypeGauge, Help: "", Unit: ""},42"poor_name_count": {Metric: "poor_name_count", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},43"scrape_foo": {Metric: "scrape_foo", Type: textparse.MetricTypeCounter, Help: "", Unit: ""},44"example_process_start_time_seconds": {Metric: "example_process_start_time_seconds",45Type: textparse.MetricTypeGauge, Help: "", Unit: ""},46"process_start_time_seconds": {Metric: "process_start_time_seconds",47Type: textparse.MetricTypeGauge, Help: "", Unit: ""},48"subprocess_start_time_seconds": {Metric: "subprocess_start_time_seconds",49Type: textparse.MetricTypeGauge, Help: "", Unit: ""},50}5152func TestTimestampFromMs(t *testing.T) {53assert.Equal(t, pcommon.Timestamp(0), timestampFromMs(0))54assert.Equal(t, pcommon.NewTimestampFromTime(time.UnixMilli(1662679535432)), timestampFromMs(1662679535432))55}5657func TestTimestampFromFloat64(t *testing.T) {58assert.Equal(t, pcommon.Timestamp(0), timestampFromFloat64(0))59// Because of float64 conversion, we check only that we are within 100ns error.60assert.InEpsilon(t, uint64(1662679535040000000), uint64(timestampFromFloat64(1662679535.040)), 100)61}6263func TestConvToMetricType(t *testing.T) {64tests := []struct {65name string66mtype textparse.MetricType67want pmetric.MetricType68wantMonotonic bool69}{70{71name: "textparse.counter",72mtype: textparse.MetricTypeCounter,73want: pmetric.MetricTypeSum,74wantMonotonic: true,75},76{77name: "textparse.gauge",78mtype: textparse.MetricTypeGauge,79want: pmetric.MetricTypeGauge,80wantMonotonic: false,81},82{83name: "textparse.unknown",84mtype: textparse.MetricTypeUnknown,85want: pmetric.MetricTypeGauge,86wantMonotonic: false,87},88{89name: "textparse.histogram",90mtype: textparse.MetricTypeHistogram,91want: pmetric.MetricTypeHistogram,92wantMonotonic: true,93},94{95name: "textparse.summary",96mtype: textparse.MetricTypeSummary,97want: pmetric.MetricTypeSummary,98wantMonotonic: true,99},100{101name: "textparse.metric_type_info",102mtype: textparse.MetricTypeInfo,103want: pmetric.MetricTypeSum,104wantMonotonic: false,105},106{107name: "textparse.metric_state_set",108mtype: textparse.MetricTypeStateset,109want: pmetric.MetricTypeSum,110wantMonotonic: false,111},112{113name: "textparse.metric_gauge_histogram",114mtype: textparse.MetricTypeGaugeHistogram,115want: pmetric.MetricTypeEmpty,116wantMonotonic: false,117},118}119120for _, tt := range tests {121tt := tt122t.Run(tt.name, func(t *testing.T) {123got, monotonic := convToMetricType(tt.mtype)124require.Equal(t, got.String(), tt.want.String())125require.Equal(t, monotonic, tt.wantMonotonic)126})127}128}129130func TestGetBoundary(t *testing.T) {131tests := []struct {132name string133mtype pmetric.MetricType134labels labels.Labels135wantValue float64136wantErr error137}{138{139name: "cumulative histogram with bucket label",140mtype: pmetric.MetricTypeHistogram,141labels: labels.FromStrings(model.BucketLabel, "0.256"),142wantValue: 0.256,143},144{145name: "gauge histogram with bucket label",146mtype: pmetric.MetricTypeHistogram,147labels: labels.FromStrings(model.BucketLabel, "11.71"),148wantValue: 11.71,149},150{151name: "summary with bucket label",152mtype: pmetric.MetricTypeSummary,153labels: labels.FromStrings(model.BucketLabel, "11.71"),154wantErr: errEmptyQuantileLabel,155},156{157name: "summary with quantile label",158mtype: pmetric.MetricTypeSummary,159labels: labels.FromStrings(model.QuantileLabel, "92.88"),160wantValue: 92.88,161},162{163name: "gauge histogram mismatched with bucket label",164mtype: pmetric.MetricTypeSummary,165labels: labels.FromStrings(model.BucketLabel, "11.71"),166wantErr: errEmptyQuantileLabel,167},168{169name: "other data types without matches",170mtype: pmetric.MetricTypeGauge,171labels: labels.FromStrings(model.BucketLabel, "11.71"),172wantErr: errNoBoundaryLabel,173},174}175176for _, tt := range tests {177tt := tt178t.Run(tt.name, func(t *testing.T) {179value, err := getBoundary(tt.mtype, tt.labels)180if tt.wantErr != nil {181assert.ErrorIs(t, err, tt.wantErr)182return183}184185assert.NoError(t, err)186assert.Equal(t, value, tt.wantValue)187})188}189}190191192