Skip to content

Commit

Permalink
s3 update filename prefix (#98)
Browse files Browse the repository at this point in the history
Signed-off-by: sachintendulkar576123 <[email protected]>
  • Loading branch information
sachintendulkar576123 authored Dec 10, 2024
1 parent f422fba commit c49a5f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/monocle_apptrace/exporters/aws/s3_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
37 changes: 37 additions & 0 deletions tests/s3_filename_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c49a5f6

Please sign in to comment.