-
Notifications
You must be signed in to change notification settings - Fork 434
feat: Add cloudwatch lambda event support to Parser utility #231
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
836c066
feat: Add cloudwatch lambda event support to Parser utility
b95a833
cr fixes
647f903
docs: add CW Logs as a supported model
heitorlessa a09f434
Merge branch 'develop' into cloudwatch
heitorlessa d07db54
fix: cloudwatch logs envelope typo
heitorlessa ccd2a00
docs: add CW Logs as a supported envelope
heitorlessa 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
10 changes: 9 additions & 1 deletion
10
aws_lambda_powertools/utilities/parser/envelopes/__init__.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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
from .base import BaseEnvelope | ||
from .cloudwatch import CloudWatchLogsEnvelope | ||
from .dynamodb import DynamoDBStreamEnvelope | ||
from .event_bridge import EventBridgeEnvelope | ||
from .sns import SnsEnvelope | ||
from .sqs import SqsEnvelope | ||
|
||
__all__ = ["DynamoDBStreamEnvelope", "EventBridgeEnvelope", "SnsEnvelope", "SqsEnvelope", "BaseEnvelope"] | ||
__all__ = [ | ||
"CloudWatchLogsEnvelope", | ||
"DynamoDBStreamEnvelope", | ||
"EventBridgeEnvelope", | ||
"SnsEnvelope", | ||
"SqsEnvelope", | ||
"BaseEnvelope", | ||
] |
42 changes: 42 additions & 0 deletions
42
aws_lambda_powertools/utilities/parser/envelopes/cloudwatch.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,42 @@ | ||
import logging | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
from ..models import CloudWatchLogsModel | ||
from ..types import Model | ||
from .base import BaseEnvelope | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CloudWatchLogsEnvelope(BaseEnvelope): | ||
"""CloudWatch Envelope to extract a List of log records. | ||
|
||
The record's body parameter is a string (after being base64 decoded and gzipped), | ||
though it can also be a JSON encoded string. | ||
Regardless of its type it'll be parsed into a BaseModel object. | ||
|
||
Note: The record will be parsed the same way so if model is str | ||
""" | ||
|
||
def parse(self, data: Optional[Union[Dict[str, Any], Any]], model: Model) -> List[Optional[Model]]: | ||
"""Parses records found with model provided | ||
|
||
Parameters | ||
---------- | ||
data : Dict | ||
Lambda event to be parsed | ||
model : Model | ||
Data model provided to parse after extracting data using envelope | ||
|
||
Returns | ||
------- | ||
List | ||
List of records parsed with model provided | ||
""" | ||
logger.debug(f"Parsing incoming data with SNS model {CloudWatchLogsModel}") | ||
parsed_envelope = CloudWatchLogsModel.parse_obj(data) | ||
logger.debug(f"Parsing CloudWatch records in `body` with {model}") | ||
output = [] | ||
for record in parsed_envelope.awslogs.decoded_data.logEvents: | ||
output.append(self._parse(data=record.message, model=model)) | ||
return output |
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
44 changes: 44 additions & 0 deletions
44
aws_lambda_powertools/utilities/parser/models/cloudwatch.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,44 @@ | ||
import base64 | ||
import json | ||
import logging | ||
import zlib | ||
from datetime import datetime | ||
from typing import List | ||
|
||
from pydantic import BaseModel, Field, validator | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CloudWatchLogsLogEvent(BaseModel): | ||
id: str # noqa AA03 VNE003 | ||
timestamp: datetime | ||
message: str | ||
|
||
|
||
class CloudWatchLogsDecode(BaseModel): | ||
messageType: str | ||
owner: str | ||
logGroup: str | ||
logStream: str | ||
subscriptionFilters: List[str] | ||
logEvents: List[CloudWatchLogsLogEvent] | ||
|
||
|
||
class CloudWatchLogsData(BaseModel): | ||
decoded_data: CloudWatchLogsDecode = Field(None, alias="data") | ||
|
||
@validator("decoded_data", pre=True) | ||
def prepare_data(cls, value): | ||
try: | ||
logger.debug("Decoding base64 cloudwatch log data before parsing") | ||
payload = base64.b64decode(value) | ||
logger.debug("Decompressing cloudwatch log data before parsing") | ||
uncompressed = zlib.decompress(payload, zlib.MAX_WBITS | 32) | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return json.loads(uncompressed.decode("utf-8")) | ||
except Exception: | ||
raise ValueError("unable to decompress data") | ||
|
||
|
||
class CloudWatchLogsModel(BaseModel): | ||
awslogs: CloudWatchLogsData |
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
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
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,74 @@ | ||
import base64 | ||
import json | ||
import zlib | ||
from typing import List | ||
|
||
import pytest | ||
|
||
from aws_lambda_powertools.utilities.parser import ValidationError, envelopes, event_parser | ||
from aws_lambda_powertools.utilities.parser.models import CloudWatchLogsLogEvent, CloudWatchLogsModel | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
from tests.functional.parser.schemas import MyCloudWatchBusiness | ||
from tests.functional.parser.utils import load_event | ||
|
||
|
||
@event_parser(model=MyCloudWatchBusiness, envelope=envelopes.CloudWatchLogsEnvelope) | ||
def handle_cloudwatch_logs(event: List[MyCloudWatchBusiness], _: LambdaContext): | ||
assert len(event) == 1 | ||
log: MyCloudWatchBusiness = event[0] | ||
assert log.my_message == "hello" | ||
assert log.user == "test" | ||
|
||
|
||
@event_parser(model=CloudWatchLogsModel) | ||
def handle_cloudwatch_logs_no_envelope(event: CloudWatchLogsModel, _: LambdaContext): | ||
assert event.awslogs.decoded_data.owner == "123456789123" | ||
assert event.awslogs.decoded_data.logGroup == "testLogGroup" | ||
assert event.awslogs.decoded_data.logStream == "testLogStream" | ||
assert event.awslogs.decoded_data.subscriptionFilters == ["testFilter"] | ||
assert event.awslogs.decoded_data.messageType == "DATA_MESSAGE" | ||
|
||
assert len(event.awslogs.decoded_data.logEvents) == 2 | ||
log_record: CloudWatchLogsLogEvent = event.awslogs.decoded_data.logEvents[0] | ||
assert log_record.id == "eventId1" | ||
convert_time = int(round(log_record.timestamp.timestamp() * 1000)) | ||
assert convert_time == 1440442987000 | ||
assert log_record.message == "[ERROR] First test message" | ||
log_record: CloudWatchLogsLogEvent = event.awslogs.decoded_data.logEvents[1] | ||
assert log_record.id == "eventId2" | ||
convert_time = int(round(log_record.timestamp.timestamp() * 1000)) | ||
assert convert_time == 1440442987001 | ||
assert log_record.message == "[ERROR] Second test message" | ||
|
||
|
||
def test_validate_event_user_model_with_envelope(): | ||
my_log_message = {"my_message": "hello", "user": "test"} | ||
inner_event_dict = { | ||
"messageType": "DATA_MESSAGE", | ||
"owner": "123456789123", | ||
"logGroup": "testLogGroup", | ||
"logStream": "testLogStream", | ||
"subscriptionFilters": ["testFilter"], | ||
"logEvents": [{"id": "eventId1", "timestamp": 1440442987000, "message": json.dumps(my_log_message)}], | ||
} | ||
dict_str = json.dumps(inner_event_dict) | ||
compressesd_str = zlib.compress(str.encode(dict_str), -1) | ||
event_dict = {"awslogs": {"data": base64.b64encode(compressesd_str)}} | ||
|
||
handle_cloudwatch_logs(event_dict, LambdaContext()) | ||
|
||
|
||
def test_validate_event_does_not_conform_with_user_dict_model(): | ||
event_dict = load_event("cloudWatchLogEvent.json") | ||
with pytest.raises(ValidationError): | ||
handle_cloudwatch_logs(event_dict, LambdaContext()) | ||
|
||
|
||
def test_handle_cloudwatch_trigger_event_no_envelope(): | ||
event_dict = load_event("cloudWatchLogEvent.json") | ||
handle_cloudwatch_logs_no_envelope(event_dict, LambdaContext()) | ||
|
||
|
||
def test_handle_invalid_event_with_envelope(): | ||
with pytest.raises(ValidationError): | ||
handle_cloudwatch_logs(event={}, context=LambdaContext()) |
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.