From 405987e8d14bf4b8657da25e96e00a966a33b657 Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Tue, 20 Jun 2023 14:43:22 -0500 Subject: [PATCH 1/6] Add otlp metrics env vars --- .../otlpmetric/internal/oconf/envconfig.go | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go index 93b1209388d..4d9ddbeb965 100644 --- a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go +++ b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go @@ -24,6 +24,8 @@ import ( "time" "go.opentelemetry.io/otel/exporters/otlp/internal/envconfig" + "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/metric/metricdata" ) // DefaultEnvOptionsReader is the default environments reader. @@ -96,6 +98,7 @@ func getOptionsFromEnv() []GenericOption { WithEnvCompression("METRICS_COMPRESSION", func(c Compression) { opts = append(opts, WithCompression(c)) }), envconfig.WithDuration("TIMEOUT", func(d time.Duration) { opts = append(opts, WithTimeout(d)) }), envconfig.WithDuration("METRICS_TIMEOUT", func(d time.Duration) { opts = append(opts, WithTimeout(d)) }), + withEnvTemporalityPreference("METRICS_TEMPORALITY_PREFERENCE", func(t metric.TemporalitySelector) { opts = append(opts, WithTemporalitySelector(t)) }), ) return opts @@ -148,3 +151,38 @@ func withTLSConfig(c *tls.Config, fn func(*tls.Config)) func(e *envconfig.EnvOpt } } } + +func withEnvTemporalityPreference(n string, fn func(metric.TemporalitySelector)) func(e *envconfig.EnvOptionsReader) { + return func(e *envconfig.EnvOptionsReader) { + if s, ok := e.GetEnvValue(n); ok { + switch strings.ToLower(s) { + case "cumulative": + fn(cumulativeTemporality) + case "delta": + fn(deltaTemporality) + case "lowmemory": + fn(lowMemory) + } + } + } +} + +func cumulativeTemporality(metric.InstrumentKind) metricdata.Temporality { + return metricdata.CumulativeTemporality +} +func deltaTemporality(ik metric.InstrumentKind) metricdata.Temporality { + switch ik { + case metric.InstrumentKindCounter, metric.InstrumentKindHistogram, metric.InstrumentKindObservableCounter: + return metricdata.DeltaTemporality + default: + return metricdata.CumulativeTemporality + } +} +func lowMemory(ik metric.InstrumentKind) metricdata.Temporality { + switch ik { + case metric.InstrumentKindCounter, metric.InstrumentKindHistogram: + return metricdata.DeltaTemporality + default: + return metricdata.CumulativeTemporality + } +} From b00b4943e37e6cba0b8dab43d461a49836af8b7a Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Fri, 7 Jul 2023 17:31:03 -0500 Subject: [PATCH 2/6] Add changelog and test. --- CHANGELOG.md | 1 + .../internal/oconf/envconfig_test.go | 102 ++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 258c09bda9f..84110ef3a83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add `PeriodicReader` struct in `go.opentelemetry.io/otel/sdk/metric`. (#4244) - Add `Exporter` struct in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc`. (#4272) - Add `Exporter` struct in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#4272) +- OTLP Metrics Exporter now supports the `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE` environment variable. (#4287) ### Changed diff --git a/exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go b/exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go new file mode 100644 index 00000000000..00e63866b1a --- /dev/null +++ b/exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go @@ -0,0 +1,102 @@ +// 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 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 +} From f9ee6e656194e6a59ae7694b5485cf5a7b20babf Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:37:15 -0500 Subject: [PATCH 3/6] Address PR comments. --- exporters/otlp/otlpmetric/internal/oconf/envconfig.go | 4 ++++ exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go index 4d9ddbeb965..1e7291f1126 100644 --- a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go +++ b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go @@ -24,6 +24,7 @@ import ( "time" "go.opentelemetry.io/otel/exporters/otlp/internal/envconfig" + "go.opentelemetry.io/otel/internal/global" "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/metricdata" ) @@ -163,6 +164,7 @@ func withEnvTemporalityPreference(n string, fn func(metric.TemporalitySelector)) case "lowmemory": fn(lowMemory) } + global.Warn("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to an invalid value. Defaulting to \"cumulative\".", "value", s) } } } @@ -170,6 +172,7 @@ func withEnvTemporalityPreference(n string, fn func(metric.TemporalitySelector)) func cumulativeTemporality(metric.InstrumentKind) metricdata.Temporality { return metricdata.CumulativeTemporality } + func deltaTemporality(ik metric.InstrumentKind) metricdata.Temporality { switch ik { case metric.InstrumentKindCounter, metric.InstrumentKindHistogram, metric.InstrumentKindObservableCounter: @@ -177,6 +180,7 @@ func deltaTemporality(ik metric.InstrumentKind) metricdata.Temporality { default: return metricdata.CumulativeTemporality } + } func lowMemory(ik metric.InstrumentKind) metricdata.Temporality { switch ik { diff --git a/exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go b/exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go index 00e63866b1a..0c54c78e29a 100644 --- a/exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go +++ b/exporters/otlp/otlpmetric/internal/oconf/envconfig_test.go @@ -19,6 +19,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/metricdata" ) From 57bef592d7ae30f28ac510a968b690ca3b80af0f Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Mon, 17 Jul 2023 13:13:22 -0500 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Tyler Yahn --- exporters/otlp/otlpmetric/internal/oconf/envconfig.go | 1 + 1 file changed, 1 insertion(+) diff --git a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go index 1e7291f1126..8309ffb6c0d 100644 --- a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go +++ b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go @@ -182,6 +182,7 @@ func deltaTemporality(ik metric.InstrumentKind) metricdata.Temporality { } } + func lowMemory(ik metric.InstrumentKind) metricdata.Temporality { switch ik { case metric.InstrumentKindCounter, metric.InstrumentKindHistogram: From e94fc7dfe1109b5a5c1f078524aa25e6dd77c257 Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Mon, 17 Jul 2023 15:20:26 -0500 Subject: [PATCH 5/6] fix warning and lint. --- exporters/otlp/otlpmetric/internal/oconf/envconfig.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go index 8309ffb6c0d..ac204e3f8dd 100644 --- a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go +++ b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go @@ -163,8 +163,9 @@ func withEnvTemporalityPreference(n string, fn func(metric.TemporalitySelector)) fn(deltaTemporality) case "lowmemory": fn(lowMemory) + default: + global.Warn("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to an invalid value. Defaulting to \"cumulative\".", "value", s) } - global.Warn("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to an invalid value. Defaulting to \"cumulative\".", "value", s) } } } @@ -180,7 +181,6 @@ func deltaTemporality(ik metric.InstrumentKind) metricdata.Temporality { default: return metricdata.CumulativeTemporality } - } func lowMemory(ik metric.InstrumentKind) metricdata.Temporality { From 215192504a705a9688a200b295d780bb6a11d45d Mon Sep 17 00:00:00 2001 From: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com> Date: Tue, 18 Jul 2023 12:51:07 -0500 Subject: [PATCH 6/6] Update exporters/otlp/otlpmetric/internal/oconf/envconfig.go Co-authored-by: Tyler Yahn --- exporters/otlp/otlpmetric/internal/oconf/envconfig.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go index ac204e3f8dd..a8cbdb8eb2a 100644 --- a/exporters/otlp/otlpmetric/internal/oconf/envconfig.go +++ b/exporters/otlp/otlpmetric/internal/oconf/envconfig.go @@ -164,7 +164,7 @@ func withEnvTemporalityPreference(n string, fn func(metric.TemporalitySelector)) case "lowmemory": fn(lowMemory) default: - global.Warn("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to an invalid value. Defaulting to \"cumulative\".", "value", s) + global.Warn("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE is set to an invalid value, ignoring.", "value", s) } } }