Skip to content

Commit 3450a11

Browse files
committed
fix sleep unit tests + renaming aws batch log processor
1 parent dc98cf8 commit 3450a11

File tree

6 files changed

+47
-35
lines changed

6 files changed

+47
-35
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_configurator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
AwsMetricAttributesSpanExporterBuilder,
2424
)
2525
from amazon.opentelemetry.distro.aws_span_metrics_processor_builder import AwsSpanMetricsProcessorBuilder
26-
from amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor import (
26+
from amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor import (
2727
AwsCloudWatchOtlpBatchLogRecordProcessor,
2828
)
2929
from amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter import OTLPAwsLogExporter
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Copyright The OpenTelemetry Authors
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
# Modifications Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
44

55
import logging
66
from typing import Mapping, Optional, Sequence, cast

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/exporter/otlp/aws/logs/otlp_aws_logs_exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Copyright The OpenTelemetry Authors
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
# Modifications Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
44

55
import gzip
66
import logging
@@ -120,7 +120,7 @@ def export(self, batch: Sequence[LogData]) -> LogExportResult:
120120
if self._shutdown_event.wait(backoff_seconds):
121121
_logger.info("Export interrupted by shutdown")
122122
return LogExportResult.FAILURE
123-
123+
124124
retry_num += 1
125125

126126
def shutdown(self) -> None:
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import List
66
from unittest.mock import MagicMock, patch
77

8-
from amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor import (
8+
from amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor import (
99
AwsCloudWatchOtlpBatchLogRecordProcessor,
1010
BatchLogExportStrategy,
1111
)
@@ -136,11 +136,11 @@ def test_process_log_data_with_cycle(self):
136136
self.assertEqual(actual_size, expected_size)
137137

138138
@patch(
139-
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.attach",
139+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.attach",
140140
return_value=MagicMock(),
141141
)
142-
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.detach")
143-
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.set_value")
142+
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.detach")
143+
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.set_value")
144144
def test_export_single_batch_under_size_limit(self, _, __, ___):
145145
"""Tests that export is only called once if a single batch is under the size limit"""
146146
log_count = 10
@@ -163,11 +163,11 @@ def test_export_single_batch_under_size_limit(self, _, __, ___):
163163
self.mock_exporter.export.assert_called_once()
164164

165165
@patch(
166-
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.attach",
166+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.attach",
167167
return_value=MagicMock(),
168168
)
169-
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.detach")
170-
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.set_value")
169+
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.detach")
170+
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.set_value")
171171
def test_export_single_batch_all_logs_over_size_limit(self, _, __, ___):
172172
"""Should make multiple export calls of batch size 1 to export logs of size > 1 MB."""
173173

@@ -188,11 +188,11 @@ def test_export_single_batch_all_logs_over_size_limit(self, _, __, ___):
188188
self.assertEqual(len(batch[0]), 1)
189189

190190
@patch(
191-
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.attach",
191+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.attach",
192192
return_value=MagicMock(),
193193
)
194-
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.detach")
195-
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor.set_value")
194+
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.detach")
195+
@patch("amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor.set_value")
196196
def test_export_single_batch_some_logs_over_size_limit(self, _, __, ___):
197197
"""Should make calls to export smaller sub-batch logs"""
198198
large_log_body = "X" * (self.processor._MAX_LOG_REQUEST_BYTE_SIZE + 1)

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/exporter/otlp/aws/logs/test_otlp_aws_logs_exporter.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def test_should_not_export_again_if_not_retryable(self, mock_request):
7373

7474
self.assertEqual(result, LogExportResult.FAILURE)
7575

76-
@patch("threading.Event.wait", side_effect=lambda x: False)
76+
@patch(
77+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter.Event.wait",
78+
side_effect=lambda x: False,
79+
)
7780
@patch("requests.Session.post", return_value=retryable_response_no_header)
7881
def test_should_export_again_with_backoff_if_retryable_and_no_retry_after_header(self, mock_request, mock_wait):
7982
"""Tests that multiple export requests are made with exponential delay if the response status code is retryable.
@@ -95,7 +98,10 @@ def test_should_export_again_with_backoff_if_retryable_and_no_retry_after_header
9598
self.assertEqual(mock_request.call_count, _MAX_RETRYS)
9699
self.assertEqual(result, LogExportResult.FAILURE)
97100

98-
@patch("threading.Event.wait", side_effect=lambda x: False)
101+
@patch(
102+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter.Event.wait",
103+
side_effect=lambda x: False,
104+
)
99105
@patch(
100106
"requests.Session.post",
101107
side_effect=[retryable_response_header, retryable_response_header, retryable_response_header, good_response],
@@ -105,6 +111,7 @@ def test_should_export_again_with_server_delay_if_retryable_and_retry_after_head
105111
delay if the response status code is retryable and there is a Retry-After header."""
106112
self.exporter._timeout = 10000 # Large timeout to avoid early exit
107113
result = self.exporter.export(self.logs)
114+
108115
delays = mock_wait.call_args_list
109116

110117
for delay in delays:
@@ -114,7 +121,10 @@ def test_should_export_again_with_server_delay_if_retryable_and_retry_after_head
114121
self.assertEqual(mock_request.call_count, 4)
115122
self.assertEqual(result, LogExportResult.SUCCESS)
116123

117-
@patch("threading.Event.wait", side_effect=lambda x: False)
124+
@patch(
125+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter.Event.wait",
126+
side_effect=lambda x: False,
127+
)
118128
@patch(
119129
"requests.Session.post",
120130
side_effect=[
@@ -131,6 +141,7 @@ def test_should_export_again_with_backoff_delay_if_retryable_and_bad_retry_after
131141
but the Retry-After header is invalid or malformed."""
132142
self.exporter._timeout = 10000 # Large timeout to avoid early exit
133143
result = self.exporter.export(self.logs)
144+
134145
delays = mock_wait.call_args_list
135146

136147
for index, delay in enumerate(delays):
@@ -152,7 +163,10 @@ def test_export_connection_error_retry(self, mock_request):
152163
self.assertEqual(mock_request.call_count, 2)
153164
self.assertEqual(result, LogExportResult.SUCCESS)
154165

155-
@patch("threading.Event.wait", side_effect=lambda x: False)
166+
@patch(
167+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter.Event.wait",
168+
side_effect=lambda x: False,
169+
)
156170
@patch("requests.Session.post", return_value=retryable_response_no_header)
157171
def test_should_stop_retrying_when_deadline_exceeded(self, mock_request, mock_wait):
158172
"""Tests that the exporter stops retrying when the deadline is exceeded."""
@@ -172,23 +186,21 @@ def test_should_stop_retrying_when_deadline_exceeded(self, mock_request, mock_wa
172186
# Verify total time passed is at the timeout limit
173187
self.assertGreaterEqual(5, self.exporter._timeout)
174188

189+
@patch(
190+
"amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter.Event.wait",
191+
side_effect=lambda x: True,
192+
)
175193
@patch("requests.Session.post", return_value=retryable_response_no_header)
176-
def test_export_interrupted_by_shutdown(self, mock_request):
194+
def test_export_interrupted_by_shutdown(self, mock_request, mock_wait):
177195
"""Tests that export can be interrupted by shutdown during retry wait."""
178196
self.exporter._timeout = 10000
179-
180-
# Mock Event.wait to call shutdown on first call, then return True (interrupted)
181-
# We cannot call shutdown() at the beginning since the exporter would just automatically return a FAILURE result without even attempting the export.
182-
def mock_wait_with_shutdown(timeout):
183-
self.exporter.shutdown()
184-
return True
185-
186-
with patch.object(self.exporter._shutdown_event, 'wait', side_effect=mock_wait_with_shutdown):
187-
result = self.exporter.export(self.logs)
188-
189-
# Should make one request, then get interrupted during retry wait
190-
self.assertEqual(mock_request.call_count, 1)
191-
self.assertEqual(result, LogExportResult.FAILURE)
197+
198+
result = self.exporter.export(self.logs)
199+
200+
# Should make one request, then get interrupted during retry wait
201+
self.assertEqual(mock_request.call_count, 1)
202+
self.assertEqual(result, LogExportResult.FAILURE)
203+
mock_wait.assert_called_once()
192204

193205
@staticmethod
194206
def generate_test_log_data(count=5):

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelementry_configurator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from amazon.opentelemetry.distro.aws_span_metrics_processor import AwsSpanMetricsProcessor
4848
from amazon.opentelemetry.distro.exporter.aws.metrics.aws_cloudwatch_emf_exporter import AwsCloudWatchEmfExporter
4949
from amazon.opentelemetry.distro.exporter.otlp.aws.common.aws_auth_session import AwsAuthSession
50-
from amazon.opentelemetry.distro.exporter.otlp.aws.logs.aws_batch_log_record_processor import (
50+
from amazon.opentelemetry.distro.exporter.otlp.aws.logs._aws_cw_otlp_batch_log_record_processor import (
5151
AwsCloudWatchOtlpBatchLogRecordProcessor,
5252
)
5353
from amazon.opentelemetry.distro.exporter.otlp.aws.logs.otlp_aws_logs_exporter import OTLPAwsLogExporter

0 commit comments

Comments
 (0)