Add PSR-6 Cache support for log group and stream initialization #6
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.
PR Documentation: Optional PSR-6 Caching for CloudWatch Initialization
What kind of change does this PR introduce?
Feature / Performance Optimization.
What is the current behavior?
When the
CloudWatchhandler is configured with$createGroup = trueor$createStream = true, it performs an initialization check every time a new instance is created (typically once per request in most PHP applications).This check involves calling AWS APIs (
describeLogGroupsanddescribeLogStreams) to verify existence before attempting creation. In high-traffic environments or serverless contexts (like AWS Lambda), these repetitive API calls can lead to:ThrottlingExceptionfrom CloudWatch Logs, potentially causing log loss or delays.What is the new behavior?
This PR introduces an optional PSR-6 Cache System integration.
By providing a
CacheItemPoolInterfaceto the handler, the initialization status of Log Groups and Log Streams is persisted. Subsequent requests will check the local cache first; if the "initialized" flag is present and not expired, the handler skips the AWSdescribeandcreateAPI calls entirely.Key Features:
$cacheItemTtlparameter (defaulting to 5 minutes) allows you to control how often the handler re-verifies existence with AWS.Why implement a Cache system if
$createGroupand$createStreamalready exist?You might wonder: "If I set these parameters to
true, the code already checks if they exist before creating them. Why do I need a cache?"The existing parameters control intent (should we ensure they exist?), but the implementation requires API calls to verify that existence.
createLogGroup.describeLogGroups.This is particularly beneficial for horizontally scaled applications where hundreds of processes might otherwise hammer the AWS API simultaneously just to confirm that a long-existing log group is still there.
Why not just set both to
false?A common suggestion to improve performance is to set
$createGroupand$createStreamtofalse(assuming the resources were created manually or by infrastructure-as-code). However, this is often not viable for the following reasons:falsewill cause theputLogEventscall to fail. Without the "create" logic enabled, the handler cannot recover automatically.$createStreamtofalseif the stream name changes frequently and hasn't been pre-created.trueensures "plug-and-play" functionality where logs "just work" in any environment without manual AWS console intervention.The Cache System provides the best of both worlds: It maintains the safety of auto-creation (
true) while achieving the performance of pre-created resources (false) by skipping the redundant API checks once the first check succeeds.Does this PR introduce a breaking change?
No. The parameters are optional, and the default behavior remains identical to the current version.
Other information
An
InvalidArgumentExceptionis thrown if acacheItemPoolis provided while bothcreateGroupandcreateStreamare set tofalse, as the cache would serve no purpose in that configuration.