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
1 change: 1 addition & 0 deletions sentry_sdk/_log_batcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def format_attribute(val: "int | float | str | bool") -> "Any":
res = {
"timestamp": int(log["time_unix_nano"]) / 1.0e9,
"trace_id": log.get("trace_id", "00000000-0000-0000-0000-000000000000"),
"span_id": log.get("span_id"),
"level": str(log["severity_text"]),
"body": str(log["body"]),
"attributes": {
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class SDKInfo(TypedDict):
"attributes": dict[str, str | bool | float | int],
"time_unix_nano": int,
"trace_id": Optional[str],
"span_id": Optional[str],
},
)

Expand Down
7 changes: 2 additions & 5 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,11 +927,8 @@ def _capture_log(self, log: "Optional[Log]") -> None:
if trace_id is not None and log.get("trace_id") is None:
log["trace_id"] = trace_id

if (
span_id is not None
and "sentry.trace.parent_span_id" not in log["attributes"]
):
log["attributes"]["sentry.trace.parent_span_id"] = span_id
if span_id is not None and log.get("span_id") is None:
log["span_id"] = span_id

# The user, if present, is always set on the isolation scope.
if isolation_scope._user is not None:
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,6 @@ def _capture_log_from_record(
"attributes": attrs,
"time_unix_nano": int(record.created * 1e9),
"trace_id": None,
"span_id": None,
},
)
1 change: 1 addition & 0 deletions sentry_sdk/integrations/loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,6 @@ def loguru_sentry_logs_handler(message: "Message") -> None:
"attributes": attrs,
"time_unix_nano": int(record["time"].timestamp() * 1e9),
"trace_id": None,
"span_id": None,
}
)
1 change: 1 addition & 0 deletions sentry_sdk/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def _capture_log(
"body": body,
"time_unix_nano": time.time_ns(),
"trace_id": None,
"span_id": None,
},
)

Expand Down
7 changes: 3 additions & 4 deletions tests/integrations/logging/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ def test_logger_with_all_attributes(sentry_init, capture_envelopes):

logs = envelopes_to_logs(envelopes)

assert "span_id" in logs[0]
assert isinstance(logs[0]["span_id"], str)

attributes = logs[0]["attributes"]

assert "process.pid" in attributes
Expand Down Expand Up @@ -478,10 +481,6 @@ def test_logger_with_all_attributes(sentry_init, capture_envelopes):

assert attributes.pop("sentry.sdk.name").startswith("sentry.python")

assert "sentry.trace.parent_span_id" in attributes
assert isinstance(attributes["sentry.trace.parent_span_id"], str)
del attributes["sentry.trace.parent_span_id"]

# Assert on the remaining non-dynamic attributes.
assert attributes == {
"foo": "bar",
Expand Down
7 changes: 3 additions & 4 deletions tests/integrations/loguru/test_loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ def test_logger_with_all_attributes(

logs = envelopes_to_logs(envelopes)

assert "span_id" in logs[0]
assert isinstance(logs[0]["span_id"], str)

attributes = logs[0]["attributes"]

assert "process.pid" in attributes
Expand Down Expand Up @@ -458,10 +461,6 @@ def test_logger_with_all_attributes(

assert attributes.pop("sentry.sdk.name").startswith("sentry.python")

assert "sentry.trace.parent_span_id" in attributes
assert isinstance(attributes["sentry.trace.parent_span_id"], str)
del attributes["sentry.trace.parent_span_id"]

# Assert on the remaining non-dynamic attributes.
assert attributes == {
"logger.name": "tests.integrations.loguru.test_loguru",
Expand Down
14 changes: 8 additions & 6 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def envelopes_to_logs(envelopes: List[Envelope]) -> List[Log]:
"attributes": otel_attributes_to_dict(log_json["attributes"]),
"time_unix_nano": int(float(log_json["timestamp"]) * 1e9),
"trace_id": log_json["trace_id"],
"span_id": log_json["span_id"],
} # type: Log

res.append(log)
return res

Expand Down Expand Up @@ -142,6 +144,7 @@ def _before_log(record, hint):
"attributes",
"time_unix_nano",
"trace_id",
"span_id",
}

if record["severity_text"] in ["fatal", "error"]:
Expand Down Expand Up @@ -319,7 +322,9 @@ def test_logs_tied_to_transactions(sentry_init, capture_envelopes):

get_client().flush()
logs = envelopes_to_logs(envelopes)
assert logs[0]["attributes"]["sentry.trace.parent_span_id"] == trx.span_id

assert "span_id" in logs[0]
assert logs[0]["span_id"] == trx.span_id


@minimum_python_37
Expand All @@ -336,7 +341,7 @@ def test_logs_tied_to_spans(sentry_init, capture_envelopes):

get_client().flush()
logs = envelopes_to_logs(envelopes)
assert logs[0]["attributes"]["sentry.trace.parent_span_id"] == span.span_id
assert logs[0]["span_id"] == span.span_id


def test_auto_flush_logs_after_100(sentry_init, capture_envelopes):
Expand Down Expand Up @@ -491,6 +496,7 @@ def record_lost_event(reason, data_category=None, item=None, *, quantity=1):
"level": "info",
"timestamp": mock.ANY,
"trace_id": mock.ANY,
"span_id": mock.ANY,
"attributes": {
"sentry.environment": {
"type": "string",
Expand All @@ -516,10 +522,6 @@ def record_lost_event(reason, data_category=None, item=None, *, quantity=1):
"type": "string",
"value": "info",
},
"sentry.trace.parent_span_id": {
"type": "string",
"value": mock.ANY,
},
"server.address": {
"type": "string",
"value": "test-server",
Expand Down
Loading