fix(api): stamp emissions with measurement time, not send time - #1341
Closed
davidberenstein1957 wants to merge 1 commit into
Closed
fix(api): stamp emissions with measurement time, not send time#1341davidberenstein1957 wants to merge 1 commit into
davidberenstein1957 wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1341 +/- ##
==========================================
- Coverage 91.43% 91.40% -0.04%
==========================================
Files 49 49
Lines 5057 5062 +5
==========================================
+ Hits 4624 4627 +3
- Misses 433 435 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
davidberenstein1957
marked this pull request as ready for review
August 12, 2026 17:37
This was referenced Aug 12, 2026
ApiClient.add_emission discarded carbon_emission["timestamp"] and called get_datetime_with_timezone() instead, so every row stored the moment the payload was built rather than the moment it was measured. Harmless while the two are milliseconds apart; wrong by the full latency as soon as a send is slow, retried or queued. Normalise the measurement timestamp to offset-aware instead, falling back to now when the payload carries none or an unparseable one (the method is public and takes a plain dict). CSV output is untouched. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
davidberenstein1957
force-pushed
the
scaling/01-emission-timestamps
branch
from
August 19, 2026 09:18
0658642 to
697e1e8
Compare
Collaborator
Author
|
Closing as folded into #1340. These two conflicted textually in codecarbon/core/api_client.py — both inserted a module-level block immediately after get_datetime_with_timezone() and both touched add_emission — so one had to rebase onto the other regardless. Since both are small and both are about the API client behaving correctly, they are now one review instead of a review, a rebase and a second review. The measurement-timestamp fix and its test came across unchanged. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of #1338.
ApiClient.add_emissionbuiltEmissionCreate(timestamp=get_datetime_with_timezone(), ...), ignoring thetimestampthe row already carries from measurement time (emissions_tracker.py:1047). So the hosted API and dashboard dated every point by receipt, and already disagreed with the CSV output for the same run.Proof
End-to-end repro against a real stdlib HTTP server, with
requests.postwrapped in a 5s delay, driven throughCodeCarbonAPIOutput.live_out:Unit test
test_add_emission_keeps_measurement_timestampis red before, green after.One deviation worth flagging
The original proposal preferred fixing this at the producer, by making
EmissionsData.timestampoffset-aware. That changes thetimestampcolumn format in everyone'semissions.csv— a user-visible output change for a bug that lives entirely in the API client. This takes the proposal's own fallback instead: normalise insideapi_client, leave CSV untouched.The fallback around the conversion is deliberately broad (
KeyError/TypeError/ValueError→ now), becauseadd_emissionis a public dict-taking method and an existing test already passestimestamp="222"._create_runstill stamps "now", which is correct there; there's now a comment saying so, so it doesn't get "fixed".Tests
uv run pytest tests/ -q -k "output or http or api or tracker"→ 252 passed, 2 skipped, over three consecutive runs. (tests/test_viz_data.pyexcluded — fails collection on master too,dashnot installed.) One run showed twotest_emissions_tracker_flush/test_logging_outputfailures; they pass consistently on re-run and in isolation on both branches — timing flakes. Ruff clean on both touched files, same as master.Risk
Server-side rows now carry measurement time rather than receipt time, so a dashboard spanning the upgrade sees a one-time discontinuity equal to the old send latency — sub-second in practice.
Not included
Client-side batching, which the original proposal bundled with this.
carbonserverhas only the single-rowPOST /emissions— no/emissions/batch— so the batch path would be dead code that 404s and falls back to the loop it replaces, and the proposal's ownapi_batch_size=1default makes it a no-op for anyone who doesn't opt in. It should land with the server endpoint or not at all.Also left alone: the
return Falsededent inadd_emission(api_client.py:186) — a real separate bug, not something to smuggle into a timestamp commit.🤖 Generated with Claude Code