From 398fe53cba012cd59a8dcb3da7395c30c89f2e9a Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 31 Jan 2024 17:22:29 -0800 Subject: [PATCH 1/2] Set histogram preferred temporality delta --- solarwinds_apm/configurator.py | 9 ++- .../test_configurator_metrics_exporter.py | 55 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/solarwinds_apm/configurator.py b/solarwinds_apm/configurator.py index 9653fb33..f03adc98 100644 --- a/solarwinds_apm/configurator.py +++ b/solarwinds_apm/configurator.py @@ -34,6 +34,11 @@ from opentelemetry.sdk._configuration import _OTelSDKConfigurator from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader +from opentelemetry.sdk.metrics import Histogram, MeterProvider +from opentelemetry.sdk.metrics.export import ( + AggregationTemporality, + PeriodicExportingMetricReader, +) from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ( @@ -358,6 +363,8 @@ def _configure_metrics_exporter( return environ_exporter_names = environ_exporter.split(",") + # Report all histograms with delta aggregation, including response_time + temporality_delta = {Histogram: AggregationTemporality.DELTA} metric_readers = [] for exporter_name in environ_exporter_names: exporter = None @@ -367,7 +374,7 @@ def _configure_metrics_exporter( "opentelemetry_metrics_exporter", exporter_name, ) - ).load()() + ).load()(preferred_temporality=temporality_delta) except Exception as ex: logger.exception("A exception was raised: %s", ex) logger.exception( diff --git a/tests/unit/test_configurator/test_configurator_metrics_exporter.py b/tests/unit/test_configurator/test_configurator_metrics_exporter.py index c9203ad1..7f993c63 100644 --- a/tests/unit/test_configurator/test_configurator_metrics_exporter.py +++ b/tests/unit/test_configurator/test_configurator_metrics_exporter.py @@ -121,6 +121,7 @@ def test_configure_metrics_exporter_invalid( test_configurator._configure_metrics_exporter( mock_apmconfig_enabled_expt, ) + mock_pemreader.assert_not_called() trace_mocks.get_tracer_provider.assert_not_called() trace_mocks.get_tracer_provider().get_tracer.assert_not_called() @@ -167,12 +168,30 @@ def test_configure_metrics_exporter_valid( # Mock Otel get_resource_mocks(mocker) trace_mocks = get_trace_mocks(mocker) + mock_histogram = mocker.patch( + "solarwinds_apm.configurator.Histogram" + ) + mock_agg_temp = mocker.patch( + "solarwinds_apm.configurator.AggregationTemporality" + ) + mock_agg_temp.DELTA = "foo-delta" # Test! test_configurator = configurator.SolarWindsConfigurator() test_configurator._configure_metrics_exporter( mock_apmconfig_enabled_expt, ) + mock_exporter_entry_point.load.assert_called_once() + mock_exporter_entry_point.load.assert_has_calls( + [ + mocker.call(), + mocker.call()( + preferred_temporality={ + mock_histogram: "foo-delta" + } + ) + ] + ) mock_pemreader.assert_called_once() trace_mocks.get_tracer_provider.assert_called_once() trace_mocks.get_tracer_provider().get_tracer.assert_called_once() @@ -229,6 +248,13 @@ def test_configure_metrics_exporter_invalid_valid_mixed( # Mock Otel get_resource_mocks(mocker) trace_mocks = get_trace_mocks(mocker) + mock_histogram = mocker.patch( + "solarwinds_apm.configurator.Histogram" + ) + mock_agg_temp = mocker.patch( + "solarwinds_apm.configurator.AggregationTemporality" + ) + mock_agg_temp.DELTA = "foo-delta" # Test! test_configurator = configurator.SolarWindsConfigurator() @@ -242,6 +268,11 @@ def test_configure_metrics_exporter_invalid_valid_mixed( mocker.call("opentelemetry_metrics_exporter", "invalid_exporter"), ] ) + mock_exporter_entry_point_invalid.load.assert_has_calls( + [ + mocker.call(), + ] + ) # Not called at all mock_pemreader.assert_not_called() trace_mocks.get_tracer_provider.assert_not_called() @@ -305,6 +336,13 @@ def test_configure_metrics_exporter_valid_invalid_mixed( # Mock Otel get_resource_mocks(mocker) trace_mocks = get_trace_mocks(mocker) + mock_histogram = mocker.patch( + "solarwinds_apm.configurator.Histogram" + ) + mock_agg_temp = mocker.patch( + "solarwinds_apm.configurator.AggregationTemporality" + ) + mock_agg_temp.DELTA = "foo-delta" # Test! test_configurator = configurator.SolarWindsConfigurator() @@ -318,6 +356,23 @@ def test_configure_metrics_exporter_valid_invalid_mixed( mocker.call("opentelemetry_metrics_exporter", "invalid_exporter"), ] ) + mock_exporter_entry_point.load.assert_called_once() + mock_exporter_entry_point.load.assert_has_calls( + [ + mocker.call(), + mocker.call()( + preferred_temporality={ + mock_histogram: "foo-delta" + } + ) + ] + ) + mock_exporter_entry_point_invalid.load.assert_called_once() + mock_exporter_entry_point_invalid.load.assert_has_calls( + [ + mocker.call(), + ] + ) # Called for the valid one mock_pemreader.assert_called_once() # Rest not called at all From a37307298b5e11fbc277912b899045a575842cda Mon Sep 17 00:00:00 2001 From: tammy-baylis-swi Date: Wed, 31 Jan 2024 17:39:15 -0800 Subject: [PATCH 2/2] lint --- solarwinds_apm/configurator.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/solarwinds_apm/configurator.py b/solarwinds_apm/configurator.py index f03adc98..829db736 100644 --- a/solarwinds_apm/configurator.py +++ b/solarwinds_apm/configurator.py @@ -32,8 +32,6 @@ from opentelemetry.propagate import set_global_textmap from opentelemetry.propagators.composite import CompositePropagator from opentelemetry.sdk._configuration import _OTelSDKConfigurator -from opentelemetry.sdk.metrics import MeterProvider -from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader from opentelemetry.sdk.metrics import Histogram, MeterProvider from opentelemetry.sdk.metrics.export import ( AggregationTemporality,