Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 6515aad

Browse files
Emmanuel T Odekeliyanhui1228
authored andcommitted
trace: Span.add_annotation method (#165)
1 parent ca282ca commit 6515aad

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

opencensus/trace/span.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ def add_attribute(self, attribute_key, attribute_value):
176176
"""
177177
self.attributes[attribute_key] = attribute_value
178178

179+
def add_annotation(self, description, **attrs):
180+
"""Add an annotation to span.
181+
182+
:type description: str
183+
:param description: A user-supplied message describing the event.
184+
The maximum length for the description is 256 bytes.
185+
186+
:type attrs: kwargs
187+
:param attrs: keyworded arguments e.g. failed=True, name='Caching'
188+
"""
189+
at = attributes.Attributes(attrs)
190+
self.add_time_event(time_event_module.TimeEvent(datetime.utcnow(),
191+
time_event_module.Annotation(description, at)))
192+
179193
def add_time_event(self, time_event):
180194
"""Add a TimeEvent.
181195

tests/unit/trace/test_span.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ def test_add_time_event(self):
152152

153153
self.assertEqual(len(span.time_events), 1)
154154

155+
def test_add_annotation(self):
156+
span_name = 'test_span_name'
157+
span = self._make_one(span_name)
158+
159+
span.add_annotation('This is a test', name='octo-span', age=98)
160+
161+
self.assertEqual(len(span.time_events), 1)
162+
a0 = span.time_events[0].annotation
163+
self.assertEqual(a0.description, 'This is a test')
164+
self.assertEqual(a0.attributes.attributes, dict(name='octo-span', age=98))
165+
155166
def test_add_link(self):
156167
from opencensus.trace.link import Link
157168

0 commit comments

Comments
 (0)