Skip to content

Commit

Permalink
refactor(opentelemetry-instrumentation-celery): fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmrebughini authored and ocelotl committed Aug 1, 2024
1 parent 2e19183 commit da60cef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ def _instrument(self, **kwargs):

signals.task_prerun.connect(self._trace_prerun, weak=False)
signals.task_postrun.connect(self._trace_postrun, weak=False)
signals.before_task_publish.connect(self._trace_before_publish, weak=False)
signals.after_task_publish.connect(self._trace_after_publish, weak=False)
signals.before_task_publish.connect(
self._trace_before_publish, weak=False
)
signals.after_task_publish.connect(
self._trace_after_publish, weak=False
)
signals.task_failure.connect(self._trace_failure, weak=False)
signals.task_retry.connect(self._trace_retry, weak=False)

Expand Down Expand Up @@ -226,7 +230,9 @@ def _trace_before_publish(self, *args, **kwargs):
else:
task_name = task.name
operation_name = f"{_TASK_APPLY_ASYNC}/{task_name}"
span = self._tracer.start_span(operation_name, kind=trace.SpanKind.PRODUCER)
span = self._tracer.start_span(
operation_name, kind=trace.SpanKind.PRODUCER
)

# apply some attributes here because most of the data is not available
if span.is_recording():
Expand All @@ -238,7 +244,9 @@ def _trace_before_publish(self, *args, **kwargs):
activation = trace.use_span(span, end_on_exit=True)
activation.__enter__() # pylint: disable=E1101

utils.attach_context(task, task_id, span, activation, None, is_publish=True)
utils.attach_context(
task, task_id, span, activation, None, is_publish=True
)

headers = kwargs.get("headers")
if headers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ def set_attributes_from_context(span, context):
elif key == "delivery_info":
# Get also destination from this
routing_key = value.get("routing_key")

if routing_key is not None:
span.set_attribute(SpanAttributes.MESSAGING_DESTINATION, routing_key)
span.set_attribute(
SpanAttributes.MESSAGING_DESTINATION, routing_key
)

value = str(value)

elif key == "id":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,14 @@ def test_set_attributes_from_context(self):
span.attributes.get(SpanAttributes.MESSAGING_DESTINATION), "celery"
)

self.assertEqual(
span.attributes["celery.delivery_info"], str({"eager": True})
)
self.assertEqual(span.attributes["celery.delivery_info"], str({"eager": True}))
self.assertEqual(span.attributes.get("celery.eta"), "soon")
self.assertEqual(span.attributes.get("celery.expires"), "later")
self.assertEqual(span.attributes.get("celery.hostname"), "localhost")

self.assertEqual(span.attributes.get("celery.reply_to"), "44b7f305")
self.assertEqual(span.attributes.get("celery.retries"), 4)
self.assertEqual(
span.attributes.get("celery.timelimit"), ("now", "later")
)
self.assertEqual(span.attributes.get("celery.timelimit"), ("now", "later"))
self.assertNotIn("custom_meta", span.attributes)

def test_set_attributes_not_recording(self):
Expand Down Expand Up @@ -132,9 +128,7 @@ def test_set_attributes_partial_timelimit_soft_limit(self):
}
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")
)
self.assertEqual(span.attributes.get("celery.timelimit"), ("", "later"))

def test_set_attributes_from_context_empty_keys(self):
# it should not extract empty keys
Expand Down Expand Up @@ -192,7 +186,9 @@ def test_optional_task_span_attach(self):
span = trace._Span("name", mock.Mock(spec=trace_api.SpanContext))

# assert this is is a no-aop
self.assertIsNone(utils.attach_context(None, task_id, span, mock.Mock(), ""))
self.assertIsNone(
utils.attach_context(None, task_id, span, mock.Mock(), "")
)

def test_span_delete_empty(self):
# ensure detach_span doesn't raise an exception if span is not present
Expand Down

0 comments on commit da60cef

Please sign in to comment.