Prometheus Pushgateway Charmed Operator

  • By Simon Aronsson
Channel Revision Published Runs on
latest/stable 16 04 Jun 2024
Ubuntu 22.04
latest/candidate 16 04 Jun 2024
Ubuntu 22.04
latest/beta 16 22 May 2024
Ubuntu 22.04
latest/edge 16 03 May 2024
Ubuntu 22.04
juju deploy prometheus-pushgateway-k8s
Show information

Platform:

charms.prometheus_pushgateway_k8s.v0.pushgateway

Interface library for the Prometheus Pushgateway.

This library wraps a relation endpoint using the pushgwateway interface and exposes an API for forwarding metrics to Prometheus.

Getting Started

Provider side of the relation

This side of the relation is to be used by Prometheus Pushgateway Charm or any other charm that provides the same service.

To get started using the library, you just need to fetch the library using charmcraft.

cd some-charm
charmcraft fetch-lib charms.prometheus_pushgateway_k8s.v0.pushgateway

In the metadata.yaml of the charm, add the following:

provides:
  push-endpoint:
    interface: pushgateway

In the source of your charm, first import the interface:

from charms.prometheus_pushgateway_k8s.v0.pushgateway import PrometheusPushgatewayProvider

Instantiate the object in your charm's __init__, like so:

from charms.prometheus_pushgateway_k8s.v0.pushgateway import PrometheusPushgatewayRequirer
from ops.charm import CharmBase

class PrometheusPushgatewayK8SOperatorCharm(CharmBase):
    def __init__(...):
        ...
        self.pushgateway_provider = PrometheusPushgatewayProvider(
            self, "push-endpoint", self._endpoint
        )

The relation name when instantiating PrometheusPushgatewayProvider defaults to push-endpoint, you can pass a different one if used other name in metadata.yaml.

Requierer side of the relation

To get started using the library, you just need to fetch the library using charmcraft.

cd some-charm
charmcraft fetch-lib charms.prometheus_pushgateway_k8s.v0.pushgateway

In the metadata.yaml of the charm, add the following:

requires:
  pushgateway:
    interface: pushgateway

In the source of your charm, first import the interface:

from charms.prometheus_pushgateway_k8s.v0.pushgateway import PrometheusPushgatewayRequirer

Instantiate the object in your charm's __init__, like so:

from charms.prometheus_pushgateway_k8s.v0.pushgateway import PrometheusPushgatewayRequirer
from ops.charm import CharmBase

class MyCharm(CharmBase):
    def __init__(...):
        ...
        self.pushgateway_requirer = PrometheusPushgatewayRequirer(self)

The relation name when instantiating PrometheusPushgatewayRequirer defaults to pushgateway, you can pass a different one if used other name in metadata.yaml.

At any moment you can use the Pushgateway Requirer to send a metric (validating that the requirer is ready), passing its name and value:

    if self.pushgateway_requirer.is_ready():
        self.pushgateway_requirer.send_metric("test_metric", 3.141592)

The requirer is ready when the relation to the Prometheus Pushgateway is properly established.

The send_metric call will just end quietly if the metric was sent succesfully, or will raise an exception if something is wrong (that error should be logged or informed to the operator).


class PrometheusPushgatewayProvider

Provider side for the Prometheus Pushgateway.

Description

This class is to be used by the Prometheus Pushgateway charm, please use the PrometheusPushgatewayRequirer class if you're bulding a charm and want to use this library to integrate with the Prometheus Pushgateway.

Methods

PrometheusPushgatewayProvider. __init__( self , charm: CharmBase , relation_name: str , endpoint: str )

Construct the interface for the Prometheus Pushgateway Provider side of the relation.

Arguments

charm

a CharmBase object that manages this object. Typically, this is self in the instantiating class.

relation_name

the name of the relation (whatever was used in the requires section in metadata.yaml for the pushgateway interface.

endpoint

the endpoint that Prometheus Pushgateway expose to consumers, for instance: "https://pushgateway-0.pushgateway-endpoints.clite.svc.cluster.local:9091" or "http://10.1.38.86:9091"

PrometheusPushgatewayProvider. update_endpoint( self , endpoint: str )

Description

Update endpoint in relation data. None

class PrometheusPushgatewayRequirer

Description

Requirer side for the Prometheus Pushgateway. None

Methods

PrometheusPushgatewayRequirer. __init__( self , charm: CharmBase , relation_name: str )

Construct the interface for the Prometheus Pushgateway.

Arguments

charm

a CharmBase object that manages this object. Typically, this is self in the instantiating class.

relation_name

the name of the relation (whatever was used in the requires section in metadata.yaml for the pushgateway interface.

PrometheusPushgatewayRequirer. is_ready( self )

Description

Return if the service is ready to send metrics. None

PrometheusPushgatewayRequirer. send_metric( self , name: str , value , ignore_error: bool , verify_ssl: bool , job_name: str )

Send a metric to the Pushgateway.

Arguments

name

the name of the metric.

value

the value of the metric.

ignore_error

raise or not error while performing the request.

verify_ssl

verify ssl certificate in the request.

job_name

name of the job for the current metric.