Skip to content
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

Tweak OTel timestamp utils #3436

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions sentry_sdk/integrations/opentelemetry/potel_span_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sentry_sdk import capture_event
from sentry_sdk.integrations.opentelemetry.utils import (
is_sentry_span,
convert_otel_timestamp,
convert_from_otel_timestamp,
extract_span_data,
)
from sentry_sdk.integrations.opentelemetry.consts import (
Expand Down Expand Up @@ -141,8 +141,8 @@ def _root_span_to_transaction_event(self, span):
# TODO-neel-potel tx source based on integration
"transaction_info": {"source": "custom"},
"contexts": contexts,
"start_timestamp": convert_otel_timestamp(span.start_time),
"timestamp": convert_otel_timestamp(span.end_time),
"start_timestamp": convert_from_otel_timestamp(span.start_time),
"timestamp": convert_from_otel_timestamp(span.end_time),
} # type: Event

return event
Expand All @@ -168,8 +168,8 @@ def _span_to_json(self, span):
"op": op,
"description": description,
"status": status,
"start_timestamp": convert_otel_timestamp(span.start_time),
"timestamp": convert_otel_timestamp(span.end_time),
"start_timestamp": convert_from_otel_timestamp(span.start_time),
"timestamp": convert_from_otel_timestamp(span.end_time),
"origin": origin or SPAN_ORIGIN,
} # type: dict[str, Any]

Expand Down
13 changes: 11 additions & 2 deletions sentry_sdk/integrations/opentelemetry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sentry_sdk._types import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Optional, Mapping, Sequence
from typing import Optional, Mapping, Sequence, Union


GRPC_ERROR_MAP = {
Expand Down Expand Up @@ -72,11 +72,20 @@ def is_sentry_span(span):
return False


def convert_otel_timestamp(time):
def convert_from_otel_timestamp(time):
# type: (int) -> datetime
"""Convert an OTel nanosecond-level timestamp to a datetime."""
return datetime.fromtimestamp(time / 1e9, timezone.utc)


def convert_to_otel_timestamp(time):
# type: (Union[datetime.datetime, float]) -> int
"""Convert a datetime to an OTel timestamp (with nanosecond precision)."""
if isinstance(time, datetime):
return int(time.timestamp() * 1e9)
return int(time * 1e9)


def extract_span_data(span):
# type: (ReadableSpan) -> tuple[str, str, Optional[str], Optional[int], Optional[str]]
op = span.name
Expand Down
Loading