Skip to content

Commit 15b75f4

Browse files
committed
sort out telemetry in logging middleware
1 parent e2497cb commit 15b75f4

File tree

5 files changed

+68
-61
lines changed

5 files changed

+68
-61
lines changed

src/titiler/application/titiler/application/main.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
logging.getLogger("botocore.credentials").disabled = True
4747
logging.getLogger("botocore.utils").disabled = True
48+
logging.getLogger("rasterio.session").setLevel(logging.ERROR)
4849
logging.getLogger("rio-tiler").setLevel(logging.ERROR)
4950

5051

@@ -238,14 +239,14 @@ def validate_access_token(access_token: str = Security(api_key_query)):
238239
{
239240
k: f"%({k})s"
240241
for k in [
241-
"method",
242-
"referer",
243-
"origin",
244-
"route",
245-
"path",
246-
"path_params",
247-
"query_params",
248-
"headers",
242+
"http.method",
243+
"http.referer",
244+
"http.origin",
245+
"http.route",
246+
"http.path",
247+
"http.request.header",
248+
"titiler.path_params",
249+
"titiler.query_params",
249250
]
250251
}
251252
)
@@ -267,12 +268,12 @@ def validate_access_token(access_token: str = Security(api_key_query)):
267268
},
268269
},
269270
"loggers": {
270-
"titlier": {
271-
"level": "WARNING",
271+
"titiler": {
272+
"level": "INFO",
272273
"handlers": ["console_detailed"],
273274
"propagate": False,
274275
},
275-
"titiler-requests": {
276+
"titiler.requests": {
276277
"level": "INFO",
277278
"handlers": ["console_request"],
278279
"propagate": False,

src/titiler/core/tests/test_logger_middleware.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Test titiler.core.middleware.TotalTimeMiddleware."""
1+
"""Test titiler.core.middleware.LoggerMiddleware."""
22

33
import json
44
import logging
@@ -11,7 +11,7 @@
1111
from titiler.core.middleware import LoggerMiddleware
1212

1313

14-
def test_timing_middleware(caplog):
14+
def test_logger_middleware(caplog):
1515
"""Create App."""
1616
app = FastAPI()
1717

@@ -44,14 +44,14 @@ async def route3(value: str = Path()):
4444
{
4545
k: f"%({k})s"
4646
for k in [
47-
"method",
48-
"referer",
49-
"origin",
50-
"route",
51-
"path",
52-
"path_params",
53-
"query_params",
54-
"headers",
47+
"http.method",
48+
"http.referer",
49+
"http.origin",
50+
"http.route",
51+
"http.path",
52+
"titiler.path_params",
53+
"titiler.query_params",
54+
"http.request.header.",
5555
]
5656
}
5757
)
@@ -84,32 +84,32 @@ async def route3(value: str = Path()):
8484
assert log.name == "titiler.requests"
8585
assert log.levelname == "INFO"
8686
assert log.message == "Request received: /route1 GET"
87-
assert hasattr(log, "query_params")
88-
assert log.route == "/route1"
87+
assert hasattr(log, "titiler.query_params")
88+
assert getattr(log, "http.route") == "/route1"
8989

9090
caplog.clear()
9191
client.get("/route1", params={"hey": "yo"})
9292
log = caplog.records[0]
9393
assert log.message == "Request received: /route1 GET"
94-
assert log.query_params == {"hey": "yo"}
94+
assert getattr(log, "titiler.query_params") == {"hey": "yo"}
9595

9696
caplog.clear()
9797
client.get(
9898
"/route1", params={"hey": "yo"}, headers={"accept-encoding": "gzip"}
9999
)
100100
log = caplog.records[0]
101101
assert log.message == "Request received: /route1 GET"
102-
assert log.query_params == {"hey": "yo"}
103-
assert log.headers["accept-encoding"] == "gzip"
102+
assert getattr(log, "titiler.query_params") == {"hey": "yo"}
103+
assert getattr(log, "http.request.header.accept-encoding") == "gzip"
104104

105105
caplog.clear()
106106
client.get("/route2/val")
107107
log = caplog.records[0]
108108
assert log.name == "titiler.requests"
109109
assert log.levelname == "INFO"
110110
assert log.message == "Request received: /route2/val GET"
111-
assert hasattr(log, "query_params")
112-
assert log.route == "/route2/{value}"
111+
assert hasattr(log, "titiler.query_params")
112+
assert getattr(log, "http.route") == "/route2/{value}"
113113

114114
caplog.clear()
115115
with pytest.raises(Exception): # noqa: B017
@@ -118,5 +118,5 @@ async def route3(value: str = Path()):
118118
assert log.name == "titiler.requests"
119119
assert log.levelname == "INFO"
120120
assert log.message == "Request received: /route3/val GET"
121-
assert hasattr(log, "query_params")
121+
assert hasattr(log, "titiler.query_params")
122122
assert log.route == "/route3/{value}"

src/titiler/core/tests/test_telemetry.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,3 @@ def test_tracing_enabled_success_path(memory_exporter):
8585
span = next(filter(lambda x: x.name == "TilerFactory.tile", finished_spans), None)
8686
assert span
8787
assert span.status.status_code == StatusCode.OK
88-
assert span.attributes["titiler.path_param.tileMatrixSetId"] == "WebMercatorQuad"
89-
assert span.attributes["titiler.path_param.z"] == TEST_Z
90-
assert span.attributes["titiler.path_param.x"] == TEST_X
91-
assert span.attributes["titiler.path_param.y"] == TEST_Y

src/titiler/core/titiler/core/factory.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -699,28 +699,28 @@ async def tileset(
699699
minzoom = src_dst.minzoom
700700
maxzoom = src_dst.maxzoom
701701

702-
collection_bbox = {
703-
"lowerLeft": [bounds[0], bounds[1]],
704-
"upperRight": [bounds[2], bounds[3]],
705-
"crs": CRS_to_uri(tms.rasterio_geographic_crs),
706-
}
702+
collection_bbox = {
703+
"lowerLeft": [bounds[0], bounds[1]],
704+
"upperRight": [bounds[2], bounds[3]],
705+
"crs": CRS_to_uri(tms.rasterio_geographic_crs),
706+
}
707707

708-
tilematrix_limit = []
709-
for zoom in range(minzoom, maxzoom + 1, 1):
710-
matrix = tms.matrix(zoom)
711-
ulTile = tms.tile(bounds[0], bounds[3], int(matrix.id))
712-
lrTile = tms.tile(bounds[2], bounds[1], int(matrix.id))
713-
minx, maxx = (min(ulTile.x, lrTile.x), max(ulTile.x, lrTile.x))
714-
miny, maxy = (min(ulTile.y, lrTile.y), max(ulTile.y, lrTile.y))
715-
tilematrix_limit.append(
716-
{
717-
"tileMatrix": matrix.id,
718-
"minTileRow": max(miny, 0),
719-
"maxTileRow": min(maxy, matrix.matrixHeight),
720-
"minTileCol": max(minx, 0),
721-
"maxTileCol": min(maxx, matrix.matrixWidth),
722-
}
723-
)
708+
tilematrix_limit = []
709+
for zoom in range(minzoom, maxzoom + 1, 1):
710+
matrix = tms.matrix(zoom)
711+
ulTile = tms.tile(bounds[0], bounds[3], int(matrix.id))
712+
lrTile = tms.tile(bounds[2], bounds[1], int(matrix.id))
713+
minx, maxx = (min(ulTile.x, lrTile.x), max(ulTile.x, lrTile.x))
714+
miny, maxy = (min(ulTile.y, lrTile.y), max(ulTile.y, lrTile.y))
715+
tilematrix_limit.append(
716+
{
717+
"tileMatrix": matrix.id,
718+
"minTileRow": max(miny, 0),
719+
"maxTileRow": min(maxy, matrix.matrixHeight),
720+
"minTileCol": max(minx, 0),
721+
"maxTileCol": min(maxx, matrix.matrixWidth),
722+
}
723+
)
724724

725725
query_string = (
726726
f"?{urlencode(request.query_params._list)}"

src/titiler/core/titiler/core/middleware.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,26 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
110110
return await self.app(scope, receive, send)
111111

112112
request = Request(scope, receive=receive)
113-
114113
data = {
115114
"http.method": request.method,
115+
"http.url": str(request.url),
116+
"http.scheme": request.url.scheme,
117+
"http.host": request.headers.get("host", request.url.hostname or "unknown"),
118+
"http.target": request.url.path
119+
+ (f"?{request.url.query}" if request.url.query else ""),
120+
"http.user_agent": request.headers.get("user-agent"),
116121
"http.referer": next(
117122
(request.headers.get(attr) for attr in ["referer", "referrer"]),
118123
None,
119124
),
120-
"http.origin": request.headers.get("origin"),
121-
"http.path": request.url.path,
122-
"http.headers": dict(request.headers),
123-
"path_params": request.path_params,
124-
"query_params": dict(request.query_params),
125+
"http.request_content_length": request.headers.get("content-length"),
126+
"http.request.header.accept-encoding": request.headers.get(
127+
"accept-encoding"
128+
),
129+
"http.request.header.origin": request.headers.get("origin"),
130+
"net.host.name": request.url.hostname,
131+
"net.host.port": request.url.port,
132+
"titiler.query_params": dict(request.query_params),
125133
}
126134

127135
telemetry.add_span_attributes(telemetry.flatten_dict(data))
@@ -133,7 +141,9 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
133141
exception = e
134142

135143
if route := scope.get("route"):
136-
data["route"] = route.path
144+
data["http.route"] = route.path
145+
146+
data["titiler.path_params"] = request.path_params
137147

138148
self.logger.info(
139149
f"Request received: {request.url.path} {request.method}",

0 commit comments

Comments
 (0)