dotnetv4/CloudWatchLogs/LargeQuery/README.md
This folder contains a .NET feature scenario that demonstrates how to perform large-scale queries on Amazon CloudWatch Logs using recursive binary search to retrieve more than the 10,000 result limit.
CloudWatch Logs Insights queries have a maximum result limit of 10,000 records per query. This example demonstrates how to overcome this limitation by using a recursive binary search algorithm that splits the time range into smaller segments when the limit is reached.
The scenario performs the following steps:
LargeQuery/
├── Actions/
│ ├── CloudWatchLogsWrapper.cs # Wrapper class for CloudWatch Logs operations
│ └── CloudWatchLogsActions.csproj # Actions project file
├── Scenarios/
│ ├── LargeQueryWorkflow.cs # Main workflow implementation
│ ├── README.md # Detailed scenario documentation
│ └── CloudWatchLogsScenario.csproj # Scenario project file
├── Tests/
│ ├── LargeQueryWorkflowTests.cs # Integration tests
│ ├── Usings.cs # Global usings
│ └── CloudWatchLogsTests.csproj # Test project file
└── CloudWatchLogsLargeQuery.sln # Solution file
Navigate to the solution directory:
cd dotnetv4/CloudWatchLogs/LargeQuery
Build the solution:
dotnet build
Run the scenario:
dotnet run --project Scenarios/CloudWatchLogsScenario.csproj
Follow the prompts to:
Run the integration tests to execute the scenario without user prompts:
dotnet test
The test verifies that the scenario completes without errors and successfully retrieves all 50,000 log entries.
The key to retrieving more than 10,000 results is the recursive binary search algorithm:
This approach ensures all logs are retrieved by progressively narrowing the time ranges until each segment contains fewer than 10,000 results.
The algorithm uses millisecond precision for timestamps to ensure accurate splitting and prevent duplicate or missing log entries. Each query adjusts the start time by 1 millisecond to avoid overlapping results.
When running the scenario, you'll see output similar to:
--------------------------------------------------------------------------------
Welcome to the CloudWatch Logs Large Query Scenario.
--------------------------------------------------------------------------------
Preparing the application...
Deploying CloudFormation stack: CloudWatchLargeQueryStack
CloudFormation stack creation started: CloudWatchLargeQueryStack
Waiting for CloudFormation stack creation to complete...
CloudFormation stack creation complete.
Stack output RoleARN: arn:aws:iam::123456789012:role/...
Generating 50,000 sample log entries...
Batch 1/5: Created 10,000 log entries
Batch 2/5: Created 10,000 log entries
...
Waiting 5 minutes for logs to be fully ingested...
--------------------------------------------------------------------------------
Starting recursive query to retrieve all logs...
Query date range: 2024-01-15T10:00:00.000Z to 2024-01-15T10:05:00.000Z. Found 10000 logs.
Query date range: 2024-01-15T10:02:30.000Z to 2024-01-15T10:03:45.000Z. Found 10000 logs.
...
Queries finished in 8.234 seconds.
Total logs found: 50000
--------------------------------------------------------------------------------
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0