Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> list[T | N
list[T | None]
List of records parsed with model provided
"""
logger.debug(f"Parsing incoming data with SNS model {CloudWatchLogsModel}")
logger.debug(f"Parsing incoming data with CloudWatchLogs model {CloudWatchLogsModel}")
parsed_envelope = CloudWatchLogsModel.model_validate(data)
if parsed_envelope.awslogs.decoded_data.messageType == "CONTROL_MESSAGE":
logger.debug("Skipping parsing for CONTROL_MESSAGE")
return []
logger.debug(f"Parsing CloudWatch records in `body` with {model}")
return [
self._parse(data=record.message, model=model) for record in parsed_envelope.awslogs.decoded_data.logEvents
Expand Down
24 changes: 20 additions & 4 deletions aws_lambda_powertools/utilities/parser/models/cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import zlib
from datetime import datetime

from pydantic import BaseModel, Field, field_validator
from pydantic import BaseModel, ConfigDict, Field, field_validator

logger = logging.getLogger(__name__)


class CloudWatchLogsLogEvent(BaseModel):
model_config = ConfigDict(populate_by_name=True)

id: str = Field(
description="Unique identifier for the log event within the batch.",
examples=["eventId1", "abc123def456"],
Expand All @@ -22,27 +24,41 @@ class CloudWatchLogsLogEvent(BaseModel):
description="The actual log message string or structured JSON payload emitted by the service or application.",
examples=["This is a sample log message", '{"statusCode":200,"path":"/hello"}'],
)
extracted_fields: dict[str, str] = Field(
default_factory=dict,
alias="extractedFields",
description="Key-value pairs of fields extracted from the log message if a filter pattern was used.",
examples=[{"timestamp": "2023-01-15T10:30:00Z", "request_id": "1234"}],
)


class CloudWatchLogsDecode(BaseModel):
messageType: str = Field(
description="The type of CloudWatch Logs message.",
examples=["DATA_MESSAGE", "CONTROL_MESSAGE"],
)
owner: str = Field(description="The AWS account ID of the originating log data.", examples=["123456789012"])
logGroup: str = Field(
owner: str | None = Field(
default=None,
description="The AWS account ID of the originating log data.",
examples=["123456789012"],
)
logGroup: str | None = Field(
default=None,
description="The name of the log group that contains the log stream.",
examples=["/aws/lambda/my-function", "/aws/apigateway/my-api"],
)
logStream: str = Field(
logStream: str | None = Field(
default=None,
description="The name of the log stream that stores the log events.",
examples=["2023/01/15/[$LATEST]abcdef1234567890", "i-1234567890abcdef0"],
)
subscriptionFilters: list[str] = Field(
default_factory=list,
description="List of subscription filter names associated with the log group.",
examples=[["LambdaStream_cloudwatch", "AlertFilter"]],
)
logEvents: list[CloudWatchLogsLogEvent] = Field(
default_factory=list,
description="Array of log events included in the message.",
examples=[[{"id": "eventId1", "timestamp": 1673779200000, "message": "Sample log line"}]],
)
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/parser/_pydantic/test_cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,44 @@ def test_cloudwatch_envelope_plain_text_still_fails_for_object_model():
model=MyCloudWatchBusiness,
envelope=envelopes.CloudWatchLogsEnvelope,
)


def _make_cloudwatch_control_event() -> dict:
inner_event_dict = {"messageType": "CONTROL_MESSAGE"}
compressed = zlib.compress(json.dumps(inner_event_dict).encode("utf-8"), -1)
return {"awslogs": {"data": base64.b64encode(compressed)}}


def test_handle_cloudwatch_control_message_no_envelope():
raw_event = _make_cloudwatch_control_event()
parsed_event: CloudWatchLogsModel = CloudWatchLogsModel(**raw_event)

assert parsed_event.awslogs.decoded_data.messageType == "CONTROL_MESSAGE"
assert parsed_event.awslogs.decoded_data.owner is None
assert parsed_event.awslogs.decoded_data.logGroup is None
assert parsed_event.awslogs.decoded_data.logStream is None
assert parsed_event.awslogs.decoded_data.subscriptionFilters == []
assert parsed_event.awslogs.decoded_data.logEvents == []
assert parsed_event.awslogs.decoded_data.policyLevel is None


def test_handle_cloudwatch_control_message_with_envelope():
raw_event = _make_cloudwatch_control_event()
parsed = parse(
event=raw_event,
model=MyCloudWatchBusiness,
envelope=envelopes.CloudWatchLogsEnvelope,
)

assert parsed == []


def test_cloudwatch_log_event_extracted_fields():
log_event = CloudWatchLogsLogEvent(
id="eventId1",
timestamp=1440442987000,
message="sample message",
extractedFields={"request_id": "abc-123", "status": "200"},
)

assert log_event.extracted_fields == {"request_id": "abc-123", "status": "200"}