Skip to content
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
9 changes: 9 additions & 0 deletions sentry_sdk/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
sentry_sdk.init(_experiments={"trace_lifecycle": "stream"}).
"""

import sys
import uuid
import warnings
from datetime import datetime, timedelta, timezone
Expand Down Expand Up @@ -294,6 +295,8 @@ def __init__(
self._start_profile()
self._set_profile_id(get_profiler_id())

self._set_segment_attributes()

self._start()

def __repr__(self) -> str:
Expand Down Expand Up @@ -552,6 +555,12 @@ def _start_profile(self) -> None:

self._continuous_profile = try_profile_lifecycle_trace_start()

def _set_segment_attributes(self) -> None:
if not self._is_segment():
return

self.set_attribute("process.command_args", sys.argv)


class NoOpStreamedSpan(StreamedSpan):
__slots__ = (
Expand Down
63 changes: 51 additions & 12 deletions tests/tracing/test_span_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,46 @@ def test_profile_stops_when_segment_ends(
assert get_profiler_id() is None, "profiler should have stopped"


def test_default_attributes(sentry_init, capture_envelopes):
sentry_init(
server_name="test-server",
release="1.0.0",
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
Comment thread
sentrivana marked this conversation as resolved.

envelopes = capture_envelopes()

with sentry_sdk.traces.start_span(name="test"):
...

sentry_sdk.get_client().flush()

assert len(envelopes) == 1
assert len(envelopes[0].items) == 1
item = envelopes[0].items[0]

assert item.type == "span"
assert item.headers == {
"type": "span",
"item_count": 1,
"content_type": "application/vnd.sentry.items.span.v2+json",
}
assert item.payload.json["items"][0]["attributes"] == {
"thread.id": {"value": mock.ANY, "type": "string"},
"thread.name": {"value": "MainThread", "type": "string"},
"process.command_args": {"value": mock.ANY, "type": "array"},
"sentry.segment.id": {"value": mock.ANY, "type": "string"},
"sentry.segment.name": {"value": "test", "type": "string"},
"sentry.sdk.name": {"value": "sentry.python", "type": "string"},
"sentry.sdk.version": {"value": mock.ANY, "type": "string"},
"server.address": {"value": "test-server", "type": "string"},
"sentry.environment": {"value": "production", "type": "string"},
"sentry.release": {"value": "1.0.0", "type": "string"},
"sentry.origin": {"value": "manual", "type": "string"},
}
Comment thread
cursor[bot] marked this conversation as resolved.


def test_transport_format(sentry_init, capture_envelopes):
sentry_init(
server_name="test-server",
Expand Down Expand Up @@ -1539,18 +1579,17 @@ def test_transport_format(sentry_init, capture_envelopes):
"is_segment": True,
"start_timestamp": mock.ANY,
"end_timestamp": mock.ANY,
"attributes": {
"thread.id": {"value": mock.ANY, "type": "string"},
"thread.name": {"value": "MainThread", "type": "string"},
"sentry.segment.id": {"value": mock.ANY, "type": "string"},
"sentry.segment.name": {"value": "test", "type": "string"},
"sentry.sdk.name": {"value": "sentry.python", "type": "string"},
"sentry.sdk.version": {"value": mock.ANY, "type": "string"},
"server.address": {"value": "test-server", "type": "string"},
"sentry.environment": {"value": "production", "type": "string"},
"sentry.release": {"value": "1.0.0", "type": "string"},
"sentry.origin": {"value": "manual", "type": "string"},
},
"attributes": mock.ANY,
}
]
}
for attribute, value in item.payload.json["items"][0]["attributes"].items():
assert isinstance(attribute, str)

assert isinstance(value, dict)
assert (
len(value) == 2
) # technically, "unit" is also supported, but we don't currently set it anywhere
assert "value" in value
assert "type" in value
assert value["type"] in ("string", "boolean", "integer", "double", "array")
Loading