Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `opentelemetry-instrumentation-celery` Populate both origin and hostname correctly to span attributes
([#3170](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3170))
- `opentelemetry-instrumentation-botocore` Add support for GenAI user events and lazy initialize tracer
([#3258](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3258))
- `opentelemetry-instrumentation-botocore` Add support for GenAI system events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -95,7 +106,15 @@ def set_attributes_from_context(span, context):
SpanAttributes.MESSAGING_DESTINATION, routing_key
)

value = str(value)
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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 value = str(value) the map becomes tricky to parse (filtering based on key).

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

Copy link
Contributor

Choose a reason for hiding this comment

The 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 :)

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ 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.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")
Expand All @@ -72,6 +70,59 @@ def test_set_attributes_from_context(self):
)
self.assertNotIn("custom_meta", span.attributes)

def test_set_nested_attributes_from_context(self):
context = {
"correlation_id": "44b7f305",
"delivery_info": {
"eager": True,
"routing_key": "api_gateway",
"priority": 0,
"redelivered": False,
},
"eta": {"time": "soon", "date": "today"},
"expires": "later",
"hostname": "localhost",
"id": "44b7f305",
"reply_to": "44b7f305",
"retries": 4,
"timelimit": ("now", "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(SpanAttributes.MESSAGING_MESSAGE_ID),
"44b7f305",
)
self.assertEqual(
span.attributes.get(SpanAttributes.MESSAGING_CONVERSATION_ID),
"44b7f305",
)
self.assertEqual(
span.attributes.get(SpanAttributes.MESSAGING_DESTINATION), "celery"
)

self.assertEqual(span.attributes["celery.delivery_info.eager"], True)
self.assertEqual(
span.attributes["celery.delivery_info.routing_key"], "api_gateway"
)
self.assertEqual(span.attributes["celery.delivery_info.priority"], 0)
self.assertEqual(
span.attributes["celery.delivery_info.redelivered"], False
)
self.assertEqual(span.attributes.get("celery.eta.time"), "soon")
self.assertEqual(span.attributes.get("celery.eta.date"), "today")

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.assertNotIn("custom_meta", span.attributes)

def test_set_attributes_not_recording(self):
# it should extract only relevant keys
context = {
Expand Down