diff --git a/CHANGELOG.md b/CHANGELOG.md index 36a4964..56859f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - **[BUGFIX]**: fix(duration): snapshot duration not set [#37](https://github.com/intergral/deep/pull/37) [@Umaaz](https://github.com/Umaaz) - **[BUGFIX]**: fix(attributes): snapshot attributes not set [#38](https://github.com/intergral/deep/pull/38) [@Umaaz](https://github.com/Umaaz) - **[BUGFIX]**: fix(resource): discovered resources overwriting each other [#41](https://github.com/intergral/deep/pull/41) [@Umaaz](https://github.com/Umaaz) +- **[BUGFIX]**: feat(events): use events for snapshot in spans [#40](https://github.com/intergral/deep/pull/40) [@Umaaz](https://github.com/Umaaz) # 1.1.0 (06/02/2024) diff --git a/src/deep/api/plugin/otel.py b/src/deep/api/plugin/otel.py index 602213c..5ff0c88 100644 --- a/src/deep/api/plugin/otel.py +++ b/src/deep/api/plugin/otel.py @@ -67,6 +67,16 @@ def add_attribute(self, key: str, value: str): """ self.proxy.set_attribute(key, value) + def add_event(self, name, attributes=None): + """ + Add an event to the span. + + :param name: the event name + :param attributes: the event attributes + :return: + """ + self.proxy.add_event(name, attributes=attributes) + def close(self): """Close the span.""" if not self.proxy.end_time: @@ -151,7 +161,10 @@ def decorate(self, snapshot_id: str, context: ActionContext) -> Optional[Bounded """ span = self.current_span() if span is not None: - span.add_attribute("snapshot", snapshot_id) + span.add_event(context.location_action.location.name, + attributes={"snapshot": snapshot_id, + "tracepoint": context.location_action.id, + "context": context.trigger_context.id}) return BoundedAttributes(attributes={ "span_name": span.name, "trace_id": span.trace_id, diff --git a/src/deep/api/plugin/span/__init__.py b/src/deep/api/plugin/span/__init__.py index e64b473..7fbd730 100644 --- a/src/deep/api/plugin/span/__init__.py +++ b/src/deep/api/plugin/span/__init__.py @@ -81,6 +81,17 @@ def add_attribute(self, key: str, value: str): """ pass + @abc.abstractmethod + def add_event(self, name, attributes=None): + """ + Add an event to the span. + + :param name: the event name + :param attributes: the event attributes + :return: + """ + pass + @abc.abstractmethod def close(self): """Close the span.""" diff --git a/tests/unit_tests/api/plugin/test_otel.py b/tests/unit_tests/api/plugin/test_otel.py index af2ddc7..d950944 100644 --- a/tests/unit_tests/api/plugin/test_otel.py +++ b/tests/unit_tests/api/plugin/test_otel.py @@ -14,6 +14,7 @@ # along with this program. If not, see . import unittest +import mockito from opentelemetry import trace from opentelemetry.sdk.resources import Resource, SERVICE_NAME from opentelemetry.sdk.trace import TracerProvider @@ -41,12 +42,23 @@ def test_load_plugin(self): def test_collect_attributes(self): with trace.get_tracer_provider().get_tracer("test").start_as_current_span("test-span"): plugin = OTelPlugin() - attributes = plugin.decorate("snap_id", None) + + mock = mockito.mock() + mock.trigger_context = mock + mock.trigger_context.id = "id" + mock.location_action = mock + mock.location_action.id = "id" + mock.location_action.location = mock + mock.location_action.location.name = "test" + + attributes = plugin.decorate("snap_id", mock) self.assertIsNotNone(attributes) self.assertEqual("test-span", attributes.get("span_name")) self.assertIsNotNone(attributes.get("span_id")) self.assertIsNotNone(attributes.get("trace_id")) - self.assertEqual(plugin.current_span().proxy.attributes, {"snapshot": "snap_id"}) + self.assertEqual(plugin.current_span().proxy.events[0].name, "test") + self.assertEqual(plugin.current_span().proxy.events[0].attributes, + {'snapshot': 'snap_id', 'tracepoint': 'id', 'context': 'id'}) def test_create_span(self): plugin = OTelPlugin()