Skip to content
Draft
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
6 changes: 4 additions & 2 deletions sentry_sdk/integrations/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
from contextlib import nullcontext

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP
from sentry_sdk.integrations import DidNotEnable, Integration
Expand Down Expand Up @@ -152,8 +154,8 @@ async def _task_with_sentry_span_creation() -> "Any":
span_ctx = sentry_sdk.start_span(
name=get_name(coro),
attributes={
"sentry.op": OP.FUNCTION,
"sentry.origin": AsyncioIntegration.origin,
ATTRIBUTE_NAMES.SENTRY_OP: OP.FUNCTION,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AsyncioIntegration.origin,
},
)

Expand Down
26 changes: 15 additions & 11 deletions sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from fnmatch import fnmatch
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.client import BaseClient
from sentry_sdk.integrations import Integration
Expand Down Expand Up @@ -414,7 +416,7 @@ def _capture_log_from_record(
project_root = client.options["project_root"]

attrs: "Any" = self._extra_from_record(record)
attrs["sentry.origin"] = "auto.log.stdlib"
attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] = "auto.log.stdlib"

parameters_set = False
if record.args is not None:
Expand All @@ -438,31 +440,33 @@ def _capture_log_from_record(
if parameters_set and isinstance(record.msg, str):
# only include template if there is at least one
# sentry.message.parameter.X set
attrs["sentry.message.template"] = record.msg
attrs[ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] = record.msg

if record.lineno:
attrs["code.line.number"] = record.lineno
attrs[ATTRIBUTE_NAMES.CODE_LINE_NUMBER] = record.lineno

if record.pathname:
if project_root is not None and record.pathname.startswith(project_root):
attrs["code.file.path"] = record.pathname[len(project_root) + 1 :]
attrs[ATTRIBUTE_NAMES.CODE_FILE_PATH] = record.pathname[
len(project_root) + 1 :
]
else:
attrs["code.file.path"] = record.pathname
attrs[ATTRIBUTE_NAMES.CODE_FILE_PATH] = record.pathname

if record.funcName:
attrs["code.function.name"] = record.funcName
attrs[ATTRIBUTE_NAMES.CODE_FUNCTION_NAME] = record.funcName

if record.thread:
attrs["thread.id"] = record.thread
attrs[ATTRIBUTE_NAMES.THREAD_ID] = record.thread
if record.threadName:
attrs["thread.name"] = record.threadName
attrs[ATTRIBUTE_NAMES.THREAD_NAME] = record.threadName

if record.process:
attrs["process.pid"] = record.process
attrs[ATTRIBUTE_NAMES.PROCESS_PID] = record.process
if record.processName:
attrs["process.executable.name"] = record.processName
attrs[ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME] = record.processName
if record.name:
attrs["logger.name"] = record.name
attrs[ATTRIBUTE_NAMES.LOGGER_NAME] = record.name

sentry_sdk.get_current_scope()._capture_log(
{
Expand Down
24 changes: 14 additions & 10 deletions sentry_sdk/integrations/loguru.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import enum
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.integrations.logging import (
Expand Down Expand Up @@ -158,31 +160,33 @@ def loguru_handler(message: "Message") -> None:
record["level"].no, SEVERITY_TO_OTEL_SEVERITY
)

attrs: "dict[str, Any]" = {"sentry.origin": "auto.log.loguru"}
attrs: "dict[str, Any]" = {ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.log.loguru"}

project_root = client.options["project_root"]
if record.get("file"):
if project_root is not None and record["file"].path.startswith(project_root):
attrs["code.file.path"] = record["file"].path[len(project_root) + 1 :]
attrs[ATTRIBUTE_NAMES.CODE_FILE_PATH] = record["file"].path[
len(project_root) + 1 :
]
else:
attrs["code.file.path"] = record["file"].path
attrs[ATTRIBUTE_NAMES.CODE_FILE_PATH] = record["file"].path

if record.get("line") is not None:
attrs["code.line.number"] = record["line"]
attrs[ATTRIBUTE_NAMES.CODE_LINE_NUMBER] = record["line"]

if record.get("function"):
attrs["code.function.name"] = record["function"]
attrs[ATTRIBUTE_NAMES.CODE_FUNCTION_NAME] = record["function"]

if record.get("thread"):
attrs["thread.name"] = record["thread"].name
attrs["thread.id"] = record["thread"].id
attrs[ATTRIBUTE_NAMES.THREAD_NAME] = record["thread"].name
attrs[ATTRIBUTE_NAMES.THREAD_ID] = record["thread"].id

if record.get("process"):
attrs["process.pid"] = record["process"].id
attrs["process.executable.name"] = record["process"].name
attrs[ATTRIBUTE_NAMES.PROCESS_PID] = record["process"].id
attrs[ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME] = record["process"].name

if record.get("name"):
attrs["logger.name"] = record["name"]
attrs[ATTRIBUTE_NAMES.LOGGER_NAME] = record["name"]

extra = record.get("extra")
if isinstance(extra, dict):
Expand Down
6 changes: 4 additions & 2 deletions sentry_sdk/integrations/rust_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from enum import Enum, auto
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.integrations import Integration
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE
Expand Down Expand Up @@ -204,8 +206,8 @@ def on_new_span(self, attrs: str, span_id: str) -> "Optional[Span]":
sentry_span = sentry_sdk.start_span(
name=sentry_span_name,
attributes={
"sentry.op": "function",
"sentry.origin": self.origin,
ATTRIBUTE_NAMES.SENTRY_OP: "function",
ATTRIBUTE_NAMES.SENTRY_ORIGIN: self.origin,
},
)
fields = metadata.get("fields", [])
Expand Down
31 changes: 19 additions & 12 deletions tests/integrations/asyncio/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest.mock import AsyncMock

import pytest
from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP
Expand Down Expand Up @@ -75,7 +76,7 @@ async def test_create_task(
items = capture_items("span")

with sentry_sdk.start_span(
name="not so important", attributes={"sentry.op": "root"}
name="not so important", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "root"}
):
foo_task = asyncio.create_task(foo())
bar_task = asyncio.create_task(bar())
Expand All @@ -95,16 +96,16 @@ async def test_create_task(

assert segment["is_segment"] is True
assert segment["name"] == "not so important"
assert segment["attributes"]["sentry.op"] == "root"
assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "root"

spans = [item.payload for item in items]
assert len(spans) == 2

assert spans[0]["attributes"]["sentry.op"] == OP.FUNCTION
assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION
assert spans[0]["name"] == "foo"
assert spans[0]["parent_span_id"] == segment["span_id"]

assert spans[1]["attributes"]["sentry.op"] == OP.FUNCTION
assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION
assert spans[1]["name"] == "bar"
assert spans[1]["parent_span_id"] == segment["span_id"]

Expand All @@ -123,7 +124,7 @@ async def test_gather(
items = capture_items("span")

with sentry_sdk.start_span(
name="not so important", attributes={"sentry.op": "root"}
name="not so important", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "root"}
):
await asyncio.gather(foo(), bar(), return_exceptions=True)

Expand All @@ -132,16 +133,16 @@ async def test_gather(
segment = items.pop().payload
assert segment["is_segment"] is True
assert segment["name"] == "not so important"
assert segment["attributes"]["sentry.op"] == "root"
assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "root"

spans = [item.payload for item in items]
assert len(spans) == 2

assert spans[0]["attributes"]["sentry.op"] == OP.FUNCTION
assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION
assert spans[0]["name"] == "foo"
assert spans[0]["parent_span_id"] == segment["span_id"]

assert spans[1]["attributes"]["sentry.op"] == OP.FUNCTION
assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION
assert spans[1]["name"] == "bar"
assert spans[1]["parent_span_id"] == segment["span_id"]

Expand Down Expand Up @@ -387,11 +388,13 @@ async def test_span_origin(

segment = items.pop().payload
assert segment["is_segment"] is True
assert segment["attributes"]["sentry.origin"] == "manual"
assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual"

spans = [item.payload for item in items]
assert len(spans) == 1
assert spans[0]["attributes"]["sentry.origin"] == "auto.function.asyncio"
assert (
spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.asyncio"
)


@minimum_python_38
Expand Down Expand Up @@ -501,7 +504,9 @@ async def test_delayed_enable_integration(

spans = [item.payload for item in items]
assert len(spans) == 1
assert spans[0]["attributes"]["sentry.origin"] == "auto.function.asyncio"
assert (
spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.asyncio"
)


@minimum_python_38
Expand Down Expand Up @@ -598,7 +603,9 @@ async def test_delayed_enable_integration_after_disabling(

spans = [item.payload for item in items]
assert len(spans) == 1
assert spans[0]["attributes"]["sentry.origin"] == "auto.function.asyncio"
assert (
spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.asyncio"
)


@minimum_python_39
Expand Down
Loading
Loading