feat(python-sdk): add opt-in metrics and tracing hooks

HE
2mo ago
+1193-230

Closes #436

Description

Adds opt-in metrics and tracing hooks to MossClient and query() for observing query latency, volume, and execution status without vendor lock-in.

Changes

  • QueryMetrics dataclass: Captures index_name, query, duration_ms, result_count, is_local, top_k, alpha, engine_time_ms, error, and provides is_success property and as_dict() serializer.
  • Opt-in on_query hook:
    • Configurable at client initialization: MossClient(project_id, project_key, on_query=...)
    • Configurable / updatable on client instance: client.on_query = ...
    • Configurable per-query: client.query(index_name, query, on_query=...)
    • Accepts single callable or sequence of callables
    • Supports both synchronous and asynchronous callbacks
  • Error isolation: User metrics sink errors are safely caught and logged as warnings so that telemetry failures never interrupt search queries.
  • Unit Tests: Added comprehensive test suite in sdks/python/sdk/tests/test_metrics.py covering sync/async hooks, multiple hooks, per-query overrides, local vs. cloud execution, error tracking, and resilience.
  • Sample & Documentation: Added examples/python/metrics_and_tracing_sample.py demonstrating custom metrics collector and async tracing integration, updated SDK documentation and changelog.

Summary by CodeRabbit

  • New Features

    • Added optional query metrics and tracing hooks for monitoring latency, result counts, execution location, and errors.
    • Added synchronous and asynchronous telemetry callbacks, configurable globally or per query.
    • Exported QueryMetrics and QueryHook for SDK integration.
    • Added a Python sample demonstrating metrics collection and tracing.
  • Documentation

    • Documented metrics and tracing configuration, callback behavior, and available metrics.
    • Updated the changelog and sample instructions.
  • Tests

    • Added coverage for metrics reporting, callback behavior, failures, and cloud fallback scenarios.