From 24095f9fb595da95f4ef239808f2a5687cdcd1c7 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 2 Jul 2024 09:45:00 -0600 Subject: [PATCH] Speed up OTLP proto gRPC exporter tests (#4014) --- .../tests/logs/test_otlp_logs_exporter.py | 8 ++++---- .../tests/test_otlp_exporter_mixin.py | 8 ++++---- .../tests/test_otlp_metrics_exporter.py | 14 +++++++------- .../tests/test_otlp_trace_exporter.py | 14 +++++++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/logs/test_otlp_logs_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/logs/test_otlp_logs_exporter.py index da66f830c2f..c281cbebce1 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/logs/test_otlp_logs_exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/logs/test_otlp_logs_exporter.py @@ -77,7 +77,7 @@ def Export(self, request, context): ( "google.rpc.retryinfo-bin", RetryInfo( - retry_delay=Duration(seconds=4) + retry_delay=Duration(nanos=int(1e7)) ).SerializeToString(), ), ) @@ -300,7 +300,7 @@ def test_otlp_headers_from_env(self): @patch("opentelemetry.exporter.otlp.proto.grpc.exporter.sleep") def test_unavailable(self, mock_sleep, mock_expo): - mock_expo.configure_mock(**{"return_value": [1]}) + mock_expo.configure_mock(**{"return_value": [0.01]}) add_LogsServiceServicer_to_server( LogsServiceServicerUNAVAILABLE(), self.server @@ -308,7 +308,7 @@ def test_unavailable(self, mock_sleep, mock_expo): self.assertEqual( self.exporter.export([self.log_data_1]), LogExportResult.FAILURE ) - mock_sleep.assert_called_with(1) + mock_sleep.assert_called_with(0.01) @patch( "opentelemetry.exporter.otlp.proto.grpc.exporter._create_exp_backoff_generator" @@ -324,7 +324,7 @@ def test_unavailable_delay(self, mock_sleep, mock_expo): self.assertEqual( self.exporter.export([self.log_data_1]), LogExportResult.FAILURE ) - mock_sleep.assert_called_with(4) + mock_sleep.assert_called_with(0.01) def test_success(self): add_LogsServiceServicer_to_server( diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_exporter_mixin.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_exporter_mixin.py index c5e1ed76e70..f7bbdabb11f 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_exporter_mixin.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_exporter_mixin.py @@ -13,8 +13,8 @@ # limitations under the License. import threading -import time from logging import WARNING +from time import time_ns from types import MethodType from typing import Sequence from unittest import TestCase @@ -163,7 +163,7 @@ def code(self): def trailing_metadata(self): return { "google.rpc.retryinfo-bin": RetryInfo( - retry_delay=Duration(seconds=1) + retry_delay=Duration(nanos=int(1e7)) ).SerializeToString() } @@ -196,9 +196,9 @@ def _exporting(self) -> str: # pylint: disable=protected-access self.assertTrue(otlp_mock_exporter._export_lock.locked()) # delay is 1 second while the default shutdown timeout is 30_000 milliseconds - start_time = time.time() + start_time = time_ns() otlp_mock_exporter.shutdown() - now = time.time() + now = time_ns() self.assertGreaterEqual(now, (start_time + 30 / 1000)) # pylint: disable=protected-access self.assertTrue(otlp_mock_exporter._shutdown) diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_metrics_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_metrics_exporter.py index c52e94a00ea..38304962ba0 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_metrics_exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_metrics_exporter.py @@ -13,13 +13,13 @@ # limitations under the License. import threading -import time from concurrent.futures import ThreadPoolExecutor # pylint: disable=too-many-lines from logging import WARNING from os import environ from os.path import dirname +from time import time_ns from typing import List from unittest import TestCase from unittest.mock import patch @@ -97,7 +97,7 @@ def Export(self, request, context): ( "google.rpc.retryinfo-bin", RetryInfo( - retry_delay=Duration(seconds=4) + retry_delay=Duration(nanos=int(1e7)) ).SerializeToString(), ), ) @@ -423,7 +423,7 @@ def test_otlp_exporter_otlp_compression_unspecified( @patch("opentelemetry.exporter.otlp.proto.grpc.exporter.sleep") def test_unavailable(self, mock_sleep, mock_expo): - mock_expo.configure_mock(**{"return_value": [1]}) + mock_expo.configure_mock(**{"return_value": [0.01]}) add_MetricsServiceServicer_to_server( MetricsServiceServicerUNAVAILABLE(), self.server @@ -432,7 +432,7 @@ def test_unavailable(self, mock_sleep, mock_expo): self.exporter.export(self.metrics["sum_int"]), MetricExportResult.FAILURE, ) - mock_sleep.assert_called_with(1) + mock_sleep.assert_called_with(0.01) @patch( "opentelemetry.exporter.otlp.proto.grpc.exporter._create_exp_backoff_generator" @@ -449,7 +449,7 @@ def test_unavailable_delay(self, mock_sleep, mock_expo): self.exporter.export(self.metrics["sum_int"]), MetricExportResult.FAILURE, ) - mock_sleep.assert_called_with(4) + mock_sleep.assert_called_with(0.01) @patch( "opentelemetry.exporter.otlp.proto.grpc.exporter._create_exp_backoff_generator" @@ -802,9 +802,9 @@ def test_shutdown_wait_last_export(self): # pylint: disable=protected-access self.assertTrue(self.exporter._export_lock.locked()) # delay is 4 seconds while the default shutdown timeout is 30_000 milliseconds - start_time = time.time() + start_time = time_ns() self.exporter.shutdown() - now = time.time() + now = time_ns() self.assertGreaterEqual(now, (start_time + 30 / 1000)) # pylint: disable=protected-access self.assertTrue(self.exporter._shutdown) diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py index 83dec3a6dd5..46624df1e67 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py +++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/test_otlp_trace_exporter.py @@ -14,9 +14,9 @@ import os import threading -import time from concurrent.futures import ThreadPoolExecutor from logging import WARNING +from time import time_ns from unittest import TestCase from unittest.mock import Mock, PropertyMock, patch @@ -91,7 +91,7 @@ def Export(self, request, context): ( "google.rpc.retryinfo-bin", RetryInfo( - retry_delay=Duration(seconds=4) + retry_delay=Duration(nanos=int(1e7)) ).SerializeToString(), ), ) @@ -466,14 +466,14 @@ def test_otlp_headers(self, mock_ssl_channel, mock_secure): @patch("opentelemetry.exporter.otlp.proto.grpc.exporter.sleep") def test_unavailable(self, mock_sleep, mock_expo): - mock_expo.configure_mock(**{"return_value": [1]}) + mock_expo.configure_mock(**{"return_value": [0.01]}) add_TraceServiceServicer_to_server( TraceServiceServicerUNAVAILABLE(), self.server ) result = self.exporter.export([self.span]) self.assertEqual(result, SpanExportResult.FAILURE) - mock_sleep.assert_called_with(1) + mock_sleep.assert_called_with(0.01) @patch( "opentelemetry.exporter.otlp.proto.grpc.exporter._create_exp_backoff_generator" @@ -489,7 +489,7 @@ def test_unavailable_delay(self, mock_sleep, mock_expo): self.assertEqual( self.exporter.export([self.span]), SpanExportResult.FAILURE ) - mock_sleep.assert_called_with(4) + mock_sleep.assert_called_with(0.01) def test_success(self): add_TraceServiceServicer_to_server( @@ -954,9 +954,9 @@ def test_shutdown_wait_last_export(self): # pylint: disable=protected-access self.assertTrue(self.exporter._export_lock.locked()) # delay is 4 seconds while the default shutdown timeout is 30_000 milliseconds - start_time = time.time() + start_time = time_ns() self.exporter.shutdown() - now = time.time() + now = time_ns() self.assertGreaterEqual(now, (start_time + 30 / 1000)) # pylint: disable=protected-access self.assertTrue(self.exporter._shutdown)