Skip to content

Commit

Permalink
Support partial timelimit for celery tasks (#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
OHeimlich authored Jan 25, 2022
1 parent b32be74 commit ec5b73c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

- `opentelemetry-instrumentation-asgi` ASGI: Conditionally create SERVER spans
([#843](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/843))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ def set_attributes_from_context(span, context):

# Skip `timelimit` if it is not set (it's default/unset value is a
# tuple or a list of `None` values
if key == "timelimit" and value in [(None, None), [None, None]]:
continue
if key == "timelimit":
if value in [(None, None), [None, None]]:
continue
if None in value:
value = ["" if tl is None else tl for tl in value]

# Skip `retries` if it's value is `0`
if key == "retries" and value == 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ def test_set_attributes_not_recording(self):
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)

def test_set_attributes_partial_timelimit_hard_limit(self):
# it should extract only relevant keys
context = {
"correlation_id": "44b7f305",
"delivery_info": {"eager": True},
"eta": "soon",
"expires": "later",
"hostname": "localhost",
"id": "44b7f305",
"reply_to": "44b7f305",
"retries": 4,
"timelimit": ("now", None),
"custom_meta": "custom_value",
"routing_key": "celery",
}
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
utils.set_attributes_from_context(span, context)
self.assertEqual(span.attributes.get("celery.timelimit"), ("now", ""))

def test_set_attributes_partial_timelimit_soft_limit(self):
# it should extract only relevant keys
context = {
"correlation_id": "44b7f305",
"delivery_info": {"eager": True},
"eta": "soon",
"expires": "later",
"hostname": "localhost",
"id": "44b7f305",
"reply_to": "44b7f305",
"retries": 4,
"timelimit": (None, "later"),
"custom_meta": "custom_value",
"routing_key": "celery",
}
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))
utils.set_attributes_from_context(span, context)
self.assertEqual(
span.attributes.get("celery.timelimit"), ("", "later")
)

def test_set_attributes_from_context_empty_keys(self):
# it should not extract empty keys
context = {
Expand Down

0 comments on commit ec5b73c

Please sign in to comment.