-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a metrics endpoint /api/v1/metrics/X which proxies to Prometheus (
#251)
- Loading branch information
1 parent
ae00831
commit dbdf65b
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
# | ||
# This provides a Prometheus endpoint on the api-gateway. It proxies | ||
# HTTP GET requests to Prometheus. | ||
# | ||
|
||
import aiohttp | ||
from aiohttp import web | ||
import asyncio | ||
from pulsar.schema import JsonSchema | ||
import uuid | ||
import logging | ||
|
||
logger = logging.getLogger("endpoint") | ||
logger.setLevel(logging.INFO) | ||
|
||
class MetricsEndpoint: | ||
|
||
def __init__(self, prometheus_url, endpoint_path, auth): | ||
|
||
self.prometheus_url = prometheus_url | ||
self.path = endpoint_path | ||
self.auth = auth | ||
self.operation = "service" | ||
|
||
async def start(self): | ||
pass | ||
|
||
def add_routes(self, app): | ||
|
||
app.add_routes([ | ||
web.get(self.path + "/{path:.*}", self.handle), | ||
]) | ||
|
||
async def handle(self, request): | ||
|
||
print(request.path, "...") | ||
|
||
try: | ||
ht = request.headers["Authorization"] | ||
tokens = ht.split(" ", 2) | ||
if tokens[0] != "Bearer": | ||
return web.HTTPUnauthorized() | ||
token = tokens[1] | ||
except: | ||
token = "" | ||
|
||
if not self.auth.permitted(token, self.operation): | ||
return web.HTTPUnauthorized() | ||
|
||
try: | ||
|
||
path = request.match_info["path"] | ||
|
||
async with aiohttp.ClientSession() as session: | ||
|
||
url = ( | ||
self.prometheus_url + "/api/v1/" + path + "?" + | ||
request.query_string | ||
) | ||
|
||
async with session.get(url) as resp: | ||
return web.Response( | ||
status=resp.status, | ||
text=await resp.text() | ||
) | ||
|
||
except Exception as e: | ||
|
||
logging.error(f"Exception: {e}") | ||
|
||
raise web.HTTPInternalServerError() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters