prometheus-fastapi-instrumentator
prometheus-fastapi-instrumentator copied to clipboard
incompatibility with fastapi-versioning
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
I'm having the same problem - any solution yet? @trallnag
Are we talking about this project?
https://github.com/DeanWay/fastapi-versioning
Not to answer for the op, but yes I think that is the github for the fastapi-version project
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.
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. :)