From c49a5f6597110aa8bfff94f0494f5c5e8af70a52 Mon Sep 17 00:00:00 2001 From: sachintendulkar576123 Date: Tue, 10 Dec 2024 12:27:09 +0530 Subject: [PATCH] s3 update filename prefix (#98) Signed-off-by: sachintendulkar576123 --- .../exporters/aws/s3_exporter.py | 4 +- tests/s3_filename_test.py | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 tests/s3_filename_test.py diff --git a/src/monocle_apptrace/exporters/aws/s3_exporter.py b/src/monocle_apptrace/exporters/aws/s3_exporter.py index d9445b1..7e85ce1 100644 --- a/src/monocle_apptrace/exporters/aws/s3_exporter.py +++ b/src/monocle_apptrace/exporters/aws/s3_exporter.py @@ -24,8 +24,8 @@ class S3SpanExporter(SpanExporterBase): def __init__(self, bucket_name=None, region_name=None): super().__init__() # Use environment variables if credentials are not provided - DEFAULT_FILE_PREFIX = "monocle_trace__" - DEFAULT_TIME_FORMAT = "%Y-%m-%d__%H.%M.%S" + DEFAULT_FILE_PREFIX = "monocle_trace_" + DEFAULT_TIME_FORMAT = "%Y-%m-%d_%H.%M.%S" self.max_batch_size = 500 self.export_interval = 1 self.s3_client = boto3.client( diff --git a/tests/s3_filename_test.py b/tests/s3_filename_test.py new file mode 100644 index 0000000..a48e131 --- /dev/null +++ b/tests/s3_filename_test.py @@ -0,0 +1,37 @@ +import unittest +from unittest.mock import patch, MagicMock +import datetime +from monocle_apptrace.exporters.aws.s3_exporter import S3SpanExporter + +class TestS3SpanExporter(unittest.TestCase): + @patch('boto3.client') + def test_file_prefix_in_file_name(self, mock_boto_client): + # Mock S3 client + mock_s3_client = MagicMock() + mock_boto_client.return_value = mock_s3_client + + # Instantiate the exporter with a custom prefix + exporter = S3SpanExporter(bucket_name="test-bucket", region_name="us-east-1") + file_prefix = "monocle_trace_" + # Mock current time for consistency + mock_current_time = datetime.datetime(2024, 12, 10, 10, 0, 0) + with patch('datetime.datetime') as mock_datetime: + mock_datetime.now.return_value = mock_current_time + mock_datetime.strftime = datetime.datetime.strftime + + # Call the private method to upload data (we are testing the file naming logic) + test_span_data = "{\"trace_id\": \"123\"}" + exporter._S3SpanExporter__upload_to_s3(test_span_data) + + # Generate expected file name + expected_file_name = f"{file_prefix}{mock_current_time.strftime(exporter.time_format)}.ndjson" + + # Verify the S3 client was called with the correct file name + mock_s3_client.put_object.assert_called_once_with( + Bucket="test-bucket", + Key=expected_file_name, + Body=test_span_data + ) + +if __name__ == '__main__': + unittest.main()