Mass ruff auto-fix:
- ruff check --fix: 109 issues fixed (F401 unused imports,
I001 unsorted imports, UP037 quoted annotations, SIM105
suppressible exception, RUF100 unused-noqa)
- ruff check --fix --unsafe-fixes: 22 additional issues
- ruff format: 70 files reformatted
- Manual pass: fix 16 misplaced import httpx lines
- Manual pass: fix remaining E402 (import-after-docstring)
Result: 283 errors -> 30 errors.
The remaining 30 are real issues that need manual review:
5 F401 unused-import (likely auto-generated stubs)
5 F821 undefined-name (real bugs in code that references
redis/pydantic/LLMRegistry without imports)
3 BLE001 (the compliance LLM fallback is intentional; the
other two are real)
3 RUF012 mutable-class-default
3 SIM105, 3 SIM117, 2 E722, 2 E741
1 B007, 1 B025, 1 E402, 1 RUF200 (pyproject.toml issue)
Tests: 436/437 pass (1 pre-existing SSE sandbox failure).
format check + import sort: now clean.
make ci: still gated on the 30 remaining real issues.
Follow-up: triage the 30 issues file-by-file.
115 lines
4 KiB
Python
115 lines
4 KiB
Python
# SPDX-License-Identifier: MIT
|
|
# Copyright (c) 2026 Rug Munch Media LLC
|
|
#
|
|
# Part of Pry — https://git.rugmunch.io/RugMunchMedia/pryscraper
|
|
# Licensed under MIT. See LICENSE.
|
|
"""Pry — Observability: Prometheus metrics, OpenTelemetry tracing, structured logging."""
|
|
|
|
import logging
|
|
import time
|
|
from contextlib import contextmanager
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Try to import Prometheus
|
|
try:
|
|
from prometheus_client import CONTENT_TYPE_LATEST, Counter, Gauge, Histogram, generate_latest
|
|
|
|
_has_prometheus = True
|
|
except ImportError:
|
|
_has_prometheus = False
|
|
|
|
# Try to import OpenTelemetry
|
|
try:
|
|
from opentelemetry import trace
|
|
from opentelemetry.sdk.trace import TracerProvider
|
|
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
|
|
|
|
_has_otel = True
|
|
except ImportError:
|
|
_has_otel = False
|
|
|
|
|
|
# Metrics
|
|
if _has_prometheus:
|
|
REQUEST_COUNT = Counter(
|
|
"pry_requests_total", "Total requests", ["method", "endpoint", "status"]
|
|
)
|
|
REQUEST_LATENCY = Histogram("pry_request_duration_seconds", "Request latency", ["endpoint"])
|
|
SCRAPE_COUNT = Counter("pry_scrapes_total", "Total scrapes", ["method", "status"])
|
|
SCRAPE_LATENCY = Histogram("pry_scrape_duration_seconds", "Scrape latency", ["method"])
|
|
LLM_CALLS = Counter("pry_llm_calls_total", "Total LLM calls", ["provider", "model"])
|
|
LLM_COST = Counter("pry_llm_cost_usd_total", "Total LLM cost in USD", ["provider"])
|
|
TEMPLATE_USAGE = Counter("pry_template_usage_total", "Template usage", ["template_id"])
|
|
CACHE_HITS = Counter("pry_cache_hits_total", "Cache hits", ["cache_type"])
|
|
ACTIVE_CONNECTIONS = Gauge("pry_active_connections", "Active connections", ["type"])
|
|
else:
|
|
REQUEST_COUNT = REQUEST_LATENCY = SCRAPE_COUNT = SCRAPE_LATENCY = None
|
|
LLM_CALLS = LLM_COST = TEMPLATE_USAGE = CACHE_HITS = ACTIVE_CONNECTIONS = None
|
|
|
|
|
|
def setup_tracing(service_name: str = "pry") -> None:
|
|
"""Initialize OpenTelemetry tracing."""
|
|
if not _has_otel:
|
|
return
|
|
try:
|
|
provider = TracerProvider()
|
|
processor = SimpleSpanProcessor(ConsoleSpanExporter())
|
|
provider.add_span_processor(processor)
|
|
trace.set_tracer_provider(provider)
|
|
logger.info("tracing_initialized", extra={"service": service_name})
|
|
except Exception as e: # noqa: BLE001
|
|
logger.warning("tracing_init_failed", extra={"error": str(e)[:80]})
|
|
|
|
|
|
@contextmanager
|
|
def track_request(endpoint: str, method: str = "GET"):
|
|
"""Context manager to track request metrics and timing."""
|
|
start = time.time()
|
|
status = "success"
|
|
try:
|
|
yield
|
|
except Exception:
|
|
status = "error"
|
|
raise
|
|
finally:
|
|
elapsed = time.time() - start
|
|
if _has_prometheus and REQUEST_COUNT and REQUEST_LATENCY:
|
|
REQUEST_COUNT.labels(method=method, endpoint=endpoint, status=status).inc()
|
|
REQUEST_LATENCY.labels(endpoint=endpoint).observe(elapsed)
|
|
|
|
|
|
@contextmanager
|
|
def track_scrape(method: str = "direct"):
|
|
"""Context manager to track scrape metrics."""
|
|
start = time.time()
|
|
status = "success"
|
|
try:
|
|
yield
|
|
except Exception:
|
|
status = "error"
|
|
raise
|
|
finally:
|
|
elapsed = time.time() - start
|
|
if _has_prometheus and SCRAPE_COUNT and SCRAPE_LATENCY:
|
|
SCRAPE_COUNT.labels(method=method, status=status).inc()
|
|
SCRAPE_LATENCY.labels(method=method).observe(elapsed)
|
|
|
|
|
|
def track_llm_call(provider: str, model: str, cost: float = 0.0) -> None:
|
|
if _has_prometheus and LLM_CALLS and LLM_COST:
|
|
LLM_CALLS.labels(provider=provider, model=model).inc()
|
|
if cost > 0:
|
|
LLM_COST.labels(provider=provider).inc(cost)
|
|
|
|
|
|
def track_template(template_id: str) -> None:
|
|
if _has_prometheus and TEMPLATE_USAGE:
|
|
TEMPLATE_USAGE.labels(template_id=template_id).inc()
|
|
|
|
|
|
def get_metrics_output() -> tuple[bytes, str]:
|
|
"""Return Prometheus metrics in text format."""
|
|
if not _has_prometheus:
|
|
return b"# Prometheus not installed\n", "text/plain"
|
|
return generate_latest(), CONTENT_TYPE_LATEST
|