Skip to content

Commit

Permalink
macro to nano
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed May 24, 2024
1 parent c111a88 commit de73b3f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
12 changes: 4 additions & 8 deletions ddtrace/opentelemetry/_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,11 @@ def add_event(self, name, attributes=None, timestamp=None):
if not self.is_recording():
return

normalized_attrs = {}
for key, value in (attributes or {}).items():
# flatten the attributes and set them as tags
if is_sequence(value):
normalized_attrs[key] = str(value)
else:
normalized_attrs[key] = value
if timestamp:
# timestamp arg is in micoseconds we must convert it to nanoseconds
timestamp = timestamp * 1000

self._ddspan._add_event(name, normalized_attrs, timestamp)
self._ddspan._add_event(name, attributes, timestamp)

def update_name(self, name):
# type: (str) -> None
Expand Down
4 changes: 2 additions & 2 deletions tests/opentelemetry/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def test_otel_span_attributes(oteltracer):
def test_otel_span_events(oteltracer):
with oteltracer.start_span("webpage.load") as span1:
span1.add_event(
"Web page unresponsive", {"error.code": "403", "unknown values": [1, ["h", "a", [False]]]}, 1714536311886793
"Web page unresponsive", {"error.code": "403", "unknown values": [1, ["h", "a", [False]]]}, 1714536311886
)

with oteltracer.start_span("web.response") as span2:
# mock time_ns to ensure the event timestamp is consistent in snapshot files
with mock.patch("ddtrace._trace.span.time_ns", return_value=1714537311986797):
with mock.patch("ddtrace._trace.span.time_ns", return_value=1714537311986000):
span2.add_event("Web page loaded")
span2.add_event("Button changed color", {"colors": [112, 215, 70], "response.time": 134.3, "success": True})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
"error": 0,
"meta": {
"_dd.p.dm": "-0",
"_dd.p.tid": "6650b20100000000",
"events": "[{\"name\": \"Web page unresponsive\", \"time_unix_nano\": 1714536311886793, \"attributes\": {\"error.code\": \"403\", \"unknown values\": \"[1, ['h', 'a', [False]]]\"}}]",
"_dd.p.tid": "6650be1d00000000",
"events": "[{\"name\": \"Web page unresponsive\", \"time_unix_nano\": 1714536311886000, \"attributes\": {\"error.code\": \"403\", \"unknown values\": [1, [\"h\", \"a\", [false]]]}}]",
"language": "python",
"runtime-id": "bdcfb7bccb49447f978e96ccbc081b57"
"runtime-id": "236113d2484c485fb40ba896b12c1992"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 3287
"process_id": 45527
},
"duration": 74000,
"start": 1716564481076253000
"duration": 160000,
"start": 1716567581636662000
}],
[
{
Expand All @@ -36,17 +36,17 @@
"error": 0,
"meta": {
"_dd.p.dm": "-0",
"_dd.p.tid": "6650b20100000000",
"events": "[{\"name\": \"Web page loaded\", \"time_unix_nano\": 1714537311986797}, {\"name\": \"Button changed color\", \"time_unix_nano\": 1714537311986797, \"attributes\": {\"colors\": \"[112, 215, 70]\", \"response.time\": 134.3, \"success\": true}}]",
"_dd.p.tid": "6650be1d00000000",
"events": "[{\"name\": \"Web page loaded\", \"time_unix_nano\": 1714537311986000}, {\"name\": \"Button changed color\", \"time_unix_nano\": 1714537311986000, \"attributes\": {\"colors\": [112, 215, 70], \"response.time\": 134.3, \"success\": true}}]",
"language": "python",
"runtime-id": "bdcfb7bccb49447f978e96ccbc081b57"
"runtime-id": "236113d2484c485fb40ba896b12c1992"
},
"metrics": {
"_dd.top_level": 1,
"_dd.tracer_kr": 1.0,
"_sampling_priority_v1": 1,
"process_id": 3287
"process_id": 45527
},
"duration": 358000,
"start": 1716564481076460000
"duration": 195000,
"start": 1716567581637819000
}]]

0 comments on commit de73b3f

Please sign in to comment.