diff --git a/.doc_gen/metadata/cloudwatch-logs_metadata.yaml b/.doc_gen/metadata/cloudwatch-logs_metadata.yaml index 8b9621d5968..850b7468ad5 100644 --- a/.doc_gen/metadata/cloudwatch-logs_metadata.yaml +++ b/.doc_gen/metadata/cloudwatch-logs_metadata.yaml @@ -99,6 +99,19 @@ cloudwatch-logs_DescribeExportTasks: - CloudWatchLogs.dotnetv3.DescribeExportTasksExammple services: cloudwatch-logs: {DescribeExportTasks} +cloudwatch-logs_DescribeLogStreams: + languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/cloudwatch + sdkguide: + excerpts: + - description: + snippet_tags: + - cloudwatch.javav2.describe.log.streams.main + services: + cloudwatch-logs: {DescribeLogStreams} cloudwatch-logs_DescribeLogGroups: languages: .NET: diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/CloudWatchReadLogs.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/CloudWatchReadLogs.java new file mode 100644 index 00000000000..7769caf91f7 --- /dev/null +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/CloudWatchReadLogs.java @@ -0,0 +1,79 @@ +// 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.FilterLogEventsRequest; +import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsResponse; +import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent; +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; + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + *

+ * For more information, see the following documentation topic: + *

+ * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +// snippet-start:[cloudwatch.javav2.describe.log group.main] +public class CloudWatchReadLogs { + + public static void main(final String[] args) { + final String usage = """ + Usage: + + Where: + logGroupName - The name of the log group (e.g., /aws/lambda/ChatAIHandler) + """; + + if (args.length != 1) { + System.out.println(usage); + System.exit(1); + } + + String logGroupName = args[0]; + try (CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() + .region(Region.US_EAST_1) + .build()) { + fetchRecentLogs(logsClient, logGroupName); + } catch (CloudWatchLogsException e) { + System.err.println("Error accessing CloudWatch Logs: " + e.awsErrorDetails().errorMessage()); + } + } + + /** + * Retrieves and prints recent log events from the specified log group across all log streams. + * + * @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs + * @param logGroupName the name of the log group from which to retrieve the log events + */ + public static void fetchRecentLogs(CloudWatchLogsClient logsClient, String logGroupName) { + FilterLogEventsRequest request = FilterLogEventsRequest.builder() + .logGroupName(logGroupName) + .limit(50) // Adjust as needed + .build(); + + FilterLogEventsResponse response = logsClient.filterLogEvents(request); + if (response.events().isEmpty()) { + System.out.println("No log events found."); + return; + } + + System.out.println("Recent log events:"); + for (FilteredLogEvent event : response.events()) { + System.out.printf("[%s] %s%n", event.timestamp(), event.message()); + } + } +} +// snippet-end:[cloudwatch.javav2.describe.log group.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DeleteSubscriptionFilter.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DeleteSubscriptionFilter.java index 787c930d5b2..1d135f67911 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DeleteSubscriptionFilter.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DeleteSubscriptionFilter.java @@ -1,63 +1,70 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.delete_subscription_filter.main] -// snippet-start:[cloudwatch.java2.delete_subscription_filter.import] -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; -import software.amazon.awssdk.services.cloudwatchlogs.model.DeleteSubscriptionFilterRequest; -// snippet-end:[cloudwatch.java2.delete_subscription_filter.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class DeleteSubscriptionFilter { - public static void main(String[] args) { - final String usage = """ - - Usage: - - - Where: - filter - The name of the subscription filter (for example, MyFilter). - logGroup - The name of the log group. (for example, testgroup). - """; - - if (args.length != 2) { - System.out.println(usage); - System.exit(1); - } - - String filter = args[0]; - String logGroup = args[1]; - CloudWatchLogsClient logs = CloudWatchLogsClient.builder() - .build(); - - deleteSubFilter(logs, filter, logGroup); - logs.close(); - } - - public static void deleteSubFilter(CloudWatchLogsClient logs, String filter, String logGroup) { - try { - DeleteSubscriptionFilterRequest request = DeleteSubscriptionFilterRequest.builder() - .filterName(filter) - .logGroupName(logGroup) - .build(); - - logs.deleteSubscriptionFilter(request); - System.out.printf("Successfully deleted CloudWatch logs subscription filter %s", filter); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.delete_subscription_filter.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.delete_subscription_filter.main] +// snippet-start:[cloudwatch.java2.delete_subscription_filter.import] +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.DeleteSubscriptionFilterRequest; +// snippet-end:[cloudwatch.java2.delete_subscription_filter.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class DeleteSubscriptionFilter { + public static void main(String[] args) { + final String usage = """ + + Usage: + + + Where: + filter - The name of the subscription filter (for example, MyFilter). + logGroup - The name of the log group. (for example, testgroup). + """; + + if (args.length != 2) { + System.out.println(usage); + System.exit(1); + } + + String filter = args[0]; + String logGroup = args[1]; + CloudWatchLogsClient logs = CloudWatchLogsClient.builder() + .build(); + + deleteSubFilter(logs, filter, logGroup); + logs.close(); + } + + /** + * Deletes a specified subscription filter from a CloudWatch Logs log group. + * + * @param logs the CloudWatchLogsClient used to interact with AWS CloudWatch Logs + * @param filter the name of the subscription filter to delete + * @param logGroup the name of the log group from which to delete the subscription filter + */ + public static void deleteSubFilter(CloudWatchLogsClient logs, String filter, String logGroup) { + try { + DeleteSubscriptionFilterRequest request = DeleteSubscriptionFilterRequest.builder() + .filterName(filter) + .logGroupName(logGroup) + .build(); + + logs.deleteSubscriptionFilter(request); + System.out.printf("Successfully deleted CloudWatch logs subscription filter %s", filter); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.delete_subscription_filter.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeRule.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeRule.java index 624cc9af6f6..1f2567a4a19 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeRule.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeRule.java @@ -1,64 +1,70 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.javav2.describe_rule.main] -// snippet-start:[cloudwatch.javav2.describe_rule.import] -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; -import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleRequest; -import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleResponse; -// snippet-end:[cloudwatch.javav2.describe_rule.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class DescribeRule { - public static void main(String[] args) { - final String usage = """ - - Usage: - - - Where: - ruleName - The name of the rule to describe. - """; - - if (args.length != 1) { - System.out.println(usage); - System.exit(1); - } - - String ruleName = args[0]; - CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() - .region(Region.US_WEST_2) - .build(); - - describeSpecificRule(cwe, ruleName); - cwe.close(); - } - - public static void describeSpecificRule(CloudWatchEventsClient cwe, String ruleName) { - try { - DescribeRuleRequest ruleRequest = DescribeRuleRequest.builder() - .name(ruleName) - .build(); - - DescribeRuleResponse ruleResp = cwe.describeRule(ruleRequest); - String schedule = ruleResp.scheduleExpression(); - System.out.println("The schedule for this rule is " + schedule); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.javav2.describe_rule.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.javav2.describe_rule.main] +// snippet-start:[cloudwatch.javav2.describe_rule.import] +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; +import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleRequest; +import software.amazon.awssdk.services.cloudwatchevents.model.DescribeRuleResponse; +// snippet-end:[cloudwatch.javav2.describe_rule.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class DescribeRule { + public static void main(String[] args) { + final String usage = """ + + Usage: + + + Where: + ruleName - The name of the rule to describe. + """; + + if (args.length != 1) { + System.out.println(usage); + System.exit(1); + } + + String ruleName = args[0]; + CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() + .region(Region.US_WEST_2) + .build(); + + describeSpecificRule(cwe, ruleName); + cwe.close(); + } + + /** + * Describes a specific CloudWatch Events rule and prints its schedule expression. + * + * @param cwe the CloudWatchEventsClient used to interact with AWS CloudWatch Events + * @param ruleName the name of the rule to describe + */ + public static void describeSpecificRule(CloudWatchEventsClient cwe, String ruleName) { + try { + DescribeRuleRequest ruleRequest = DescribeRuleRequest.builder() + .name(ruleName) + .build(); + + DescribeRuleResponse ruleResp = cwe.describeRule(ruleRequest); + String schedule = ruleResp.scheduleExpression(); + System.out.println("The schedule for this rule is " + schedule); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.javav2.describe_rule.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeSubscriptionFilters.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeSubscriptionFilters.java index 00debca652f..e23b479ac5e 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeSubscriptionFilters.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeSubscriptionFilters.java @@ -1,92 +1,98 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.describe_subscription_filters.main] -// snippet-start:[cloudwatch.java2.describe_subscription_filters.import] -import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; -import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeSubscriptionFiltersRequest; -import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeSubscriptionFiltersResponse; -import software.amazon.awssdk.services.cloudwatchlogs.model.SubscriptionFilter; -// snippet-end:[cloudwatch.java2.describe_subscription_filters.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class DescribeSubscriptionFilters { - public static void main(String[] args) { - - final String usage = """ - - Usage: - - - Where: - logGroup - A log group name (for example, myloggroup). - """; - - if (args.length != 1) { - System.out.println(usage); - System.exit(1); - } - - String logGroup = args[0]; - CloudWatchLogsClient logs = CloudWatchLogsClient.builder() - .credentialsProvider(ProfileCredentialsProvider.create()) - .build(); - - describeFilters(logs, logGroup); - logs.close(); - } - - public static void describeFilters(CloudWatchLogsClient logs, String logGroup) { - try { - boolean done = false; - String newToken = null; - - while (!done) { - DescribeSubscriptionFiltersResponse response; - if (newToken == null) { - DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() - .logGroupName(logGroup) - .limit(1).build(); - - response = logs.describeSubscriptionFilters(request); - } else { - DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() - .nextToken(newToken) - .logGroupName(logGroup) - .limit(1).build(); - response = logs.describeSubscriptionFilters(request); - } - - for (SubscriptionFilter filter : response.subscriptionFilters()) { - System.out.printf("Retrieved filter with name %s, " + "pattern %s " + "and destination arn %s", - filter.filterName(), - filter.filterPattern(), - filter.destinationArn()); - } - - if (response.nextToken() == null) { - done = true; - } else { - newToken = response.nextToken(); - } - } - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - System.out.printf("Done"); - } -} -// snippet-end:[cloudwatch.java2.describe_subscription_filters.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.describe_subscription_filters.main] +// snippet-start:[cloudwatch.java2.describe_subscription_filters.import] +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeSubscriptionFiltersRequest; +import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeSubscriptionFiltersResponse; +import software.amazon.awssdk.services.cloudwatchlogs.model.SubscriptionFilter; +// snippet-end:[cloudwatch.java2.describe_subscription_filters.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class DescribeSubscriptionFilters { + public static void main(String[] args) { + + final String usage = """ + + Usage: + + + Where: + logGroup - A log group name (for example, myloggroup). + """; + + if (args.length != 1) { + System.out.println(usage); + System.exit(1); + } + + String logGroup = args[0]; + CloudWatchLogsClient logs = CloudWatchLogsClient.builder() + .credentialsProvider(ProfileCredentialsProvider.create()) + .build(); + + describeFilters(logs, logGroup); + logs.close(); + } + + /** + * Describes all subscription filters associated with the specified CloudWatch Logs log group. + * + * @param logs the CloudWatchLogsClient used to interact with AWS CloudWatch Logs + * @param logGroup the name of the log group from which to describe the subscription filters + */ + public static void describeFilters(CloudWatchLogsClient logs, String logGroup) { + try { + boolean done = false; + String newToken = null; + + while (!done) { + DescribeSubscriptionFiltersResponse response; + if (newToken == null) { + DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() + .logGroupName(logGroup) + .limit(1).build(); + + response = logs.describeSubscriptionFilters(request); + } else { + DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() + .nextToken(newToken) + .logGroupName(logGroup) + .limit(1).build(); + response = logs.describeSubscriptionFilters(request); + } + + for (SubscriptionFilter filter : response.subscriptionFilters()) { + System.out.printf("Retrieved filter with name %s, " + "pattern %s " + "and destination arn %s", + filter.filterName(), + filter.filterPattern(), + filter.destinationArn()); + } + + if (response.nextToken() == null) { + done = true; + } else { + newToken = response.nextToken(); + } + } + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + System.out.printf("Done"); + } +} +// snippet-end:[cloudwatch.java2.describe_subscription_filters.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DisableAlarmActions.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DisableAlarmActions.java index a21a1adde6c..a31d4a1ceb9 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DisableAlarmActions.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DisableAlarmActions.java @@ -45,6 +45,13 @@ public static void main(String[] args) { cw.close(); } + /** + * Disables the actions associated with a specified CloudWatch alarm. + * + * @param cw the {@link CloudWatchClient} used to interact with the CloudWatch service + * @param alarmName the name of the CloudWatch alarm whose actions are to be disabled + * + */ public static void disableActions(CloudWatchClient cw, String alarmName) { try { DisableAlarmActionsRequest request = DisableAlarmActionsRequest.builder() diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/EnableAlarmActions.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/EnableAlarmActions.java index eec07f5414d..88a1f5a295c 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/EnableAlarmActions.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/EnableAlarmActions.java @@ -45,6 +45,17 @@ public static void main(String[] args) { cw.close(); } + /** + * Enables actions on the specified Amazon CloudWatch alarm. + * + *

This method sends a request to Amazon CloudWatch to enable actions on the given alarm name. + * Alarm actions can include notifications, auto scaling, or other automated responses triggered by alarm state changes.

+ * + * @param cw The {@link CloudWatchClient} used to send the request. + * @param alarm The name of the alarm to enable actions on. + * + * @throws CloudWatchException if the request fails due to client-side issues or service errors. + */ public static void enableActions(CloudWatchClient cw, String alarm) { try { EnableAlarmActionsRequest request = EnableAlarmActionsRequest.builder() diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/FilterLogEvents.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/FilterLogEvents.java index cb28c7824c3..1cbe0bef853 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/FilterLogEvents.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/FilterLogEvents.java @@ -1,77 +1,85 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.filter_logs.main] -// snippet-start:[cloudwatch.java2.filter_logs.import] -import java.util.List; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; -import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsRequest; -import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent; -// snippet-end:[cloudwatch.java2.filter_logs.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class FilterLogEvents { - public static void main(String[] args) { - - final String usage = """ - - Usage: - - - Where: - logGroupName - The name of the log group (for example, myloggroup). - startTime - The start of the time range, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC (for example, 1620940080). - endTime - The end of the time range, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC (for example, 1620949080) - """; - - if (args.length != 3) { - System.out.print(usage); - System.exit(1); - } - - String logGroupName = args[0]; - Long startTime = Long.parseLong(args[1]); - Long endTime = Long.parseLong(args[2]); - Region region = Region.US_WEST_2; - CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() - .region(region) - .build(); - - filterCWLogEvents(cloudWatchLogsClient, logGroupName, startTime, endTime); - cloudWatchLogsClient.close(); - } - - public static void filterCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, Long startTime, - Long endTime) { - try { - FilterLogEventsRequest filterLogEventsRequest = FilterLogEventsRequest.builder() - .logGroupName(logGroupName) - .startTime(startTime) - .endTime(endTime) - .build(); - - List events = cloudWatchLogsClient.filterLogEvents(filterLogEventsRequest).events(); - for (FilteredLogEvent event : events) { - System.out.println(event.message()); - } - - System.out.println("Successfully got CloudWatch log events!"); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.filter_logs.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.filter_logs.main] +// snippet-start:[cloudwatch.java2.filter_logs.import] +import java.util.List; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsRequest; +import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent; +// snippet-end:[cloudwatch.java2.filter_logs.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class FilterLogEvents { + public static void main(String[] args) { + + final String usage = """ + + Usage: + + + Where: + logGroupName - The name of the log group (for example, myloggroup). + startTime - The start of the time range, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC (for example, 1620940080). + endTime - The end of the time range, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC (for example, 1620949080) + """; + + if (args.length != 3) { + System.out.print(usage); + System.exit(1); + } + + String logGroupName = args[0]; + Long startTime = Long.parseLong(args[1]); + Long endTime = Long.parseLong(args[2]); + Region region = Region.US_WEST_2; + CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() + .region(region) + .build(); + + filterCWLogEvents(cloudWatchLogsClient, logGroupName, startTime, endTime); + cloudWatchLogsClient.close(); + } + + /** + * Filters and prints CloudWatch log events within the specified log group and time range. + * + * @param cloudWatchLogsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs + * @param logGroupName the name of the log group from which to filter the log events + * @param startTime the start time (in milliseconds since epoch) for filtering log events + * @param endTime the end time (in milliseconds since epoch) for filtering log events + */ + public static void filterCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, Long startTime, + Long endTime) { + try { + FilterLogEventsRequest filterLogEventsRequest = FilterLogEventsRequest.builder() + .logGroupName(logGroupName) + .startTime(startTime) + .endTime(endTime) + .build(); + + List events = cloudWatchLogsClient.filterLogEvents(filterLogEventsRequest).events(); + for (FilteredLogEvent event : events) { + System.out.println(event.message()); + } + + System.out.println("Successfully got CloudWatch log events!"); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.filter_logs.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/GetLogEvents.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/GetLogEvents.java index 1931da0fb3b..4fa9820294e 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/GetLogEvents.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/GetLogEvents.java @@ -1,74 +1,81 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.get_logs.main] -// snippet-start:[cloudwatch.java2.get_logs.import] -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; -import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; -// snippet-end:[cloudwatch.java2.get_logs.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class GetLogEvents { - - public static void main(String[] args) { - - final String usage = """ - - Usage: - - - Where: - logStreamName - The name of the log stream (for example, mystream). - logGroupName - The name of the log group (for example, myloggroup). - """; - - if (args.length != 2) { - System.out.print(usage); - System.exit(1); - } - - String logStreamName = args[0]; - String logGroupName = args[1]; - Region region = Region.US_WEST_2; - CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() - .region(region) - .build(); - - getCWLogEvents(cloudWatchLogsClient, logGroupName, logStreamName); - cloudWatchLogsClient.close(); - } - - public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, - String logStreamName) { - try { - GetLogEventsRequest getLogEventsRequest = GetLogEventsRequest.builder() - .logGroupName(logGroupName) - .logStreamName(logStreamName) - .startFromHead(true) - .build(); - - int logLimit = cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().size(); - for (int c = 0; c < logLimit; c++) { - System.out.println(cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().get(c).message()); - } - - System.out.println("Successfully got CloudWatch log events!"); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.get_logs.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.get_logs.main] +// snippet-start:[cloudwatch.java2.get_logs.import] +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; +// snippet-end:[cloudwatch.java2.get_logs.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class GetLogEvents { + + public static void main(String[] args) { + + final String usage = """ + + Usage: + + + Where: + logStreamName - The name of the log stream (for example, mystream). + logGroupName - The name of the log group (for example, myloggroup). + """; + + if (args.length != 2) { + System.out.print(usage); + System.exit(1); + } + + String logStreamName = args[0]; + String logGroupName = args[1]; + Region region = Region.US_WEST_2; + CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() + .region(region) + .build(); + + getCWLogEvents(cloudWatchLogsClient, logGroupName, logStreamName); + cloudWatchLogsClient.close(); + } + + /** + * Retrieves and prints CloudWatch log events from the specified log group and log stream. + * + * @param cloudWatchLogsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs + * @param logGroupName the name of the log group from which to retrieve the log events + * @param logStreamName the name of the log stream from which to retrieve the log events + */ + public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, + String logStreamName) { + try { + GetLogEventsRequest getLogEventsRequest = GetLogEventsRequest.builder() + .logGroupName(logGroupName) + .logStreamName(logStreamName) + .startFromHead(true) + .build(); + + int logLimit = cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().size(); + for (int c = 0; c < logLimit; c++) { + System.out.println(cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().get(c).message()); + } + + System.out.println("Successfully got CloudWatch log events!"); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.get_logs.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/HelloService.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/HelloService.java index 23ff545e025..a92db283fdc 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/HelloService.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/HelloService.java @@ -44,6 +44,12 @@ public static void main(String[] args) { cw.close(); } + /** + * Lists all metrics within the specified namespace using AWS CloudWatch. + * + * @param cw the CloudWatchClient used to interact with AWS CloudWatch + * @param namespace the namespace from which to list the metrics + */ public static void listMets(CloudWatchClient cw, String namespace) { try { ListMetricsRequest request = ListMetricsRequest.builder() diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutEvents.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutEvents.java index 94b33716d99..77df696cd5e 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutEvents.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutEvents.java @@ -1,70 +1,79 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.put_events.main] -// snippet-start:[cloudwatch.java2.put_events.import] -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; -import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest; -import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry; -// snippet-end:[cloudwatch.java2.put_events.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class PutEvents { - public static void main(String[] args) { - final String usage = """ - - Usage: - - - Where: - resourceArn - An Amazon Resource Name (ARN) related to the events. - """; - - if (args.length != 1) { - System.out.println(usage); - System.exit(1); - } - - String resourceArn = args[0]; - CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() - .build(); - - putCWEvents(cwe, resourceArn); - cwe.close(); - } - - public static void putCWEvents(CloudWatchEventsClient cwe, String resourceArn) { - try { - final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; - - PutEventsRequestEntry requestEntry = PutEventsRequestEntry.builder() - .detail(EVENT_DETAILS) - .detailType("sampleSubmitted") - .resources(resourceArn) - .source("aws-sdk-java-cloudwatch-example") - .build(); - - PutEventsRequest request = PutEventsRequest.builder() - .entries(requestEntry) - .build(); - - cwe.putEvents(request); - System.out.println("Successfully put CloudWatch event"); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.put_events.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.put_events.main] +// snippet-start:[cloudwatch.java2.put_events.import] +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; +import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest; +import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry; +// snippet-end:[cloudwatch.java2.put_events.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class PutEvents { + public static void main(String[] args) { + final String usage = """ + + Usage: + + + Where: + resourceArn - An Amazon Resource Name (ARN) related to the events. + """; + + if (args.length != 1) { + System.out.println(usage); + System.exit(1); + } + + String resourceArn = args[0]; + CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() + .build(); + + putCWEvents(cwe, resourceArn); + cwe.close(); + } + + /** + * Sends a custom event to Amazon CloudWatch Events. + * + * @param cwe The {@link CloudWatchEventsClient} used to interact with + * Amazon CloudWatch Events. + * @param resourceArn The ARN (Amazon Resource Name) of the resource associated + * with the event. + * + */ + public static void putCWEvents(CloudWatchEventsClient cwe, String resourceArn) { + try { + final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; + + PutEventsRequestEntry requestEntry = PutEventsRequestEntry.builder() + .detail(EVENT_DETAILS) + .detailType("sampleSubmitted") + .resources(resourceArn) + .source("aws-sdk-java-cloudwatch-example") + .build(); + + PutEventsRequest request = PutEventsRequest.builder() + .entries(requestEntry) + .build(); + + cwe.putEvents(request); + System.out.println("Successfully put CloudWatch event"); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.put_events.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutLogEvents.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutLogEvents.java index 3f3b06ff861..d8f0b38f4d7 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutLogEvents.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutLogEvents.java @@ -1,88 +1,96 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.put_log_events.main] -// snippet-start:[cloudwatch.java2.put_log_events.import] -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; -import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest; -import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse; -import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent; -import software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest; -import java.util.Arrays; -// snippet-end:[cloudwatch.java2.put_log_events.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class PutLogEvents { - public static void main(String[] args) { - final String usage = """ - - Usage: - - - Where: - logGroupName - A log group name. - streamName - A stream name. - """; - - if (args.length != 2) { - System.out.println(usage); - System.exit(1); - } - - String logGroupName = args[0]; - String streamName = args[1]; - CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() - .build(); - - putCWLogEvents(logsClient, logGroupName, streamName); - logsClient.close(); - } - - public static void putCWLogEvents(CloudWatchLogsClient logsClient, String logGroupName, String streamName) { - try { - DescribeLogStreamsRequest logStreamRequest = DescribeLogStreamsRequest.builder() - .logGroupName(logGroupName) - .logStreamNamePrefix(streamName) - .build(); - DescribeLogStreamsResponse describeLogStreamsResponse = logsClient.describeLogStreams(logStreamRequest); - - // Assume that a single stream is returned since a specific stream name was - // specified in the previous request. - String sequenceToken = describeLogStreamsResponse.logStreams().get(0).uploadSequenceToken(); - - // Build an input log message to put to CloudWatch. - InputLogEvent inputLogEvent = InputLogEvent.builder() - .message("{ \"key1\": \"value1\", \"key2\": \"value2\" }") - .timestamp(System.currentTimeMillis()) - .build(); - - // Specify the request parameters. - // Sequence token is required so that the log can be written to the - // latest location in the stream. - PutLogEventsRequest putLogEventsRequest = PutLogEventsRequest.builder() - .logEvents(Arrays.asList(inputLogEvent)) - .logGroupName(logGroupName) - .logStreamName(streamName) - .sequenceToken(sequenceToken) - .build(); - - logsClient.putLogEvents(putLogEventsRequest); - System.out.println("Successfully put CloudWatch log event"); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.put_log_events.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.put_log_events.main] +// snippet-start:[cloudwatch.java2.put_log_events.import] +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; +import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest; +import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse; +import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent; +import software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest; +import java.util.Arrays; +// snippet-end:[cloudwatch.java2.put_log_events.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class PutLogEvents { + public static void main(String[] args) { + final String usage = """ + + Usage: + + + Where: + logGroupName - A log group name. + streamName - A stream name. + """; + + if (args.length != 2) { + System.out.println(usage); + System.exit(1); + } + + String logGroupName = args[0]; + String streamName = args[1]; + CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() + .build(); + + putCWLogEvents(logsClient, logGroupName, streamName); + logsClient.close(); + } + + /** + * Puts a CloudWatch log event to the specified log stream within the given log group. + * + * @param logsClient The CloudWatchLogsClient used to interact with AWS CloudWatch Logs. + * @param logGroupName The name of the log group where the log stream is located. + * @param streamName The name of the log stream to which the log event will be added. + * + */ + public static void putCWLogEvents(CloudWatchLogsClient logsClient, String logGroupName, String streamName) { + try { + DescribeLogStreamsRequest logStreamRequest = DescribeLogStreamsRequest.builder() + .logGroupName(logGroupName) + .logStreamNamePrefix(streamName) + .build(); + DescribeLogStreamsResponse describeLogStreamsResponse = logsClient.describeLogStreams(logStreamRequest); + + // Assume that a single stream is returned since a specific stream name was + // specified in the previous request. + String sequenceToken = describeLogStreamsResponse.logStreams().get(0).uploadSequenceToken(); + + // Build an input log message to put to CloudWatch. + InputLogEvent inputLogEvent = InputLogEvent.builder() + .message("{ \"key1\": \"value1\", \"key2\": \"value2\" }") + .timestamp(System.currentTimeMillis()) + .build(); + + // Specify the request parameters. + // Sequence token is required so that the log can be written to the + // latest location in the stream. + PutLogEventsRequest putLogEventsRequest = PutLogEventsRequest.builder() + .logEvents(Arrays.asList(inputLogEvent)) + .logGroupName(logGroupName) + .logStreamName(streamName) + .sequenceToken(sequenceToken) + .build(); + + logsClient.putLogEvents(putLogEventsRequest); + System.out.println("Successfully put CloudWatch log event"); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.put_log_events.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutRule.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutRule.java index 826cf129e10..cf868cc5c78 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutRule.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutRule.java @@ -1,69 +1,78 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.put_rule.main] -// snippet-start:[cloudwatch.java2.put_rule.import] -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; -import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleRequest; -import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleResponse; -import software.amazon.awssdk.services.cloudwatchevents.model.RuleState; -// snippet-end:[cloudwatch.java2.put_rule.import] - -/** - * Before running this Java V2 code example, set up your development - * environment, including your credentials. - * - * For more information, see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class PutRule { - public static void main(String[] args) { - final String usage = """ - - Usage: - roleArn>\s - - Where: - ruleName - A rule name (for example, myrule). - roleArn - A role ARN value (for example, arn:aws:iam::xxxxxx047983:user/MyUser). - """; - - if (args.length != 2) { - System.out.println(usage); - System.exit(1); - } - - String ruleName = args[0]; - String roleArn = args[1]; - CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() - .build(); - - putCWRule(cwe, ruleName, roleArn); - cwe.close(); - } - - public static void putCWRule(CloudWatchEventsClient cwe, String ruleName, String roleArn) { - try { - PutRuleRequest request = PutRuleRequest.builder() - .name(ruleName) - .roleArn(roleArn) - .scheduleExpression("rate(5 minutes)") - .state(RuleState.ENABLED) - .build(); - - PutRuleResponse response = cwe.putRule(request); - System.out.printf( - "Successfully created CloudWatch events rule %s with arn %s", - roleArn, response.ruleArn()); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.put_rule.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.put_rule.main] +// snippet-start:[cloudwatch.java2.put_rule.import] +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; +import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleRequest; +import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleResponse; +import software.amazon.awssdk.services.cloudwatchevents.model.RuleState; +// snippet-end:[cloudwatch.java2.put_rule.import] + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class PutRule { + public static void main(String[] args) { + final String usage = """ + + Usage: + roleArn>\s + + Where: + ruleName - A rule name (for example, myrule). + roleArn - A role ARN value (for example, arn:aws:iam::xxxxxx047983:user/MyUser). + """; + + if (args.length != 2) { + System.out.println(usage); + System.exit(1); + } + + String ruleName = args[0]; + String roleArn = args[1]; + CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() + .build(); + + putCWRule(cwe, ruleName, roleArn); + cwe.close(); + } + + /** + * Creates a new CloudWatch Events rule with the specified name and role ARN. + * The rule is configured to trigger every 5 minutes and is enabled upon creation. + * + * @param cwe The {@link CloudWatchEventsClient} used to interact with AWS CloudWatch Events. + * @param ruleName The name of the CloudWatch Events rule to be created. + * @param roleArn The ARN of the IAM role that CloudWatch Events can assume to execute the rule. + * + */ + public static void putCWRule(CloudWatchEventsClient cwe, String ruleName, String roleArn) { + try { + PutRuleRequest request = PutRuleRequest.builder() + .name(ruleName) + .roleArn(roleArn) + .scheduleExpression("rate(5 minutes)") + .state(RuleState.ENABLED) + .build(); + + PutRuleResponse response = cwe.putRule(request); + System.out.printf( + "Successfully created CloudWatch events rule %s with arn %s", + roleArn, response.ruleArn()); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.put_rule.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutSubscriptionFilter.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutSubscriptionFilter.java index 222b4fe380a..4978288c244 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutSubscriptionFilter.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutSubscriptionFilter.java @@ -1,96 +1,105 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.put_subscription_filter.main] -// snippet-start:[cloudwatch.java2.put_subscription_filter.import] -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.PutSubscriptionFilterRequest; -// snippet-end:[cloudwatch.java2.put_subscription_filter.import] - -/** - * Before running this code example, you need to grant permission to CloudWatch - * Logs the right to execute your Lambda function. - * To perform this task, you can use this CLI command: - * - * aws lambda add-permission --function-name "lamda1" --statement-id "lamda1" - * --principal "logs.us-west-2.amazonaws.com" --action "lambda:InvokeFunction" - * --source-arn "arn:aws:logs:us-west-2:111111111111:log-group:testgroup:*" - * --source-account "111111111111" - * - * Make sure you replace the function name with your function name and replace - * '111111111111' with your account details. - * For more information, see "Subscription Filters with AWS Lambda" in the - * Amazon CloudWatch Logs Guide. - * - * - * Also, before running this Java V2 code example,set up your development - * environment,including your credentials. - * - * For more information,see the following documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - * - */ - -public class PutSubscriptionFilter { - public static void main(String[] args) { - final String usage = """ - - Usage: - \s - - Where: - filter - A filter name (for example, myfilter). - pattern - A filter pattern (for example, ERROR). - logGroup - A log group name (testgroup). - functionArn - An AWS Lambda function ARN (for example, arn:aws:lambda:us-west-2:111111111111:function:lambda1) . - """; - - if (args.length != 4) { - System.out.println(usage); - System.exit(1); - } - - String filter = args[0]; - String pattern = args[1]; - String logGroup = args[2]; - String functionArn = args[3]; - Region region = Region.US_WEST_2; - CloudWatchLogsClient cwl = CloudWatchLogsClient.builder() - .region(region) - .build(); - - putSubFilters(cwl, filter, pattern, logGroup, functionArn); - cwl.close(); - } - - public static void putSubFilters(CloudWatchLogsClient cwl, - String filter, - String pattern, - String logGroup, - String functionArn) { - - try { - PutSubscriptionFilterRequest request = PutSubscriptionFilterRequest.builder() - .filterName(filter) - .filterPattern(pattern) - .logGroupName(logGroup) - .destinationArn(functionArn) - .build(); - - cwl.putSubscriptionFilter(request); - System.out.printf( - "Successfully created CloudWatch logs subscription filter %s", - filter); - - } catch (CloudWatchLogsException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.put_subscription_filter.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.put_subscription_filter.main] +// snippet-start:[cloudwatch.java2.put_subscription_filter.import] +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.PutSubscriptionFilterRequest; +// snippet-end:[cloudwatch.java2.put_subscription_filter.import] + +/** + * Before running this code example, you need to grant permission to CloudWatch + * Logs the right to execute your Lambda function. + * To perform this task, you can use this CLI command: + * + * aws lambda add-permission --function-name "lamda1" --statement-id "lamda1" + * --principal "logs.us-west-2.amazonaws.com" --action "lambda:InvokeFunction" + * --source-arn "arn:aws:logs:us-west-2:111111111111:log-group:testgroup:*" + * --source-account "111111111111" + * + * Make sure you replace the function name with your function name and replace + * '111111111111' with your account details. + * For more information, see "Subscription Filters with AWS Lambda" in the + * Amazon CloudWatch Logs Guide. + * + * + * Also, before running this Java V2 code example,set up your development + * environment,including your credentials. + * + * For more information,see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + * + */ + +public class PutSubscriptionFilter { + public static void main(String[] args) { + final String usage = """ + + Usage: + \s + + Where: + filter - A filter name (for example, myfilter). + pattern - A filter pattern (for example, ERROR). + logGroup - A log group name (testgroup). + functionArn - An AWS Lambda function ARN (for example, arn:aws:lambda:us-west-2:111111111111:function:lambda1) . + """; + + if (args.length != 4) { + System.out.println(usage); + System.exit(1); + } + + String filter = args[0]; + String pattern = args[1]; + String logGroup = args[2]; + String functionArn = args[3]; + Region region = Region.US_WEST_2; + CloudWatchLogsClient cwl = CloudWatchLogsClient.builder() + .region(region) + .build(); + + putSubFilters(cwl, filter, pattern, logGroup, functionArn); + cwl.close(); + } + + /** + * Creates a new subscription filter for a specified CloudWatch Logs log group. + * + * @param cwl the CloudWatchLogsClient used to interact with AWS CloudWatch Logs + * @param filter the name of the subscription filter to create + * @param pattern the filter pattern for the subscription filter + * @param logGroup the name of the log group to which the subscription filter should be applied + * @param functionArn the ARN of the destination (e.g., a Lambda function) for the subscription filter + */ + public static void putSubFilters(CloudWatchLogsClient cwl, + String filter, + String pattern, + String logGroup, + String functionArn) { + + try { + PutSubscriptionFilterRequest request = PutSubscriptionFilterRequest.builder() + .filterName(filter) + .filterPattern(pattern) + .logGroupName(logGroup) + .destinationArn(functionArn) + .build(); + + cwl.putSubscriptionFilter(request); + System.out.printf( + "Successfully created CloudWatch logs subscription filter %s", + filter); + + } catch (CloudWatchLogsException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.put_subscription_filter.main] diff --git a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutTargets.java b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutTargets.java index ea8b5b569c4..3b6b00afdf4 100644 --- a/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutTargets.java +++ b/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutTargets.java @@ -1,73 +1,82 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.cloudwatch; - -// snippet-start:[cloudwatch.java2.put_targets.main] -// snippet-start:[cloudwatch.java2.put_targets.import] -import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; -import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; -import software.amazon.awssdk.services.cloudwatchevents.model.PutTargetsRequest; -import software.amazon.awssdk.services.cloudwatchevents.model.Target; -// snippet-end:[cloudwatch.java2.put_targets.import] - -/** - * To run this Java V2 code example, ensure that you have setup your development - * environment, including your credentials. - * - * For information, see this documentation topic: - * - * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - */ -public class PutTargets { - public static void main(String[] args) { - final String usage = """ - - Usage: - \s - - Where: - ruleName - A rule name (for example, myrule). - functionArn - An AWS Lambda function ARN (for example, arn:aws:lambda:us-west-2:xxxxxx047983:function:lamda1). - targetId - A target id value. - """; - - if (args.length != 3) { - System.out.println(usage); - System.exit(1); - } - - String ruleName = args[0]; - String functionArn = args[1]; - String targetId = args[2]; - CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() - .build(); - - putCWTargets(cwe, ruleName, functionArn, targetId); - cwe.close(); - } - - public static void putCWTargets(CloudWatchEventsClient cwe, String ruleName, String functionArn, String targetId) { - try { - Target target = Target.builder() - .arn(functionArn) - .id(targetId) - .build(); - - PutTargetsRequest request = PutTargetsRequest.builder() - .targets(target) - .rule(ruleName) - .build(); - - cwe.putTargets(request); - System.out.printf( - "Successfully created CloudWatch events target for rule %s", - ruleName); - - } catch (CloudWatchException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -} -// snippet-end:[cloudwatch.java2.put_targets.main] +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.cloudwatch; + +// snippet-start:[cloudwatch.java2.put_targets.main] +// snippet-start:[cloudwatch.java2.put_targets.import] +import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; +import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; +import software.amazon.awssdk.services.cloudwatchevents.model.PutTargetsRequest; +import software.amazon.awssdk.services.cloudwatchevents.model.Target; +// snippet-end:[cloudwatch.java2.put_targets.import] + +/** + * To run this Java V2 code example, ensure that you have setup your development + * environment, including your credentials. + * + * For information, see this documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ +public class PutTargets { + public static void main(String[] args) { + final String usage = """ + + Usage: + \s + + Where: + ruleName - A rule name (for example, myrule). + functionArn - An AWS Lambda function ARN (for example, arn:aws:lambda:us-west-2:xxxxxx047983:function:lamda1). + targetId - A target id value. + """; + + if (args.length != 3) { + System.out.println(usage); + System.exit(1); + } + + String ruleName = args[0]; + String functionArn = args[1]; + String targetId = args[2]; + CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() + .build(); + + putCWTargets(cwe, ruleName, functionArn, targetId); + cwe.close(); + } + + /** + * Adds a target to a specified CloudWatch Events rule. + * + * @param cwe The CloudWatchEventsClient used to interact with Amazon CloudWatch Events. + * @param ruleName The name of the CloudWatch Events rule to which the target will be added. + * @param functionArn The Amazon Resource Name (ARN) of the AWS Lambda function to be invoked by the target. + * @param targetId The unique identifier for the target. + * + */ + public static void putCWTargets(CloudWatchEventsClient cwe, String ruleName, String functionArn, String targetId) { + try { + Target target = Target.builder() + .arn(functionArn) + .id(targetId) + .build(); + + PutTargetsRequest request = PutTargetsRequest.builder() + .targets(target) + .rule(ruleName) + .build(); + + cwe.putTargets(request); + System.out.printf( + "Successfully created CloudWatch events target for rule %s", + ruleName); + + } catch (CloudWatchException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } +} +// snippet-end:[cloudwatch.java2.put_targets.main]