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] Implement ReportsHealth capability #35488

Merged
Show file tree
Hide file tree
Changes from 4 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/opamp-extension-reportshealth.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: Implement `ReportsHealth` capability in OpAMP extension

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

# (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: []
5 changes: 5 additions & 0 deletions extension/opampextension/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type AgentDescription struct {
type Capabilities struct {
// ReportsEffectiveConfig enables the OpAMP ReportsEffectiveConfig Capability. (default: true)
ReportsEffectiveConfig bool `mapstructure:"reports_effective_config"`
// ReportsHealth enables the OpAMP ReportsHealth Capability. (default: true)
ReportsHealth bool `mapstructure:"reports_health"`
}

func (caps Capabilities) toAgentCapabilities() protobufs.AgentCapabilities {
Expand All @@ -62,6 +64,9 @@ func (caps Capabilities) toAgentCapabilities() protobufs.AgentCapabilities {
if caps.ReportsEffectiveConfig {
agentCapabilities |= protobufs.AgentCapabilities_AgentCapabilities_ReportsEffectiveConfig
}
if caps.ReportsHealth {
agentCapabilities |= protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth
}

return agentCapabilities
}
Expand Down
41 changes: 41 additions & 0 deletions extension/opampextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package opampextension

import (
"github.com/open-telemetry/opamp-go/protobufs"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -39,6 +40,7 @@ func TestUnmarshalConfig(t *testing.T) {
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZ",
Capabilities: Capabilities{
ReportsEffectiveConfig: true,
ReportsHealth: true,
},
PPIDPollInterval: 5 * time.Second,
}, cfg)
Expand All @@ -63,6 +65,7 @@ func TestUnmarshalHttpConfig(t *testing.T) {
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZ",
Capabilities: Capabilities{
ReportsEffectiveConfig: true,
ReportsHealth: true,
},
PPIDPollInterval: 5 * time.Second,
}, cfg)
Expand Down Expand Up @@ -286,3 +289,41 @@ func TestConfig_Validate(t *testing.T) {
})
}
}

func TestCapabilities_toAgentCapabilities(t *testing.T) {
type fields struct {
ReportsEffectiveConfig bool
ReportsHealth bool
}
tests := []struct {
name string
fields fields
want protobufs.AgentCapabilities
}{
{
name: "default capabilities",
fields: fields{
ReportsEffectiveConfig: false,
ReportsHealth: false,
},
want: protobufs.AgentCapabilities_AgentCapabilities_ReportsStatus,
},
{
name: "all supported capabilities enabled",
fields: fields{
ReportsEffectiveConfig: true,
ReportsHealth: true,
},
want: protobufs.AgentCapabilities_AgentCapabilities_ReportsStatus | protobufs.AgentCapabilities_AgentCapabilities_ReportsEffectiveConfig | protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
caps := Capabilities{
ReportsEffectiveConfig: tt.fields.ReportsEffectiveConfig,
ReportsHealth: tt.fields.ReportsHealth,
}
assert.Equalf(t, tt.want, caps.toAgentCapabilities(), "toAgentCapabilities()")
})
}
}
1 change: 1 addition & 0 deletions extension/opampextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func createDefaultConfig() component.Config {
Server: &OpAMPServer{},
Capabilities: Capabilities{
ReportsEffectiveConfig: true,
ReportsHealth: true,
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
},
PPIDPollInterval: 5 * time.Second,
}
Expand Down
33 changes: 25 additions & 8 deletions extension/opampextension/opamp_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"runtime"
"sort"
"strings"
"sync"

"github.com/google/uuid"
"github.com/oklog/ulid/v2"
"github.com/open-telemetry/opamp-go/client"
Expand All @@ -27,6 +20,12 @@ import (
"go.uber.org/zap"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
"net/http"
"os"
"runtime"
"sort"
"strings"
"sync"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/opampcustommessages"
)
Expand Down Expand Up @@ -112,6 +111,10 @@ func (o *opampAgent) Start(ctx context.Context, host component.Host) error {
return err
}

if err := o.opampClient.SetHealth(&protobufs.ComponentHealth{Healthy: false}); err != nil {
o.logger.Error("Could not report health to OpAMP server", zap.Error(err))
}

o.logger.Debug("Starting OpAMP client...")

if err := o.opampClient.Start(context.Background(), settings); err != nil {
Expand All @@ -120,6 +123,10 @@ func (o *opampAgent) Start(ctx context.Context, host component.Host) error {

o.logger.Debug("OpAMP client started")

if err := o.opampClient.SetHealth(&protobufs.ComponentHealth{Healthy: true}); err != nil {
o.logger.Error("Could not report health to OpAMP server", zap.Error(err))
}

return nil
}

Expand All @@ -132,8 +139,18 @@ func (o *opampAgent) Shutdown(ctx context.Context) error {
if o.opampClient == nil {
return nil
}

err := o.opampClient.SetHealth(
&protobufs.ComponentHealth{
Healthy: false, LastError: "Agent is shut down",
},
)
if err != nil {
o.logger.Error("Could not report health to OpAMP server", zap.Error(err))
}

o.logger.Debug("Stopping OpAMP client...")
err := o.opampClient.Stop(ctx)
err = o.opampClient.Stop(ctx)
// Opamp-go considers this an error, but the collector does not.
// https://github.com/open-telemetry/opamp-go/issues/255
if err != nil && strings.EqualFold(err.Error(), "cannot stop because not started") {
Expand Down
1 change: 1 addition & 0 deletions extension/opampextension/opamp_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestNewOpampAgent(t *testing.T) {
assert.Equal(t, "test version", o.agentVersion)
assert.NotEmpty(t, o.instanceID.String())
assert.True(t, o.capabilities.ReportsEffectiveConfig)
assert.True(t, o.capabilities.ReportsHealth)
assert.Empty(t, o.effectiveConfig)
assert.Nil(t, o.agentDescription)
}
Expand Down