✨Added tracing decorator, and add some example usage#9161
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9161 +/- ##
==========================================
+ Coverage 87.34% 89.01% +1.67%
==========================================
Files 2065 1410 -655
Lines 81471 59906 -21565
Branches 1475 194 -1281
==========================================
- Hits 71162 53328 -17834
+ Misses 9900 6517 -3383
+ Partials 409 61 -348
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR introduces a reusable tracing decorator in servicelib and applies it across several services to generate OpenTelemetry spans with minimal boilerplate, plus adds autoscaling-specific helpers to link EC2 instance lifecycle spans back to the launch span via EC2 tags.
Changes:
- Added a
@traceddecorator (sync + async) built on top oftraced_operation, with new unit tests. - Instrumented key periodic/background routines (webserver garbage collector, storage DSM cleaner, clusters-keeper cluster checks, autoscaling cycles) via
@traced. - Added autoscaling utilities to persist
traceparentin EC2 tags and create linked lifecycle spans (launch/attach/drain/terminate).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| services/web/server/src/simcore_service_webserver/garbage_collector/_core.py | Wraps garbage collection entrypoint with @traced for span creation. |
| services/storage/src/simcore_service_storage/dsm_cleaner.py | Adds @traced to DSM cleaner periodic task. |
| services/clusters-keeper/src/simcore_service_clusters_keeper/modules/clusters_management_core.py | Adds @traced to cluster heartbeat/termination/start/broken handling functions. |
| services/autoscaling/src/simcore_service_autoscaling/utils/tracing.py | New helper module to store trace context in EC2 tags and create linked lifecycle spans. |
| services/autoscaling/src/simcore_service_autoscaling/modules/cluster_scaling/_warm_buffer_machines_pool_core.py | Adds @traced spans around warm-buffer monitoring/termination workflows; modernizes type aliases to type ... =. |
| services/autoscaling/src/simcore_service_autoscaling/modules/cluster_scaling/_auto_scaling_core.py | Adds @traced to key autoscaling steps and introduces per-instance lifecycle spans with launch linking. |
| packages/service-library/tests/fastapi/test_tracing.py | Adds tests validating @traced behavior (async/sync/custom names/attributes+links/no-op warning). |
| packages/service-library/src/servicelib/tracing.py | Implements the @traced decorator and tracing-config resolution logic. |
| @contextmanager | ||
| def traced_instance_lifecycle( | ||
| operation_name: str, | ||
| *, | ||
| app: FastAPI, | ||
| ec2_instance: EC2InstanceData, | ||
| hostname: str | None = None, | ||
| **extra_attributes: str, | ||
| ): |
| # For root spans (no active parent), create a link back to the launch span | ||
| current_span = trace.get_current_span() | ||
| is_root_span = not current_span.is_recording() |
| def get_trace_carrier_ec2_tags() -> EC2Tags: | ||
| """Return EC2 tags containing the current trace context (traceparent). | ||
|
|
||
| Should be called during instance launch within an active span so that | ||
| the tag is set on the new EC2 instance. | ||
| Returns an empty dict if no active trace context. | ||
| """ | ||
| carrier = get_trace_carrier_from_current_context() | ||
| if carrier and (traceparent := carrier.get("traceparent")): | ||
| return {TRACEPARENT_EC2_TAG_KEY: TypeAdapter(AWSTagValue).validate_python(traceparent)} | ||
| return {} |
| set_and_clean_settings_env_vars: None, | ||
| tracing_settings_in: tuple[str, int | str, float], | ||
| faker: Faker, | ||
| ) -> None: |
|



What do these changes do?
Related issue/s
How to test
Dev-ops