-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Java V2 Added a new example for CloudWatch logs for tix V1818828107 #7512
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
+121
−4
Merged
Changes from all commits
Commits
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
89 changes: 89 additions & 0 deletions
89
javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/CloudWatchLogQuery.java
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,89 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
|
|
||
| package com.example.cloudwatch; | ||
|
|
||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.OrderBy; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.OutputLogEvent; | ||
| import java.util.List; | ||
|
|
||
| // snippet-start:[cloudwatch.javav2.describe.log.streams.main] | ||
| /** | ||
| * Before running this Java V2 code example, set up your development | ||
| * environment, including your credentials. | ||
| * <p> | ||
| * For more information, see the following documentation topic: | ||
| * <p> | ||
| * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
| */ | ||
| public class CloudWatchLogQuery { | ||
| public static void main(final String[] args) { | ||
| final String usage = """ | ||
| Usage: | ||
| <logGroupName> | ||
|
|
||
| Where: | ||
| logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler). | ||
| """; | ||
|
|
||
| if (args.length != 1) { | ||
| System.out.print(usage); | ||
| System.exit(1); | ||
| } | ||
|
|
||
| String logGroupName = "/aws/lambda/ChatAIHandler" ; //args[0]; | ||
| CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() | ||
| .region(Region.US_EAST_1) | ||
| .build(); | ||
|
|
||
| describeMostRecentLogStream(logsClient, logGroupName); | ||
| } | ||
|
|
||
| /** | ||
| * Describes and prints metadata about the most recent log stream in the specified log group. | ||
| * | ||
| * @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs | ||
| * @param logGroupName the name of the log group | ||
| */ | ||
| public static void describeMostRecentLogStream(CloudWatchLogsClient logsClient, String logGroupName) { | ||
| DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder() | ||
| .logGroupName(logGroupName) | ||
| .orderBy(OrderBy.LAST_EVENT_TIME) | ||
| .descending(true) | ||
| .limit(1) | ||
| .build(); | ||
|
|
||
| try { | ||
| DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest); | ||
| List<LogStream> logStreams = streamsResponse.logStreams(); | ||
|
|
||
| if (logStreams.isEmpty()) { | ||
| System.out.println("No log streams found for log group: " + logGroupName); | ||
| return; | ||
| } | ||
|
|
||
| LogStream stream = logStreams.get(0); | ||
| System.out.println("Most Recent Log Stream:"); | ||
| System.out.println(" Name: " + stream.logStreamName()); | ||
| System.out.println(" ARN: " + stream.arn()); | ||
| System.out.println(" Creation Time: " + stream.creationTime()); | ||
| System.out.println(" First Event Time: " + stream.firstEventTimestamp()); | ||
| System.out.println(" Last Event Time: " + stream.lastEventTimestamp()); | ||
| System.out.println(" Stored Bytes: " + stream.storedBytes()); | ||
| System.out.println(" Upload Sequence Token: " + stream.uploadSequenceToken()); | ||
|
|
||
| } catch (CloudWatchLogsException e) { | ||
| System.err.println("Failed to describe log stream: " + e.awsErrorDetails().errorMessage()); | ||
| } | ||
| } | ||
| } | ||
| // snippet-end:[cloudwatch.javav2.describe.log.streams.main] |
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.
Uh oh!
There was an error while loading. Please reload this page.