Expose a metrics endpoint

Integrating with the metrics endpoint interface allows you to, through a minimal amount of code, enable your charm to get scraped by a charm like prometheus-k8s or grafana-agent-k8s.

Prerequisites

  • A charm with a workload that exposes a metrics endpoint.
  • A Juju deployment with COS Lite deployed.

Fetch the Library

Fetch the prometheus_scrape library using the charmcraft command:

charmcraft fetch-lib charms.prometheus_k8s.v0.prometheus_scrape

Import the Library

At the top of your charm’s src/charm.py source file, add an import of the charm library you just imported.

from charms.prometheus_k8s.v0.prometheus_scrape import MetricsEndpointProvider

Using the constructor

In the __init__ function of your charm class, instantiate the MetricsEndpointProvider. In its simplest form, where the workload exposes its metrics endpoint over port 80, with the path /metrics.

class ScrapableCharm:
    # ...
    def __init__(self, *args):
        # ...
        self.metrics_endpoint = MetricsEndpointProvider(self)

To override the default targets or the metrics path, you may then supply them as additional arguments while doing the instantiation.

class ScrapableCharm:
    # ...
    def __init__(self, *args):
        # ...
        self.metrics_endpoint_provider = MetricsEndpointProvider(
                self,                                  
                jobs=[{
                    "metrics_path": "/my/strange/metrics/path",
                    "static_configs": [{"targets": ["*:8080"]}],
                }])

Declaring the relation

As a last step, you need to declare the relation in your charms metadata.yaml file.

provides:
  metrics-endpoint:
    interface: prometheus_scrape

Congratulations! You will now be able to add an integration between your charm and a scraper! :tada:

1 Like