Skip to main content

manager.go

Filename: manager.go

  1. Overview The manager.go file defines the Manager struct and its associated methods for managing agent configurations, including subscribing to updates, recommending configurations based on features, reporting deployment status, and interacting with a database repository for configuration versions. It also handles updates to filter and sampling processors.

  2. Detailed Documentation

AgentFeatureType

type AgentFeatureType string
  • Purpose: Defines a type alias for agent feature types, represented as a string.

Manager

type Manager struct {
Repo
lock uint32
agentFeatures []AgentFeature
configSubscribers map[string]func()
configSubscribersLock sync.Mutex
}
  • Purpose: Manages agent configurations, including database interactions, feature handling, and update subscriptions.
    • Repo: An embedded Repo struct for database operations.
    • lock: A uint32 used as a mutex to prevent concurrent updates to remote agents.
    • agentFeatures: A slice of AgentFeature interfaces, defining the features to apply to agent configurations.
    • configSubscribers: A map of subscriber IDs to callback functions, allowing subscription to configuration updates.
    • configSubscribersLock: A mutex to protect concurrent access to the configSubscribers map.

ManagerOptions

type ManagerOptions struct {
DB *sqlx.DB
AgentFeatures []AgentFeature
}
  • Purpose: Defines options for initializing a Manager instance.
    • DB: A pointer to an sqlx.DB database connection.
    • AgentFeatures: A slice of AgentFeature interfaces to be used by the manager.

Initiate

func Initiate(options *ManagerOptions) (*Manager, error) {
  • Purpose: Initializes a new Manager instance with the given options.
    • Parameters:
      • options: A pointer to a ManagerOptions struct containing the initialization parameters.
    • Returns:
      • A pointer to the initialized Manager instance.
      • An error, if any occurred during initialization.
m = &Manager{
Repo: Repo{options.DB},
agentFeatures: options.AgentFeatures,
configSubscribers: map[string]func(){},
}

SubscribeToConfigUpdates

func (m *Manager) SubscribeToConfigUpdates(callback func()) (unsubscribe func()) {
  • Purpose: Allows subscribing to configuration updates.
    • Parameters:
      • callback: A function to be called when a configuration update occurs.
    • Returns:
      • A function that, when called, unsubscribes the callback from future updates.
subscriberId := uuid.NewString()
m.configSubscribers[subscriberId] = callback

return func() {
delete(m.configSubscribers, subscriberId)
}

notifyConfigUpdateSubscribers

func (m *Manager) notifyConfigUpdateSubscribers() {
  • Purpose: Notifies all subscribers of a configuration update by calling their registered callback functions.

RecommendAgentConfig

func (m *Manager) RecommendAgentConfig(currentConfYaml []byte) (
recommendedConfYaml []byte,
configId string,
err error,
) {
  • Purpose: Recommends an agent configuration based on the current configuration and the registered agent features.
    • Parameters:
      • currentConfYaml: The current agent configuration in YAML format.
    • Returns:
      • recommendedConfYaml: The recommended agent configuration in YAML format.
      • configId: An opaque ID of the recommended configuration.
      • err: An error, if any occurred during the recommendation process.
configVersion := -1
if latestConfig != nil {
configVersion = latestConfig.Version
}
configId := fmt.Sprintf("%s:%d", featureType, configVersion)

ReportConfigDeploymentStatus

func (m *Manager) ReportConfigDeploymentStatus(
agentId string,
configId string,
err error,
) {
  • Purpose: Reports the deployment status of a configuration to the manager.
    • Parameters:
      • agentId: The ID of the agent.
      • configId: The ID of the deployed configuration.
      • err: An error, if any occurred during deployment.

GetLatestVersion

func GetLatestVersion(
ctx context.Context, elementType ElementTypeDef,
) (*ConfigVersion, *model.ApiError) {
return m.GetLatestVersion(ctx, elementType)
}
  • Purpose: Retrieves the latest version of a configuration element.
    • Parameters:
      • ctx: The context.Context.
      • elementType: The type of the configuration element.
    • Returns:
      • A pointer to the latest ConfigVersion.
      • A pointer to a model.ApiError, if any occurred.

GetConfigVersion

func GetConfigVersion(
ctx context.Context, elementType ElementTypeDef, version int,
) (*ConfigVersion, *model.ApiError) {
return m.GetConfigVersion(ctx, elementType, version)
}
  • Purpose: Retrieves a specific version of a configuration element.
    • Parameters:
      • ctx: The context.Context.
      • elementType: The type of the configuration element.
      • version: The version number to retrieve.
    • Returns:
      • A pointer to the ConfigVersion.
      • A pointer to a model.ApiError, if any occurred.

GetConfigHistory

func GetConfigHistory(
ctx context.Context, typ ElementTypeDef, limit int,
) ([]ConfigVersion, *model.ApiError) {
return m.GetConfigHistory(ctx, typ, limit)
}
  • Purpose: Retrieves the configuration history for a given type.
    • Parameters:
      • ctx: The context.Context.
      • typ: The type of the configuration element.
      • limit: The maximum number of versions to retrieve.
    • Returns:
      • A slice of ConfigVersion structs.
      • A pointer to a model.ApiError, if any occurred.

StartNewVersion

func StartNewVersion(
ctx context.Context, userId string, eleType ElementTypeDef, elementIds []string,
) (*ConfigVersion, *model.ApiError) {
  • Purpose: Starts a new configuration version for a given set of elements.
    • Parameters:
      • ctx: The context.Context.
      • userId: The ID of the user starting the new version.
      • eleType: The type of the configuration element.
      • elementIds: A slice of element IDs to include in the new version.
    • Returns:
      • A pointer to the new ConfigVersion.
      • A pointer to a model.ApiError, if any occurred.

NotifyConfigUpdate

func NotifyConfigUpdate(ctx context.Context) {
m.notifyConfigUpdateSubscribers()
}
  • Purpose: Notifies subscribers of a configuration update.
    • Parameters:
      • ctx: The context.Context (Unused).

Redeploy

func Redeploy(ctx context.Context, typ ElementTypeDef, version int) *model.ApiError {
  • Purpose: Redeploys a specific version of a configuration element.
    • Parameters:
      • ctx: The context.Context.
      • typ: The type of the configuration element.
      • version: The version number to redeploy.
    • Returns:
      • A pointer to a model.ApiError, if any occurred.
var config *tsp.Config
if err := yaml.Unmarshal([]byte(configVersion.LastConf), &config); err != nil {
zap.L().Debug("failed to read last conf correctly", zap.Error(err))
return model.BadRequest(fmt.Errorf("failed to read the stored config correctly"))
}

UpsertFilterProcessor

func UpsertFilterProcessor(ctx context.Context, version int, config *filterprocessor.Config) error {
  • Purpose: Updates the agent configuration with a new filter processor.
    • Parameters:
      • ctx: The context.Context.
      • version: The configuration version.
      • config: The filter processor configuration.
    • Returns:
      • An error, if any occurred.

OnConfigUpdate

func (m *Manager) OnConfigUpdate(agentId string, hash string, err error) {
  • Purpose: Callback function for Opamp server, called when a configuration is updated.
    • Parameters:
      • agentId: The ID of the agent.
      • hash: The hash of the configuration.
      • err: An error, if any occurred during the update.

UpsertSamplingProcessor

func UpsertSamplingProcessor(ctx context.Context, version int, config *tsp.Config) error {
  • Purpose: Updates the agent configuration with a new sampling processor.
    • Parameters:
      • ctx: The context.Context.
      • version: The configuration version.
      • config: The sampling processor configuration.
    • Returns:
      • An error, if any occurred.
  1. Code Examples None

  2. Clarity and Accuracy The documentation accurately reflects the code's functionality.

  3. Markdown & MDX Perfection The markdown is properly formatted.

  4. Edge Cases To Avoid Breaking MDX All edge cases are handled.

  5. Getting Started Relevance Include in Getting Started: YES