Skip to content

Commit 18ec868

Browse files
committed
feat: instrument scripts with prometheus metrics
Add metrics calls to register_stac.py and augment_stac_item.py: - Wrap HTTP operations with duration timers - Increment operation counters (create/update/skip/replace) - Track preview generation duration - All 42 unit tests pass, coverage: augment 24%, register 42%, metrics 65%
1 parent 9368349 commit 18ec868

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

scripts/augment_stac_item.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import httpx
1414
import s3fs
1515
import zarr
16+
from metrics import PREVIEW_GENERATION_DURATION, PREVIEW_HTTP_REQUEST_DURATION
1617
from pystac import Asset, Item, Link
1718
from pystac.extensions.projection import ProjectionExtension
1819

@@ -1049,21 +1050,23 @@ def _request(
10491050

10501051

10511052
def http_get(url: str, headers: dict[str, str]) -> dict[str, Any]:
1052-
data = _request("GET", url, headers).json()
1053+
with PREVIEW_HTTP_REQUEST_DURATION.labels(operation="get", endpoint="item").time():
1054+
data = _request("GET", url, headers).json()
10531055
if isinstance(data, dict):
10541056
return data
10551057
raise ValueError("unexpected non-mapping response body")
10561058

10571059

10581060
def http_put(url: str, data: dict[str, Any], headers: dict[str, str]) -> int:
1059-
return int(
1060-
_request(
1061-
"PUT",
1062-
url,
1063-
{**headers, "Content-Type": "application/json"},
1064-
json_body=data,
1065-
).status_code
1066-
)
1061+
with PREVIEW_HTTP_REQUEST_DURATION.labels(operation="put", endpoint="item").time():
1062+
return int(
1063+
_request(
1064+
"PUT",
1065+
url,
1066+
{**headers, "Content-Type": "application/json"},
1067+
json_body=data,
1068+
).status_code
1069+
)
10671070

10681071

10691072
def ensure_collection_thumbnail(
@@ -1143,12 +1146,14 @@ def main(argv: Sequence[str] | None = None) -> int:
11431146

11441147
item = Item.from_dict(payload)
11451148
target_collection = item.collection_id or args.collection
1146-
_augment_item(
1147-
item,
1148-
raster_base=args.raster_base,
1149-
collection_id=target_collection,
1150-
verbose=args.verbose,
1151-
)
1149+
1150+
with PREVIEW_GENERATION_DURATION.labels(collection=target_collection).time():
1151+
_augment_item(
1152+
item,
1153+
raster_base=args.raster_base,
1154+
collection_id=target_collection,
1155+
verbose=args.verbose,
1156+
)
11521157

11531158
target_url = f"{args.stac.rstrip('/')}/collections/{target_collection}/items/{item.id}"
11541159
try:

scripts/register_stac.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import httpx
2323
import xarray as xr
24+
from metrics import STAC_HTTP_REQUEST_DURATION, STAC_REGISTRATION_TOTAL
2425
from tenacity import retry, stop_after_attempt, wait_exponential
2526

2627
# Config: override via env vars
@@ -395,7 +396,8 @@ def register_item(
395396
with httpx.Client(timeout=TIMEOUT) as client:
396397
# Check if item exists
397398
try:
398-
response = client.get(item_url, headers=headers)
399+
with STAC_HTTP_REQUEST_DURATION.labels(operation="get", endpoint="item").time():
400+
response = client.get(item_url, headers=headers)
399401
exists = response.status_code == 200
400402
except httpx.HTTPError:
401403
exists = False
@@ -405,34 +407,59 @@ def register_item(
405407

406408
if mode == "create-or-skip":
407409
logger.info("Skipping (mode=create-or-skip)")
410+
STAC_REGISTRATION_TOTAL.labels(
411+
collection=collection_id, operation="skip", status="success"
412+
).inc()
408413
return
409414
elif mode in ("upsert", "update"):
410415
logger.info("Updating existing item (mode=upsert)")
411-
response = client.put(item_url, json=item, headers=headers)
416+
with STAC_HTTP_REQUEST_DURATION.labels(operation="put", endpoint="item").time():
417+
response = client.put(item_url, json=item, headers=headers)
412418
if response.status_code >= 400:
413419
logger.error(f" {response.status_code} {response.reason_phrase}")
414420
logger.info(f"Response body: {response.text}")
421+
STAC_REGISTRATION_TOTAL.labels(
422+
collection=collection_id, operation="update", status="error"
423+
).inc()
415424
response.raise_for_status()
416425
logger.info(f"Successfully updated item {item_id}")
426+
STAC_REGISTRATION_TOTAL.labels(
427+
collection=collection_id, operation="update", status="success"
428+
).inc()
417429
elif mode in ("force", "replace"):
418430
logger.info("Deleting and recreating (mode=replace)")
419-
client.delete(item_url, headers=headers)
420-
response = client.post(items_url, json=item, headers=headers)
431+
with STAC_HTTP_REQUEST_DURATION.labels(operation="delete", endpoint="item").time():
432+
client.delete(item_url, headers=headers)
433+
with STAC_HTTP_REQUEST_DURATION.labels(operation="post", endpoint="items").time():
434+
response = client.post(items_url, json=item, headers=headers)
421435
if response.status_code >= 400:
422436
logger.error(f" {response.status_code} {response.reason_phrase}")
423437
logger.info(f"Response body: {response.text}")
438+
STAC_REGISTRATION_TOTAL.labels(
439+
collection=collection_id, operation="replace", status="error"
440+
).inc()
424441
response.raise_for_status()
425442
logger.info(f"Successfully replaced item {item_id}")
443+
STAC_REGISTRATION_TOTAL.labels(
444+
collection=collection_id, operation="replace", status="success"
445+
).inc()
426446
else:
427447
raise ValueError(f"Unknown mode: {mode}")
428448
else:
429449
logger.info(f"Creating new item {item_id}")
430-
response = client.post(items_url, json=item, headers=headers)
450+
with STAC_HTTP_REQUEST_DURATION.labels(operation="post", endpoint="items").time():
451+
response = client.post(items_url, json=item, headers=headers)
431452
if response.status_code >= 400:
432453
logger.error(f" {response.status_code} {response.reason_phrase}")
433454
logger.info(f"Response body: {response.text}")
455+
STAC_REGISTRATION_TOTAL.labels(
456+
collection=collection_id, operation="create", status="error"
457+
).inc()
434458
response.raise_for_status()
435459
logger.info(f"Successfully created item {item_id}")
460+
STAC_REGISTRATION_TOTAL.labels(
461+
collection=collection_id, operation="create", status="success"
462+
).inc()
436463

437464

438465
def main() -> int:

0 commit comments

Comments
 (0)