From 4ac884f9bd407597515c36070049a7313a08bb62 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 12 Aug 2024 15:00:41 +0200 Subject: [PATCH 1/2] Tweak OTel timestamp utils --- .../opentelemetry/potel_span_processor.py | 10 +++++----- sentry_sdk/integrations/opentelemetry/utils.py | 13 +++++++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/potel_span_processor.py b/sentry_sdk/integrations/opentelemetry/potel_span_processor.py index 9604676dce..b63cc47ff7 100644 --- a/sentry_sdk/integrations/opentelemetry/potel_span_processor.py +++ b/sentry_sdk/integrations/opentelemetry/potel_span_processor.py @@ -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 ( @@ -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 @@ -169,8 +169,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), } # type: dict[str, Any] if parent_span_id: diff --git a/sentry_sdk/integrations/opentelemetry/utils.py b/sentry_sdk/integrations/opentelemetry/utils.py index df668799cf..47a2a08822 100644 --- a/sentry_sdk/integrations/opentelemetry/utils.py +++ b/sentry_sdk/integrations/opentelemetry/utils.py @@ -13,7 +13,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 = { @@ -71,11 +71,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 ns-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 ns 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]] op = span.name From 9405a0d5ad19101049e6ffab033be13b62d54f61 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 12 Aug 2024 16:02:26 +0200 Subject: [PATCH 2/2] . --- sentry_sdk/integrations/opentelemetry/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/utils.py b/sentry_sdk/integrations/opentelemetry/utils.py index 47a2a08822..15e5cf3871 100644 --- a/sentry_sdk/integrations/opentelemetry/utils.py +++ b/sentry_sdk/integrations/opentelemetry/utils.py @@ -73,13 +73,13 @@ def is_sentry_span(span): def convert_from_otel_timestamp(time): # type: (int) -> datetime - """Convert an OTel ns-level timestamp to a 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 ns precision).""" + """Convert a datetime to an OTel timestamp (with nanosecond precision).""" if isinstance(time, datetime): return int(time.timestamp() * 1e9) return int(time * 1e9)