Skip to content
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

Default metrics are gone when adding a custom instrumentation #278

Open
stephen-lazarionok opened this issue Nov 29, 2023 · 3 comments
Open

Comments

@stephen-lazarionok
Copy link

Once I add my custom instrumentation the default metrics are gone..

def my_custom_http_instrumentation(cfg) -> Callable[[Info], None]:
    cntr = Counter(
        "my_http_reqs_total",
        "Number of request accepted.",
        [
            "tenant",
            "tenant_version",
            "method",
            "handler",
            "api_user",
            "response_code",
        ],
    )

    def instrumentation(info: Info) -> None:
        ...
        # implementation
       ...

    return instrumentation


instrumentator = Instrumentator(
    should_group_status_codes=False,
    should_ignore_untemplated=True,
    should_respect_env_var=True,
    should_instrument_requests_inprogress=True,
    excluded_handlers=["/metrics"],
    env_var_name="ENABLE_METRICS",
    inprogress_name="inprogress",
    inprogress_labels=True,
)

instrumentator.add(my_custom_http_instrumentation(my_config))
instrumentator.instrument(app)
instrumentator.expose(app, include_in_schema=False, should_gzip=True)
@dasshit
Copy link

dasshit commented Dec 5, 2023

@dasshit
Copy link

dasshit commented Dec 5, 2023

Following code will resolve your issue

from prometheus_fastapi_instrumentator import metrics


def my_custom_http_instrumentation(cfg) -> Callable[[Info], None]:
    cntr = Counter(
        "my_http_reqs_total",
        "Number of request accepted.",
        [
            "tenant",
            "tenant_version",
            "method",
            "handler",
            "api_user",
            "response_code",
        ],
    )

    def instrumentation(info: Info) -> None:
        ...
        # implementation
       ...

    return instrumentation


instrumentator = Instrumentator(
    should_group_status_codes=False,
    should_ignore_untemplated=True,
    should_respect_env_var=True,
    should_instrument_requests_inprogress=True,
    excluded_handlers=["/metrics"],
    env_var_name="ENABLE_METRICS",
    inprogress_name="inprogress",
    inprogress_labels=True,
)

instrumentator.add(my_custom_http_instrumentation(my_config))
instrumentator.add(metrics.default())
instrumentator.instrument(app)
instrumentator.expose(app, include_in_schema=False, should_gzip=True)

@lucasalvarezlacasa
Copy link

Thank you so much, I was experiencing this same issue today!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants