-
Notifications
You must be signed in to change notification settings - Fork 798
celery: use python dict instead of string as attribute value to enable parsing #3170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,17 @@ | |
) | ||
|
||
|
||
def flatten_dict(data, parent_key="", sep="."): | ||
items = [] | ||
for key, value in data.items(): | ||
new_key = f"{parent_key}{sep}{key}" if parent_key else key | ||
if isinstance(value, dict): | ||
items.extend(flatten_dict(value, new_key, sep=sep).items()) | ||
else: | ||
items.append((new_key, value)) | ||
return dict(items) | ||
|
||
|
||
# pylint:disable=too-many-branches | ||
def set_attributes_from_context(span, context): | ||
"""Helper to extract meta values from a Celery Context""" | ||
|
@@ -95,7 +106,15 @@ def set_attributes_from_context(span, context): | |
SpanAttributes.MESSAGING_DESTINATION, routing_key | ||
) | ||
|
||
value = str(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this does match with the value an attributes can have https://opentelemetry.io/docs/concepts/signals/traces/#attributes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I opened this and realized that it is intentional. Right now, since we do How about if I flatten the map and then stringily (or convert to corresponding datatype) the values? celery.delivery_info ⇒ `{'exchange': '', 'routing_key': 'distilled-guidance-dpo-25-10k-v1', 'priority': 0, 'redelivered': False}` would become celery.delivery_info.exchange: "routing_key"
celery.delivery_info.routing_key: "distilled-guidance-dpo-25-10k-v1"
celery.delivery_info.priority: 0
celery.delivery_info.redelivered: false There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flattening the map would indeed be better I think than going out of spec :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated the changes to add span attributes with flattened map. PTAL, thanks! |
||
# Flatten the dictionary | ||
if value: | ||
flattened = flatten_dict(value) | ||
for item_key, item_value in flattened.items(): | ||
if item_value is not None and item_value != "": | ||
span.set_attribute( | ||
f"celery.delivery_info.{item_key}", item_value | ||
) | ||
continue | ||
|
||
elif key == "id": | ||
attribute_name = SpanAttributes.MESSAGING_MESSAGE_ID | ||
|
@@ -117,6 +136,15 @@ def set_attributes_from_context(span, context): | |
value = "topic" | ||
break | ||
|
||
# If the value is a dictionary, flatten it | ||
if isinstance(value, dict): | ||
flattened = flatten_dict(value) | ||
for nested_key, nested_value in flattened.items(): | ||
if nested_value is not None and nested_value != "": | ||
nested_attr_name = f"celery.{key}.{nested_key}" | ||
span.set_attribute(nested_attr_name, nested_value) | ||
continue | ||
|
||
# set attribute name if not set specially for a key | ||
if attribute_name is None: | ||
attribute_name = f"celery.{key}" | ||
|
Uh oh!
There was an error while loading. Please reload this page.