Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[extension/opamp] Report Available Components via OpAMP extension #37249

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions .chloggen/report-available-components-opamp-extension.yaml
Original file line number Diff line number Diff line change
@@ -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: opampextension

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support retrieval of available components via the OpAMP extension.

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

# (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: []
1 change: 1 addition & 0 deletions extension/opampextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The following settings are optional for both transports:
- `capabilities`: Keys with boolean true/false values that enable a particular OpAMP capability.
- `reports_effective_config`: Whether to enable the OpAMP ReportsEffectiveConfig capability. Default is `true`.
- `reports_health`: Whether to enable the OpAMP ReportsHealth capability. Default is `true`.
- `reports_available_components`: Whether to enable the OpAMP ReportsAvailableComponents capability. Default is `true`.
- `agent_description`: Setting that modifies the agent description reported to the OpAMP server.
- `non_identifying_attributes`: A map of key value pairs that will be added to the [non-identifying attributes](https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#agentdescriptionnon_identifying_attributes) reported to the OpAMP server. If an attribute collides with the default non-identifying attributes that are automatically added, the ones specified here take precedence.
- `ppid`: An optional process ID to monitor. When this process is no longer running, the extension will emit a fatal error, causing the collector to exit. This is meant to be set by the Supervisor or some other parent process, and should not be configured manually.
Expand Down
6 changes: 6 additions & 0 deletions extension/opampextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Capabilities struct {
ReportsEffectiveConfig bool `mapstructure:"reports_effective_config"`
// ReportsHealth enables the OpAMP ReportsHealth Capability. (default: true)
ReportsHealth bool `mapstructure:"reports_health"`
// ReportsAvailableComponents enables the OpAMP ReportsAvailableComponents Capability (default: true)
ReportsAvailableComponents bool `mapstructure:"reports_available_components"`
}

func (caps Capabilities) toAgentCapabilities() protobufs.AgentCapabilities {
Expand All @@ -69,6 +71,10 @@ func (caps Capabilities) toAgentCapabilities() protobufs.AgentCapabilities {
agentCapabilities |= protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth
}

if caps.ReportsAvailableComponents {
agentCapabilities |= protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents
}

return agentCapabilities
}

Expand Down
32 changes: 19 additions & 13 deletions extension/opampextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func TestUnmarshalConfig(t *testing.T) {
},
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZ",
Capabilities: Capabilities{
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsAvailableComponents: true,
},
PPIDPollInterval: 5 * time.Second,
}, cfg)
Expand All @@ -64,8 +65,9 @@ func TestUnmarshalHttpConfig(t *testing.T) {
},
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZ",
Capabilities: Capabilities{
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsAvailableComponents: true,
},
PPIDPollInterval: 5 * time.Second,
}, cfg)
Expand Down Expand Up @@ -292,8 +294,9 @@ func TestConfig_Validate(t *testing.T) {

func TestCapabilities_toAgentCapabilities(t *testing.T) {
type fields struct {
ReportsEffectiveConfig bool
ReportsHealth bool
ReportsEffectiveConfig bool
ReportsHealth bool
ReportsAvailableComponents bool
}
tests := []struct {
name string
Expand All @@ -303,25 +306,28 @@ func TestCapabilities_toAgentCapabilities(t *testing.T) {
{
name: "default capabilities",
fields: fields{
ReportsEffectiveConfig: false,
ReportsHealth: false,
ReportsEffectiveConfig: false,
ReportsHealth: false,
ReportsAvailableComponents: false,
},
want: protobufs.AgentCapabilities_AgentCapabilities_ReportsStatus,
},
{
name: "all supported capabilities enabled",
fields: fields{
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsAvailableComponents: true,
},
want: protobufs.AgentCapabilities_AgentCapabilities_ReportsStatus | protobufs.AgentCapabilities_AgentCapabilities_ReportsEffectiveConfig | protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth,
want: protobufs.AgentCapabilities_AgentCapabilities_ReportsStatus | protobufs.AgentCapabilities_AgentCapabilities_ReportsEffectiveConfig | protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth | protobufs.AgentCapabilities_AgentCapabilities_ReportsAvailableComponents,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
caps := Capabilities{
ReportsEffectiveConfig: tt.fields.ReportsEffectiveConfig,
ReportsHealth: tt.fields.ReportsHealth,
ReportsEffectiveConfig: tt.fields.ReportsEffectiveConfig,
ReportsHealth: tt.fields.ReportsHealth,
ReportsAvailableComponents: tt.fields.ReportsEffectiveConfig,
}
assert.Equalf(t, tt.want, caps.toAgentCapabilities(), "toAgentCapabilities()")
})
Expand Down
5 changes: 3 additions & 2 deletions extension/opampextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func createDefaultConfig() component.Config {
return &Config{
Server: &OpAMPServer{},
Capabilities: Capabilities{
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsEffectiveConfig: true,
ReportsHealth: true,
ReportsAvailableComponents: true,
},
PPIDPollInterval: 5 * time.Second,
}
Expand Down
4 changes: 3 additions & 1 deletion extension/opampextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/extension/opamp

go 1.22.0

replace github.com/open-telemetry/opamp-go => /Users/ian/git/opamp-go

require (
github.com/google/uuid v1.6.0
github.com/oklog/ulid/v2 v2.1.0
github.com/open-telemetry/opamp-go v0.18.0
github.com/open-telemetry/opamp-go v0.18.1-0.20250109233938-e6fac32dddf5
github.com/open-telemetry/opentelemetry-collector-contrib/extension/opampcustommessages v0.117.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/status v0.117.0
github.com/shirou/gopsutil/v4 v4.24.12
Expand Down
2 changes: 2 additions & 0 deletions extension/opampextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 84 additions & 2 deletions extension/opampextension/opamp_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package opampextension // import "github.com/open-telemetry/opentelemetry-collec

import (
"context"
"crypto/sha256"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -66,7 +67,8 @@ type opampAgent struct {

capabilities Capabilities

agentDescription *protobufs.AgentDescription
agentDescription *protobufs.AgentDescription
availableComponents *protobufs.AvailableComponents

opampClient client.OpAMPClient

Expand Down Expand Up @@ -133,7 +135,8 @@ func (o *opampAgent) Start(ctx context.Context, host component.Host) error {
},
OnMessage: o.onMessage,
},
Capabilities: o.capabilities.toAgentCapabilities(),
Capabilities: o.capabilities.toAgentCapabilities(),
AvailableComponents: o.availableComponents,
}

if err := o.createAgentDescription(); err != nil {
Expand Down Expand Up @@ -303,6 +306,8 @@ func newOpampAgent(cfg *Config, set extension.Settings) (*opampAgent, error) {
agent.initHealthReporting()
}

agent.initAvailableComponents(set.ModuleInfo)

return agent, nil
}

Expand Down Expand Up @@ -472,6 +477,83 @@ func (o *opampAgent) initHealthReporting() {
go o.componentHealthEventLoop()
}

func (o *opampAgent) initAvailableComponents(set extension.ModuleInfo) {
if !o.capabilities.ReportsAvailableComponents {
return
}

o.availableComponents = &protobufs.AvailableComponents{
Hash: generateAvailableComponentsHash(set),
Components: map[string]*protobufs.ComponentDetails{
"receivers": {
SubComponentMap: createComponentTypeAvailableComponentDetails(set.Receiver),
},
"processors": {
SubComponentMap: createComponentTypeAvailableComponentDetails(set.Processor),
},
"exporters": {
SubComponentMap: createComponentTypeAvailableComponentDetails(set.Exporter),
},
"extensions": {
SubComponentMap: createComponentTypeAvailableComponentDetails(set.Extension),
},
"connectors": {
SubComponentMap: createComponentTypeAvailableComponentDetails(set.Connector),
},
},
}
}

func generateAvailableComponentsHash(set extension.ModuleInfo) []byte {
var builder strings.Builder

addComponentTypeComponentsToStringBuilder(&builder, set.Receiver, "receiver")
addComponentTypeComponentsToStringBuilder(&builder, set.Processor, "processor")
addComponentTypeComponentsToStringBuilder(&builder, set.Exporter, "exporter")
addComponentTypeComponentsToStringBuilder(&builder, set.Extension, "extension")
addComponentTypeComponentsToStringBuilder(&builder, set.Connector, "connector")

// Compute the SHA-256 hash of the serialized representation.
hash := sha256.Sum256([]byte(builder.String()))
return hash[:]
}

func addComponentTypeComponentsToStringBuilder(builder *strings.Builder, componentTypeComponents map[component.Type]string, componentType string) {
// Collect components and sort them to ensure deterministic ordering.
components := make([]component.Type, 0, len(componentTypeComponents))
for k := range componentTypeComponents {
components = append(components, k)
}
sort.Slice(components, func(i, j int) bool {
return components[i].String() < components[j].String()
})

// Append the component type and its sorted key-value pairs.
builder.WriteString(componentType + ":")
for _, k := range components {
builder.WriteString(k.String() + "=" + componentTypeComponents[k] + ";")
}
}

func createComponentTypeAvailableComponentDetails(componentTypeComponents map[component.Type]string) map[string]*protobufs.ComponentDetails {
availableComponentDetails := map[string]*protobufs.ComponentDetails{}
for componentType, r := range componentTypeComponents {
availableComponentDetails[componentType.String()] = &protobufs.ComponentDetails{
Metadata: []*protobufs.KeyValue{
{
Key: "code.namespace",
Value: &protobufs.AnyValue{
Value: &protobufs.AnyValue_StringValue{
StringValue: r,
},
},
},
},
}
}
return availableComponentDetails
}

func (o *opampAgent) componentHealthEventLoop() {
// Record events with component.StatusStarting, but queue other events until
// PipelineWatcher.Ready is called. This prevents aggregate statuses from
Expand Down
Loading
Loading