-
Notifications
You must be signed in to change notification settings - Fork 25
Add Compact Console Log Exporter for Lambda Environment #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jj22ee
merged 6 commits into
aws-observability:main
from
jj22ee:compressed-console-log-exporter
Jul 31, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
80fdab5
add compressed console log exporter for lambda environment
jj22ee 0c008b0
update comment
jj22ee f259212
Merge branch 'main' into compressed-console-log-exporter
jj22ee 821ad50
lint
jj22ee c0190b2
update compressed -> compact
jj22ee d10b6a6
lint
jj22ee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
.../src/amazon/opentelemetry/distro/exporter/console/logs/compressed_console_log_exporter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import re | ||
from typing import Sequence | ||
|
||
from opentelemetry.sdk._logs import LogData | ||
from opentelemetry.sdk._logs.export import ConsoleLogExporter, LogExportResult | ||
|
||
|
||
class CompressedConsoleLogExporter(ConsoleLogExporter): | ||
def export(self, batch: Sequence[LogData]): | ||
for data in batch: | ||
formatted_json = self.formatter(data.log_record) | ||
print(re.sub(r"\s*([{}[\]:,])\s*", r"\1", formatted_json), flush=True) | ||
|
||
return LogExportResult.SUCCESS |
72 changes: 72 additions & 0 deletions
72
...amazon/opentelemetry/distro/exporter/console/logs/test_compressed_console_log_exporter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from amazon.opentelemetry.distro.exporter.console.logs.compressed_console_log_exporter import ( | ||
CompressedConsoleLogExporter, | ||
) | ||
from opentelemetry.sdk._logs.export import LogExportResult | ||
|
||
|
||
class TestCompressedConsoleLogExporter(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.exporter = CompressedConsoleLogExporter() | ||
|
||
@patch("builtins.print") | ||
def test_export_compresses_json(self, mock_print): | ||
# Mock log data | ||
mock_log_data = Mock() | ||
mock_log_record = Mock() | ||
mock_log_data.log_record = mock_log_record | ||
|
||
# Mock formatted JSON with whitespace | ||
formatted_json = '{\n "body": "test message",\n "severity_number": 9,\n "attributes": {\n "key": "value"\n }\n}' # noqa: E501 | ||
self.exporter.formatter = Mock(return_value=formatted_json) | ||
|
||
# Call export | ||
result = self.exporter.export([mock_log_data]) | ||
|
||
# Verify result | ||
self.assertEqual(result, LogExportResult.SUCCESS) | ||
|
||
# Verify print calls | ||
self.assertEqual(mock_print.call_count, 1) | ||
mock_print.assert_called_with( | ||
'{"body":"test message","severity_number":9,"attributes":{"key":"value"}}', flush=True | ||
) | ||
|
||
@patch("builtins.print") | ||
def test_export_multiple_records(self, mock_print): | ||
# Mock multiple log data | ||
mock_log_data1 = Mock() | ||
mock_log_data2 = Mock() | ||
mock_log_data1.log_record = Mock() | ||
mock_log_data2.log_record = Mock() | ||
|
||
formatted_json = '{\n "body": "test"\n}' | ||
self.exporter.formatter = Mock(return_value=formatted_json) | ||
|
||
# Call export | ||
result = self.exporter.export([mock_log_data1, mock_log_data2]) | ||
|
||
# Verify result | ||
self.assertEqual(result, LogExportResult.SUCCESS) | ||
|
||
# Verify print calls | ||
self.assertEqual(mock_print.call_count, 2) # 2 records | ||
# Each record should print compressed JSON | ||
expected_calls = [unittest.mock.call('{"body":"test"}', flush=True)] * 2 | ||
mock_print.assert_has_calls(expected_calls) | ||
|
||
@patch("builtins.print") | ||
def test_export_empty_batch(self, mock_print): | ||
# Call export with empty batch | ||
result = self.exporter.export([]) | ||
|
||
# Verify result | ||
self.assertEqual(result, LogExportResult.SUCCESS) | ||
|
||
# Verify print calls | ||
mock_print.assert_not_called() # No records, no prints |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.