Skip to content

Commit d42dbcd

Browse files
Merge pull request #298 from solarwinds/NH-70998-otlp-span-time-ms
NH-70998 OTLP response_time metrics in milliseconds
2 parents ddd6c8f + 9d48cf5 commit d42dbcd

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

solarwinds_apm/trace/base_metrics_processor.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ def calculate_span_time(
9090
self,
9191
start_time: int,
9292
end_time: int,
93+
time_conversion: int = 1e3,
9394
) -> int:
94-
"""Calculate span time in microseconds (us) using start and end time
95-
in nanoseconds (ns). OTel span start/end_time are optional."""
95+
"""Calculate span time (via time_conversion e.g. 1e3, 1e6)
96+
using start and end time in nanoseconds (ns). OTel span
97+
start/end_time are optional."""
9698
if not start_time or not end_time:
9799
return 0
98-
return int((end_time - start_time) // 1e3)
100+
ms_start_time = int(start_time // time_conversion)
101+
ms_end_time = int(end_time // time_conversion)
102+
return ms_end_time - ms_start_time

solarwinds_apm/trace/inbound_metrics_processor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def on_end(self, span: "ReadableSpan") -> None:
7070
trans_name = tnames.custom_name
7171

7272
is_span_http = self.is_span_http(span)
73+
# convert from ns to microseconds
7374
span_time = self.calculate_span_time(
7475
span.start_time,
7576
span.end_time,

solarwinds_apm/trace/otlp_metrics_processor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ def on_end(self, span: "ReadableSpan") -> None:
105105
meter_attrs.update({"sw.is_error": "false"})
106106

107107
is_span_http = self.is_span_http(span)
108+
# convert from ns to milliseconds
108109
span_time = self.calculate_span_time(
109110
span.start_time,
110111
span.end_time,
112+
1e6,
111113
)
112114

113115
if is_span_http:

tests/unit/test_processors/test_base_metrics_processor.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,18 @@ def test_calculate_span_time_missing(self, mocker):
310310
assert 0 == processor.calculate_span_time(0, 1000)
311311
assert 0 == processor.calculate_span_time(1000, 0)
312312

313-
def test_calculate_span_time(self, mocker):
313+
def test_calculate_span_time_default_1e3(self, mocker):
314314
processor = _SwBaseMetricsProcessor(
315315
mocker.Mock(),
316316
)
317-
assert 1 == processor.calculate_span_time(2000, 3000)
317+
assert 1 == processor.calculate_span_time(2000, 3000)
318+
319+
def test_calculate_span_time_1e6(self, mocker):
320+
processor = _SwBaseMetricsProcessor(
321+
mocker.Mock(),
322+
)
323+
assert 1 == processor.calculate_span_time(
324+
2000000,
325+
3000000,
326+
1e6,
327+
)

0 commit comments

Comments
 (0)