Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix: "In Progress" stuck agent config (#10476)

Piyush Singariyafd19ff8e5e4d
diff --git a/pkg/query-service/agentConf/db.go b/pkg/query-service/agentConf/db.go
index 22e0774ee0..5299d66bbc 100644
--- a/pkg/query-service/agentConf/db.go
+++ b/pkg/query-service/agentConf/db.go
@@ -115,7 +115,6 @@ func (r *Repo) GetLatestVersion(
func (r *Repo) insertConfig(
ctx context.Context, orgId valuer.UUID, userId valuer.UUID, c *opamptypes.AgentConfigVersion, elements []string,
) error {
-
if c.ElementType.StringValue() == "" {
return errors.NewInvalidInputf(CodeElementTypeRequired, "element type is required for creating agent config version")
}
@@ -229,6 +228,25 @@ func (r *Repo) updateDeployStatus(ctx context.Context,
return nil
}
+// GetDeployStatusByHash returns the DeployStatus for the given config hash
+// (stored with orgId prefix). Returns DeployStatusUnknown when no matching row exists.
+func (r *Repo) GetDeployStatusByHash(ctx context.Context, orgId valuer.UUID, configHash string) (opamptypes.DeployStatus, error) {
+ var version opamptypes.AgentConfigVersion
+ err := r.store.BunDB().NewSelect().
+ Model(&version).
+ ColumnExpr("deploy_status").
+ Where("hash = ?", configHash).
+ Where("org_id = ?", orgId).
+ Scan(ctx)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return opamptypes.DeployStatusUnknown, nil
+ }
+ return opamptypes.DeployStatusUnknown, errors.WrapInternalf(err, errors.CodeInternal, "failed to query deploy status by hash")
+ }
+ return version.DeployStatus, nil
+}
+
func (r *Repo) updateDeployStatusByHash(
ctx context.Context, orgId valuer.UUID, confighash string, status string, result string,
) error {
diff --git a/pkg/query-service/agentConf/manager.go b/pkg/query-service/agentConf/manager.go
index 9d5db2a8d8..d923751e0a 100644
--- a/pkg/query-service/agentConf/manager.go
+++ b/pkg/query-service/agentConf/manager.go
@@ -180,6 +180,12 @@ func (m *Manager) ReportConfigDeploymentStatus(
}
}
+// Implements model.AgentConfigProvider
+func (m *Manager) GetDeployStatusByHash(ctx context.Context, orgId valuer.UUID, configHash string) (opamptypes.DeployStatus, error) {
+ return m.Repo.GetDeployStatusByHash(ctx, orgId, configHash)
+}
+
+
func GetLatestVersion(
ctx context.Context, orgId valuer.UUID, elementType opamptypes.ElementType,
) (*opamptypes.AgentConfigVersion, error) {
diff --git a/pkg/query-service/app/opamp/config_provider.go b/pkg/query-service/app/opamp/config_provider.go
index b73d94a2c5..921256c386 100644
--- a/pkg/query-service/app/opamp/config_provider.go
+++ b/pkg/query-service/app/opamp/config_provider.go
@@ -1,6 +1,8 @@
package opamp
-import "github.com/SigNoz/signoz/pkg/query-service/app/opamp/model"
+import (
+ "github.com/SigNoz/signoz/pkg/query-service/app/opamp/model"
+)
// Interface for a source of otel collector config recommendations.
type AgentConfigProvider interface {
diff --git a/pkg/query-service/app/opamp/mocks.go b/pkg/query-service/app/opamp/mocks.go
index b017f726ff..4d6093d1b3 100644
--- a/pkg/query-service/app/opamp/mocks.go
+++ b/pkg/query-service/app/opamp/mocks.go
@@ -5,6 +5,7 @@ import (
"log"
"net"
+ "github.com/SigNoz/signoz/pkg/types/opamptypes"
"github.com/SigNoz/signoz/pkg/valuer"
"github.com/google/uuid"
"github.com/knadh/koanf"
@@ -127,6 +128,11 @@ func (ta *MockAgentConfigProvider) HasReportedDeploymentStatus(orgID valuer.UUID
return exists
}
+// AgentConfigProvider interface
+func (ta *MockAgentConfigProvider) GetDeployStatusByHash(_ context.Context, _ valuer.UUID, _ string) (opamptypes.DeployStatus, error) {
+ return opamptypes.DeployStatusUnknown, nil
+}
+
// AgentConfigProvider interface
func (ta *MockAgentConfigProvider) SubscribeToConfigUpdates(callback func()) func() {
subscriberId := uuid.NewString()
diff --git a/pkg/query-service/app/opamp/model/agent.go b/pkg/query-service/app/opamp/model/agent.go
index 2ee705831d..28cc1dcdeb 100644
--- a/pkg/query-service/app/opamp/model/agent.go
+++ b/pkg/query-service/app/opamp/model/agent.go
@@ -111,90 +111,99 @@ func ExtractLbFlag(agentDescr *protobufs.AgentDescription) bool {
return false
}
-func (agent *Agent) updateAgentDescription(newStatus *protobufs.AgentToServer) (agentDescrChanged bool) {
- prevStatus := agent.Status
-
- if agent.Status == nil {
- // First time this Agent reports a status, remember it.
- agent.Status = newStatus
- agentDescrChanged = true
- } else {
- // Not a new Agent. Update the Status.
- agent.Status.SequenceNum = newStatus.SequenceNum
-
- // Check what's changed in the AgentDescription.
- if newStatus.AgentDescription != nil {
- // If the AgentDescription field is set it means the Agent tells us
- // something is changed in the field since the last status report
- // (or this is the first report).
- // Make full comparison of previous and new descriptions to see if it
- // really is different.
- if prevStatus != nil && proto.Equal(prevStatus.AgentDescription, newStatus.AgentDescription) {
- // Agent description didn't change.
- agentDescrChanged = false
- } else {
- // Yes, the description is different, update it.
- agent.Status.AgentDescription = newStatus.AgentDescription
- agentDescrChanged = true
- }
- } else {
- // AgentDescription field is not set, which means description didn't change.
- agentDescrChanged = false
- }
-
- // Update remote config status if it is included and is different from what we have.
- if newStatus.RemoteConfigStatus != nil &&
- !proto.Equal(agent.Status.RemoteConfigStatus, newStatus.RemoteConfigStatus) {
- agent.Status.RemoteConfigStatus = newStatus.RemoteConfigStatus
+// agentDescriptionChanged returns true when the agent sends updated properties
+// (e.g. capability flag, version) mid-connection, signalling the server to
+// recompute and push a new RemoteConfig.
+//
+// On reconnect this always returns false: handleFirstStatus pre-copies
+// AgentDescription into agent.Status so no diff is detected, avoiding a
+// redundant config recompute.
+func (agent *Agent) agentDescriptionChanged(newStatus *protobufs.AgentToServer) bool {
+ // nil AgentDescription means no change per OpAMP protocol.
+ if newStatus.AgentDescription == nil {
+ return false
+ }
+ if proto.Equal(agent.Status.AgentDescription, newStatus.AgentDescription) {
+ return false
+ }
+ agent.CanLB = ExtractLbFlag(newStatus.AgentDescription)
+ return true
+}
- // todo: need to address multiple agent scenario here
- // for now, the first response will be sent back to the UI
- if agent.Status.RemoteConfigStatus.Status == protobufs.RemoteConfigStatuses_RemoteConfigStatuses_APPLIED {
- onConfigSuccess(agent.OrgID, agent.AgentID, string(agent.Status.RemoteConfigStatus.LastRemoteConfigHash))
- }
+// updateRemoteConfigStatus updates the stored RemoteConfigStatus and notifies
+// subscribers if the status has changed relative to what we have stored.
+func (agent *Agent) updateRemoteConfigStatus(newStatus *protobufs.AgentToServer) {
+ if newStatus.RemoteConfigStatus == nil ||
+ proto.Equal(agent.Status.RemoteConfigStatus, newStatus.RemoteConfigStatus) {
+ return
+ }
- if agent.Status.RemoteConfigStatus.Status == protobufs.RemoteConfigStatuses_RemoteConfigStatuses_FAILED {
- onConfigFailure(agent.OrgID, agent.AgentID, string(agent.Status.RemoteConfigStatus.LastRemoteConfigHash), agent.Status.RemoteConfigStatus.ErrorMessage)
- }
- }
+ // todo: need to address multiple agent scenario here
+ // for now, the first response will be sent back to the UI
+ hash := string(newStatus.RemoteConfigStatus.LastRemoteConfigHash)
+ switch newStatus.RemoteConfigStatus.Status {
+ case protobufs.RemoteConfigStatuses_RemoteConfigStatuses_APPLIED:
+ onConfigSuccess(agent.OrgID, agent.AgentID, hash)
+ case protobufs.RemoteConfigStatuses_RemoteConfigStatuses_FAILED:
+ onConfigFailure(agent.OrgID, agent.AgentID, hash, newStatus.RemoteConfigStatus.ErrorMessage)
}
+}
- if agentDescrChanged {
- agent.CanLB = ExtractLbFlag(newStatus.AgentDescription)
+// handleFirstStatus initializes agent.Status on the first message received from
+// this agent instance. It is a no-op for all subsequent messages.
+func (agent *Agent) handleFirstStatus(newStatus *protobufs.AgentToServer, configProvider AgentConfigProvider) {
+ if agent.Status != nil {
+ return
}
- return agentDescrChanged
-}
+ // Initialize with a clean slate.
+ agent.Status = &protobufs.AgentToServer{
+ RemoteConfigStatus: &protobufs.RemoteConfigStatus{
+ Status: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_UNSET,
+ },
+ }
-func (agent *Agent) updateHealth(newStatus *protobufs.AgentToServer) {
- if newStatus.Health == nil {
+ if newStatus.RemoteConfigStatus == nil ||
+ newStatus.RemoteConfigStatus.Status == protobufs.RemoteConfigStatuses_RemoteConfigStatuses_UNSET {
+ // Agent just started fresh — no prior deployment to reconcile with the DB.
return
}
- agent.Status.Health = newStatus.Health
+ // Since the server's connection is restarted;
+ // copy the agent description; so no change is detected by agentDescriptionChanged
+ agent.Status.AgentDescription = newStatus.AgentDescription
- if agent.Status != nil && agent.Status.Health != nil && agent.Status.Health.Healthy {
- agent.TimeAuditable.UpdatedAt = time.Unix(0, int64(agent.Status.Health.StartTimeUnixNano)).UTC()
+ // Server reconnected while the agent was already running.
+ // Reconcile deployment status with DB; DB is the source of truth.
+ // If DB says in_progress but agent now reports APPLIED/FAILED,
+ // updateRemoteConfigStatus will detect the transition and notify subscribers.
+ rawHash := string(newStatus.RemoteConfigStatus.LastRemoteConfigHash)
+ deployStatus, err := configProvider.GetDeployStatusByHash(context.Background(), agent.OrgID, agent.OrgID.String()+rawHash)
+ if err != nil {
+ return
}
-}
-func (agent *Agent) updateRemoteConfigStatus(newStatus *protobufs.AgentToServer) {
- // Update remote config status if it is included and is different from what we have.
- if newStatus.RemoteConfigStatus != nil {
- agent.Status.RemoteConfigStatus = newStatus.RemoteConfigStatus
+ agent.Status.RemoteConfigStatus.Status = opamptypes.DeployStatusToProtoStatus[deployStatus]
+
+ // If the deployment is still in-flight, rehydrate the subscriber so that
+ // updateRemoteConfigStatus can fire onConfigSuccess/onConfigFailure when
+ // the agent next reports a terminal status.
+ if deployStatus != opamptypes.Deployed && deployStatus != opamptypes.DeployFailed {
+ ListenToConfigUpdate(agent.OrgID, agent.AgentID, rawHash, configProvider.ReportConfigDeploymentStatus)
}
}
-func (agent *Agent) updateStatusField(newStatus *protobufs.AgentToServer) (agentDescrChanged bool) {
- if agent.Status == nil {
- // First time this Agent reports a status, remember it.
- agent.Status = newStatus
- agentDescrChanged = true
+func (agent *Agent) updateStatusField(newStatus *protobufs.AgentToServer, configProvider AgentConfigProvider) bool {
+ agent.handleFirstStatus(newStatus, configProvider)
+ agentDescrChanged := agent.agentDescriptionChanged(newStatus)
+ // record healthy timestamp
+ if newStatus.Health != nil && newStatus.Health.Healthy {
+ agent.TimeAuditable.UpdatedAt = time.Unix(0, int64(newStatus.Health.StartTimeUnixNano)).UTC()
}
-
- agentDescrChanged = agent.updateAgentDescription(newStatus) || agentDescrChanged
+ // notify subscribers first; this will update the status in the DB
agent.updateRemoteConfigStatus(newStatus)
- agent.updateHealth(newStatus)
+ // update local reference in last.
+ agent.Status = newStatus
return agentDescrChanged
}
@@ -237,7 +246,7 @@ func (agent *Agent) processStatusUpdate(
// current status is not up-to-date.
lostPreviousUpdate := (agent.Status == nil) || (agent.Status != nil && agent.Status.SequenceNum+1 != newStatus.SequenceNum)
- agentDescrChanged := agent.updateStatusField(newStatus)
+ agentDescrChanged := agent.updateStatusField(newStatus, configProvider)
// Check if any fields were omitted in the status report.
effectiveConfigOmitted := newStatus.EffectiveConfig == nil &&
diff --git a/pkg/query-service/app/opamp/model/config.go b/pkg/query-service/app/opamp/model/config.go
index fddc1e80d8..f155f21f30 100644
--- a/pkg/query-service/app/opamp/model/config.go
+++ b/pkg/query-service/app/opamp/model/config.go
@@ -1,6 +1,11 @@
package model
-import "github.com/SigNoz/signoz/pkg/valuer"
+import (
+ "context"
+
+ "github.com/SigNoz/signoz/pkg/types/opamptypes"
+ "github.com/SigNoz/signoz/pkg/valuer"
+)
// Interface for source of otel collector config recommendations.
type AgentConfigProvider interface {
@@ -20,4 +25,10 @@ type AgentConfigProvider interface {
configId string,
err error,
)
+
+ // GetDeployStatusByHash returns the DeployStatus for the given config hash
+ // (with orgId prefix as stored in the DB). Returns DeployStatusUnknown when
+ // no matching row exists. Used by the agent's first-connect handler to
+ // determine whether the reported RemoteConfigStatus resolves a pending deployment.
+ GetDeployStatusByHash(ctx context.Context, orgId valuer.UUID, configHash string) (opamptypes.DeployStatus, error)
}
diff --git a/pkg/query-service/app/opamp/model/coordinator.go b/pkg/query-service/app/opamp/model/coordinator.go
index 8c9ab346e5..d78818e474 100644
--- a/pkg/query-service/app/opamp/model/coordinator.go
+++ b/pkg/query-service/app/opamp/model/coordinator.go
@@ -66,6 +66,7 @@ func ListenToConfigUpdate(orgId valuer.UUID, agentId string, hash string, ss OnC
defer coordinator.mutex.Unlock()
key := getSubscriberKey(orgId, hash)
+
if subs, ok := coordinator.subscribers[key]; ok {
subs = append(subs, ss)
coordinator.subscribers[key] = subs
diff --git a/pkg/types/opamptypes/agent.go b/pkg/types/opamptypes/agent.go
index 8d61bceacc..80e70cf8e5 100644
--- a/pkg/types/opamptypes/agent.go
+++ b/pkg/types/opamptypes/agent.go
@@ -6,6 +6,7 @@ import (
"github.com/SigNoz/signoz/pkg/sqlstore"
"github.com/SigNoz/signoz/pkg/types"
"github.com/SigNoz/signoz/pkg/valuer"
+ "github.com/open-telemetry/opamp-go/protobufs"
"github.com/uptrace/bun"
)
@@ -17,6 +18,15 @@ const (
AgentStatusDisconnected
)
+var DeployStatusToProtoStatus = map[DeployStatus]protobufs.RemoteConfigStatuses{
+ PendingDeploy: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_UNSET,
+ Deploying: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_APPLYING,
+ Deployed: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_APPLIED,
+ DeployInitiated: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_APPLYING,
+ DeployFailed: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_FAILED,
+ DeployStatusUnknown: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_UNSET,
+}
+
type StorableAgent struct {
bun.BaseModel `bun:"table:agent"`
@@ -30,16 +40,6 @@ type StorableAgent struct {
Config string `bun:"config,type:text,notnull"`
}
-func NewStorableAgent(store sqlstore.SQLStore, orgID valuer.UUID, agentID string, status AgentStatus) StorableAgent {
- return StorableAgent{
- OrgID: orgID,
- Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
- AgentID: agentID,
- TimeAuditable: types.TimeAuditable{CreatedAt: time.Now(), UpdatedAt: time.Now()},
- Status: status,
- }
-}
-
type ElementType struct{ valuer.String }
var (
@@ -49,24 +49,6 @@ var (
ElementTypeLbExporter = ElementType{valuer.NewString("lb_exporter")}
)
-// NewElementType creates a new ElementType from a string value.
-// Returns the corresponding ElementType constant if the string matches,
-// otherwise returns an empty ElementType.
-func NewElementType(value string) ElementType {
- switch valuer.NewString(value) {
- case ElementTypeSamplingRules.String:
- return ElementTypeSamplingRules
- case ElementTypeDropRules.String:
- return ElementTypeDropRules
- case ElementTypeLogPipelines.String:
- return ElementTypeLogPipelines
- case ElementTypeLbExporter.String:
- return ElementTypeLbExporter
- default:
- return ElementType{valuer.NewString("")}
- }
-}
-
type DeployStatus struct{ valuer.String }
var (
@@ -98,6 +80,26 @@ type AgentConfigVersion struct {
Config string `json:"config" bun:"config,type:text"`
}
+type AgentConfigElement struct {
+ bun.BaseModel `bun:"table:agent_config_element"`
+
+ types.Identifiable
+ types.TimeAuditable
+ ElementID string `bun:"element_id,type:text,notnull,unique:element_type_version_idx"`
+ ElementType string `bun:"element_type,type:text,notnull,unique:element_type_version_idx"`
+ VersionID valuer.UUID `bun:"version_id,type:text,notnull,unique:element_type_version_idx"`
+}
+
+func NewStorableAgent(store sqlstore.SQLStore, orgID valuer.UUID, agentID string, status AgentStatus) StorableAgent {
+ return StorableAgent{
+ OrgID: orgID,
+ Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
+ AgentID: agentID,
+ TimeAuditable: types.TimeAuditable{CreatedAt: time.Now(), UpdatedAt: time.Now()},
+ Status: status,
+ }
+}
+
func NewAgentConfigVersion(orgId valuer.UUID, userId valuer.UUID, elementType ElementType) *AgentConfigVersion {
return &AgentConfigVersion{
TimeAuditable: types.TimeAuditable{
@@ -118,12 +120,20 @@ func (a *AgentConfigVersion) IncrementVersion(lastVersion int) {
a.Version = lastVersion + 1
}
-type AgentConfigElement struct {
- bun.BaseModel `bun:"table:agent_config_element"`
-
- types.Identifiable
- types.TimeAuditable
- ElementID string `bun:"element_id,type:text,notnull,unique:element_type_version_idx"`
- ElementType string `bun:"element_type,type:text,notnull,unique:element_type_version_idx"`
- VersionID valuer.UUID `bun:"version_id,type:text,notnull,unique:element_type_version_idx"`
+// NewElementType creates a new ElementType from a string value.
+// Returns the corresponding ElementType constant if the string matches,
+// otherwise returns an empty ElementType.
+func NewElementType(value string) ElementType {
+ switch valuer.NewString(value) {
+ case ElementTypeSamplingRules.String:
+ return ElementTypeSamplingRules
+ case ElementTypeDropRules.String:
+ return ElementTypeDropRules
+ case ElementTypeLogPipelines.String:
+ return ElementTypeLogPipelines
+ case ElementTypeLbExporter.String:
+ return ElementTypeLbExporter
+ default:
+ return ElementType{valuer.NewString("")}
+ }
}