Skip to content

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

Closed
Alicepoltora wants to merge 1 commit into
genlayerlabs:v0.123-devfrom
Alicepoltora:fix/usage-metrics-datetime-utcnow
Closed

Alicepoltora wants to merge 1 commit into
genlayerlabs:v0.123-devfrom
Alicepoltora:fix/usage-metrics-datetime-utcnow

Conversation

@Alicepoltora

@Alicepoltora Alicepoltora commented Sep 26, 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
    • Usage records created without a timestamp now receive a timezone-aware UTC timestamp.

…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.
@github-actions
github-actions Bot changed the base branch from main to v0.123-dev September 26, 2026 21:22
@github-actions

Copy link
Copy Markdown
Contributor

This PR targeted main, which is only the default/static branch.

I retargeted it to v0.123-dev, the active development branch. Pushes to v0.123-dev automatically fast-forward main.

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

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

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: a34ef6db-5d50-4f4d-a735-829d2f1dee4f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ 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