...

Text file src/go.opentelemetry.io/otel/internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl

Documentation: go.opentelemetry.io/otel/internal/shared/otlp/otlpmetric/oconf

     1// Code created by gotmpl. DO NOT MODIFY.
     2// source: internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl
     3
     4// Copyright The OpenTelemetry Authors
     5//
     6// Licensed under the Apache License, Version 2.0 (the "License");
     7// you may not use this file except in compliance with the License.
     8// You may obtain a copy of the License at
     9//
    10//     http://www.apache.org/licenses/LICENSE-2.0
    11//
    12// Unless required by applicable law or agreed to in writing, software
    13// distributed under the License is distributed on an "AS IS" BASIS,
    14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15// See the License for the specific language governing permissions and
    16// limitations under the License.
    17
    18package oconf
    19
    20import (
    21	"testing"
    22
    23	"github.com/stretchr/testify/assert"
    24	"github.com/stretchr/testify/require"
    25
    26	"go.opentelemetry.io/otel/sdk/metric"
    27	"go.opentelemetry.io/otel/sdk/metric/metricdata"
    28)
    29
    30func TestWithEnvTemporalityPreference(t *testing.T) {
    31	origReader := DefaultEnvOptionsReader.GetEnv
    32	tests := []struct {
    33		name     string
    34		envValue string
    35		want     map[metric.InstrumentKind]metricdata.Temporality
    36	}{
    37		{
    38			name:     "default do not set the selector",
    39			envValue: "",
    40		},
    41		{
    42			name:     "non-normative do not set the selector",
    43			envValue: "non-normative",
    44		},
    45		{
    46			name:     "cumulative",
    47			envValue: "cumulative",
    48			want: map[metric.InstrumentKind]metricdata.Temporality{
    49				metric.InstrumentKindCounter:                 metricdata.CumulativeTemporality,
    50				metric.InstrumentKindHistogram:               metricdata.CumulativeTemporality,
    51				metric.InstrumentKindUpDownCounter:           metricdata.CumulativeTemporality,
    52				metric.InstrumentKindObservableCounter:       metricdata.CumulativeTemporality,
    53				metric.InstrumentKindObservableUpDownCounter: metricdata.CumulativeTemporality,
    54				metric.InstrumentKindObservableGauge:         metricdata.CumulativeTemporality,
    55			},
    56		},
    57		{
    58			name:     "delta",
    59			envValue: "delta",
    60			want: map[metric.InstrumentKind]metricdata.Temporality{
    61				metric.InstrumentKindCounter:                 metricdata.DeltaTemporality,
    62				metric.InstrumentKindHistogram:               metricdata.DeltaTemporality,
    63				metric.InstrumentKindUpDownCounter:           metricdata.CumulativeTemporality,
    64				metric.InstrumentKindObservableCounter:       metricdata.DeltaTemporality,
    65				metric.InstrumentKindObservableUpDownCounter: metricdata.CumulativeTemporality,
    66				metric.InstrumentKindObservableGauge:         metricdata.CumulativeTemporality,
    67			},
    68		},
    69		{
    70			name:     "lowmemory",
    71			envValue: "lowmemory",
    72			want: map[metric.InstrumentKind]metricdata.Temporality{
    73				metric.InstrumentKindCounter:                 metricdata.DeltaTemporality,
    74				metric.InstrumentKindHistogram:               metricdata.DeltaTemporality,
    75				metric.InstrumentKindUpDownCounter:           metricdata.CumulativeTemporality,
    76				metric.InstrumentKindObservableCounter:       metricdata.CumulativeTemporality,
    77				metric.InstrumentKindObservableUpDownCounter: metricdata.CumulativeTemporality,
    78				metric.InstrumentKindObservableGauge:         metricdata.CumulativeTemporality,
    79			},
    80		},
    81	}
    82	for _, tt := range tests {
    83		t.Run(tt.name, func(t *testing.T) {
    84			DefaultEnvOptionsReader.GetEnv = func(key string) string {
    85				if key == "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE" {
    86					return tt.envValue
    87				}
    88				return origReader(key)
    89			}
    90			cfg := Config{}
    91			cfg = ApplyGRPCEnvConfigs(cfg)
    92
    93			if tt.want == nil {
    94				// There is no function set, the SDK's default is used.
    95				assert.Nil(t, cfg.Metrics.TemporalitySelector)
    96				return
    97			}
    98
    99			require.NotNil(t, cfg.Metrics.TemporalitySelector)
   100			for ik, want := range tt.want {
   101				assert.Equal(t, want, cfg.Metrics.TemporalitySelector(ik))
   102			}
   103		})
   104	}
   105	DefaultEnvOptionsReader.GetEnv = origReader
   106}
   107
   108func TestWithEnvAggPreference(t *testing.T) {
   109	origReader := DefaultEnvOptionsReader.GetEnv
   110	tests := []struct {
   111		name     string
   112		envValue string
   113		want     map[metric.InstrumentKind]metric.Aggregation
   114	}{
   115		{
   116			name:     "default do not set the selector",
   117			envValue: "",
   118		},
   119		{
   120			name:     "non-normative do not set the selector",
   121			envValue: "non-normative",
   122		},
   123		{
   124			name:     "explicit_bucket_histogram",
   125			envValue: "explicit_bucket_histogram",
   126			want: map[metric.InstrumentKind]metric.Aggregation{
   127				metric.InstrumentKindCounter:                 metric.DefaultAggregationSelector(metric.InstrumentKindCounter),
   128				metric.InstrumentKindHistogram:               metric.DefaultAggregationSelector(metric.InstrumentKindHistogram),
   129				metric.InstrumentKindUpDownCounter:           metric.DefaultAggregationSelector(metric.InstrumentKindUpDownCounter),
   130				metric.InstrumentKindObservableCounter:       metric.DefaultAggregationSelector(metric.InstrumentKindObservableCounter),
   131				metric.InstrumentKindObservableUpDownCounter: metric.DefaultAggregationSelector(metric.InstrumentKindObservableUpDownCounter),
   132				metric.InstrumentKindObservableGauge:         metric.DefaultAggregationSelector(metric.InstrumentKindObservableGauge),
   133			},
   134		},
   135		{
   136			name:     "base2_exponential_bucket_histogram",
   137			envValue: "base2_exponential_bucket_histogram",
   138			want: map[metric.InstrumentKind]metric.Aggregation{
   139				metric.InstrumentKindCounter: metric.DefaultAggregationSelector(metric.InstrumentKindCounter),
   140				metric.InstrumentKindHistogram: metric.AggregationBase2ExponentialHistogram{
   141					MaxSize:  160,
   142					MaxScale: 20,
   143					NoMinMax: false,
   144				},
   145				metric.InstrumentKindUpDownCounter:           metric.DefaultAggregationSelector(metric.InstrumentKindUpDownCounter),
   146				metric.InstrumentKindObservableCounter:       metric.DefaultAggregationSelector(metric.InstrumentKindObservableCounter),
   147				metric.InstrumentKindObservableUpDownCounter: metric.DefaultAggregationSelector(metric.InstrumentKindObservableUpDownCounter),
   148				metric.InstrumentKindObservableGauge:         metric.DefaultAggregationSelector(metric.InstrumentKindObservableGauge),
   149			},
   150		},
   151	}
   152	for _, tt := range tests {
   153		t.Run(tt.name, func(t *testing.T) {
   154			DefaultEnvOptionsReader.GetEnv = func(key string) string {
   155				if key == "OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION" {
   156					return tt.envValue
   157				}
   158				return origReader(key)
   159			}
   160			cfg := Config{}
   161			cfg = ApplyGRPCEnvConfigs(cfg)
   162
   163			if tt.want == nil {
   164				// There is no function set, the SDK's default is used.
   165				assert.Nil(t, cfg.Metrics.AggregationSelector)
   166				return
   167			}
   168
   169			require.NotNil(t, cfg.Metrics.AggregationSelector)
   170			for ik, want := range tt.want {
   171				assert.Equal(t, want, cfg.Metrics.AggregationSelector(ik))
   172			}
   173		})
   174	}
   175	DefaultEnvOptionsReader.GetEnv = origReader
   176}

View as plain text