claws / aioprometheus

A Prometheus Python client library for asyncio-based applications
175 stars 21 forks source link

Add an auth parameter to Pusher class #31

Closed matclab closed 2 years ago

matclab commented 6 years ago

The push gateway is often used with an authentication. It would be nice to have an auth parameter that would be passed to the aiohttp.ClientSession.

claws commented 6 years ago

So something similar to the aiohttp Client auth example?

My initial thoughts are to add a auth keyword argument to the Pusher.__init__ method allowing a user to pass a BasicAuth object that would then be passed to the aiohttp.ClientSession in the add, replace and delete methods.

matclab commented 6 years ago

Yes, it would fulfill the need for me.

claws commented 2 years ago

75 recently delivered a change that allows kwargs to be passed on to the aiohttp.ClientSession used within the Pusher. This means you can now supply auth parameters which should resolve this issue.

The example code below shows how you can achieve it.

import aiohttp
from aioprometheus import REGISTRY, Counter
from aioprometheus.pusher import Pusher

auth = aiohttp.BasicAuth(username, password=password)
p = Pusher("my-job", push_gateway_url)
c = Counter("total_requests", "Total requests.", {})
c.inc({"url": "/p/user"})
# Push to the pushgateway
resp = await p.replace(REGISTRY, auth=auth)
assert resp.status == 200