Skip to content

Commit ca20ae2

Browse files
authored
Merge pull request #38 from intergral/35-missing-tracepoint-attribute
fix(attributes): add tracepoint attribute
2 parents c4c2ced + 3d03927 commit ca20ae2

File tree

11 files changed

+44
-15
lines changed

11 files changed

+44
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- **[FEATURE]**: feat(capture): add support for capture tracepoints [#34](https://github.com/intergral/deep/pull/34) [@Umaaz](https://github.com/Umaaz)
44
- **[BUGFIX]**: fix(duration): snapshot duration not set [#37](https://github.com/intergral/deep/pull/37) [@Umaaz](https://github.com/Umaaz)
5+
- **[BUGFIX]**: fix(attributes): snapshot attributes not set [#38](https://github.com/intergral/deep/pull/38) [@Umaaz](https://github.com/Umaaz)
56

67
# 1.1.0 (06/02/2024)
78

src/deep/api/plugin/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ class SnapshotDecorator(Plugin, abc.ABC):
148148
"""Implement this to decorate collected snapshots with attributes."""
149149

150150
@abc.abstractmethod
151-
def decorate(self, context: ActionContext) -> Optional[BoundedAttributes]:
151+
def decorate(self, snapshot_id: str, context: ActionContext) -> Optional[BoundedAttributes]:
152152
"""
153153
Decorate a snapshot with additional data.
154154
155+
:param snapshot_id: the id of the collected snapshot
155156
:param context: the action context for this action
156157
157158
:return: the additional attributes to attach

src/deep/api/plugin/otel.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,22 @@ class OTelPlugin(ResourceProvider, SnapshotDecorator, SpanProcessor):
9191
Provide span and trace information to the snapshot.
9292
"""
9393

94-
def create_span(self, name: str) -> Optional['Span']:
94+
def create_span(self, name: str, context_id: str, tracepoint_id: str) -> Optional['Span']:
9595
"""
9696
Create and return a new span.
9797
9898
:param name: the name of the span to create
99+
:param context_id: the id of the context
100+
:param tracepoint_id: the id of thr tracepoint
99101
:return: the created span
100102
"""
101-
span = trace.get_tracer("deep").start_as_current_span(name, end_on_exit=False, attributes={'dynamic': 'deep'})
103+
span = trace.get_tracer("deep").start_as_current_span(name,
104+
end_on_exit=False,
105+
attributes={'dynamic': 'deep',
106+
'context': context_id,
107+
"tracepoint": tracepoint_id
108+
},
109+
)
102110
if span:
103111
# noinspection PyUnresolvedReferences
104112
# this is a generator contextlib._GeneratorContextManager
@@ -132,16 +140,18 @@ def resource(self) -> Optional[Resource]:
132140
return Resource.create(attributes=attributes)
133141
return None
134142

135-
def decorate(self, context: ActionContext) -> Optional[BoundedAttributes]:
143+
def decorate(self, snapshot_id: str, context: ActionContext) -> Optional[BoundedAttributes]:
136144
"""
137145
Decorate a snapshot with additional data.
138146
147+
:param snapshot_id: the id of the collected snapshot
139148
:param context: the action context for this action
140149
141150
:return: the additional attributes to attach
142151
"""
143152
span = self.current_span()
144153
if span is not None:
154+
span.add_attribute("snapshot", snapshot_id)
145155
return BoundedAttributes(attributes={
146156
"span_name": span.name,
147157
"trace_id": span.trace_id,

src/deep/api/plugin/python.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ class PythonPlugin(ResourceProvider, SnapshotDecorator, TracepointLogger):
3535
This plugin provides the python version to the resource, and the thread name to the attributes.
3636
"""
3737

38-
def decorate(self, context: ActionContext) -> Optional[BoundedAttributes]:
38+
def decorate(self, snapshot_id: str, context: ActionContext) -> Optional[BoundedAttributes]:
3939
"""
4040
Decorate a snapshot with additional data.
4141
42+
:param snapshot_id: the id of the collected snapshot
4243
:param context: the action context for this action
4344
4445
:return: the additional attributes to attach

src/deep/api/plugin/span/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ class SpanProcessor(Plugin, abc.ABC):
2929
"""Span processor connects Deep to a span provider."""
3030

3131
@abc.abstractmethod
32-
def create_span(self, name: str) -> Optional['Span']:
32+
def create_span(self, name: str, context_id: str, tracepoint_id: str) -> Optional['Span']:
3333
"""
3434
Create and return a new span.
3535
3636
:param name: the name of the span to create
37+
:param context_id: the id of the context
38+
:param tracepoint_id: the id of thr tracepoint
3739
:return: the created span
3840
"""
3941
pass

src/deep/api/tracepoint/eventsnapshot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ def id(self):
7373
"""The id of this snapshot."""
7474
return self._id
7575

76+
@property
77+
def id_str(self):
78+
"""The id of this snapshot."""
79+
return format(self._id, "032x")
80+
7681
@property
7782
def tracepoint(self):
7883
"""The tracepoint that triggered this snapshot."""

src/deep/processor/context/snapshot_action.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ def process(self, ctx: 'TriggerContext') -> Optional[ActionCallback]:
177177
return DeferredSnapshotActionCallback(self.action_context, snapshot)
178178

179179
def _decorate_snapshot(self, ctx):
180-
attributes = BoundedAttributes(attributes={'ctx_id': ctx.id}, immutable=False)
180+
attributes = BoundedAttributes(
181+
attributes={'context': ctx.id, 'tracepoint': self.action_context.location_action.tracepoint.id},
182+
immutable=False)
181183
for decorator in ctx.config.snapshot_decorators:
182184
try:
183-
decorate = decorator.decorate(self.action_context)
185+
decorate = decorator.decorate(self.snapshot.id_str, self.action_context)
184186
if decorate is not None:
185187
attributes.merge_in(decorate)
186188
except Exception:
@@ -231,8 +233,7 @@ def __init__(self, action_context: ActionContext, snapshot: EventSnapshot):
231233
:param action_context: the action context that created this result
232234
:param snapshot: the snapshot result
233235
"""
234-
self.action_context = action_context
235-
self.snapshot = snapshot
236+
super().__init__(action_context, snapshot)
236237

237238
def process(self, ctx: 'TriggerContext') -> Optional[ActionCallback]:
238239
"""

src/deep/processor/context/span_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _process_action(self):
8686
spans = []
8787

8888
for span_processor in self.trigger_context.config.span_processors:
89-
span = span_processor.create_span(name)
89+
span = span_processor.create_span(name, self.trigger_context.id, self.location_action.tracepoint.id)
9090
if span:
9191
spans.append(span)
9292

tests/unit_tests/api/plugin/test_otel.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ def test_load_plugin(self):
3939
def test_collect_attributes(self):
4040
with trace.get_tracer_provider().get_tracer("test").start_as_current_span("test-span"):
4141
plugin = OTelPlugin()
42-
attributes = plugin.decorate(None)
42+
attributes = plugin.decorate("snap_id", None)
4343
self.assertIsNotNone(attributes)
4444
self.assertEqual("test-span", attributes.get("span_name"))
4545
self.assertIsNotNone(attributes.get("span_id"))
4646
self.assertIsNotNone(attributes.get("trace_id"))
47+
self.assertEqual(plugin.current_span().proxy.attributes, {"snapshot": "snap_id"})
48+
49+
def test_create_span(self):
50+
plugin = OTelPlugin()
51+
span = plugin.create_span("test", "ctx_id", "tp_id")
52+
self.assertIsNotNone(span)
53+
attributes = span.proxy.attributes
54+
self.assertEqual({"dynamic": "deep", "context": "ctx_id", "tracepoint": "tp_id"}, attributes)

tests/unit_tests/api/plugin/test_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ def test_load_plugin(self):
2727

2828
def test_collect_attributes(self):
2929
plugin = PythonPlugin()
30-
attributes = plugin.decorate(None)
30+
attributes = plugin.decorate("", None)
3131
self.assertIsNotNone(attributes)
3232
self.assertEqual("MainThread", attributes.get("thread_name"))

0 commit comments

Comments
 (0)