Skip to content

fix(usage-metrics): replace deprecated datetime.utcnow() with timezone-aware datetime.now(timezone.utc) - #1779

Closed
Alicepoltora wants to merge 1 commit into
genlayerlabs:mainfrom
Alicepoltora:fix/usage-metrics-datetime-utcnow
Closed

Alicepoltora wants to merge 1 commit into
genlayerlabs:mainfrom
Alicepoltora:fix/usage-metrics-datetime-utcnow

Conversation

@Alicepoltora

@Alicepoltora Alicepoltora commented Sep 27, 2026 •

Copy link
Copy Markdown

Problem

_format_created_at() in UsageMetricsService used datetime.utcnow() to produce the fallback ISO 8601 timestamp when created_at is None. datetime.utcnow() was deprecated in Python 3.12 and returns a naive datetime (no timezone). The returned string has no offset (e.g. 2024-01-01T12:00:00.123456), which is inconsistent with every other timestamp in Studio (all others are +00:00-aware).

Fix

Replace datetime.utcnow().isoformat() with datetime.now(timezone.utc).isoformat() so the fallback string carries a timezone offset (+00:00).

Tests

  • tests/unit/test_usage_metrics_service.py: 2 new regression tests:
    • test_format_created_at_returns_utc_isoformat — fails on main (naive datetime has no + offset), passes with the fix
    • test_format_created_at_returns_original_datetime_isoformat — ensures datetime objects retain their original format

Summary by CodeRabbit

  • Bug Fixes
    • Automatically generated usage-metric timestamps now include an explicit UTC timezone offset, making their timezone clear. Existing timestamps retain their previous formatting.
  • Tests
    • Added coverage to verify the timezone offset for generated timestamps and confirm existing timestamps are formatted consistently.

…e-aware datetime.now(timezone.utc)

datetime.utcnow() was removed in Python 3.12 and returned a naive datetime
(no timezone). _format_created_at() should emit ISO 8601 strings that carry
timezone info (+00:00) so the upstream metrics API can parse the offset
consistently with the rest of Studio.

The change replaces:
    datetime.utcnow().isoformat()
with:
    datetime.now(timezone.utc).isoformat()

Regression tests verify the returned string contains a timezone offset.
@coderabbitai

coderabbitai Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 148ea16c-9ce7-4d3b-8c6d-7748da574fa9

📥 Commits

Reviewing files that changed from the base of the PR and between c940729 and ee71e36.

📒 Files selected for processing (2)
  • backend/services/usage_metrics_service.py
  • tests/unit/test_usage_metrics_service.py
 ______________________________
< Here's looking at you, code. >
 ------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Alicepoltora
Alicepoltora deleted the fix/usage-metrics-datetime-utcnow branch September 27, 2026 18:21
@Alicepoltora
Alicepoltora restored the fix/usage-metrics-datetime-utcnow branch September 27, 2026 18:21
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