Generic Exporter charm-lifecycle

Charm lifecycle

This guide outlines how the Generic Exporter operator responds to core Juju events and manages the exporter snap throughout its lifecycle.

Install

On install:

  • Validate that a usable configuration is present:
    • A snap-name is provided, and
    • Either snap-channel or snap-revision is set (but not both).
  • Initialize the snap manager using SnapCache.
  • Install the exporter snap:
    • Using the requested channel or revision.
    • Respecting snap-classic when classic confinement is required.
  • Apply any JSON snap configuration from snap-config using snap.set().

If configuration is missing or invalid (for example, snap-name not set), the charm sets:

BlockedStatus("Missing required configuration fields: snap-name")

and does not proceed with installation.

Config-changed

On config-changed:

  • Re-validate the charm configuration:
    • Reject cases where both snap-channel and snap-revision are set simultaneously.
    • Parse snap-config as JSON; if parsing fails, block with an appropriate message.
  • Reapply snap configuration:
    • Call snap.set() with the parsed configuration.
    • Call snap.unset() if any configuration was unset.
    • On failure, stop the snap and set a Blocked status.
  • Reapply snap-plugs by connecting the requested interfaces.
  • Detect a change in the exporter identity:
    • If the configured exporter changes (for example, snap-name changed), uninstall the previous snap and clean up its alerts directory.
  • Reconcile alert rules:
    • If an alerts resource is present, re-read and rewrite rules to /run/<app-name>-<unit-id>/alerts.yaml.
  • Notify COS integration:
    • Update scrape configuration and alert rules via COSAgentProvider, using config-changed as a refresh event.

Update-status

On update-status, the charm performs a simple health check against the exporter’s HTTP endpoint:

  • Issue a GET request to http://localhost:<port>/<metrics-path>.
    • If the request fails or the endpoint is unreachable, set:
    BlockedStatus("Metrics endpoint not reachable")
    
  • Ensure that the snap services are active.
  • Check that the charm is related to Grafana Agent
    • If the check fails, the charm sets:
    BlockedStatus("Missing relation: [cos-agent]")
    

If every check passes, the charm enters Active state. This checks ensure that both the snap and its configuration are functioning as expected.

Remove

On remove:

  • Stop and remove the exporter snap from the unit.
  • Remove the alerts directory and any associated files.
  • Allow Grafana Agent to reconcile the disappearance of the metrics endpoint and alerts via metrics-endpoint relation updates.