Observability Libs

  • Jon Seager
Channel Revision Published Runs on
latest/edge 50 11 Oct 2024
Ubuntu 20.04
juju deploy observability-libs --channel edge
Show information

Platform:

charms.observability_libs.v0.metrics_endpoint_discovery

MetricsEndpointDiscovery Library.

This library provides functionality for discovering metrics endpoints exposed by applications deployed to a Kubernetes cluster.

It comprises:

  • A custom event and event source for handling metrics endpoint changes.
  • Logic to observe cluster events and emit the events as appropriate.
Using the Library
Handling Events

To ensure that your charm can react to changing metrics endpoint events, use the CharmEvents extension.

import json

from charms.observability_libs.v0.metrics_endpoint_discovery import
    MetricsEndpointCharmEvents,
    MetricsEndpointObserver
)

class MyCharm(CharmBase):

    on = MetricsEndpointChangeCharmEvents()

    def __init__(self, *args):
        super().__init__(*args)

        self._observer = MetricsEndpointObserver(self, {"app.kubernetes.io/name": ["grafana-k8s"]})
        self.framework.observe(self.on.metrics_endpoint_change, self._on_endpoints_change)

    def _on_endpoints_change(self, event):
        self.unit.status = ActiveStatus(json.dumps(event.discovered))

class MetricsEndpointChangeEvent

Description

A custom event for metrics endpoint changes. None

Methods

MetricsEndpointChangeEvent. __init__( self , handle )

MetricsEndpointChangeEvent. snapshot( self )

Description

Save the event payload data. None

MetricsEndpointChangeEvent. restore( self , snapshot )

Description

Restore the event payload data. None

MetricsEndpointChangeEvent. discovered( self )

Description

Return the payload of detected endpoint changes for this event. None

class MetricsEndpointChangeCharmEvents

A CharmEvents extension for metrics endpoint changes.

Description

Includes :class:MetricsEndpointChangeEvent in those that can be handled.

class MetricsEndpointObserver

Observes changing metrics endpoints in the cluster.

Description

Observed endpoint changes cause :class"MetricsEndpointChangeEvent to be emitted.

Methods

MetricsEndpointObserver. __init__( self , charm: CharmBase , labels )

Constructor for MetricsEndpointObserver.

Arguments

charm

the charm that is instantiating the library.

labels

dictionary of label/value to be observed for changing metrics endpoints.

MetricsEndpointObserver. start_observer( self )

Description

Start the metrics endpoint observer running in a new process. None

MetricsEndpointObserver. stop_observers( self )

Description

Stops all running instances of the observer. None

MetricsEndpointObserver. unit_tag( self )

Description

Juju-style tag identifying the unit being run by this charm. None

def write_payload(payload)

Description

Write the input event data to event payload file. None

def dispatch(
    run_cmd,
    unit,
    charm_dir
)

Description

Use the input juju-run command to dispatch a :class:MetricsEndpointChangeEvent. None

def main()

Main watch and dispatch loop.

Description

Watch the input k8s service names. When changes are detected, write the observed data to the payload file, and dispatch the change event.