1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
from fastapi import FastAPI
from helpers import utils
from prometheus_client import Counter
from starlette.testclient import TestClient
from prometheus_fastapi_instrumentator import Instrumentator, metrics
def test_mounted_app_with_app():
"""Tests handling of mounted app when root app is instrumented."""
utils.reset_collectors()
app = FastAPI()
@app.get("/app")
def read_main():
return {"message": "Hello World from main app"}
subapp = FastAPI()
@subapp.get("/sub")
def read_sub():
return {"message": "Hello World from sub API"}
app.mount("/subapi", subapp)
metric = Counter("test", "Test.", ("modified_handler", "handler"))
def instrumentation(info: metrics.Info) -> None:
metric.labels(
modified_handler=info.modified_handler, handler=str(info.request.url)
).inc()
Instrumentator().add(instrumentation).instrument(app).expose(app)
client = TestClient(app)
for url in ["/subapi/sub", "/subapi", "/app"]:
print(f"GET {url} " + client.get(url).content.decode())
response = client.get("/metrics").content.decode()
print("GET /metrics\n" + response)
want = '{handler="http://testserver/subapi/sub",modified_handler="/subapi/sub"} 1.0\n'
assert want in response
want = '{handler="http://testserver/subapi",modified_handler="none"} 1.0\n'
assert want in response
want = '{handler="http://testserver/subapi/",modified_handler="none"} 1.0\n'
assert want in response
want = '{handler="http://testserver/app",modified_handler="/app"} 1.0\n'
assert want in response
def test_mounted_app_instrumented_only():
"""Tests case when mounted app is instrumented and not root app."""
utils.reset_collectors()
app = FastAPI()
@app.get("/app")
def read_main():
return {"message": "Hello World from main app"}
subapp = FastAPI()
@subapp.get("/sub")
def read_sub():
return {"message": "Hello World from sub API"}
app.mount("/subapi", subapp)
metric = Counter("test", "Test.", ("modified_handler", "handler"))
def instrumentation(info: metrics.Info) -> None:
metric.labels(
modified_handler=info.modified_handler, handler=str(info.request.url)
).inc()
Instrumentator().add(instrumentation).instrument(subapp).expose(app)
client = TestClient(app)
for url in ["/subapi/sub", "/subapi", "/app"]:
print(f"GET {url} " + client.get(url).content.decode())
response = client.get("/metrics").content.decode()
print("GET /metrics\n" + response)
# Note the modified_handler. It is relative to the instrumented subapp.
want = '{handler="http://testserver/subapi/sub",modified_handler="/sub"} 1.0\n'
assert want in response
want = '{handler="http://testserver/subapi/",modified_handler="none"} 1.0\n'
assert want in response
want = '{handler="http://testserver/subapi"'
assert want not in response
want = '{handler="http://testserver/app"'
assert want not in response
|