From e1f78cc96b35a4f7549d3277f65306ff1443ac20 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Fri, 29 Sep 2023 13:03:11 -0700 Subject: [PATCH] opamp agent type and version from collector resource and buildinfo Signed-off-by: Sean Porter --- pkg/extension/opampextension/factory.go | 2 +- pkg/extension/opampextension/go.mod | 3 +- pkg/extension/opampextension/go.sum | 2 + pkg/extension/opampextension/opamp_agent.go | 33 +++++++++++++-- .../opampextension/opamp_agent_test.go | 40 ++++++++++++++----- 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/pkg/extension/opampextension/factory.go b/pkg/extension/opampextension/factory.go index 550d6a5792..6e1c512771 100644 --- a/pkg/extension/opampextension/factory.go +++ b/pkg/extension/opampextension/factory.go @@ -37,5 +37,5 @@ func createDefaultConfig() component.Config { } func createExtension(_ context.Context, set extension.CreateSettings, cfg component.Config) (extension.Extension, error) { - return newOpampAgent(cfg.(*Config), set.Logger) + return newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) } diff --git a/pkg/extension/opampextension/go.mod b/pkg/extension/opampextension/go.mod index b394afce4b..f775ef8765 100644 --- a/pkg/extension/opampextension/go.mod +++ b/pkg/extension/opampextension/go.mod @@ -15,6 +15,8 @@ require ( go.opentelemetry.io/collector/config/confighttp v0.85.0 go.opentelemetry.io/collector/confmap v0.85.0 go.opentelemetry.io/collector/extension v0.85.0 + go.opentelemetry.io/collector/pdata v1.0.0-rcv0014 + go.opentelemetry.io/collector/semconv v0.85.0 go.uber.org/zap v1.25.0 ) @@ -57,7 +59,6 @@ require ( go.opentelemetry.io/collector/config/internal v0.85.0 // indirect go.opentelemetry.io/collector/extension/auth v0.85.0 // indirect go.opentelemetry.io/collector/featuregate v1.0.0-rcv0014 // indirect - go.opentelemetry.io/collector/pdata v1.0.0-rcv0014 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.43.0 // indirect go.opentelemetry.io/otel v1.17.0 // indirect go.opentelemetry.io/otel/metric v1.17.0 // indirect diff --git a/pkg/extension/opampextension/go.sum b/pkg/extension/opampextension/go.sum index 91259f3dc6..a29b53ea18 100644 --- a/pkg/extension/opampextension/go.sum +++ b/pkg/extension/opampextension/go.sum @@ -125,6 +125,8 @@ go.opentelemetry.io/collector/featuregate v1.0.0-rcv0014 h1:C9o0mbP0MyygqFnKueVQ go.opentelemetry.io/collector/featuregate v1.0.0-rcv0014/go.mod h1:0mE3mDLmUrOXVoNsuvj+7dV14h/9HFl/Fy9YTLoLObo= go.opentelemetry.io/collector/pdata v1.0.0-rcv0014 h1:iT5qH0NLmkGeIdDtnBogYDx7L58t6CaWGL378DEo2QY= go.opentelemetry.io/collector/pdata v1.0.0-rcv0014/go.mod h1:BRvDrx43kiSoUx3mr7SoA7h9B8+OY99mUK+CZSQFWW4= +go.opentelemetry.io/collector/semconv v0.85.0 h1:TVXgaWeYADXnytlhNq44zyv2W9m3VodBR1ffiMFDusI= +go.opentelemetry.io/collector/semconv v0.85.0/go.mod h1:j/8THcqVxFna1FpvA2zYIsUperEtOaRaqoLYIN4doWw= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.43.0 h1:HKORGpiOY0R0nAPtKx/ub8/7XoHhRooP8yNRkuPfelI= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.43.0/go.mod h1:e+y1M74SYXo/FcIx3UATwth2+5dDkM8dBi7eXg1tbw8= go.opentelemetry.io/otel v1.17.0 h1:MW+phZ6WZ5/uk2nd93ANk/6yJ+dVrvNWUjGhnnFU5jM= diff --git a/pkg/extension/opampextension/opamp_agent.go b/pkg/extension/opampextension/opamp_agent.go index a69f5284d4..d6095f6e8f 100644 --- a/pkg/extension/opampextension/opamp_agent.go +++ b/pkg/extension/opampextension/opamp_agent.go @@ -34,6 +34,8 @@ import ( "github.com/open-telemetry/opamp-go/client" "github.com/open-telemetry/opamp-go/client/types" "github.com/open-telemetry/opamp-go/protobufs" + "go.opentelemetry.io/collector/pdata/pcommon" + semconv "go.opentelemetry.io/collector/semconv/v1.18.0" "github.com/SumoLogic/sumologic-otel-collector/pkg/extension/sumologicextension" ) @@ -227,8 +229,31 @@ func (o *opampAgent) watchCredentials(ctx context.Context, callback func(ctx con return nil } -func newOpampAgent(cfg *Config, logger *zap.Logger) (*opampAgent, error) { - uid := ulid.Make() // TODO: Replace with https://github.com/open-telemetry/opentelemetry-collector/issues/6599 +func newOpampAgent(cfg *Config, logger *zap.Logger, build component.BuildInfo, res pcommon.Resource) (*opampAgent, error) { + agentType := build.Command + + sn, ok := res.Attributes().Get(semconv.AttributeServiceName) + if ok { + agentType = sn.AsString() + } + + agentVersion := build.Version + + sv, ok := res.Attributes().Get(semconv.AttributeServiceVersion) + if ok { + agentVersion = sv.AsString() + } + + uid := ulid.Make() + + sid, ok := res.Attributes().Get(semconv.AttributeServiceInstanceID) + if ok { + puid, err := ulid.Parse(sid.AsString()) + if err != nil { + return nil, err + } + uid = puid + } if cfg.InstanceUID != "" { puid, err := ulid.Parse(cfg.InstanceUID) @@ -241,8 +266,8 @@ func newOpampAgent(cfg *Config, logger *zap.Logger) (*opampAgent, error) { agent := &opampAgent{ cfg: cfg, logger: logger, - agentType: "io.opentelemetry.collector", - agentVersion: "1.0.0", // TODO: Replace with actual collector version info. + agentType: agentType, + agentVersion: agentVersion, instanceId: uid, } diff --git a/pkg/extension/opampextension/opamp_agent_test.go b/pkg/extension/opampextension/opamp_agent_test.go index dcdf3e8dad..fcdeda06b2 100644 --- a/pkg/extension/opampextension/opamp_agent_test.go +++ b/pkg/extension/opampextension/opamp_agent_test.go @@ -23,24 +23,44 @@ import ( "github.com/oklog/ulid/v2" "github.com/open-telemetry/opamp-go/protobufs" "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/extension/extensiontest" + semconv "go.opentelemetry.io/collector/semconv/v1.18.0" ) func TestNewOpampAgent(t *testing.T) { cfg := createDefaultConfig() set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg.(*Config), set.Logger) + set.BuildInfo = component.BuildInfo{Version: "test version", Command: "otelcoltest"} + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) + assert.Equal(t, o.agentType, "otelcoltest") + assert.Equal(t, o.agentVersion, "test version") assert.NotEmpty(t, o.instanceId.String()) assert.Empty(t, o.effectiveConfig) assert.Nil(t, o.agentDescription) } +func TestNewOpampAgentAttributes(t *testing.T) { + cfg := createDefaultConfig() + set := extensiontest.NewNopCreateSettings() + set.BuildInfo = component.BuildInfo{Version: "test version", Command: "otelcoltest"} + set.Resource.Attributes().PutStr(semconv.AttributeServiceName, "otelcol-sumo") + set.Resource.Attributes().PutStr(semconv.AttributeServiceVersion, "sumo.0") + set.Resource.Attributes().PutStr(semconv.AttributeServiceInstanceID, "01BX5ZZKBKACTAV9WEVGEMMVRZ") + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) + assert.NoError(t, err) + assert.Equal(t, o.agentType, "otelcol-sumo") + assert.Equal(t, o.agentVersion, "sumo.0") + assert.Equal(t, o.instanceId.String(), "01BX5ZZKBKACTAV9WEVGEMMVRZ") +} + func TestCreateAgentDescription(t *testing.T) { cfg := createDefaultConfig() set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg.(*Config), set.Logger) + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) assert.Nil(t, o.agentDescription) @@ -51,7 +71,7 @@ func TestCreateAgentDescription(t *testing.T) { func TestLoadEffectiveConfig(t *testing.T) { cfg := createDefaultConfig() set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg.(*Config), set.Logger) + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) assert.Empty(t, o.effectiveConfig) @@ -64,7 +84,7 @@ func TestLoadEffectiveConfig(t *testing.T) { func TestSaveEffectiveConfig(t *testing.T) { cfg := createDefaultConfig() set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg.(*Config), set.Logger) + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) f, err := os.CreateTemp("", "opamp-remote-config.yaml") @@ -77,7 +97,7 @@ func TestSaveEffectiveConfig(t *testing.T) { func TestUpdateAgentIdentity(t *testing.T) { cfg := createDefaultConfig() set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg.(*Config), set.Logger) + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) olduid := o.instanceId @@ -93,7 +113,7 @@ func TestUpdateAgentIdentity(t *testing.T) { func TestComposeEffectiveConfig(t *testing.T) { cfg := createDefaultConfig() set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg.(*Config), set.Logger) + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) ec := o.composeEffectiveConfig() @@ -103,7 +123,7 @@ func TestComposeEffectiveConfig(t *testing.T) { func TestApplyRemoteConfig(t *testing.T) { cfg := createDefaultConfig() set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg.(*Config), set.Logger) + o, err := newOpampAgent(cfg.(*Config), set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) assert.Empty(t, o.effectiveConfig) @@ -133,7 +153,7 @@ func TestShutdown(t *testing.T) { cfg := createDefaultConfig().(*Config) cfg.HTTPClientSettings.Auth = nil set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg, set.Logger) + o, err := newOpampAgent(cfg, set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) // Shutdown with no OpAMP client @@ -144,7 +164,7 @@ func TestStart(t *testing.T) { cfg := createDefaultConfig().(*Config) cfg.HTTPClientSettings.Auth = nil set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg, set.Logger) + o, err := newOpampAgent(cfg, set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) assert.NoError(t, o.Start(context.Background(), componenttest.NewNopHost())) @@ -154,7 +174,7 @@ func TestReload(t *testing.T) { cfg := createDefaultConfig().(*Config) cfg.HTTPClientSettings.Auth = nil set := extensiontest.NewNopCreateSettings() - o, err := newOpampAgent(cfg, set.Logger) + o, err := newOpampAgent(cfg, set.Logger, set.BuildInfo, set.Resource) assert.NoError(t, err) ctx := context.Background()