-
-
Notifications
You must be signed in to change notification settings - Fork 307
feat: add SDK telemetry into emissions tracker #1200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidberenstein1957
wants to merge
32
commits into
master
Choose a base branch
from
feat/add-telemetry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
0eea792
feat: add telemetry module with Tier 1 payload builder
davidberenstein1957 0347f01
fix: improve code quality for Task 1 (security, docs, types)
davidberenstein1957 0ae7783
feat: add Tier 1 telemetry send with session dedup and silent fail
davidberenstein1957 ed2d3d2
feat: wire Tier 1 telemetry into BaseEmissionsTracker (opt-out via se…
davidberenstein1957 5c568e7
feat: first version of telemetry
inimaz 39eef0b
tests: add a test so that client and server do not differ
inimaz f2ada76
merge: integrate telemetry backend from PR #1171
davidberenstein1957 9e97561
refactor: use TelemetryClient for minimal tracker telemetry
davidberenstein1957 453860f
feat: enhance telemetry module with minimal payload builder and impro…
davidberenstein1957 954910e
feat: enhance telemetry functionality with new configuration and sess…
davidberenstein1957 2a865f6
refactor: enhance telemetry level resolution and configuration handling
davidberenstein1957 1ffc45d
Delete docs/plans/2026-05-19-telemetry-configuration.md
davidberenstein1957 0a319bc
fix: improve error handling and logging for AMD GPU metrics
davidberenstein1957 dcec0c4
feat: enhance telemetry schema and collection methods
davidberenstein1957 1ecc237
refactor: update telemetry schema and enhance data collection
davidberenstein1957 6c873eb
refactor: streamline telemetry schema and enhance privacy measures
davidberenstein1957 e3dca3f
refactor: simplify telemetry framework detection and enhance privacy
davidberenstein1957 de3fff1
refactor: simplify telemetry client and enhance telemetry handling
davidberenstein1957 5a65220
refactor: overhaul telemetry handling and structure
davidberenstein1957 aefea4c
refactor: improve telemetry imports and code formatting
davidberenstein1957 adb50c3
feat: introduce telemetry module and enhance telemetry context manage…
davidberenstein1957 eaa75f1
merge: rebase feat/add-telemetry onto origin/master
davidberenstein1957 74eed94
Align SDK telemetry with server schema and simplify collection
davidberenstein1957 29664d7
Apply pre-commit formatting for telemetry files
davidberenstein1957 a231750
Fix CodeQL logging alert and raise telemetry test coverage
davidberenstein1957 edb2d03
Apply black formatting to telemetry schema tests
davidberenstein1957 13cde15
Potential fix for pull request finding 'CodeQL / Clear-text logging o…
davidberenstein1957 c5e6c4a
Merge branch 'master' into feat/add-telemetry
davidberenstein1957 2145b61
Raise PR coverage for telemetry CLI and API helpers
davidberenstein1957 ea4f275
Refactor telemetry schema docstring
davidberenstein1957 8b995d3
Update telemetry configuration in emissions tracker
davidberenstein1957 08cc67c
Add telemetry experiment ID and default settings integration
davidberenstein1957 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../shared/telemetry_defaults.py |
164 changes: 164 additions & 0 deletions
164
carbonserver/tests/api/integration/test_telemetry_local_api.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| """Integration tests for telemetry against a running local carbonserver API.""" | ||
|
|
||
| import os | ||
| import sys | ||
| import uuid | ||
| from datetime import datetime, timezone | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| import requests | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[4] | ||
| sys.path.insert(0, str(REPO_ROOT)) | ||
|
|
||
| from codecarbon.core.telemetry.client import post_private, post_public_summary | ||
| from codecarbon.core.telemetry.collect import TelemetryContext, build_payload | ||
| from codecarbon.core.telemetry.defaults import ( | ||
| DEFAULT_TELEMETRY_API_KEY, | ||
| DEFAULT_TELEMETRY_EXPERIMENT_ID, | ||
| ) | ||
| from codecarbon.core.telemetry.schemas import TelemetryLevel | ||
| from codecarbon.core.telemetry.settings import TelemetrySettings | ||
| from codecarbon.output_methods.emissions_data import EmissionsData | ||
|
|
||
| URL = os.getenv("CODECARBON_API_URL") | ||
| if URL is None: | ||
| pytest.exit("CODECARBON_API_URL is not defined (e.g. http://localhost:8008)") | ||
|
|
||
|
|
||
| def _api_url(path: str) -> str: | ||
| base = URL.rstrip("/") | ||
| if not base.endswith("/api"): | ||
| base = f"{base}/api" | ||
| return f"{base}{path}" | ||
|
|
||
|
|
||
| def _local_settings() -> TelemetrySettings: | ||
| return TelemetrySettings.resolve( | ||
| external_conf={ | ||
| "telemetry_level": "extensive", | ||
| "telemetry_api_url": URL.rstrip("/"), | ||
| "telemetry_api_key": DEFAULT_TELEMETRY_API_KEY, | ||
| "telemetry_experiment_id": DEFAULT_TELEMETRY_EXPERIMENT_ID, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _sample_emissions() -> EmissionsData: | ||
| return EmissionsData( | ||
| timestamp="2026-01-01T00:00:00", | ||
| project_name="telemetry-local", | ||
| run_id="local-run", | ||
| experiment_id=DEFAULT_TELEMETRY_EXPERIMENT_ID, | ||
| duration=10.0, | ||
| emissions=0.001, | ||
| emissions_rate=0.0001, | ||
| cpu_power=0.0, | ||
| gpu_power=0.0, | ||
| ram_power=0.0, | ||
| cpu_energy=0.0, | ||
| gpu_energy=0.0, | ||
| ram_energy=0.0, | ||
| energy_consumed=0.01, | ||
| water_consumed=0.0, | ||
| country_name="France", | ||
| country_iso_code="FRA", | ||
| region="idf", | ||
| cloud_provider="", | ||
| cloud_region="", | ||
| os="Linux", | ||
| python_version="3.12", | ||
| codecarbon_version="3.2.8", | ||
| cpu_count=4, | ||
| cpu_model="test-cpu", | ||
| gpu_count=0, | ||
| gpu_model="", | ||
| longitude=0.0, | ||
| latitude=0.0, | ||
| ram_total_size=16.0, | ||
| tracking_mode="process", | ||
| ) | ||
|
|
||
|
|
||
| def test_local_api_is_up(): | ||
| response = requests.get(URL.rstrip("/") + "/", timeout=5) | ||
| assert response.status_code == 200 | ||
| assert response.json()["status"] == "OK" | ||
|
|
||
|
|
||
| def test_local_telemetry_post_accepts_sdk_payload(): | ||
| settings = _local_settings() | ||
| payload = build_payload( | ||
| TelemetryContext( | ||
| conf={ | ||
| "os": "Linux-5.10.0-x86_64", | ||
| "codecarbon_version": "3.2.8", | ||
| "cpu_count": 4, | ||
| "python_version": "3.12", | ||
| "tracking_mode": "process", | ||
| }, | ||
| emissions=_sample_emissions(), | ||
| hardware=[], | ||
| resource_tracker=None, | ||
| output_methods=[], | ||
| tasks={}, | ||
| measure_power_secs=15, | ||
| integration="library", | ||
| ), | ||
| level=TelemetryLevel.minimal, | ||
| ) | ||
| assert post_private(settings, payload) is True | ||
|
|
||
|
|
||
| def test_local_extensive_run_and_emission_flow(): | ||
| settings = _local_settings() | ||
| run_payload = { | ||
| "timestamp": datetime.now(timezone.utc).isoformat(), | ||
| "experiment_id": settings.experiment_id, | ||
| "os": "Linux", | ||
| "python_version": "3.12", | ||
| "codecarbon_version": "3.2.8", | ||
| "cpu_count": 4, | ||
| "tracking_mode": "process", | ||
| } | ||
| run_response = requests.post( | ||
| _api_url("/runs"), | ||
| json=run_payload, | ||
| headers={"x-api-token": settings.api_key}, | ||
| timeout=10, | ||
| ) | ||
| assert run_response.status_code == 201, run_response.text | ||
| run_id = run_response.json()["id"] | ||
|
|
||
| emission_payload = { | ||
| "timestamp": datetime.now(timezone.utc).isoformat(), | ||
| "run_id": run_id, | ||
| "duration": 10, | ||
| "emissions_sum": 0.001, | ||
| "emissions_rate": 0.0001, | ||
| "cpu_power": 0.0, | ||
| "gpu_power": 0.0, | ||
| "ram_power": 0.0, | ||
| "cpu_energy": 0.0, | ||
| "gpu_energy": 0.0, | ||
| "ram_energy": 0.0, | ||
| "energy_consumed": 0.01, | ||
| "wue": 0, | ||
| } | ||
| emission_response = requests.post( | ||
| _api_url("/emissions"), | ||
| json=emission_payload, | ||
| headers={"x-api-token": settings.api_key}, | ||
| timeout=10, | ||
| ) | ||
| assert emission_response.status_code == 201, emission_response.text | ||
| assert uuid.UUID(emission_response.json()) | ||
|
|
||
|
|
||
| def test_local_post_public_summary_helper(): | ||
| settings = _local_settings() | ||
| assert ( | ||
| post_public_summary(settings, {"os": "Linux", "tracking_mode": "process"}, _sample_emissions()) | ||
| is True | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is not needed as discussed