Пример #1
0
    should_respect_env_var=True,
    should_instrument_requests_inprogress=True,
    excluded_handlers=[".*admin.*", "/metrics"],
    env_var_name="ENABLE_METRICS",
    inprogress_name="inprogress",
    inprogress_labels=True,
)

instrumentator.add(
    metrics.request_size(
        should_include_handler=True,
        should_include_method=False,
        should_include_status=True,
        metric_namespace="a",
        metric_subsystem="b",
    )).add(
        metrics.response_size(
            should_include_handler=True,
            should_include_method=False,
            should_include_status=True,
            metric_namespace="namespace",
            metric_subsystem="subsystem",
        ))


@app.exception_handler(UnicornException)
async def unicorn_exception_handler(request: Request, exc: UnicornException):
    return JSONResponse(
        status_code=exc.status,
        content={
            "code": exc.code,
Пример #2
0
    )

    def instrumentation(info: Info) -> None:
        if info.modified_handler == "/predict":
            predicted_quality = info.response.headers.get("X-model-score")
            if predicted_quality:
                METRIC.observe(float(predicted_quality))

    return instrumentation


# ----- add metrics -----
instrumentator.add(
    metrics.request_size(
        should_include_handler=True,
        should_include_method=True,
        should_include_status=True,
        metric_namespace=NAMESPACE,
        metric_subsystem=SUBSYSTEM,
    ))
instrumentator.add(
    metrics.response_size(
        should_include_handler=True,
        should_include_method=True,
        should_include_status=True,
        metric_namespace=NAMESPACE,
        metric_subsystem=SUBSYSTEM,
    ))
instrumentator.add(
    metrics.latency(
        should_include_handler=True,
        should_include_method=True,
Пример #3
0
def error_metric() -> Callable[[Info], None]:
    """Basic error counter metric.

    Refer to prometheus_fastapi_instrumentator documentation for details.
    """
    metric = Counter(
        "errors_total",
        "Errors counter by http method and error type",
        labelnames=(
            "method",
            "error_type",
        ),
    )

    def instrumentation(info: Info) -> None:
        if info.response.status_code == 422:
            metric.labels(info.request.method, "validation error").inc()
        if info.response.status_code == 409:
            metric.labels(info.request.method, "duplicate").inc()
        if info.response.status_code == 404:
            metric.labels(info.request.method, "not found").inc()

    return instrumentation


metrics_instrumentator = Instrumentator(
    excluded_handlers=["/docs", "/openapi.json", "/metrics"])
metrics_instrumentator.add(error_metric())
metrics.requests()