Skip to content

feat: wire minimal telemetry into emissions tracker#1200

Open
davidberenstein1957 wants to merge 19 commits into
feat/telemetry-backendfrom
feat/add-telemetry
Open

feat: wire minimal telemetry into emissions tracker#1200
davidberenstein1957 wants to merge 19 commits into
feat/telemetry-backendfrom
feat/add-telemetry

Conversation

@davidberenstein1957
Copy link
Copy Markdown
Collaborator

Summary

  • Adds tracker-facing minimal telemetry on top of the shared TelemetryClient / TelemetryCreate stack from feat: first version of telemetry #1171
  • Wires send_telemetry (opt-out, default on) into BaseEmissionsTracker and OfflineEmissionsTracker with once-per-session deduplication
  • Maps tracker conf to the minimal API schema (timestamp, telemetry_level, hardware/geo fields, ram_total_size_gb, etc.)

Builds on PR #1171 (feat/telemetry-backend) — intended to merge into that branch before master.

Test plan

  • uv run pytest tests/test_telemetry.py tests/test_telemetry_client.py
  • Run carbonserver telemetry API tests after DB migration exists
  • Manual smoke: start EmissionsTracker(send_telemetry=True) and confirm POST /telemetry returns 201 against local API

Made with Cursor

davidberenstein1957 and others added 6 commits April 29, 2026 19:44
- Move hardcoded TELEMETRY_API_KEY to environment variable with fallback
- Add comment clarifying it's a public key with limited permissions
- Add module docstring describing Tier 1 and Tier 2 telemetry
- Improve type hints on collect_tier1_payload (dict[str, Any])
- Remove unused 'import sys' from test_telemetry.py
- Convert test from function-based to class-based unittest.TestCase style
- All tests passing
- Implement send_tier1_telemetry() function that POSTs environment metadata
  to the telemetry API once per session
- Module-level _TIER1_SENT flag ensures deduplication (returns False on
  duplicate calls within same session)
- Silent failure handling: catches all exceptions, logs error, returns False
- Tests verify: deduplication prevents multiple POSTs, exceptions are caught
  and logged without being raised

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Align Tier 1 send path with PR #1171 schemas and HTTP 201 contract while
keeping session dedup and emissions tracker opt-out wiring.

Co-authored-by: Cursor <cursoragent@cursor.com>
@davidberenstein1957 davidberenstein1957 requested a review from a team as a code owner May 19, 2026 18:01
@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

❌ Patch coverage is 78.40671% with 103 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.00%. Comparing base (39eef0b) to head (adb50c3).

Files with missing lines Patch % Lines
codecarbon/cli/telemetry_cli.py 53.63% 51 Missing ⚠️
codecarbon/core/telemetry/collect.py 76.88% 43 Missing ⚠️
codecarbon/core/telemetry/client.py 84.84% 5 Missing ⚠️
codecarbon/core/telemetry/dispatcher.py 89.47% 4 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                     @@
##           feat/telemetry-backend    #1200      +/-   ##
==========================================================
- Coverage                   88.98%   88.00%   -0.98%     
==========================================================
  Files                          47       52       +5     
  Lines                        4411     4826     +415     
==========================================================
+ Hits                         3925     4247     +322     
- Misses                        486      579      +93     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…ved session handling

- Introduced `build_minimal_telemetry_dict` to create a minimal telemetry payload from tracker configuration.
- Updated `send_tier1_telemetry` to utilize the new payload builder and handle optional API endpoint URLs.
- Refactored tests to validate the new telemetry structure and ensure deduplication logic remains intact.
- Improved error handling and logging for telemetry sending failures.
…ion handling

- Added support for telemetry level configuration via `.codecarbon.config`, allowing users to specify tiers (disabled, minimal, extensive).
- Introduced `get_config_file_settings` to read configuration settings from global and local files.
- Updated `BaseEmissionsTracker` to utilize the new telemetry level parameter, improving flexibility in telemetry data collection.
- Refactored telemetry sending methods to handle new configurations and ensure proper logging and error handling.
- Enhanced tests to cover new telemetry configurations and ensure correct behavior across different scenarios.
- Updated `resolve_telemetry_level` to incorporate external configuration, allowing for more flexible telemetry tier resolution.
- Improved handling of telemetry level overrides from environment variables and configuration files.
- Enhanced documentation to clarify telemetry configuration options and resolution order.
- Added tests to validate new behavior for telemetry level resolution and ensure correct precedence of settings.
- Updated warning message for amdsmi configuration issues to provide clearer guidance on potential causes and solutions.
- Adjusted test to reflect the new warning message format, ensuring accurate validation of GPU import warnings.
- Added new fields to the TelemetryBase schema, including `on_cloud`, `python_package_manager`, `integration_surface`, `offline_mode`, and `save_to_api_enabled`.
- Updated MINIMAL_TELEMETRY_FIELDS to include new fields for improved telemetry data collection.
- Refactored telemetry sending methods to utilize the new schema and ensure accurate data transmission.
- Enhanced tests to validate the new telemetry fields and their integration into the existing telemetry framework.
- Replaced MINIMAL_TELEMETRY_FIELDS with a dynamic frozenset that excludes privacy-related fields.
- Introduced EXCLUDED_PRIVACY_TELEMETRY_FIELDS to ensure sensitive data is not collected.
- Updated telemetry payload building methods to accept a telemetry level parameter, allowing for more flexible data handling.
- Enhanced tests to validate the acceptance of framework versions and rejection of privacy fields in telemetry submissions.
- Improved documentation to clarify the changes in telemetry data collection and privacy considerations.
- Removed sensitive fields from the TelemetryBase schema, including `longitude`, `latitude`, and `python_executable_hash`.
- Consolidated privacy-related field management into a single frozenset, `PRIVATE_TELEMETRY_FIELDS`.
- Updated telemetry payload building methods to reflect the new schema and ensure compliance with privacy standards.
- Refactored tests to validate the rejection of unknown fields and ensure proper handling of telemetry submissions.
- Improved documentation to clarify the changes in telemetry data collection and privacy considerations.
- Removed version tracking for ML frameworks from telemetry payload to improve privacy.
- Updated the framework packages list to exclude version fields, focusing solely on presence flags.
- Adjusted telemetry context collection to reflect the changes in framework detection.
- Modified tests to ensure the telemetry payload omits framework versions as intended.
- Updated documentation to clarify the changes in ML stack data collection.
- Replaced the TelemetryClient class with a standalone function, post_private_telemetry, for sending telemetry data.
- Updated telemetry payload construction to improve clarity and maintainability.
- Refactored telemetry warning handling to ensure it is shown only once during execution.
- Enhanced tests to validate the new telemetry sending method and ensure proper payload structure.
- Improved error handling and logging for telemetry requests, including handling of connection errors and HTTP response statuses.
- Replaced the existing telemetry client and settings management with a more modular approach, introducing a dedicated Telemetry class for dispatching telemetry data.
- Consolidated telemetry payload construction and validation into a streamlined process, enhancing clarity and maintainability.
- Removed deprecated telemetry client and settings files, ensuring a cleaner codebase.
- Updated telemetry context collection to improve data accuracy and privacy compliance.
- Enhanced tests to validate the new telemetry structure and ensure proper functionality across various scenarios.
- Reorganized imports in emissions_tracker.py for clarity and consistency.
- Simplified the definition of CONTAINER_RUNTIME_ENV in collect.py for better readability.
- Enhanced formatting in settings.py and various test files to maintain consistent code style.
- Updated test cases to improve readability and ensure proper mocking of telemetry functions.
…ment

- Added a new telemetry module to handle product telemetry at tracker stop, including necessary imports and payload construction.
- Updated tests to ensure proper handling of telemetry levels and error management with the Typer library.
- Improved clarity and organization of telemetry-related code for better maintainability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant