Skip to content

Commit ec5b73c

Browse files
authored
Support partial timelimit for celery tasks (#847)
1 parent b32be74 commit ec5b73c

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626

2727
- `opentelemetry-instrumentation-flask` Flask: Conditionally create SERVER spans
2828
([#828](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/828))
29+
- `opentelemetry-instrumentation-celery` Celery: Support partial task time limit
30+
([#846](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/846))
2931

3032
- `opentelemetry-instrumentation-asgi` ASGI: Conditionally create SERVER spans
3133
([#843](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/843))

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ def set_attributes_from_context(span, context):
6161

6262
# Skip `timelimit` if it is not set (it's default/unset value is a
6363
# tuple or a list of `None` values
64-
if key == "timelimit" and value in [(None, None), [None, None]]:
65-
continue
64+
if key == "timelimit":
65+
if value in [(None, None), [None, None]]:
66+
continue
67+
if None in value:
68+
value = ["" if tl is None else tl for tl in value]
6669

6770
# Skip `retries` if it's value is `0`
6871
if key == "retries" and value == 0:

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,46 @@ def test_set_attributes_not_recording(self):
9696
self.assertFalse(mock_span.set_attribute.called)
9797
self.assertFalse(mock_span.set_status.called)
9898

99+
def test_set_attributes_partial_timelimit_hard_limit(self):
100+
# it should extract only relevant keys
101+
context = {
102+
"correlation_id": "44b7f305",
103+
"delivery_info": {"eager": True},
104+
"eta": "soon",
105+
"expires": "later",
106+
"hostname": "localhost",
107+
"id": "44b7f305",
108+
"reply_to": "44b7f305",
109+
"retries": 4,
110+
"timelimit": ("now", None),
111+
"custom_meta": "custom_value",
112+
"routing_key": "celery",
113+
}
114+
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
115+
utils.set_attributes_from_context(span, context)
116+
self.assertEqual(span.attributes.get("celery.timelimit"), ("now", ""))
117+
118+
def test_set_attributes_partial_timelimit_soft_limit(self):
119+
# it should extract only relevant keys
120+
context = {
121+
"correlation_id": "44b7f305",
122+
"delivery_info": {"eager": True},
123+
"eta": "soon",
124+
"expires": "later",
125+
"hostname": "localhost",
126+
"id": "44b7f305",
127+
"reply_to": "44b7f305",
128+
"retries": 4,
129+
"timelimit": (None, "later"),
130+
"custom_meta": "custom_value",
131+
"routing_key": "celery",
132+
}
133+
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
134+
utils.set_attributes_from_context(span, context)
135+
self.assertEqual(
136+
span.attributes.get("celery.timelimit"), ("", "later")
137+
)
138+
99139
def test_set_attributes_from_context_empty_keys(self):
100140
# it should not extract empty keys
101141
context = {

0 commit comments

Comments
 (0)