logbrew-flask
Flask integration for capturing LogBrew request spans and exceptions with the public Python SDK.
python3 -m pip install logbrew-sdk logbrew-flask
logbrew-flask requires Python 3.10 or newer.
The package is typed, ships py.typed, depends on the core logbrew-sdk, and keeps Flask as a normal framework dependency instead of monkeypatching Flask globally.
Use a project-scoped server ingest key, for example LOGBREW_SERVER_API_KEY.
import os
from flask import Flask
from logbrew_flask import add_logbrew_middleware
from logbrew_sdk import LogBrewClient
client = LogBrewClient.create(
api_key=os.environ["LOGBREW_SERVER_API_KEY"],
release=os.environ.get("LOGBREW_RELEASE"),
environment=os.environ.get("LOGBREW_ENVIRONMENT"),
sdk_name="checkout-api",
sdk_version="1.0.0",
)
app = Flask(__name__)
add_logbrew_middleware(app, client=client)
@app.get("/health")
def health() -> dict[str, bool]:
return {"ok": True}
What It Captures
The middleware records one request span for each captured response. It can also record request duration metrics and exception issues.
Request spans use the Flask route template, such as GET /orders/<int:order_id>, for low-noise grouping. Span metadata includes routeTemplate; concrete dynamic paths are not emitted when a route template is available. Valid inbound W3C traceparent headers are continued with a fresh child span id.
Handlers can call get_active_logbrew_trace() or use LogBrewLoggingHandler; logs emitted during the request share the active request trace and span.
from logbrew_flask import get_active_logbrew_trace
@app.get("/orders/<int:order_id>")
def order_detail(order_id: int) -> dict[str, str | None]:
trace = get_active_logbrew_trace()
return {"traceId": trace.trace_id if trace else None}
Set capture_request_metrics=True to emit an explicit http.server.duration histogram for each request. Apps can pass span_id_factory when deterministic child span ids are useful for controlled diagnostics; production apps usually let LogBrew generate span ids.
Outbound HTTP Child Spans
When a handler calls another service, use the core Python HTTP helpers inside the Flask request. They automatically reuse the active Flask request trace, create a child span, and inject one W3C traceparent header whose span id matches the emitted outbound span.
from logbrew_sdk import requests_request_with_logbrew_span
@app.post("/checkout/<order_id>")
def checkout(order_id: str) -> dict[str, bool]:
response = requests_request_with_logbrew_span(
"POST",
"https://payments.example.com/payments/authorize",
client=client,
event_id="evt_checkout_payment",
route_template="/payments/authorize",
)
return {"accepted": response.status_code == 202}
The helper does not patch requests globally. It records method, low-cardinality route template, status code, trace id, span id, and parent span id. It does not capture full URLs, query strings, request bodies, response bodies, arbitrary headers, cookies, baggage, or tracestate.
Database, Cache, And Queue Child Spans
Use the core dependency helpers inside a Flask handler to connect database, cache, and queue work to the active request trace.
from logbrew_sdk import (
cache_operation_with_logbrew_span,
database_operation_with_logbrew_span,
queue_operation_with_logbrew_span,
)
@app.post("/checkout/<order_id>")
def checkout(order_id: str) -> dict[str, bool]:
inventory = database_operation_with_logbrew_span(
"SELECT inventory",
client=client,
operation=lambda: database.execute("SELECT quantity FROM inventory WHERE sku = ?", ("sku_123",)).fetchone(),
system="sqlite",
statement_template="SELECT inventory WHERE sku = ?",
)
cached_inventory = cache_operation_with_logbrew_span(
"GET inventory",
client=client,
operation=lambda: cache["sku_123"],
system="memory-cache",
cache_name="inventory-cache",
cache_hit=True,
)
published = queue_operation_with_logbrew_span(
"PUBLISH checkout.completed",
client=client,
operation=lambda: queue.append("checkout.completed") or len(queue),
system="memory-queue",
operation_kind="publish",
queue_name="checkout-events",
task_name="checkout.completed",
)
return {"ok": inventory is not None and cached_inventory > 0 and published == 1}
Run python -m logbrew_flask.examples dependency-spans to see a request span with database, cache, and queue child spans under the same trace. These helpers record operation names, systems, status, trace ids, span ids, parent span ids, and primitive metadata. They do not capture SQL bind values, result payloads, queue message payloads, cache values, arbitrary headers, baggage, or tracestate.
Privacy Defaults
LogBrew does not capture request bodies, response bodies, cookies, arbitrary headers, query strings, raw traceparent values, baggage, or tracestate. Exception issues include the exception type and message but not stack frames.
Delivery Failures
By default, transport failures do not break the Flask response path. Set raise_flush_errors=True only when your app wants delivery failures to surface as request errors in controlled diagnostics.
Tradeoff
Sentry, Datadog, and OpenTelemetry provide broader automatic Flask, outbound HTTP, and dependency instrumentation, including global patching and deeper view/template/client hooks. LogBrew starts with explicit app-owned Flask and dependency helpers because that keeps setup reversible, simple to reason about, and safer for privacy-sensitive services.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file logbrew_flask-0.1.2.tar.gz.
File metadata
- Download URL: logbrew_flask-0.1.2.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b4a1b83230297098a50cf614876e7b9efcbfcb85b57d8d49cb7b874cabdddad
|
|
| MD5 |
e54a5126f1a952d26383bb0b5c5225f1
|
|
| BLAKE2b-256 |
eb60f9e22446eaa7ad3f4cf4ceeb5e6d89df28b3ed0d0cb718766d082c19cefe
|
Provenance
The following attestation bundles were made for logbrew_flask-0.1.2.tar.gz:
Publisher:
publish-packages.yml on LogBrewCo/sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logbrew_flask-0.1.2.tar.gz -
Subject digest:
7b4a1b83230297098a50cf614876e7b9efcbfcb85b57d8d49cb7b874cabdddad - Sigstore transparency entry: 2286922412
- Sigstore integration time:
-
Permalink:
LogBrewCo/sdk@62b607b8028318ee5e9ae4d0e5e32381b88cce5a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/LogBrewCo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
self-hosted -
Publication workflow:
publish-packages.yml@62b607b8028318ee5e9ae4d0e5e32381b88cce5a -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file logbrew_flask-0.1.2-py3-none-any.whl.
File metadata
- Download URL: logbrew_flask-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
894d359084a9ff9d900ff6a49dc66ff43e71c4bfb2601f7de936d89aacbd6a4c
|
|
| MD5 |
322fbe3a04cf24889c8fa15a784de2af
|
|
| BLAKE2b-256 |
4ae650c6b53d4f690c704ed0159392a29ba5f5fddf9a184a22a1eb87c830727e
|
Provenance
The following attestation bundles were made for logbrew_flask-0.1.2-py3-none-any.whl:
Publisher:
publish-packages.yml on LogBrewCo/sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logbrew_flask-0.1.2-py3-none-any.whl -
Subject digest:
894d359084a9ff9d900ff6a49dc66ff43e71c4bfb2601f7de936d89aacbd6a4c - Sigstore transparency entry: 2286922421
- Sigstore integration time:
-
Permalink:
LogBrewCo/sdk@62b607b8028318ee5e9ae4d0e5e32381b88cce5a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/LogBrewCo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
self-hosted -
Publication workflow:
publish-packages.yml@62b607b8028318ee5e9ae4d0e5e32381b88cce5a -
Trigger Event:
workflow_dispatch
-
Statement type: