prometheus-fastapi-instrumentator icon indicating copy to clipboard operation
prometheus-fastapi-instrumentator copied to clipboard

incompatibility with fastapi-versioning

Open fiddletwix opened this issue 4 years ago • 5 comments
trafficstars

Whenever I use the instrumentator and I use the fastapi-versionin lib, all my handlers match just my version. I think it's because of the prefix format here:

app = VersionedFastAPI(
    app,
    version_format="{major}",
    prefix_format="/v{major}",
    default_version=(1, 0),
)

I'm not sure this is fixable but I did want to report it here

fiddletwix avatar Apr 17 '21 05:04 fiddletwix

I'm having the same problem - any solution yet? @trallnag

nikitira avatar May 26 '21 11:05 nikitira

Are we talking about this project?

https://github.com/DeanWay/fastapi-versioning

trallnag avatar May 26 '21 15:05 trallnag

Not to answer for the op, but yes I think that is the github for the fastapi-version project

james-aliya avatar Aug 17 '21 23:08 james-aliya

Hi,

I found a quick workaround, since fastpi-versioning mounts an api for each version, you can attach the metrics to each mounted api seperatly:

app = VersionedFastAPI(...)

# add Prometheus Metrics
for route in app.routes:
    if isinstance(route, starlette.routing.Mount):
        Instrumentator().instrument(route.app).expose(route.app)

This of course generates one metrics endpoint per versioned-api (localhost:XXXX/v1/metrics), but its better than nothing.

JoniB avatar Nov 03 '21 10:11 JoniB

Hi,

I found a quick workaround, since fastpi-versioning mounts an api for each version, you can attach the metrics to each mounted api seperatly:

app = VersionedFastAPI(...)

# add Prometheus Metrics
for route in app.routes:
    if isinstance(route, starlette.routing.Mount):
        Instrumentator().instrument(route.app).expose(route.app)

This of course generates one metrics endpoint per versioned-api (localhost:XXXX/v1/metrics), but its better than nothing.

This was very helpful, I was still getting an error because some of the default metrics shared names between different versions, so I changed the default metrics names before instrumenting, like so:

for route in app.routes:
    if isinstance(route, starlette.routing.Mount):
        instr = Instrumentator()
        default_metrics = metrics.default(
            metric_namespace=route.path.strip("/"),
        )
        instr.add(default_metrics)
        instr.instrument(route.app).expose(route.app)

Just putting it here for reference. :)

lucacampanella avatar Apr 29 '22 13:04 lucacampanella