Attention, ce billet se traine depuis plus de 3 mois. Les informations qu'il contient ne sont peut-être plus à jour.
Comment prendre un peu de Python pour faire un Exporter Prometheus
Rédigé par / / 4 commentaires

Du Python 3
Une API
La logique globale
Exemple
r = requests.get("https://diaspodon.fr/api/v1/accounts/1")
data = json.loads(r.content.decode())
metric = Metric('following_count', 'Number of following account', 'gauge')
metric.add_sample('following_count', value=data["following_count"], labels={})
yield metricmetric.add_sample('following_count', value=data["following_count"], labels={})Lancer la boucle infinie
class JsonCollector(object):
def __init__(self):
pass
def collect(self):
url = mastodon_host + 'api/v1/accounts/1'
r = requests.get(url)
data = json.loads(r.content.decode())
metric = Metric('following_count', 'Number of following account', 'gauge')
metric.add_sample('following_count', value=data["following_count"], labels={})
yield metric
if __name__ == "__main__":
start_http_server(9400)
REGISTRY.register(JsonCollector())
while True: time.sleep(1)
root@server ~ # curl localhost:9400
# HELP following_count Number of following account
# TYPE following_count gauge
following_count 552.0
Brancher l'exporter à Prometheus
- job_name: 'mastodon'
static_configs:
- targets: ['localhost:9400']
