Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .chloggen/opamp-matching-service-instance-id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
component: extension/opamp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Decorrelate `service.instance.id` and OpAMP `instance_uid`

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [46495]

# (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: |
Previously:
- the `service.instance.id` reported in the AgentDescription was based on the OpAMP instance UID
- the instance UID was typically set based on the `service.instance.id` from the Collector resource attributes
- it could be overriden using the `instance_uid` configuration of the OpAMP extension

This meant that the reported `service.instance.id` did not always match the Collector resource attributes,
which is a problem for correlation, and that server implementations got used to the typical case of
`service.instance.id` and `instance_uid` matching, despite there being no guarantee of this.

Now:
- the reported value of `service.instance.id` always matches the Collector resource attributes
- the instance UID is either taken from the `instance_uid` configuration or generated randomly.

This means that the two values can never be expected to match, unless both configurations are explicitly set to the same value.
That is what the OpAMP supervisor does, which means its behavior is unaffected.


# 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: [user]
44 changes: 20 additions & 24 deletions cmd/opampsupervisor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,36 +1234,32 @@ func TestSupervisorAgentDescriptionConfigApplies(t *testing.T) {
t.Fatal("Failed to get agent description after 5 seconds")
}

expectedDescription := &protobufs.AgentDescription{
IdentifyingAttributes: []*protobufs.KeyValue{
stringKeyValue("client.id", "my-client-id"),
stringKeyValue("service.instance.id", uuid.UUID(ad.InstanceUid).String()),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed? Looks weird. I think we should assert on some expected service instance ID here, no?

Copy link
Copy Markdown
Contributor Author

@jade-guiton-dd jade-guiton-dd Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, you're right. I removed it because the test was failing and I thought it was testing the extension (in which case we expect the instance ID to be random), but since this seems to be for the supervisor I would expect it to match the instance UID...

The usual error seems to be "could not get bootstrap info from the Collector: collector's OpAMP client never connected to the Supervisor", which I can't easily relate to any of my changes. I've been trying to debug these tests for a while but I'm honestly somewhat at a loss, mostly because all the useful logic happens in a different process, so the debugger never gets attached. Do you have advice on where the discrepancy could come from?

Copy link
Copy Markdown
Member

@douglascamata douglascamata Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried some stuff locally and I hit a similar problem to you, @jade-guiton-dd. I could run only this specific test with go test -tags=e2e -run 'TestSupervisorAgentDescriptionConfigApplies' -count=1 -v . (in the cmd/opampsupervisor folder) though.

It seems like require.Subset ends up comparing values with reflect.DeepEqual (see https://github.com/stretchr/testify/blob/v1.11.1/assert/assertions.go#L71) and I think the protobuf types might somehow be interfering with the comparison now, but I don't understand why.

I did some local changes to convert []*protobufs.Keyvalue to a map[string]string and I got a proper test failure due to mismatch in expected values:

diff --git a/cmd/opampsupervisor/e2e_test.go b/cmd/opampsupervisor/e2e_test.go
index 5efe2bc722c..8a0ce229c19 100644
--- a/cmd/opampsupervisor/e2e_test.go
+++ b/cmd/opampsupervisor/e2e_test.go
@@ -1237,6 +1237,7 @@ func TestSupervisorAgentDescriptionConfigApplies(t *testing.T) {
 	expectedDescription := &protobufs.AgentDescription{
 		IdentifyingAttributes: []*protobufs.KeyValue{
 			stringKeyValue("client.id", "my-client-id"),
+			stringKeyValue("service.instance.id", uuid.UUID(ad.InstanceUid).String()),
 			stringKeyValue("service.name", command),
 			stringKeyValue("service.version", version),
 		},
@@ -1248,8 +1249,14 @@ func TestSupervisorAgentDescriptionConfigApplies(t *testing.T) {
 		},
 	}
 
-	require.Subset(t, ad.AgentDescription.IdentifyingAttributes, expectedDescription.IdentifyingAttributes)
-	require.Subset(t, ad.AgentDescription.NonIdentifyingAttributes, expectedDescription.NonIdentifyingAttributes)
+	actualIdentifyingAttributes := keyValuesToStringMap(ad.AgentDescription.IdentifyingAttributes)
+	require.Subset(t, actualIdentifyingAttributes, keyValuesToStringMap(expectedDescription.IdentifyingAttributes))
+	require.Contains(t, actualIdentifyingAttributes, "service.instance.id")
+	_, err = uuid.Parse(actualIdentifyingAttributes["service.instance.id"])
+	require.NoError(t, err)
+
+	actualNonIdentifyingAttributes := keyValuesToStringMap(ad.AgentDescription.NonIdentifyingAttributes)
+	require.Subset(t, actualNonIdentifyingAttributes, keyValuesToStringMap(expectedDescription.NonIdentifyingAttributes))
 
 	time.Sleep(250 * time.Millisecond)
 }
@@ -1265,6 +1272,14 @@ func stringKeyValue(key, val string) *protobufs.KeyValue {
 	}
 }
 
+func keyValuesToStringMap(kvs []*protobufs.KeyValue) map[string]string {
+	out := make(map[string]string, len(kvs))
+	for _, kv := range kvs {
+		out[kv.Key] = kv.Value.GetStringValue()
+	}
+	return out
+}
+
 // Creates a Collector config that reads and writes logs to files and provides
 // file descriptors for I/O operations to those files. The files are placed
 // in a unique temp directory that is cleaned up after the test's completion.

When I run the test above with the assertion that this PR removed on the service.instance.id plus this patch applied I get this:

(...)
    e2e_test.go:1253:
                Error Trace:    /Users/douglas/code/opentelemetry-collector-contrib/cmd/opampsupervisor/e2e_test.go:1253
                Error:          map[string]string{"client.id":"my-client-id", "service.instance.id":"6b1f4a09-081d-4d28-ae2c-85c59ab349fe", "service.name":"otelcontribcol", "service.version":"0.146.0-dev"} does not contain map[string]string{"client.id":"my-client-id", "service.instance.id":"019cd884-2d8a-72b3-b7ab-00a3a0eb0623", "service.name":"otelcontribcol", "service.version":"0.146.0-dev"}
                Test:           TestSupervisorAgentDescriptionConfigApplies
(...)

Copy link
Copy Markdown
Contributor Author

@jade-guiton-dd jade-guiton-dd Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so that confirms that the supervisor is not properly setting service.instance.id at the Collector level.

I originally assumed it did because of this template, but after looking deeper into it, I'm not actually sure how ResourceAttributes is filled. It definitely contains the explicitly-specified values from agent.description.identifying_attributes, but I'm not sure if service.instance.id is ever added in there.

So I guess there are two options:

  • either keep the existing behavior in the Supervisor where service.instance.id is always set to the OpAMP instance UID, which would require modifying the code to make sure the resource attribute is always overwritten
  • apply the same "radical" change Tigran proposed in the Supervisor as well, and completely decorrelate them (which amounts to removing a test case in the way I did above)

Copy link
Copy Markdown
Contributor Author

@jade-guiton-dd jade-guiton-dd Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since for some reason the test is still failing for me locally, I tried pushing a new version with a modified composeExtraTelemetryConfig that adds the test line back in and always sets service.instance.id to the instance UID (unless explicitly set in s.agentDescription)

Copy link
Copy Markdown
Contributor Author

@jade-guiton-dd jade-guiton-dd Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I figured it out.

  • The reason why the tests were failing locally for me with that different error is that the bootstrap Collector tries to start its Prometheus endpoint, but I had a leftover Collector running on my system, causing a port conflict and preventing the bootstrap Collector from starting.
  • The reason why my latest change apparently failed to set service.instance.id is because composeExtraTelemetryConfig is not called when composing the configuration for the bootstrap Collector. So I instead decided to set the attribute directly inside templates/opampextension.yaml. This seems to fix the test, which is also simplified a bit from the patch you suggested above. I am assuming this will compose gracefully with the "extra telemetry" configuration.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. I'm happy that this is fixed and that you could use a simpler patch 😃

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do a full review pass in a bit. Great work!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@douglascamata did you find the time to review this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll find some time to check it out later today or tomorrow. 👍

stringKeyValue("service.name", command),
stringKeyValue("service.version", version),
},
NonIdentifyingAttributes: []*protobufs.KeyValue{
stringKeyValue("env", "prod"),
stringKeyValue("host.arch", runtime.GOARCH),
stringKeyValue("host.name", host),
stringKeyValue("os.type", runtime.GOOS),
},
expectedIdentifyingAttributes := map[string]string{
"client.id": "my-client-id",
"service.instance.id": uuid.UUID(ad.InstanceUid).String(),
"service.name": command,
"service.version": version,
}

require.Subset(t, ad.AgentDescription.IdentifyingAttributes, expectedDescription.IdentifyingAttributes)
require.Subset(t, ad.AgentDescription.NonIdentifyingAttributes, expectedDescription.NonIdentifyingAttributes)
expectedNonIdentifyingAttributes := map[string]string{
"env": "prod",
"host.arch": runtime.GOARCH,
"host.name": host,
"os.type": runtime.GOOS,
}
actualIdentifyingAttributes := keyValuesToStringMap(ad.AgentDescription.IdentifyingAttributes)
require.Subset(t, actualIdentifyingAttributes, expectedIdentifyingAttributes)
actualNonIdentifyingAttributes := keyValuesToStringMap(ad.AgentDescription.NonIdentifyingAttributes)
require.Subset(t, actualNonIdentifyingAttributes, expectedNonIdentifyingAttributes)

time.Sleep(250 * time.Millisecond)
}

func stringKeyValue(key, val string) *protobufs.KeyValue {
return &protobufs.KeyValue{
Key: key,
Value: &protobufs.AnyValue{
Value: &protobufs.AnyValue_StringValue{
StringValue: val,
},
},
func keyValuesToStringMap(kvs []*protobufs.KeyValue) map[string]string {
out := make(map[string]string, len(kvs))
for _, kv := range kvs {
out[kv.Key] = kv.Value.GetStringValue()
}
return out
}

// Creates a Collector config that reads and writes logs to files and provides
Expand Down
34 changes: 12 additions & 22 deletions cmd/opampsupervisor/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"crypto/tls"
_ "embed"
"encoding/hex"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -483,34 +484,23 @@ func (s *Supervisor) getBootstrapInfo() (err error) {
},
onMessage: func(_ serverTypes.Connection, message *protobufs.AgentToServer) *protobufs.ServerToAgent {
response := &protobufs.ServerToAgent{}

if !slices.Equal(message.GetInstanceUid(), s.persistentState.InstanceID[:]) {
done <- fmt.Errorf(
"the Collector's instance ID (%s) does not match with the instance ID set by the Supervisor (%s): %w",
hex.EncodeToString(message.GetInstanceUid()),
s.persistentState.InstanceID.String(),
errNonMatchingInstanceUID,
)
return response
}

if message.GetAvailableComponents() != nil {
s.setAvailableComponents(message.AvailableComponents)
}

if message.AgentDescription != nil {
instanceIDSeen := false
s.setAgentDescription(message.AgentDescription)
identAttr := message.AgentDescription.IdentifyingAttributes

for _, attr := range identAttr {
if attr.Key == string(conventions.ServiceInstanceIDKey) {
if attr.Value.GetStringValue() != s.persistentState.InstanceID.String() {
done <- fmt.Errorf(
"the Collector's instance ID (%s) does not match with the instance ID set by the Supervisor (%s): %w",
attr.Value.GetStringValue(),
s.persistentState.InstanceID.String(),
errNonMatchingInstanceUID,
)
return response
}
instanceIDSeen = true
}
}

if !instanceIDSeen {
done <- errors.New("the Collector did not specify an instance ID in its AgentDescription message")
return response
}
}

// agent description must be defined
Expand Down
16 changes: 13 additions & 3 deletions cmd/opampsupervisor/supervisor/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ service:
- stderr
output_paths:
- stdout
resource: null
resource:
service.instance.id: 018fee23-4a51-7303-a441-73faed7d9deb
`

remoteConfig := &protobufs.AgentRemoteConfig{
Expand Down Expand Up @@ -655,7 +656,8 @@ service:
- stderr
output_paths:
- stdout
resource: null
resource:
service.instance.id: 018fee23-4a51-7303-a441-73faed7d9deb
`

remoteConfig := &protobufs.AgentRemoteConfig{
Expand Down Expand Up @@ -841,7 +843,8 @@ service:
- stderr
output_paths:
- stdout
resource: null
resource:
service.instance.id: 018fee23-4a51-7303-a441-73faed7d9deb
`

// store the initial remote config message so the supervisor is initialized with it
Expand Down Expand Up @@ -1722,6 +1725,7 @@ service:
endpoint: localhost-metrics
protocol: http/protobuf
resource:
service.instance.id: 018fee23-4a51-7303-a441-73faed7d9deb
service.name: otelcol
traces:
processors:
Expand Down Expand Up @@ -1841,6 +1845,9 @@ service:
- nop
receivers:
- nop
telemetry:
resource:
service.instance.id: 018fee23-4a51-7303-a441-73faed7d9deb
`
s := Supervisor{
persistentState: &persistentState{
Expand Down Expand Up @@ -1884,6 +1891,9 @@ service:
- nop
receivers:
- nop
telemetry:
resource:
service.instance.id: 018fee23-4a51-7303-a441-73faed7d9deb
`
s := Supervisor{
persistentState: &persistentState{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ extensions:

service:
extensions: [opamp]
telemetry:
resource:
"service.instance.id": "{{.InstanceUid}}"
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ service:
error_output_paths: ["stderr"]
output_paths: ["stdout"]
resource:
service.instance.id: 00000000-0000-0000-0000-000000000000
service.name: otelcol
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ service:
error_output_paths: ["stderr"]
output_paths: ["stdout"]
resource:
service.instance.id: 00000000-0000-0000-0000-000000000000
service.name: otelcol
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ service:
error_output_paths: ["stderr"]
output_paths: ["stdout"]
resource:
service.instance.id: 00000000-0000-0000-0000-000000000000
service.name: otelcol
56 changes: 30 additions & 26 deletions extension/opampextension/opamp_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ type opampAgent struct {
cfg *Config
logger *zap.Logger

agentType string
agentVersion string
resourceAttrs map[string]string
serviceName string
serviceVersion string
serviceInstanceID string
resourceAttrs map[string]string

instanceID uuid.UUID
instanceUID uuid.UUID

eclk sync.RWMutex
effectiveConfig *confmap.Conf
Expand Down Expand Up @@ -133,7 +134,7 @@ func (o *opampAgent) Start(ctx context.Context, host component.Host) error {
HeaderFunc: headerFunc,
TLSConfig: tls,
OpAMPServerURL: o.cfg.Server.GetEndpoint(),
InstanceUid: types.InstanceUid(o.instanceID),
InstanceUid: types.InstanceUid(o.instanceUID),
Callbacks: types.Callbacks{
OnConnect: func(_ context.Context) {
o.logger.Debug("Connected to the OpAMP server")
Expand Down Expand Up @@ -282,39 +283,41 @@ func (o *opampAgent) updateEffectiveConfig(conf *confmap.Conf) {
}

func newOpampAgent(cfg *Config, set extension.Settings) (*opampAgent, error) {
agentType := set.BuildInfo.Command
serviceName := set.BuildInfo.Command

sn, ok := set.Resource.Attributes().Get(string(conventions.ServiceNameKey))
if ok {
agentType = sn.AsString()
serviceName = sn.AsString()
}

agentVersion := set.BuildInfo.Version
serviceVersion := set.BuildInfo.Version

sv, ok := set.Resource.Attributes().Get(string(conventions.ServiceVersionKey))
if ok {
agentVersion = sv.AsString()
serviceVersion = sv.AsString()
}

uid, err := uuid.NewV7()
if err != nil {
return nil, fmt.Errorf("could not generate uuidv7: %w", err)
serviceInstanceID := ""

if sid, ok := set.Resource.Attributes().Get(string(conventions.ServiceInstanceIDKey)); ok {
serviceInstanceID = sid.Str()
}

var uid uuid.UUID
if cfg.InstanceUID != "" {
var err error
uid, err = parseInstanceIDString(cfg.InstanceUID)
if err != nil {
return nil, fmt.Errorf("could not parse configured instance id: %w", err)
}
} else {
sid, ok := set.Resource.Attributes().Get(string(conventions.ServiceInstanceIDKey))
if ok {
uid, err = uuid.Parse(sid.AsString())
if err != nil {
return nil, err
}
var err error
uid, err = uuid.NewV7()
if err != nil {
return nil, fmt.Errorf("could not generate uuidv7: %w", err)
}
}

resourceAttrs := make(map[string]string, set.Resource.Attributes().Len())
set.Resource.Attributes().Range(func(k string, v pcommon.Value) bool {
resourceAttrs[k] = v.Str()
Expand All @@ -325,9 +328,10 @@ func newOpampAgent(cfg *Config, set extension.Settings) (*opampAgent, error) {
agent := &opampAgent{
cfg: cfg,
logger: set.Logger,
agentType: agentType,
agentVersion: agentVersion,
instanceID: uid,
serviceName: serviceName,
serviceVersion: serviceVersion,
serviceInstanceID: serviceInstanceID,
instanceUID: uid,
capabilities: cfg.Capabilities,
opampClient: opampClient,
resourceAttrs: resourceAttrs,
Expand Down Expand Up @@ -377,9 +381,9 @@ func (o *opampAgent) createAgentDescription() error {
description := getOSDescription(o.logger)

ident := []*protobufs.KeyValue{
stringKeyValue(string(conventions.ServiceInstanceIDKey), o.instanceID.String()),
stringKeyValue(string(conventions.ServiceNameKey), o.agentType),
stringKeyValue(string(conventions.ServiceVersionKey), o.agentVersion),
stringKeyValue(string(conventions.ServiceInstanceIDKey), o.serviceInstanceID),
stringKeyValue(string(conventions.ServiceNameKey), o.serviceName),
stringKeyValue(string(conventions.ServiceVersionKey), o.serviceVersion),
}

// Initially construct using a map to properly deduplicate any keys that
Expand Down Expand Up @@ -421,9 +425,9 @@ func (o *opampAgent) createAgentDescription() error {

func (o *opampAgent) updateAgentIdentity(instanceID uuid.UUID) {
o.logger.Debug("OpAMP agent identity is being changed",
zap.String("old_id", o.instanceID.String()),
zap.String("old_id", o.instanceUID.String()),
zap.String("new_id", instanceID.String()))
o.instanceID = instanceID
o.instanceUID = instanceID
}

func (o *opampAgent) composeEffectiveConfig() *protobufs.EffectiveConfig {
Expand Down
Loading
Loading