From c3c67abd3421fe70de11e4fbbf13ebe699254222 Mon Sep 17 00:00:00 2001 From: Rafael Roquetto Date: Thu, 27 Feb 2025 17:56:58 -0600 Subject: [PATCH] Generalise endpoint resolution func --- pkg/export/otel/common.go | 16 +++++++++++++ pkg/export/otel/common_test.go | 42 ++++++++++++++++++++++++++++++++++ pkg/export/otel/metrics.go | 14 +----------- pkg/export/otel/traces.go | 14 +----------- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/pkg/export/otel/common.go b/pkg/export/otel/common.go index fdc70aa66..22ce14861 100644 --- a/pkg/export/otel/common.go +++ b/pkg/export/otel/common.go @@ -404,3 +404,19 @@ func ResourceAttrsFromEnv(svc *svc.Attrs) []attribute.KeyValue { parseOTELEnvVar(svc, envResourceAttrs, apply) return otelResourceAttrs } + +func ResolveOTLPEndpoint(endpoint, common string, grafana *GrafanaOTLP) (string, bool) { + if endpoint != "" { + return endpoint, false + } + + if common != "" { + return common, true + } + + if grafana != nil && grafana.CloudZone != "" && grafana.Endpoint() != "" { + return grafana.Endpoint(), true + } + + return "", false +} diff --git a/pkg/export/otel/common_test.go b/pkg/export/otel/common_test.go index e748cdb25..c51e129f6 100644 --- a/pkg/export/otel/common_test.go +++ b/pkg/export/otel/common_test.go @@ -186,3 +186,45 @@ func TestParseOTELEnvVar_nil(t *testing.T) { assert.True(t, reflect.DeepEqual(actual, map[string]string{})) } + +func TestResolveOTLPEndpoint(t *testing.T) { + grafana1 := GrafanaOTLP{ + CloudZone: "foo", + } + + const grafanaEndpoint = "https://otlp-gateway-foo.grafana.net/otlp" + + grafana2 := GrafanaOTLP{} + + type expected struct { + e string + common bool + } + + type testCase struct { + endpoint string + common string + grafana *GrafanaOTLP + expected expected + } + + testCases := []testCase{ + {endpoint: "e1", common: "c1", grafana: nil, expected: expected{e: "e1", common: false}}, + {endpoint: "e1", common: "", grafana: nil, expected: expected{e: "e1", common: false}}, + {endpoint: "", common: "c1", grafana: nil, expected: expected{e: "c1", common: true}}, + {endpoint: "", common: "", grafana: nil, expected: expected{e: "", common: false}}, + {endpoint: "e1", common: "c1", grafana: &grafana1, expected: expected{e: "e1", common: false}}, + {endpoint: "", common: "c1", grafana: &grafana1, expected: expected{e: "c1", common: true}}, + {endpoint: "", common: "", grafana: &grafana1, expected: expected{e: grafanaEndpoint, common: true}}, + {endpoint: "", common: "", grafana: &grafana2, expected: expected{e: "", common: false}}, + } + + for _, tc := range testCases { + t.Run(fmt.Sprint(tc), func(t *testing.T) { + ep, common := ResolveOTLPEndpoint(tc.endpoint, tc.common, tc.grafana) + + assert.Equal(t, ep, tc.expected.e) + assert.Equal(t, common, tc.expected.common) + }) + } +} diff --git a/pkg/export/otel/metrics.go b/pkg/export/otel/metrics.go index c6f1eeb36..7928183a6 100644 --- a/pkg/export/otel/metrics.go +++ b/pkg/export/otel/metrics.go @@ -146,19 +146,7 @@ func (m *MetricsConfig) GuessProtocol() Protocol { } func (m *MetricsConfig) OTLPMetricsEndpoint() (string, bool) { - isCommon := false - endpoint := m.MetricsEndpoint - if endpoint == "" { - endpoint = m.CommonEndpoint - if endpoint == "" && m.Grafana != nil && m.Grafana.CloudZone != "" { - endpoint = m.Grafana.Endpoint() - } - if endpoint != "" { - isCommon = true - } - } - - return endpoint, isCommon + return ResolveOTLPEndpoint(m.MetricsEndpoint, m.CommonEndpoint, m.Grafana) } // EndpointEnabled specifies that the OTEL metrics node is enabled if and only if diff --git a/pkg/export/otel/traces.go b/pkg/export/otel/traces.go index e8c7245e7..df7a180c3 100644 --- a/pkg/export/otel/traces.go +++ b/pkg/export/otel/traces.go @@ -115,19 +115,7 @@ func (m *TracesConfig) GetProtocol() Protocol { } func (m *TracesConfig) OTLPTracesEndpoint() (string, bool) { - endpoint := m.TracesEndpoint - isCommon := false - if endpoint == "" { - endpoint = m.CommonEndpoint - if endpoint == "" && m.Grafana != nil && m.Grafana.CloudZone != "" { - endpoint = m.Grafana.Endpoint() - } - if endpoint != "" { - isCommon = true - } - } - - return endpoint, isCommon + return ResolveOTLPEndpoint(m.TracesEndpoint, m.CommonEndpoint, m.Grafana) } func (m *TracesConfig) guessProtocol() Protocol {