Skip to content

Commit

Permalink
Merge pull request #298 from solarwinds/NH-70998-otlp-span-time-ms
Browse files Browse the repository at this point in the history
NH-70998 OTLP response_time metrics in milliseconds
  • Loading branch information
tammy-baylis-swi authored Feb 1, 2024
2 parents ddd6c8f + 9d48cf5 commit d42dbcd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
10 changes: 7 additions & 3 deletions solarwinds_apm/trace/base_metrics_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ def calculate_span_time(
self,
start_time: int,
end_time: int,
time_conversion: int = 1e3,
) -> int:
"""Calculate span time in microseconds (us) using start and end time
in nanoseconds (ns). OTel span start/end_time are optional."""
"""Calculate span time (via time_conversion e.g. 1e3, 1e6)
using start and end time in nanoseconds (ns). OTel span
start/end_time are optional."""
if not start_time or not end_time:
return 0
return int((end_time - start_time) // 1e3)
ms_start_time = int(start_time // time_conversion)
ms_end_time = int(end_time // time_conversion)
return ms_end_time - ms_start_time
1 change: 1 addition & 0 deletions solarwinds_apm/trace/inbound_metrics_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def on_end(self, span: "ReadableSpan") -> None:
trans_name = tnames.custom_name

is_span_http = self.is_span_http(span)
# convert from ns to microseconds
span_time = self.calculate_span_time(
span.start_time,
span.end_time,
Expand Down
2 changes: 2 additions & 0 deletions solarwinds_apm/trace/otlp_metrics_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ def on_end(self, span: "ReadableSpan") -> None:
meter_attrs.update({"sw.is_error": "false"})

is_span_http = self.is_span_http(span)
# convert from ns to milliseconds
span_time = self.calculate_span_time(
span.start_time,
span.end_time,
1e6,
)

if is_span_http:
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/test_processors/test_base_metrics_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,18 @@ def test_calculate_span_time_missing(self, mocker):
assert 0 == processor.calculate_span_time(0, 1000)
assert 0 == processor.calculate_span_time(1000, 0)

def test_calculate_span_time(self, mocker):
def test_calculate_span_time_default_1e3(self, mocker):
processor = _SwBaseMetricsProcessor(
mocker.Mock(),
)
assert 1 == processor.calculate_span_time(2000, 3000)
assert 1 == processor.calculate_span_time(2000, 3000)

def test_calculate_span_time_1e6(self, mocker):
processor = _SwBaseMetricsProcessor(
mocker.Mock(),
)
assert 1 == processor.calculate_span_time(
2000000,
3000000,
1e6,
)

0 comments on commit d42dbcd

Please sign in to comment.