Skip to content

Commit

Permalink
opamp agent type and version from collector resource and buildinfo
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Porter <[email protected]>
  • Loading branch information
portertech committed Sep 29, 2023
1 parent 15d3d11 commit e1f78cc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/extension/opampextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/extension/opampextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/extension/opampextension/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
33 changes: 29 additions & 4 deletions pkg/extension/opampextension/opamp_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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,
}

Expand Down
40 changes: 30 additions & 10 deletions pkg/extension/opampextension/opamp_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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()))
Expand All @@ -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()
Expand Down

0 comments on commit e1f78cc

Please sign in to comment.