feat(python-sdk): add opt-in metrics and tracing hooks
HE
2mo ago+1193+1193+0123456789012345678901234567890123456789-230-230-012345678901234567890123456789
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
QueryMetricsdataclass: Capturesindex_name,query,duration_ms,result_count,is_local,top_k,alpha,engine_time_ms,error, and providesis_successproperty andas_dict()serializer.- Opt-in
on_queryhook:- 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
- Configurable at client initialization:
- 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.pycovering 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.pydemonstrating 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
QueryMetricsandQueryHookfor 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.