Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion sentry_sdk/integrations/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING

import sentry_sdk
from sentry_sdk.api import get_current_scope
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import Integration
from sentry_sdk.scope import add_global_event_processor
Expand Down Expand Up @@ -52,7 +53,18 @@
def add_python_runtime_context(
event: "Event", hint: "Hint"
) -> "Optional[Event]":
if sentry_sdk.get_client().get_integration(StdlibIntegration) is not None:
client = sentry_sdk.get_client()
if client.get_integration(StdlibIntegration) is not None:
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
if is_span_streaming_enabled:
current_scope = get_current_scope()
segment = getattr(current_scope.streamed_span, "_segment", None)

if segment:
segment.set_attribute(
"process.runtime.description", sys.version
Comment thread
ericapisani marked this conversation as resolved.
Outdated
)

Check warning on line 66 in sentry_sdk/integrations/stdlib.py

View check run for this annotation

@sentry/warden / warden: code-review

process.runtime.description only set on segment as side effect of event capture, not unconditionally like the other runtime attributes

Mutating a span attribute inside `add_global_event_processor` means `process.runtime.description` is only stamped on the segment when at least one non-span event (error, log, etc.) is captured during the trace. Traces that complete without any captured events will be missing this attribute, while `process.runtime.name` and `process.runtime.version` are always present via `scope.set_global_attributes()`. Consider adding `process.runtime.description` to `set_global_attributes()` in `scope.py` alongside the other two runtime attributes instead.

Check warning on line 66 in sentry_sdk/integrations/stdlib.py

View check run for this annotation

@sentry/warden / warden: find-bugs

`process.runtime.description` never set on segments that complete without error events

Setting `process.runtime.description` inside a global event processor means it is only applied to the currently active segment when an error event fires; segments that complete normally (without any captured errors) will never receive this attribute, making the three runtime attributes inconsistent.
Comment thread
ericapisani marked this conversation as resolved.
Outdated
Comment thread
ericapisani marked this conversation as resolved.
Outdated
Comment thread
ericapisani marked this conversation as resolved.
Outdated
Comment thread
ericapisani marked this conversation as resolved.
Outdated

contexts = event.setdefault("contexts", {})
if isinstance(contexts, dict) and "runtime" not in contexts:
contexts["runtime"] = _RUNTIME_CONTEXT
Expand Down
10 changes: 10 additions & 0 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import sys
import warnings
from collections import deque
Expand Down Expand Up @@ -379,6 +380,15 @@ def set_global_attributes(self) -> None:
self.set_attribute(SPANDATA.SENTRY_SDK_NAME, SDK_INFO["name"])
self.set_attribute(SPANDATA.SENTRY_SDK_VERSION, SDK_INFO["version"])

self.set_attribute(
"process.runtime.name",
platform.python_implementation(),
)
self.set_attribute(
"process.runtime.version",
f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
)

options = sentry_sdk.get_client().options

server_name = options.get("server_name")
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/logging/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import platform
import sys
import warnings

import pytest
Expand Down Expand Up @@ -537,6 +539,8 @@ def test_logger_with_all_attributes(sentry_init, capture_items):
"logger.name": "test-logger",
"sentry.origin": "auto.log.stdlib",
"sentry.message.template": "log #%d",
"process.runtime.name": platform.python_implementation(),
"process.runtime.version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
"sentry.message.parameter.0": 1,
"sentry.environment": "production",
"sentry.sdk.version": VERSION,
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/loguru/test_loguru.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import platform
import re
import sys
from unittest.mock import MagicMock, patch

import pytest
Expand Down Expand Up @@ -466,6 +468,8 @@ def test_logger_with_all_attributes(
"logger.name": "tests.integrations.loguru.test_loguru",
"sentry.origin": "auto.log.loguru",
"sentry.environment": "production",
"process.runtime.name": platform.python_implementation(),
"process.runtime.version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
"sentry.sdk.version": VERSION,
"sentry.severity_number": 13,
"sentry.severity_text": "warn",
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import platform
import sys

import pytest

Expand Down Expand Up @@ -4975,6 +4977,8 @@ async def test_ai_client_span_streaming_responses_async_api(
"sentry.environment": "production",
"sentry.op": "gen_ai.responses",
"sentry.origin": "auto.ai.openai",
"process.runtime.name": platform.python_implementation(),
"process.runtime.version": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
"sentry.release": mock.ANY,
"sentry.sdk.name": "sentry.python",
"sentry.sdk.version": mock.ANY,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import platform
import sys
import time
from unittest import mock
Expand Down Expand Up @@ -495,6 +496,14 @@ def test_transport_format(sentry_init, capture_envelopes):
"type": "integer",
"value": 13,
},
"process.runtime.name": {
"type": "string",
"value": platform.python_implementation(),
},
"process.runtime.version": {
"type": "string",
"value": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
},
"sentry.severity_text": {
"type": "string",
"value": "warn",
Expand Down Expand Up @@ -574,6 +583,14 @@ def record_lost_event(reason, data_category=None, item=None, *, quantity=1):
"type": "integer",
"value": 9,
},
"process.runtime.name": {
"type": "string",
"value": platform.python_implementation(),
},
"process.runtime.version": {
"type": "string",
"value": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
},
"sentry.severity_text": {
"type": "string",
"value": "info",
Expand Down
9 changes: 9 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import sys
from unittest import mock

Expand Down Expand Up @@ -284,6 +285,14 @@ def test_transport_format(sentry_init, capture_envelopes):
"type": "string",
"value": "1.0.0",
},
"process.runtime.name": {
"type": "string",
"value": platform.python_implementation(),
},
"process.runtime.version": {
"type": "string",
"value": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
},
"sentry.sdk.name": {
"type": "string",
"value": mock.ANY,
Expand Down
9 changes: 9 additions & 0 deletions tests/tracing/test_span_streaming.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import platform
import re
import sys
import time
Expand Down Expand Up @@ -1722,4 +1723,12 @@ def test_default_attributes(sentry_init, capture_envelopes):
"sentry.dist": {"value": "1.0", "type": "string"},
"sentry.origin": {"value": "manual", "type": "string"},
"sentry.sdk.integrations": {"value": mock.ANY, "type": "array"},
"process.runtime.name": {
"type": "string",
"value": platform.python_implementation(),
},
"process.runtime.version": {
"type": "string",
"value": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
},
}
Loading