Skip to content

Promethus Metrics #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
'http_sfv>=0.9.8',
'dataclasses_json>=0.5.7',
'apispec>=6.3.0',
'prometheus-client>=0.17.1'
],
extras_require={
# eg:
Expand Down
19 changes: 18 additions & 1 deletion src/keria/app/agenting.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
from ..core.keeping import RemoteManager
from ..db import basing

from .metrics import MetricsMiddleware, MetricsEndpoint

logger = ogler.getLogger()


Expand All @@ -68,7 +70,18 @@ def setup(name, bran, adminPort, bootPort, base='', httpPort=None, configFile=No
'signify-resource', 'signify-timestamp']))
if os.getenv("KERI_AGENT_CORS", "false").lower() in ("true", "1"):
app.add_middleware(middleware=httping.HandleCORS())
app.add_middleware(authing.SignatureValidationComponent(agency=agency, authn=authn, allowed=["/agent"]))
allowed_routes = ["/agent"]
if os.getenv("KERI_AGENT_METRICS", "false").lower() in ("true", "1"):
promethus = MetricsMiddleware()
bootApp.add_middleware(promethus)
MetricsEnds = MetricsEndpoint(promethus.registry)
bootApp.add_route("/metrics", MetricsEnds)

app.add_middleware(promethus)
app.add_route("/metrics", promethus)
allowed_routes.append("/metrics")

app.add_middleware(authing.SignatureValidationComponent(agency=agency, authn=authn, allowed=allowed_routes))
app.req_options.media_handlers.update(media.Handlers())
app.resp_options.media_handlers.update(media.Handlers())

Expand All @@ -82,6 +95,10 @@ def setup(name, bran, adminPort, bootPort, base='', httpPort=None, configFile=No
presenting.loadEnds(app=app)
notifying.loadEnds(app=app)





if httpPort:
happ = falcon.App(middleware=falcon.CORSMiddleware(
allow_origins='*', allow_credentials='*',
Expand Down
41 changes: 41 additions & 0 deletions src/keria/app/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from prometheus_client import CollectorRegistry, Counter, Histogram, generate_latest
import time

# Define metrics
REQUESTS = Counter('requests_total', 'Total Request Count', ['method', 'path', 'resource'])
ERRORS = Counter('errors_total', 'Total Error Count', ['method', 'path', 'resource', 'status'])
DURATION = Histogram('request_duration_seconds', 'Duration of requests', ['method', 'path', 'resource'])

class MetricsMiddleware:
def __init__(self):
"""Initialize the middleware with Prometheus metrics."""
self.registry = CollectorRegistry()
self.registry.register(REQUESTS)
self.registry.register(ERRORS)
self.registry.register(DURATION)
def process_request(self, req, resp):
"""Process the request before routing it."""
req.context['resource'] = req.get_header('signify-resource')
req.context['start_time'] = time.time()

def process_resource(self, req, resp, resource, params):
"""Process the request after routing."""
REQUESTS.labels(method=req.method, path=req.path, resource=req.context['resource']).inc()

def process_response(self, req, resp, resource, req_succeeded):
"""Post-processing of the response (after routing)."""
if 'start_time' in req.context:
duration = time.time() - req.context['start_time']
DURATION.labels(method=req.method, path=req.path, resource=req.context.get('resource', '')).observe(duration)

if not req_succeeded:
ERRORS.labels(method=req.method, path=req.path, resource=req.context.get('resource', ''), status=resp.status).inc()

class MetricsEndpoint:
def __init__(self, registry):
self.registry = registry

def on_get(self, req, resp):
resp.content_type = 'text/plain; version=0.0.4; charset=utf-8'
data = generate_latest(self.registry)
resp.body = str(data.decode('utf-8'))