Skip to content

Commit b8c3794

Browse files
herin049xrmx
andauthored
opentelemetry-instrumentation-celery: remove usage of deprecated SpanAttributes where applicable (#4056)
* refactor: remove usage of deprecated SpanAttributes where applicable * update CHANGELOG.md * fix lint and formatting errors * fix CHANGELOG.md --------- Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent d295f84 commit b8c3794

5 files changed

Lines changed: 31 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4444
([#4058](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4058))
4545
- `opentelemetry-instrumentation-django`: Replace SpanAttributes with semconv constants where applicable
4646
([#4059](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4059))
47+
- `opentelemetry-instrumentation-celery`: Replace SpanAttributes with semconv constants where applicable
48+
([#4056](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4056))
4749
- `opentelemetry-instrumentation-confluent-kafka`: Replace SpanAttributes with semconv constants where applicable
4850
([#4057](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4057))
4951

instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def add(x, y):
7676
from opentelemetry.metrics import get_meter
7777
from opentelemetry.propagate import extract, inject
7878
from opentelemetry.propagators.textmap import Getter
79-
from opentelemetry.semconv.trace import SpanAttributes
79+
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
80+
MESSAGING_MESSAGE_ID,
81+
)
8082
from opentelemetry.trace.status import Status, StatusCode
8183

8284
if VERSION >= (4, 0, 1):
@@ -240,7 +242,7 @@ def _trace_before_publish(self, *args, **kwargs):
240242
# apply some attributes here because most of the data is not available
241243
if span.is_recording():
242244
span.set_attribute(_TASK_TAG_KEY, _TASK_APPLY_ASYNC)
243-
span.set_attribute(SpanAttributes.MESSAGING_MESSAGE_ID, task_id)
245+
span.set_attribute(MESSAGING_MESSAGE_ID, task_id)
244246
span.set_attribute(_TASK_NAME_KEY, task_name)
245247
utils.set_attributes_from_context(span, kwargs)
246248

instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
from celery import registry # pylint: disable=no-name-in-module
2121
from celery.app.task import Task
2222

23+
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
24+
MESSAGING_MESSAGE_ID,
25+
)
2326
from opentelemetry.semconv.trace import SpanAttributes
2427
from opentelemetry.trace import Span
2528

@@ -98,7 +101,7 @@ def set_attributes_from_context(span, context):
98101
value = str(value)
99102

100103
elif key == "id":
101-
attribute_name = SpanAttributes.MESSAGING_MESSAGE_ID
104+
attribute_name = MESSAGING_MESSAGE_ID
102105

103106
elif key == "correlation_id":
104107
attribute_name = SpanAttributes.MESSAGING_CONVERSATION_ID

instrumentation/opentelemetry-instrumentation-celery/tests/test_tasks.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,22 @@
2020
from opentelemetry import baggage, context
2121
from opentelemetry.instrumentation.celery import CeleryInstrumentor, utils
2222
from opentelemetry.instrumentation.utils import unwrap
23+
from opentelemetry.semconv.attributes.exception_attributes import (
24+
EXCEPTION_MESSAGE,
25+
EXCEPTION_STACKTRACE,
26+
EXCEPTION_TYPE,
27+
)
2328
from opentelemetry.semconv.trace import SpanAttributes
2429
from opentelemetry.test.test_base import TestBase
2530
from opentelemetry.trace import SpanKind, StatusCode
2631

27-
from .celery_test_tasks import app, task_add, task_raises, task_returns_baggage
32+
from .celery_test_tasks import (
33+
CustomError,
34+
app,
35+
task_add,
36+
task_raises,
37+
task_returns_baggage,
38+
)
2839

2940

3041
class TestCeleryInstrumentation(TestBase):
@@ -127,15 +138,15 @@ def test_task_raises(self):
127138
self.assertEqual(1, len(consumer.events))
128139
event = consumer.events[0]
129140

130-
self.assertIn(SpanAttributes.EXCEPTION_STACKTRACE, event.attributes)
141+
self.assertIn(EXCEPTION_STACKTRACE, event.attributes)
131142

132-
# TODO: use plain assertEqual after 1.25 is released (https://github.com/open-telemetry/opentelemetry-python/pull/3837)
133-
self.assertIn(
134-
"CustomError", event.attributes[SpanAttributes.EXCEPTION_TYPE]
143+
self.assertEqual(
144+
f"{CustomError.__module__}.{CustomError.__qualname__}",
145+
event.attributes[EXCEPTION_TYPE],
135146
)
136147

137148
self.assertEqual(
138-
event.attributes[SpanAttributes.EXCEPTION_MESSAGE],
149+
event.attributes[EXCEPTION_MESSAGE],
139150
"The task failed!",
140151
)
141152

instrumentation/opentelemetry-instrumentation-celery/tests/test_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
from opentelemetry import trace as trace_api
2121
from opentelemetry.instrumentation.celery import utils
2222
from opentelemetry.sdk import trace
23+
from opentelemetry.semconv._incubating.attributes.messaging_attributes import (
24+
MESSAGING_MESSAGE_ID,
25+
)
2326
from opentelemetry.semconv.trace import SpanAttributes
2427

2528

@@ -47,7 +50,7 @@ def test_set_attributes_from_context(self):
4750
utils.set_attributes_from_context(span, context)
4851

4952
self.assertEqual(
50-
span.attributes.get(SpanAttributes.MESSAGING_MESSAGE_ID),
53+
span.attributes.get(MESSAGING_MESSAGE_ID),
5154
"44b7f305",
5255
)
5356
self.assertEqual(

0 commit comments

Comments
 (0)