From 9d48cf5fa44618eefd7ea0827255246d3be82d8d Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Thu, 1 Feb 2024 10:03:32 -0800 Subject: [PATCH] otlp response_time metrics as ms --- solarwinds_apm/trace/base_metrics_processor.py | 10 +++++++--- solarwinds_apm/trace/inbound_metrics_processor.py | 1 + solarwinds_apm/trace/otlp_metrics_processor.py | 2 ++ .../test_processors/test_base_metrics_processor.py | 14 ++++++++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/solarwinds_apm/trace/base_metrics_processor.py b/solarwinds_apm/trace/base_metrics_processor.py index b6c3e32e..d0527cc1 100644 --- a/solarwinds_apm/trace/base_metrics_processor.py +++ b/solarwinds_apm/trace/base_metrics_processor.py @@ -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 diff --git a/solarwinds_apm/trace/inbound_metrics_processor.py b/solarwinds_apm/trace/inbound_metrics_processor.py index 867b694b..e4bae07b 100644 --- a/solarwinds_apm/trace/inbound_metrics_processor.py +++ b/solarwinds_apm/trace/inbound_metrics_processor.py @@ -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, diff --git a/solarwinds_apm/trace/otlp_metrics_processor.py b/solarwinds_apm/trace/otlp_metrics_processor.py index 17c3db88..4b8fcc70 100644 --- a/solarwinds_apm/trace/otlp_metrics_processor.py +++ b/solarwinds_apm/trace/otlp_metrics_processor.py @@ -91,9 +91,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: diff --git a/tests/unit/test_processors/test_base_metrics_processor.py b/tests/unit/test_processors/test_base_metrics_processor.py index f9dc657a..2af9c1a4 100644 --- a/tests/unit/test_processors/test_base_metrics_processor.py +++ b/tests/unit/test_processors/test_base_metrics_processor.py @@ -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) \ No newline at end of file + 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, + ) \ No newline at end of file