From 19fa645be717bd57aa146fd64e2d3626af4c8429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 17 Apr 2024 11:55:55 +0200 Subject: [PATCH] Revert envconfig_test.go.tmpl --- .../otlpmetric/otlpmetricgrpc/internal/gen.go | 1 + .../internal/oconf/envconfig_test.go | 3 + .../otlpmetric/otlpmetrichttp/internal/gen.go | 1 + .../internal/oconf/envconfig_test.go | 7 +- .../otlpmetric/oconf/envconfig_test.go.tmpl | 165 ++++++++++++++++++ 5 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/gen.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/gen.go index 57d4c7ada52a..88c0795a57f2 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/gen.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/gen.go @@ -12,6 +12,7 @@ package internal // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/o //go:generate gotmpl --body=../../../../../internal/shared/otlp/envconfig/envconfig.go.tmpl "--data={}" --out=envconfig/envconfig.go //go:generate gotmpl --body=../../../../../internal/shared/otlp/envconfig/envconfig_test.go.tmpl "--data={}" --out=envconfig/envconfig_test.go +//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl "--data={}" --out=oconf/envconfig_test.go //go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/optiontypes.go.tmpl "--data={}" --out=oconf/optiontypes.go //go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/tls.go.tmpl "--data={}" --out=oconf/tls.go diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/envconfig_test.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/envconfig_test.go index ee90ff821b1a..5c621d7b47e6 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/envconfig_test.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/envconfig_test.go @@ -1,3 +1,6 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl + // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/gen.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/gen.go index 51890a033a12..feac511bb8a0 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/gen.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/gen.go @@ -12,6 +12,7 @@ package internal // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/o //go:generate gotmpl --body=../../../../../internal/shared/otlp/envconfig/envconfig.go.tmpl "--data={}" --out=envconfig/envconfig.go //go:generate gotmpl --body=../../../../../internal/shared/otlp/envconfig/envconfig_test.go.tmpl "--data={}" --out=envconfig/envconfig_test.go +//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl "--data={}" --out=oconf/envconfig_test.go //go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/optiontypes.go.tmpl "--data={}" --out=oconf/optiontypes.go //go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/tls.go.tmpl "--data={}" --out=oconf/tls.go diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/envconfig_test.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/envconfig_test.go index ba99959db56b..5c621d7b47e6 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/envconfig_test.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/envconfig_test.go @@ -1,3 +1,6 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl + // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 @@ -74,7 +77,7 @@ func TestWithEnvTemporalityPreference(t *testing.T) { return origReader(key) } cfg := Config{} - cfg = ApplyHTTPEnvConfigs(cfg) + cfg = ApplyGRPCEnvConfigs(cfg) if tt.want == nil { // There is no function set, the SDK's default is used. @@ -144,7 +147,7 @@ func TestWithEnvAggPreference(t *testing.T) { return origReader(key) } cfg := Config{} - cfg = ApplyHTTPEnvConfigs(cfg) + cfg = ApplyGRPCEnvConfigs(cfg) if tt.want == nil { // There is no function set, the SDK's default is used. diff --git a/internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl b/internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl new file mode 100644 index 000000000000..5c621d7b47e6 --- /dev/null +++ b/internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl @@ -0,0 +1,165 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl + +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package oconf + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/metric/metricdata" +) + +func TestWithEnvTemporalityPreference(t *testing.T) { + origReader := DefaultEnvOptionsReader.GetEnv + tests := []struct { + name string + envValue string + want map[metric.InstrumentKind]metricdata.Temporality + }{ + { + name: "default do not set the selector", + envValue: "", + }, + { + name: "non-normative do not set the selector", + envValue: "non-normative", + }, + { + name: "cumulative", + envValue: "cumulative", + want: map[metric.InstrumentKind]metricdata.Temporality{ + metric.InstrumentKindCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindHistogram: metricdata.CumulativeTemporality, + metric.InstrumentKindUpDownCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableUpDownCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableGauge: metricdata.CumulativeTemporality, + }, + }, + { + name: "delta", + envValue: "delta", + want: map[metric.InstrumentKind]metricdata.Temporality{ + metric.InstrumentKindCounter: metricdata.DeltaTemporality, + metric.InstrumentKindHistogram: metricdata.DeltaTemporality, + metric.InstrumentKindUpDownCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableCounter: metricdata.DeltaTemporality, + metric.InstrumentKindObservableUpDownCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableGauge: metricdata.CumulativeTemporality, + }, + }, + { + name: "lowmemory", + envValue: "lowmemory", + want: map[metric.InstrumentKind]metricdata.Temporality{ + metric.InstrumentKindCounter: metricdata.DeltaTemporality, + metric.InstrumentKindHistogram: metricdata.DeltaTemporality, + metric.InstrumentKindUpDownCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableUpDownCounter: metricdata.CumulativeTemporality, + metric.InstrumentKindObservableGauge: metricdata.CumulativeTemporality, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + DefaultEnvOptionsReader.GetEnv = func(key string) string { + if key == "OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE" { + return tt.envValue + } + return origReader(key) + } + cfg := Config{} + cfg = ApplyGRPCEnvConfigs(cfg) + + if tt.want == nil { + // There is no function set, the SDK's default is used. + assert.Nil(t, cfg.Metrics.TemporalitySelector) + return + } + + require.NotNil(t, cfg.Metrics.TemporalitySelector) + for ik, want := range tt.want { + assert.Equal(t, want, cfg.Metrics.TemporalitySelector(ik)) + } + }) + } + DefaultEnvOptionsReader.GetEnv = origReader +} + +func TestWithEnvAggPreference(t *testing.T) { + origReader := DefaultEnvOptionsReader.GetEnv + tests := []struct { + name string + envValue string + want map[metric.InstrumentKind]metric.Aggregation + }{ + { + name: "default do not set the selector", + envValue: "", + }, + { + name: "non-normative do not set the selector", + envValue: "non-normative", + }, + { + name: "explicit_bucket_histogram", + envValue: "explicit_bucket_histogram", + want: map[metric.InstrumentKind]metric.Aggregation{ + metric.InstrumentKindCounter: metric.DefaultAggregationSelector(metric.InstrumentKindCounter), + metric.InstrumentKindHistogram: metric.DefaultAggregationSelector(metric.InstrumentKindHistogram), + metric.InstrumentKindUpDownCounter: metric.DefaultAggregationSelector(metric.InstrumentKindUpDownCounter), + metric.InstrumentKindObservableCounter: metric.DefaultAggregationSelector(metric.InstrumentKindObservableCounter), + metric.InstrumentKindObservableUpDownCounter: metric.DefaultAggregationSelector(metric.InstrumentKindObservableUpDownCounter), + metric.InstrumentKindObservableGauge: metric.DefaultAggregationSelector(metric.InstrumentKindObservableGauge), + }, + }, + { + name: "base2_exponential_bucket_histogram", + envValue: "base2_exponential_bucket_histogram", + want: map[metric.InstrumentKind]metric.Aggregation{ + metric.InstrumentKindCounter: metric.DefaultAggregationSelector(metric.InstrumentKindCounter), + metric.InstrumentKindHistogram: metric.AggregationBase2ExponentialHistogram{ + MaxSize: 160, + MaxScale: 20, + NoMinMax: false, + }, + metric.InstrumentKindUpDownCounter: metric.DefaultAggregationSelector(metric.InstrumentKindUpDownCounter), + metric.InstrumentKindObservableCounter: metric.DefaultAggregationSelector(metric.InstrumentKindObservableCounter), + metric.InstrumentKindObservableUpDownCounter: metric.DefaultAggregationSelector(metric.InstrumentKindObservableUpDownCounter), + metric.InstrumentKindObservableGauge: metric.DefaultAggregationSelector(metric.InstrumentKindObservableGauge), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + DefaultEnvOptionsReader.GetEnv = func(key string) string { + if key == "OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION" { + return tt.envValue + } + return origReader(key) + } + cfg := Config{} + cfg = ApplyGRPCEnvConfigs(cfg) + + if tt.want == nil { + // There is no function set, the SDK's default is used. + assert.Nil(t, cfg.Metrics.AggregationSelector) + return + } + + require.NotNil(t, cfg.Metrics.AggregationSelector) + for ik, want := range tt.want { + assert.Equal(t, want, cfg.Metrics.AggregationSelector(ik)) + } + }) + } + DefaultEnvOptionsReader.GetEnv = origReader +}