Skip to content

fix: span duration property has inconsistent time formats#245

Merged
monoxgas merged 2 commits into
mainfrom
mk/bug_span_duration
Dec 2, 2025
Merged

fix: span duration property has inconsistent time formats#245
monoxgas merged 2 commits into
mainfrom
mk/bug_span_duration

Conversation

@mkultraWasHere
Copy link
Copy Markdown
Contributor

@mkultraWasHere mkultraWasHere commented Dec 1, 2025

Bug Fix - span.duration() has inconsistent timestamp format

The Problem

end_time = self.end_time or time.time()
return (end_time - self.start_time) if self.start_time else 0.0

self.start_time and self.end_time come from OpenTelemetry and are in nanoseconds
time.time() returns seconds

When the span is still running (self.end_time is None), the fallback time.time() returns a value ~10⁹ times smaller than self.start_time, producing a negative or nonsensical duration.

Example

self.start_time = 1733000000000000000  # nanoseconds (2024 timestamp)
self.end_time = None                    # span still running
time.time() = 1733000005.0              # seconds
end_time = self.end_time or time.time()  # = 1733000005.0 (seconds)
duration = 1733000005.0 - 1733000000000000000  # = -1.73e18 (WRONG!)

Generated Summary:

  • Updated the duration calculation in the Span class to use time.time_ns() instead of time.time().
  • Changed the return value of the duration to be in seconds by dividing the nanoseconds result by 1e9.
  • Added a check for start_time early in the duration method for better clarity.
  • These changes improve precision in span duration calculation, potentially impacting timing and performance analysis within tracing.

This summary was generated with ❤️ by rigging

Michael Kouremetis added 2 commits December 1, 2025 09:56
@mkultraWasHere mkultraWasHere changed the title [bug] Fix - span duration property has inconsistent time formats Dec 1, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical bug in the Span.duration() method where mixing time formats (nanoseconds from OpenTelemetry vs seconds from time.time()) produced incorrect negative durations for active spans.

Key Changes:

  • Replaced time.time() with time.time_ns() to maintain consistent nanosecond precision
  • Added division by 1e9 to convert the nanosecond result to seconds as documented
  • Refactored the early return logic for better code clarity

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mkultraWasHere mkultraWasHere changed the title Fix - span duration property has inconsistent time formats Fix: span duration property has inconsistent time formats Dec 1, 2025
@mkultraWasHere mkultraWasHere changed the title Fix: span duration property has inconsistent time formats fix: span duration property has inconsistent time formats Dec 1, 2025
@monoxgas monoxgas merged commit 7d91c21 into main Dec 2, 2025
14 of 19 checks passed
@monoxgas monoxgas deleted the mk/bug_span_duration branch December 2, 2025 23:33
mkultraWasHere added a commit that referenced this pull request Dec 4, 2025
* fix

* fix

---------

Co-authored-by: Michael Kouremetis <michael@Michaels-MacBook-Pro.local>
briangreunke added a commit that referenced this pull request Dec 6, 2025
… file (#247)

* chore(deps): update actions/checkout action to v6 (#238)

| datasource  | package          | from   | to     |
| ----------- | ---------------- | ------ | ------ |
| github-tags | actions/checkout | v5.0.1 | v6.0.0 |

Co-authored-by: dreadnode-renovate-bot[bot] <184170622+dreadnode-renovate-bot[bot]@users.noreply.github.com>

* chore(deps): update pre-commit hook pycqa/bandit to v1.9.2 (#241)

| datasource  | package      | from  | to    |
| ----------- | ------------ | ----- | ----- |
| github-tags | PyCQA/bandit | 1.9.1 | 1.9.2 |

Co-authored-by: dreadnode-renovate-bot[bot] <184170622+dreadnode-renovate-bot[bot]@users.noreply.github.com>

* chore(deps): update actions/setup-python action to v6.1.0 (#242)

| datasource  | package              | from   | to     |
| ----------- | -------------------- | ------ | ------ |
| github-tags | actions/setup-python | v6.0.0 | v6.1.0 |

Co-authored-by: dreadnode-renovate-bot[bot] <184170622+dreadnode-renovate-bot[bot]@users.noreply.github.com>

* feat(user-data): Scope credentials fetch by organization and workspace (#232)

* feat(project): add project key for identification and creation (#240)

* feat(project): add project key for identification and creation

* chore: updated docs

* fix: score value race condition (#244)

* bugfix score value race condition

* revert agent change

* fix: span duration property has inconsistent time formats (#245)

* fix

* fix

---------

Co-authored-by: Michael Kouremetis <michael@Michaels-MacBook-Pro.local>

* chore(deps): update actions/checkout action to v6.0.1 (#246)

| datasource  | package          | from   | to     |
| ----------- | ---------------- | ------ | ------ |
| github-tags | actions/checkout | v6.0.0 | v6.0.1 |

Co-authored-by: dreadnode-renovate-bot[bot] <184170622+dreadnode-renovate-bot[bot]@users.noreply.github.com>

* feat(dataset): add push, save-to-disk, and refactor API

---------

Co-authored-by: dreadnode-renovate-bot[bot] <184170622+dreadnode-renovate-bot[bot]@users.noreply.github.com>
Co-authored-by: Vincent Abruzzo <6225496+vabruzzo@users.noreply.github.com>
Co-authored-by: Michael Kouremetis <mkouremetis16@gmail.com>
Co-authored-by: Michael Kouremetis <michael@Michaels-MacBook-Pro.local>
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.

4 participants