Skip to content

Commit

Permalink
chore: Adjust things
Browse files Browse the repository at this point in the history
  • Loading branch information
trallnag committed Mar 19, 2023
1 parent 1ac5de9 commit 8fc421d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
exclude = .git,.lock
max-complexity = 10
max-complexity = 12
max-line-length = 90
ignore=E501,W503
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0).

## Unreleased

Nothing.
### Changed

- **BREAKING:** Moved passing response body to instrumentation functions behind
feature flag / whitelist that is empty by default. This changes a feature
introduced with `5.10.0` and affects users that have custom instrumentation
functions accessing `info.response.body`.

Opt-in via new parameter `body_handlers` added to instrumentator and
middleware constructors. Parameter expects list of pattern strings to match
handlers. To keep the old behavior, pass the argument `[r".*"]` to match all
handlers.

## [5.11.2](https://github.com/trallnag/prometheus-fastapi-instrumentator/compare/v5.11.1...v5.11.2) / 2023-03-19

Expand Down
8 changes: 4 additions & 4 deletions src/prometheus_fastapi_instrumentator/instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
should_respect_env_var: bool = False,
should_instrument_requests_inprogress: bool = False,
excluded_handlers: List[str] = [],
body_metric_handlers: List[str] = [],
body_handlers: List[str] = [],
round_latency_decimals: int = 4,
env_var_name: str = "ENABLE_METRICS",
inprogress_name: str = "http_requests_inprogress",
Expand Down Expand Up @@ -72,7 +72,7 @@ def __init__(
to regex patterns. All matches will be skipped and not
instrumented. Defaults to `[]`.
body_metric_handlers (List[str]): List of strings that will be compiled
body_handlers (List[str]): List of strings that will be compiled
to regex patterns. All matches will include response body content
in metric instrumentation. Defaults to `[]`.
Expand Down Expand Up @@ -115,7 +115,7 @@ def __init__(
self.inprogress_labels = inprogress_labels

self.excluded_handlers = [re.compile(path) for path in excluded_handlers]
self.body_metric_handlers = [re.compile(path) for path in body_metric_handlers]
self.body_handlers = [re.compile(path) for path in body_handlers]

self.instrumentations: List[Callable[[metrics.Info], None]] = []
self.async_instrumentations: List[Callable[[metrics.Info], Awaitable[None]]] = []
Expand Down Expand Up @@ -207,7 +207,7 @@ def instrument(
instrumentations=self.instrumentations,
async_instrumentations=self.async_instrumentations,
excluded_handlers=self.excluded_handlers,
body_metric_handlers=self.body_metric_handlers,
body_handlers=self.body_handlers,
metric_namespace=metric_namespace,
metric_subsystem=metric_subsystem,
should_only_respect_2xx_for_highr=should_only_respect_2xx_for_highr,
Expand Down
11 changes: 7 additions & 4 deletions src/prometheus_fastapi_instrumentator/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
should_respect_env_var: bool = False,
should_instrument_requests_inprogress: bool = False,
excluded_handlers: Sequence[str] = (),
body_metric_handlers: Sequence[str] = (),
body_handlers: Sequence[str] = (),
round_latency_decimals: int = 4,
env_var_name: str = "ENABLE_METRICS",
inprogress_name: str = "http_requests_inprogress",
Expand Down Expand Up @@ -80,7 +80,7 @@ def __init__(
self.registry = registry

self.excluded_handlers = [re.compile(path) for path in excluded_handlers]
self.body_metric_handlers = [re.compile(path) for path in body_metric_handlers]
self.body_handlers = [re.compile(path) for path in body_handlers]

if instrumentations:
self.instrumentations = instrumentations
Expand Down Expand Up @@ -141,8 +141,9 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
headers = []
body = b""

# Message body collected for handlers matching body_metric_handlers patterns
if any(pattern.search(handler) for pattern in self.body_metric_handlers):
# Message body collected for handlers matching body_handlers patterns.
if any(pattern.search(handler) for pattern in self.body_handlers):

async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.start":
nonlocal status_code, headers
Expand All @@ -152,7 +153,9 @@ async def send_wrapper(message: Message) -> None:
nonlocal body
body += message["body"]
await send(message)

else:

async def send_wrapper(message: Message) -> None:
if message["type"] == "http.response.start":
nonlocal status_code, headers
Expand Down
8 changes: 4 additions & 4 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def instrumentation(info: metrics.Info) -> None:
assert len(info.response.body) == 20
assert info.response.body.decode() == "0xxx1xxx2xxx3xxx4xxx"

Instrumentator().instrument(app).add(instrumentation)
Instrumentator(body_handlers=[r".*"]).instrument(app).add(instrumentation)

response = client.get("/")
assert instrumentation_executed
Expand Down Expand Up @@ -78,7 +78,7 @@ def instrumentation(info: metrics.Info) -> None:
instrumentation_executed = True
assert len(info.response.body) >= 50000000

Instrumentator().instrument(app).add(instrumentation)
Instrumentator(body_handlers=[r".*"]).instrument(app).add(instrumentation)

response = client.get("/")
assert instrumentation_executed
Expand Down Expand Up @@ -106,7 +106,7 @@ def instrumentation(info: metrics.Info) -> None:
assert len(info.response.body) == 9
assert info.response.body == b"123456789"

Instrumentator().instrument(app).add(instrumentation)
Instrumentator(body_handlers=[r".*"]).instrument(app).add(instrumentation)

response = client.get("/")
assert instrumentation_executed
Expand Down Expand Up @@ -134,7 +134,7 @@ def instrumentation(info: metrics.Info) -> None:
instrumentation_executed = True
assert len(info.response.body) == 50_000_000

Instrumentator().instrument(app).add(instrumentation)
Instrumentator(body_handlers=[r".*"]).instrument(app).add(instrumentation)

response = client.get("/")
assert instrumentation_executed
Expand Down

0 comments on commit 8fc421d

Please sign in to comment.