Skip to content

Commit

Permalink
Merge pull request #10 from grafana/otel_gen_fix
Browse files Browse the repository at this point in the history
Generalise endpoint resolution func
  • Loading branch information
grcevski authored Feb 28, 2025
2 parents 0a86c8b + c3c67ab commit 740f620
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
16 changes: 16 additions & 0 deletions pkg/export/otel/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
42 changes: 42 additions & 0 deletions pkg/export/otel/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
14 changes: 1 addition & 13 deletions pkg/export/otel/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 1 addition & 13 deletions pkg/export/otel/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 740f620

Please sign in to comment.