diff --git a/sentry_sdk/integrations/asyncio.py b/sentry_sdk/integrations/asyncio.py index e4a5b174e8..5eb3dca396 100644 --- a/sentry_sdk/integrations/asyncio.py +++ b/sentry_sdk/integrations/asyncio.py @@ -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 @@ -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, }, ) diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index d505c33517..92a176aaa2 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -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 @@ -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: @@ -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( { diff --git a/sentry_sdk/integrations/loguru.py b/sentry_sdk/integrations/loguru.py index 5b5645b31a..07a46c040e 100644 --- a/sentry_sdk/integrations/loguru.py +++ b/sentry_sdk/integrations/loguru.py @@ -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 ( @@ -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): diff --git a/sentry_sdk/integrations/rust_tracing.py b/sentry_sdk/integrations/rust_tracing.py index 1c59b76b46..446b7b1aba 100644 --- a/sentry_sdk/integrations/rust_tracing.py +++ b/sentry_sdk/integrations/rust_tracing.py @@ -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 @@ -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", []) diff --git a/tests/integrations/asyncio/test_asyncio.py b/tests/integrations/asyncio/test_asyncio.py index 78cd26ac31..356d747ea5 100644 --- a/tests/integrations/asyncio/test_asyncio.py +++ b/tests/integrations/asyncio/test_asyncio.py @@ -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 @@ -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()) @@ -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"] @@ -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) @@ -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"] @@ -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 @@ -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 @@ -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 diff --git a/tests/integrations/logging/test_logging.py b/tests/integrations/logging/test_logging.py index 0c94fd7fe9..6d9b8c945f 100644 --- a/tests/integrations/logging/test_logging.py +++ b/tests/integrations/logging/test_logging.py @@ -3,6 +3,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import get_client @@ -325,7 +326,10 @@ def test_sentry_logs_collection_opt_in(sentry_init, capture_items): assert len(items) == 1 log = items[0].payload - assert log["attributes"]["sentry.message.template"] == "this is %s a template %s" + assert ( + log["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] + == "this is %s a template %s" + ) assert log["attributes"]["sentry.severity_number"] == 13 assert log["attributes"]["sentry.severity_text"] == "warn" @@ -475,14 +479,14 @@ def test_sentry_logs_warning(sentry_init, capture_items): sentry_sdk.flush() logs = [item.payload for item in items] attrs = logs[0]["attributes"] - assert attrs["sentry.message.template"] == "this is %s a template %s" - assert "code.file.path" in attrs - assert "code.line.number" in attrs - assert attrs["logger.name"] == "test-logger" - assert attrs["sentry.environment"] == "production" + assert attrs[ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "this is %s a template %s" + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attrs + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attrs + assert attrs[ATTRIBUTE_NAMES.LOGGER_NAME] == "test-logger" + assert attrs[ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT] == "production" assert attrs["sentry.message.parameter.0"] == "1" assert attrs["sentry.message.parameter.1"] == "2" - assert attrs["sentry.origin"] == "auto.log.stdlib" + assert attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.log.stdlib" assert logs[0]["attributes"]["sentry.severity_number"] == 13 assert logs[0]["attributes"]["sentry.severity_text"] == "warn" @@ -539,16 +543,18 @@ def test_logging_errors(sentry_init, capture_envelopes, capture_items): logs = [item.payload for item in items] assert logs[0]["attributes"]["sentry.severity_text"] == "error" - assert "sentry.message.template" not in logs[0]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE not in logs[0]["attributes"] assert "sentry.message.parameter.0" not in logs[0]["attributes"] - assert "code.line.number" in logs[0]["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in logs[0]["attributes"] assert logs[1]["attributes"]["sentry.severity_text"] == "error" - assert logs[1]["attributes"]["sentry.message.template"] == "error is %s" + assert ( + logs[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "error is %s" + ) assert ( logs[1]["attributes"]["sentry.message.parameter.0"] == "Exception('test exc 2')" ) - assert "code.line.number" in logs[1]["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in logs[1]["attributes"] assert len(logs) == 2 @@ -580,7 +586,7 @@ def test_log_strips_project_root(sentry_init, capture_items): logs = [item.payload for item in items] assert len(logs) == 1 attrs = logs[0]["attributes"] - assert attrs["code.file.path"] == "blah/path.py" + assert attrs[ATTRIBUTE_NAMES.CODE_FILE_PATH] == "blah/path.py" def test_logger_with_all_attributes(sentry_init, capture_items): @@ -604,57 +610,57 @@ def test_logger_with_all_attributes(sentry_init, capture_items): attributes = logs[0]["attributes"] - assert "process.pid" in attributes - assert isinstance(attributes["process.pid"], int) - del attributes["process.pid"] + assert ATTRIBUTE_NAMES.PROCESS_PID in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.PROCESS_PID], int) + del attributes[ATTRIBUTE_NAMES.PROCESS_PID] - assert "sentry.release" in attributes - assert isinstance(attributes["sentry.release"], str) - del attributes["sentry.release"] + assert ATTRIBUTE_NAMES.SENTRY_RELEASE in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.SENTRY_RELEASE], str) + del attributes[ATTRIBUTE_NAMES.SENTRY_RELEASE] - assert "server.address" in attributes - assert isinstance(attributes["server.address"], str) - del attributes["server.address"] + assert ATTRIBUTE_NAMES.SERVER_ADDRESS in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS], str) + del attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS] - assert "thread.id" in attributes - assert isinstance(attributes["thread.id"], int) - del attributes["thread.id"] + assert ATTRIBUTE_NAMES.THREAD_ID in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.THREAD_ID], int) + del attributes[ATTRIBUTE_NAMES.THREAD_ID] - assert "code.file.path" in attributes - assert isinstance(attributes["code.file.path"], str) - del attributes["code.file.path"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.CODE_FILE_PATH], str) + del attributes[ATTRIBUTE_NAMES.CODE_FILE_PATH] - assert "code.function.name" in attributes - assert isinstance(attributes["code.function.name"], str) - del attributes["code.function.name"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION_NAME in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.CODE_FUNCTION_NAME], str) + del attributes[ATTRIBUTE_NAMES.CODE_FUNCTION_NAME] - assert "code.line.number" in attributes - assert isinstance(attributes["code.line.number"], int) - del attributes["code.line.number"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.CODE_LINE_NUMBER], int) + del attributes[ATTRIBUTE_NAMES.CODE_LINE_NUMBER] - assert "process.executable.name" in attributes - assert isinstance(attributes["process.executable.name"], str) - del attributes["process.executable.name"] + assert ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME], str) + del attributes[ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME] - assert "thread.name" in attributes - assert isinstance(attributes["thread.name"], str) - del attributes["thread.name"] + assert ATTRIBUTE_NAMES.THREAD_NAME in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.THREAD_NAME], str) + del attributes[ATTRIBUTE_NAMES.THREAD_NAME] - assert attributes.pop("sentry.sdk.name").startswith("sentry.python") + assert attributes.pop(ATTRIBUTE_NAMES.SENTRY_SDK_NAME).startswith("sentry.python") # Assert on the remaining non-dynamic attributes. assert attributes == { "foo": "bar", "numeric": 42, "more_complex": "{'nested': 'data'}", - "logger.name": "test-logger", - "sentry.origin": "auto.log.stdlib", - "sentry.message.template": "log #%d", - "process.runtime.name": mock.ANY, - "process.runtime.version": mock.ANY, + ATTRIBUTE_NAMES.LOGGER_NAME: "test-logger", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.log.stdlib", + ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE: "log #%d", + ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME: mock.ANY, + ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION: mock.ANY, "sentry.message.parameter.0": 1, - "sentry.environment": "production", - "sentry.sdk.version": VERSION, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: VERSION, "sentry.severity_number": 13, "sentry.severity_text": "warn", } @@ -687,7 +693,7 @@ def test_sentry_logs_named_parameters(sentry_init, capture_items): # Check that the template is captured assert ( - attrs["sentry.message.template"] + attrs[ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "%(source)s call completed, %(input_tk)i input tk, %(output_tk)i output tk (model %(model)s, cost $%(cost).4f)" ) @@ -699,8 +705,8 @@ def test_sentry_logs_named_parameters(sentry_init, capture_items): assert attrs["sentry.message.parameter.cost"] == 0.0234 # Check other standard attributes - assert attrs["logger.name"] == "test-logger" - assert attrs["sentry.origin"] == "auto.log.stdlib" + assert attrs[ATTRIBUTE_NAMES.LOGGER_NAME] == "test-logger" + assert attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.log.stdlib" assert logs[0]["attributes"]["sentry.severity_number"] == 9 # info level assert logs[0]["attributes"]["sentry.severity_text"] == "info" @@ -755,4 +761,4 @@ def test_sentry_logs_no_parameters_no_template(sentry_init, capture_items): assert len(logs) == 1 attrs = logs[0]["attributes"] - assert "sentry.message.template" not in attrs + assert ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE not in attrs diff --git a/tests/integrations/loguru/test_loguru.py b/tests/integrations/loguru/test_loguru.py index 51409b8ce7..c404b0b5a9 100644 --- a/tests/integrations/loguru/test_loguru.py +++ b/tests/integrations/loguru/test_loguru.py @@ -4,6 +4,7 @@ import pytest from loguru import logger from loguru._recattrs import RecordFile, RecordLevel +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import VERSION @@ -48,7 +49,10 @@ def test_defaults_enabled(sentry_init, capture_items): assert len(logs) == 1 assert logs[0].payload["body"] == "test" assert logs[0].payload["level"] == "error" - assert logs[0].payload["attributes"]["sentry.origin"] == "auto.log.loguru" + assert ( + logs[0].payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.log.loguru" + ) @pytest.mark.parametrize( @@ -132,7 +136,10 @@ def test_levels( for log in logs: assert log.payload["level"] == expected_log_level assert log.payload["body"] == "test" - assert log.payload["attributes"]["sentry.origin"] == "auto.log.loguru" + assert ( + log.payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.log.loguru" + ) def test_breadcrumb_format(sentry_init, uninstall_integration, request): @@ -196,11 +203,11 @@ def test_sentry_logs_warning( logs = [item.payload for item in items] attrs = logs[0]["attributes"] - assert "code.file.path" in attrs - assert "code.line.number" in attrs - assert attrs["logger.name"] == "tests.integrations.loguru.test_loguru" - assert attrs["sentry.environment"] == "production" - assert attrs["sentry.origin"] == "auto.log.loguru" + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attrs + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attrs + assert attrs[ATTRIBUTE_NAMES.LOGGER_NAME] == "tests.integrations.loguru.test_loguru" + assert attrs[ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT] == "production" + assert attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.log.loguru" assert logs[0]["attributes"]["sentry.severity_number"] == 13 assert logs[0]["attributes"]["sentry.severity_text"] == "warn" @@ -346,10 +353,10 @@ def test_logging_errors( logs = [item.payload for item in items] assert logs[0]["attributes"]["sentry.severity_text"] == "error" - assert "code.line.number" in logs[0]["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in logs[0]["attributes"] assert logs[1]["attributes"]["sentry.severity_text"] == "error" - assert "code.line.number" in logs[1]["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in logs[1]["attributes"] assert len(logs) == 2 @@ -400,7 +407,7 @@ def record(self, val): logs = [item.payload for item in items] assert len(logs) == 1 attrs = logs[0]["attributes"] - assert attrs["code.file.path"] == "blah/path.py" + assert attrs[ATTRIBUTE_NAMES.CODE_FILE_PATH] == "blah/path.py" def test_log_keeps_full_path_if_not_in_project_root( @@ -449,7 +456,7 @@ def record(self, val): logs = [item.payload for item in items] assert len(logs) == 1 attrs = logs[0]["attributes"] - assert attrs["code.file.path"] == "/blah/path.py" + assert attrs[ATTRIBUTE_NAMES.CODE_FILE_PATH] == "/blah/path.py" def test_logger_with_all_attributes( @@ -470,52 +477,52 @@ def test_logger_with_all_attributes( attributes = logs[0]["attributes"] - assert "process.pid" in attributes - assert isinstance(attributes["process.pid"], int) - del attributes["process.pid"] + assert ATTRIBUTE_NAMES.PROCESS_PID in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.PROCESS_PID], int) + del attributes[ATTRIBUTE_NAMES.PROCESS_PID] - assert "sentry.release" in attributes - assert isinstance(attributes["sentry.release"], str) - del attributes["sentry.release"] + assert ATTRIBUTE_NAMES.SENTRY_RELEASE in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.SENTRY_RELEASE], str) + del attributes[ATTRIBUTE_NAMES.SENTRY_RELEASE] - assert "server.address" in attributes - assert isinstance(attributes["server.address"], str) - del attributes["server.address"] + assert ATTRIBUTE_NAMES.SERVER_ADDRESS in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS], str) + del attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS] - assert "thread.id" in attributes - assert isinstance(attributes["thread.id"], int) - del attributes["thread.id"] + assert ATTRIBUTE_NAMES.THREAD_ID in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.THREAD_ID], int) + del attributes[ATTRIBUTE_NAMES.THREAD_ID] - assert "code.file.path" in attributes - assert isinstance(attributes["code.file.path"], str) - del attributes["code.file.path"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.CODE_FILE_PATH], str) + del attributes[ATTRIBUTE_NAMES.CODE_FILE_PATH] - assert "code.function.name" in attributes - assert isinstance(attributes["code.function.name"], str) - del attributes["code.function.name"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION_NAME in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.CODE_FUNCTION_NAME], str) + del attributes[ATTRIBUTE_NAMES.CODE_FUNCTION_NAME] - assert "code.line.number" in attributes - assert isinstance(attributes["code.line.number"], int) - del attributes["code.line.number"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.CODE_LINE_NUMBER], int) + del attributes[ATTRIBUTE_NAMES.CODE_LINE_NUMBER] - assert "process.executable.name" in attributes - assert isinstance(attributes["process.executable.name"], str) - del attributes["process.executable.name"] + assert ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME], str) + del attributes[ATTRIBUTE_NAMES.PROCESS_EXECUTABLE_NAME] - assert "thread.name" in attributes - assert isinstance(attributes["thread.name"], str) - del attributes["thread.name"] + assert ATTRIBUTE_NAMES.THREAD_NAME in attributes + assert isinstance(attributes[ATTRIBUTE_NAMES.THREAD_NAME], str) + del attributes[ATTRIBUTE_NAMES.THREAD_NAME] - assert attributes.pop("sentry.sdk.name").startswith("sentry.python") + assert attributes.pop(ATTRIBUTE_NAMES.SENTRY_SDK_NAME).startswith("sentry.python") # Assert on the remaining non-dynamic attributes. assert attributes == { - "logger.name": "tests.integrations.loguru.test_loguru", - "sentry.origin": "auto.log.loguru", - "sentry.environment": "production", - "process.runtime.name": ANY, - "process.runtime.version": ANY, - "sentry.sdk.version": VERSION, + ATTRIBUTE_NAMES.LOGGER_NAME: "tests.integrations.loguru.test_loguru", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.log.loguru", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: "production", + ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME: ANY, + ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION: ANY, + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: VERSION, "sentry.severity_number": 13, "sentry.severity_text": "warn", } @@ -633,4 +640,4 @@ def test_no_parameters_no_template( logs = [item.payload for item in items] attributes = logs[0]["attributes"] - assert "sentry.message.template" not in attributes + assert ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE not in attributes diff --git a/tests/integrations/rust_tracing/test_rust_tracing.py b/tests/integrations/rust_tracing/test_rust_tracing.py index a057cabe8d..eeec7a92df 100644 --- a/tests/integrations/rust_tracing/test_rust_tracing.py +++ b/tests/integrations/rust_tracing/test_rust_tracing.py @@ -2,6 +2,7 @@ from typing import Dict import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -95,9 +96,9 @@ def test_on_new_span_on_close( # Ensure the span metadata is wired up span = spans[0] - assert span["attributes"]["sentry.op"] == "function" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" assert ( - span["attributes"]["sentry.origin"] + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.rust_tracing.test_on_new_span_on_close" ) assert span["name"] == "_bindings::fibonacci" @@ -160,15 +161,15 @@ def test_nested_on_new_span_on_close( # Ensure the span metadata is wired up for all spans first_span, second_span, _ = spans - assert first_span["attributes"]["sentry.op"] == "function" + assert first_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" assert ( - first_span["attributes"]["sentry.origin"] + first_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.rust_tracing.test_nested_on_new_span_on_close" ) assert first_span["name"] == "_bindings::fibonacci" - assert second_span["attributes"]["sentry.op"] == "function" + assert second_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" assert ( - second_span["attributes"]["sentry.origin"] + second_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.rust_tracing.test_nested_on_new_span_on_close" ) assert second_span["name"] == "_bindings::fibonacci" @@ -498,9 +499,11 @@ def test_include_tracing_fields( span_after_record = sentry_sdk.get_current_span()._to_json() if tracing_fields_expected: - assert span_after_record["attributes"]["sentry.op"] == "function" assert ( - span_after_record["attributes"]["sentry.origin"] + span_after_record["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" + ) + assert ( + span_after_record["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.rust_tracing.test_record" ) assert span_after_record["attributes"]["use_memoized"] is True @@ -508,9 +511,11 @@ def test_include_tracing_fields( assert span_after_record["attributes"]["index"] == 10 else: - assert span_after_record["attributes"]["sentry.op"] == "function" assert ( - span_after_record["attributes"]["sentry.origin"] + span_after_record["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" + ) + assert ( + span_after_record["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.function.rust_tracing.test_record" ) assert span_after_record["attributes"]["use_memoized"] == "[Filtered]"