Skip to content

Commit

Permalink
Correctly handle status compression on the server side
Browse files Browse the repository at this point in the history
We check the omitted and the corresponding capability bit to understand
if the compression was used.

Related to spec issue open-telemetry/opamp-spec#109
The issue was discarded in favour of the implementation that does not
change the spec, but instead uses already existing information.
  • Loading branch information
tigrannajaryan committed Jul 22, 2022
1 parent 63c9165 commit e1dbe01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
7 changes: 1 addition & 6 deletions client/internal/clientcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,7 @@ func (c *ClientCommon) PrepareFirstMessage(ctx context.Context) error {
msg.EffectiveConfig = cfg
msg.RemoteConfigStatus = c.ClientSyncedState.RemoteConfigStatus()
msg.PackageStatuses = c.ClientSyncedState.PackageStatuses()

if c.PackagesStateProvider != nil {
// We have a state provider, so package related capabilities can work.
msg.Capabilities |= protobufs.AgentCapabilities_AcceptsPackages
msg.Capabilities |= protobufs.AgentCapabilities_ReportsPackageStatuses
}
msg.Capabilities = c.Capabilities
},
)
return nil
Expand Down
31 changes: 25 additions & 6 deletions internal/examples/server/data/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,35 @@ func (agent *Agent) updateEffectiveConfig(
}
}

func (agent *Agent) hasCapability(capability protobufs.AgentCapabilities) bool {
return agent.Status.Capabilities&capability != 0
}

func (agent *Agent) processStatusUpdate(
newStatus *protobufs.AgentToServer,
response *protobufs.ServerToAgent,
) {
if agent.Status != nil && agent.Status.SequenceNum+1 != newStatus.SequenceNum {
// We lost the previous status update. Request full status update from the agent.
response.Flags |= protobufs.ServerToAgent_ReportFullState
}
// We don't have any status for this Agent, or we lost the previous status update from the Agent, so our
// current status is not up-to-date.
lostPreviousUpdate := (agent.Status == nil) || (agent.Status != nil && agent.Status.SequenceNum+1 != newStatus.SequenceNum)

agentDescrChanged := agent.updateStatusField(newStatus)

// Check if any fields were omitted in the status report.
effectiveConfigOmitted := agent.hasCapability(protobufs.AgentCapabilities_ReportsEffectiveConfig) && newStatus.EffectiveConfig == nil
packageStatusesOmitted := agent.hasCapability(protobufs.AgentCapabilities_ReportsPackageStatuses) && newStatus.PackageStatuses == nil
remoteConfigStatusOmitted := agent.hasCapability(protobufs.AgentCapabilities_AcceptsRemoteConfig) && newStatus.RemoteConfigStatus == nil
healthOmitted := agent.hasCapability(protobufs.AgentCapabilities_ReportsHealth) && newStatus.Health == nil

// True if the status was not fully reported.
statusIsCompressed := effectiveConfigOmitted || packageStatusesOmitted || remoteConfigStatusOmitted || healthOmitted

if statusIsCompressed && lostPreviousUpdate {
// The status message is not fully set in the message that we received, but we lost the previous
// status update. Request full status update from the agent.
response.Flags |= protobufs.ServerToAgent_ReportFullState
}

configChanged := false
if agentDescrChanged {
// Agent description is changed.
Expand All @@ -219,14 +237,15 @@ func (agent *Agent) processStatusUpdate(
// If remote config is changed and different from what the Agent has then
// send the new remote config to the Agent.
if configChanged ||
(newStatus.RemoteConfigStatus != nil &&
bytes.Compare(newStatus.RemoteConfigStatus.LastRemoteConfigHash, agent.remoteConfig.ConfigHash) != 0) {
(agent.Status.RemoteConfigStatus != nil &&
bytes.Compare(agent.Status.RemoteConfigStatus.LastRemoteConfigHash, agent.remoteConfig.ConfigHash) != 0) {
// The new status resulted in a change in the config of the Agent or the Agent
// does not have this config (hash is different). Send the new config the Agent.
response.RemoteConfig = agent.remoteConfig
}

agent.updateEffectiveConfig(newStatus, response)

}

// SetCustomConfig sets a custom config for this Agent.
Expand Down

0 comments on commit e1dbe01

Please sign in to comment.