Skip to content

Commit

Permalink
Enhance exclusion filters
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Apr 21, 2023
1 parent fc521c6 commit 62b7647
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion emmett_prometheus/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.2"
__version__ = "0.1.3"
39 changes: 26 additions & 13 deletions emmett_prometheus/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,22 @@ def on_load(self):
def _inject_pipes(self, route, f):
if not self.config.auto_load:
return
if route.name in self._excluded_routes:
return
if f == self._metrics_route:
return
if self._get_route_name(route, f) in self._excluded_routes:
return
if self.config.enable_http_metrics and isinstance(route, HTTPRoutingRule):
route.pipeline.insert(0, self._pipe_http)
if self.config.enable_ws_metrics and isinstance(route, WebsocketRoutingRule):
route.pipeline.insert(0, self._pipe_ws)

@staticmethod
def _get_route_name(route, f):
rv = route.name or route.build_name(f)
if rv.endswith("."):
rv = rv + f.__name__
return rv

async def _metrics_route(self):
response.content_type = prometheus_client.exposition.CONTENT_TYPE_LATEST
return prometheus_client.exposition.generate_latest(prometheus_client.REGISTRY)
Expand Down Expand Up @@ -121,18 +128,24 @@ async def close_request(self):
status=response.status
).inc()
if (
request.method not in self.ext._httph_filter_methods and
response.status in self.ext._httph_only_status
self.ext._httph_filter_methods and
request.method in self.ext._httph_filter_methods
):
self._http_histogram.labels(
route=request.name,
method=request.method,
status=response.status
).observe(
(
time.perf_counter_ns() - request._prometheus_http_histogram_ts
) / 1_000_000
)
return
if (
self.ext._httph_only_status and
request.method not in self.ext._httph_only_status
):
return
self._http_histogram.labels(
route=request.name,
method=request.method,
status=response.status
).observe(
(
time.perf_counter_ns() - request._prometheus_http_histogram_ts
) / 1_000_000
)


class PrometheusWSPipe(Pipe):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "emmett-prometheus"
version = "0.1.2"
version = "0.1.3"
description = "Prometheus extension for Emmett framework"
authors = ["Giovanni Barillari <[email protected]>"]
license = "BSD-3-Clause"
Expand Down

0 comments on commit 62b7647

Please sign in to comment.