Skip to content

Commit

Permalink
Use is_recording flag in jinja, celery, esearch, falcon instrumentati…
Browse files Browse the repository at this point in the history
…ons (#1241)
  • Loading branch information
lzchen committed Oct 14, 2020
1 parent d73eb04 commit 8ab4cda
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ def _trace_postrun(*args, **kwargs):
return

# request context tags
span.set_attribute(_TASK_TAG_KEY, _TASK_RUN)
utils.set_attributes_from_context(span, kwargs)
utils.set_attributes_from_context(span, task.request)
span.set_attribute(_TASK_NAME_KEY, task.name)
if span.is_recording():
span.set_attribute(_TASK_TAG_KEY, _TASK_RUN)
utils.set_attributes_from_context(span, kwargs)
utils.set_attributes_from_context(span, task.request)
span.set_attribute(_TASK_NAME_KEY, task.name)

activation.__exit__(None, None, None)
utils.detach_span(task, task_id)
Expand All @@ -169,10 +170,11 @@ def _trace_before_publish(self, *args, **kwargs):
)

# apply some attributes here because most of the data is not available
span.set_attribute(_TASK_TAG_KEY, _TASK_APPLY_ASYNC)
span.set_attribute(_MESSAGE_ID_ATTRIBUTE_NAME, task_id)
span.set_attribute(_TASK_NAME_KEY, task.name)
utils.set_attributes_from_context(span, kwargs)
if span.is_recording():
span.set_attribute(_TASK_TAG_KEY, _TASK_APPLY_ASYNC)
span.set_attribute(_MESSAGE_ID_ATTRIBUTE_NAME, task_id)
span.set_attribute(_TASK_NAME_KEY, task.name)
utils.set_attributes_from_context(span, kwargs)

activation = self._tracer.use_span(span, end_on_exit=True)
activation.__enter__()
Expand Down Expand Up @@ -209,7 +211,7 @@ def _trace_failure(*args, **kwargs):

# retrieve and pass exception info to activation
span, _ = utils.retrieve_span(task, task_id)
if span is None:
if span is None or not span.is_recording():
return

status_kwargs = {"canonical_code": StatusCanonicalCode.UNKNOWN}
Expand Down Expand Up @@ -238,7 +240,7 @@ def _trace_retry(*args, **kwargs):
return

span, _ = utils.retrieve_span(task, task_id)
if span is None:
if span is None or not span.is_recording():
return

# Add retry reason metadata to span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
# pylint:disable=too-many-branches
def set_attributes_from_context(span, context):
"""Helper to extract meta values from a Celery Context"""
if not span.is_recording():
return
for key in CELERY_CONTEXT_ATTRIBUTES:
value = context.get(key)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ def test_set_attributes_from_context(self):
)
self.assertNotIn("custom_meta", span.attributes)

def test_set_attributes_not_recording(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", "later"),
"custom_meta": "custom_value",
"routing_key": "celery",
}

mock_span = mock.Mock()
mock_span.is_recording.return_value = False
utils.set_attributes_from_context(mock_span, context)
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)

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

0 comments on commit 8ab4cda

Please sign in to comment.