From 5cde35299f30e3e3d9498cd519e6c6cff48b4203 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:16:25 -0800 Subject: [PATCH] [cmd/opampsupervisor] support headers configuration for own telemetry (#37353) This updates the configuration for reporting the collector's own telemetry to utilize the headers configuration passed in via opamp Follows https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/37346 --------- Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- .../codeboten_add-support-for-headers.yaml | 27 +++++++++++++++++++ cmd/opampsupervisor/supervisor/supervisor.go | 19 ++++++------- .../supervisor/supervisor_test.go | 21 ++++++++++++--- .../supervisor/templates/owntelemetry.yaml | 6 +++++ 4 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 .chloggen/codeboten_add-support-for-headers.yaml diff --git a/.chloggen/codeboten_add-support-for-headers.yaml b/.chloggen/codeboten_add-support-for-headers.yaml new file mode 100644 index 000000000000..8a4b190724f9 --- /dev/null +++ b/.chloggen/codeboten_add-support-for-headers.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: opampsupervisor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: add support for headers configuration for reporting own telemetry + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [37353] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/cmd/opampsupervisor/supervisor/supervisor.go b/cmd/opampsupervisor/supervisor/supervisor.go index 894854273c6a..0afe9c2ed569 100644 --- a/cmd/opampsupervisor/supervisor/supervisor.go +++ b/cmd/opampsupervisor/supervisor/supervisor.go @@ -869,19 +869,16 @@ func (s *Supervisor) setupOwnMetrics(_ context.Context, settings *protobufs.Tele } else { s.logger.Debug("Enabling own metrics pipeline in the config") - port, err := s.findRandomPort() - if err != nil { - s.logger.Error("Could not setup own metrics", zap.Error(err)) - return + data := map[string]any{ + "MetricsEndpoint": settings.DestinationEndpoint, + "MetricsHeaders": []protobufs.Header{}, } - err = s.ownTelemetryTemplate.Execute( - &cfg, - map[string]any{ - "PrometheusPort": port, - "MetricsEndpoint": settings.DestinationEndpoint, - }, - ) + if settings.Headers != nil { + data["MetricsHeaders"] = settings.Headers.Headers + } + + err := s.ownTelemetryTemplate.Execute(&cfg, data) if err != nil { s.logger.Error("Could not setup own metrics", zap.Error(err)) return diff --git a/cmd/opampsupervisor/supervisor/supervisor_test.go b/cmd/opampsupervisor/supervisor/supervisor_test.go index 0ea373a32778..80c16c67c938 100644 --- a/cmd/opampsupervisor/supervisor/supervisor_test.go +++ b/cmd/opampsupervisor/supervisor/supervisor_test.go @@ -357,7 +357,13 @@ func Test_onMessage(t *testing.T) { }, }, OwnMetricsConnSettings: &protobufs.TelemetryConnectionSettings{ - DestinationEndpoint: "http://localhost:4318", + DestinationEndpoint: "http://127.0.0.1:4318", + Headers: &protobufs.Headers{ + Headers: []*protobufs.Header{ + {Key: "testkey", Value: "testval"}, + {Key: "testkey2", Value: "testval2"}, + }, + }, }, }) @@ -1125,7 +1131,13 @@ func TestSupervisor_setupOwnMetrics(t *testing.T) { require.NoError(t, err) configChanged := s.setupOwnMetrics(context.Background(), &protobufs.TelemetryConnectionSettings{ - DestinationEndpoint: "localhost", + DestinationEndpoint: "http://127.0.0.1:4318", + Headers: &protobufs.Headers{ + Headers: []*protobufs.Header{ + {Key: "testkey", Value: "testval"}, + {Key: "testkey2", Value: "testval2"}, + }, + }, }) expectedOwnMetricsSection := ` @@ -1137,7 +1149,10 @@ service: exporter: otlp: protocol: http/protobuf - endpoint: localhost + endpoint: http://127.0.0.1:4318 + headers: + "testkey": "testval" + "testkey2": "testval2" ` assert.True(t, configChanged) diff --git a/cmd/opampsupervisor/supervisor/templates/owntelemetry.yaml b/cmd/opampsupervisor/supervisor/templates/owntelemetry.yaml index 976e7d3f4037..ec02e2cdfd1b 100644 --- a/cmd/opampsupervisor/supervisor/templates/owntelemetry.yaml +++ b/cmd/opampsupervisor/supervisor/templates/owntelemetry.yaml @@ -8,3 +8,9 @@ service: otlp: protocol: http/protobuf endpoint: {{.MetricsEndpoint}} + {{- if .MetricsHeaders}} + headers: + {{- range $k := .MetricsHeaders}} + "{{$k.Key}}": "{{$k.Value}}" + {{- end}} + {{- end}}