-
Notifications
You must be signed in to change notification settings - Fork 98
Query execution logs #282
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
Draft
mcopik
wants to merge
2
commits into
master
Choose a base branch
from
claude/add-benchmark-cli-command-rcOAh
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Query execution logs #282
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -335,6 +335,130 @@ def process(**kwargs): | |
| sebs_client.logging.info("Save results to {}".format(output_file)) | ||
|
|
||
|
|
||
| @benchmark.command("logs") | ||
| @click.argument( | ||
| "target", | ||
| type=str, | ||
| required=True, | ||
| ) | ||
| @click.option( | ||
| "--request-id", | ||
| type=str, | ||
| default=None, | ||
| help="Request/invocation ID to query (optional, for specific invocation).", | ||
| ) | ||
| @click.option( | ||
| "--minutes", | ||
| type=int, | ||
| default=30, | ||
| help="Number of minutes to look back for logs (default: 30, only for function name mode).", | ||
| ) | ||
| @common_params | ||
| def query_logs(target, request_id, minutes, **kwargs): | ||
| """ | ||
| Query and display logs for benchmark invocations. | ||
|
|
||
| TARGET can be either: | ||
| 1. Path to experiments.json file - queries logs for all invocations in that file | ||
| 2. Function name - queries logs for that function (requires --request-id) | ||
|
|
||
| Examples: | ||
| ./sebs.py benchmark logs experiments.json --config config.json | ||
| ./sebs.py benchmark logs my-function --request-id abc123 --config config.json | ||
| ./sebs.py benchmark logs my-function --request-id abc123 --minutes 60 --config config.json | ||
| """ | ||
| ( | ||
| config, | ||
| output_dir, | ||
| logging_filename, | ||
| sebs_client, | ||
| deployment_client, | ||
| ) = parse_common_params(**kwargs) | ||
|
|
||
| # Detect if target is a file or function name | ||
| is_file = os.path.isfile(target) | ||
|
|
||
| # Case 1: Load from experiments file | ||
| if is_file: | ||
| sebs_client.logging.info(f"Loading invocations from {target}") | ||
| with open(target, "r") as in_f: | ||
| exp_config = json.load(in_f) | ||
| experiments = sebs.experiments.ExperimentResult.deserialize( | ||
| exp_config, | ||
| sebs_client.cache_client, | ||
| sebs_client.generate_logging_handlers(logging_filename), | ||
| ) | ||
|
|
||
| # Get time bounds from experiments | ||
| exp_start_time, exp_end_time = experiments.times() | ||
| start_time = int(exp_start_time) | ||
| end_time = int(exp_end_time) | ||
|
|
||
| # Query logs for all functions and invocations | ||
| for func_name in experiments.functions(): | ||
| invocations = experiments.invocations(func_name) | ||
| sebs_client.logging.info( | ||
| f"\n{'=' * 80}\nFunction: {func_name} ({len(invocations)} invocations)\n{'=' * 80}" | ||
| ) | ||
|
|
||
| for req_id, execution_result in invocations.items(): | ||
| sebs_client.logging.info(f"\n{'-' * 80}\nRequest ID: {req_id}\n{'-' * 80}") | ||
|
|
||
| try: | ||
| logs = deployment_client.get_invocation_logs( | ||
| func_name, req_id, start_time, end_time | ||
| ) | ||
|
|
||
| if logs: | ||
| for log_line in logs: | ||
| if log_line.strip(): # Skip empty lines | ||
| print(log_line) | ||
| else: | ||
| sebs_client.logging.warning(f"No logs found for request {req_id}") | ||
|
|
||
| except Exception as e: | ||
| sebs_client.logging.error(f"Error retrieving logs for {req_id}: {e}") | ||
|
|
||
| # Case 2: Query specific function | ||
| else: | ||
| function_name = target | ||
|
|
||
| # Validate that request_id is provided | ||
| if not request_id: | ||
| sebs_client.logging.error( | ||
| "When querying by function name, please provide --request-id. " | ||
| "Alternatively, use an experiments.json file to query all invocations." | ||
| ) | ||
| return | ||
|
|
||
| import time | ||
|
|
||
| # Calculate time window - look back X minutes from now | ||
| end_time = int(time.time()) | ||
| start_time = end_time - (minutes * 60) | ||
|
|
||
| sebs_client.logging.info( | ||
| f"Querying logs for function '{function_name}', request ID '{request_id}' " | ||
| f"(last {minutes} minutes)" | ||
| ) | ||
|
|
||
| try: | ||
| logs = deployment_client.get_invocation_logs( | ||
| function_name, request_id, start_time, end_time | ||
| ) | ||
|
|
||
| if logs: | ||
| sebs_client.logging.info(f"\n{'-' * 80}\nLogs:\n{'-' * 80}") | ||
| for log_line in logs: | ||
| if log_line.strip(): # Skip empty lines | ||
| print(log_line) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
| else: | ||
| sebs_client.logging.warning("No logs found") | ||
|
|
||
| except Exception as e: | ||
| sebs_client.logging.error(f"Error retrieving logs: {e}") | ||
|
|
||
|
|
||
| @benchmark.command() | ||
| @click.argument( | ||
| "benchmark-input-size", type=click.Choice(["test", "small", "large"]) | ||
|
|
||
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,5 +1,5 @@ | ||
| """ | ||
| SeBS | ||
| SeBS | ||
| """ | ||
|
|
||
| from .version import __version__ # noqa | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -525,6 +525,86 @@ def get_invocation_error(self, function_name: str, start_time: int, end_time: in | |
| if value["field"] == "@message": | ||
| self.logging.error(value["value"]) | ||
|
|
||
| def get_invocation_logs( | ||
| self, function_name: str, request_id: str, start_time: int, end_time: int | ||
| ) -> List[str]: | ||
| """ | ||
| Retrieve full logs (stdout and stderr) for a specific invocation. | ||
|
|
||
| Args: | ||
| function_name: Name of the Lambda function | ||
| request_id: AWS request ID for the invocation | ||
| start_time: Start time as Unix timestamp | ||
| end_time: End time as Unix timestamp | ||
|
|
||
| Returns: | ||
| List of log messages for the invocation | ||
| """ | ||
| if not self.logs_client: | ||
| self.logs_client = boto3.client( | ||
| service_name="logs", | ||
| aws_access_key_id=self.config.credentials.access_key, | ||
| aws_secret_access_key=self.config.credentials.secret_key, | ||
| region_name=self.config.region, | ||
| ) | ||
|
|
||
| # Query CloudWatch Logs for the specific request ID | ||
| query_string = ( | ||
| f'filter @requestId = "{request_id}" | ' | ||
| f"fields @timestamp, @message | sort @timestamp asc" | ||
| ) | ||
|
|
||
| response = None | ||
| retries = 0 | ||
| max_retries = 3 | ||
|
|
||
| while retries < max_retries: | ||
| query = self.logs_client.start_query( | ||
| logGroupName="/aws/lambda/{}".format(function_name), | ||
| queryString=query_string, | ||
| startTime=math.floor(start_time), | ||
| endTime=math.ceil(end_time + 60), # Add buffer for log delivery | ||
| ) | ||
| query_id = query["queryId"] | ||
|
|
||
| # Poll for query completion | ||
| while response is None or response["status"] == "Running": | ||
| time.sleep(1) | ||
| response = self.logs_client.get_query_results(queryId=query_id) | ||
|
|
||
| if len(response["results"]) > 0: | ||
| break | ||
|
|
||
| # Logs might not be available yet | ||
| retries += 1 | ||
| if retries < max_retries: | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Max retires should be a cli parameter |
||
| self.logging.info( | ||
| f"AWS logs not yet available for request {request_id}, " | ||
| f"retrying in 10s... ({retries}/{max_retries})" | ||
| ) | ||
| time.sleep(10) | ||
| response = None | ||
|
|
||
| # Extract log messages | ||
| log_messages = [] | ||
| if response and "results" in response: | ||
| for log_entry in response["results"]: | ||
| message = None | ||
| timestamp = None | ||
| for field in log_entry: | ||
| if field["field"] == "@message": | ||
| message = field["value"] | ||
| elif field["field"] == "@timestamp": | ||
| timestamp = field["value"] | ||
|
|
||
| if message: | ||
| if timestamp: | ||
| log_messages.append(f"[{timestamp}] {message}") | ||
| else: | ||
| log_messages.append(message) | ||
|
|
||
| return log_messages | ||
|
|
||
| def download_metrics( | ||
| self, | ||
| function_name: str, | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use our logging, and aggregate logs according to invocation ID.