From d355edc8acf862e8c16ca4fb097b8bf95faaf479 Mon Sep 17 00:00:00 2001 From: Eric Chlebek Date: Wed, 24 Apr 2024 13:52:56 -0700 Subject: [PATCH] Use default SumoLogic URL for OpAmp clients When the OpAmp URL is not explicitly configured, use the default SumoLogic URL. --- pkg/extension/opampextension/opamp_agent.go | 17 ++++++++++++++++- .../opampextension/opamp_agent_test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/extension/opampextension/opamp_agent.go b/pkg/extension/opampextension/opamp_agent.go index caff26c472..4bb7a54e3d 100644 --- a/pkg/extension/opampextension/opamp_agent.go +++ b/pkg/extension/opampextension/opamp_agent.go @@ -41,6 +41,8 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/extension/sumologicextension" ) +const DefaultSumoLogicOpAmpURL = "wss://opamp-events.sumologic.com/v1/opamp" + type opampAgent struct { cfg *Config host component.Host @@ -99,6 +101,9 @@ func (o *opampAgent) Start(ctx context.Context, host component.Host) error { } o.endpoint = o.cfg.Endpoint + if o.endpoint == "" { + o.endpoint = DefaultSumoLogicOpAmpURL + } if o.authExtension == nil { return o.startClient(ctx) @@ -144,7 +149,7 @@ func (o *opampAgent) Reload(ctx context.Context) error { return o.Start(ctx, o.host) } -func (o *opampAgent) startClient(ctx context.Context) error { +func (o *opampAgent) startSettings() types.StartSettings { settings := types.StartSettings{ Header: o.authHeader, OpAMPServerURL: o.endpoint, @@ -172,6 +177,16 @@ func (o *opampAgent) startClient(ctx context.Context) error { Capabilities: o.getAgentCapabilities(), } + if settings.OpAMPServerURL == "" { + settings.OpAMPServerURL = DefaultSumoLogicOpAmpURL + } + + return settings +} + +func (o *opampAgent) startClient(ctx context.Context) error { + settings := o.startSettings() + o.logger.Debug("Starting OpAMP client...") if err := o.opampClient.Start(ctx, settings); err != nil { diff --git a/pkg/extension/opampextension/opamp_agent_test.go b/pkg/extension/opampextension/opamp_agent_test.go index af20f952ec..16affa2c35 100644 --- a/pkg/extension/opampextension/opamp_agent_test.go +++ b/pkg/extension/opampextension/opamp_agent_test.go @@ -485,3 +485,16 @@ func TestReload(t *testing.T) { assert.NoError(t, o.Start(ctx, componenttest.NewNopHost())) assert.NoError(t, o.Reload(ctx)) } + +func TestDefaultEndpointSetOnStart(t *testing.T) { + cfg := createDefaultConfig().(*Config) + set := extensiontest.NewNopCreateSettings() + o, err := newOpampAgent(cfg, set.Logger, set.BuildInfo, set.Resource) + if err != nil { + t.Fatal(err) + } + settings := o.startSettings() + if settings.OpAMPServerURL != DefaultSumoLogicOpAmpURL { + t.Error("expected unconfigured opamp endpoint to result in default sumo opamp url setting") + } +}