文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用适用于 Java 的 SDK 的 X-Ray 示例 2.x
以下代码示例向您展示了如何使用 with X-Ray 来执行操作和实现常见场景。 AWS SDK for Java 2.x
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示了如何使用 CreateGroup。
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.CreateGroupRequest; import software.amazon.awssdk.services.xray.model.CreateGroupResponse; import software.amazon.awssdk.services.xray.model.XRayException; /** * 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 CreateGroup { public static void main(String[] args) { final String usage = """ Usage: <groupName> Where: groupName - The name of the group to create\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String groupName = args[0]; Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .build(); createNewGroup(xRayClient, groupName); } public static void createNewGroup(XRayClient xRayClient, String groupName) { try { CreateGroupRequest groupRequest = CreateGroupRequest.builder() .filterExpression("fault = true AND http.url CONTAINS \"example/game\" AND responsetime >= 5") .groupName(groupName) .build(); CreateGroupResponse groupResponse = xRayClient.createGroup(groupRequest); System.out.println("The Group ARN is " + groupResponse.group().groupARN()); } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考CreateGroup中的。
-
以下代码示例演示了如何使用 CreateSamplingRule。
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.CreateSamplingRuleRequest; import software.amazon.awssdk.services.xray.model.SamplingRule; import software.amazon.awssdk.services.xray.model.XRayException; import software.amazon.awssdk.services.xray.model.CreateSamplingRuleResponse; /** * 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 CreateSamplingRule { public static void main(String[] args) { final String usage = """ Usage: <ruleName> Where: ruleName - The name of the rule\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .build(); createRule(xRayClient, ruleName); } public static void createRule(XRayClient xRayClient, String ruleName) { try { SamplingRule rule = SamplingRule.builder() .ruleName(ruleName) .priority(1) .httpMethod("*") .serviceType("*") .serviceName("*") .urlPath("*") .version(1) .host("*") .resourceARN("*") .build(); CreateSamplingRuleRequest ruleRequest = CreateSamplingRuleRequest.builder() .samplingRule(rule) .build(); CreateSamplingRuleResponse ruleResponse = xRayClient.createSamplingRule(ruleRequest); System.out.println( "The ARN of the new rule is " + ruleResponse.samplingRuleRecord().samplingRule().ruleARN()); } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考CreateSamplingRule中的。
-
以下代码示例演示了如何使用 DeleteGroup。
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.DeleteGroupRequest; import software.amazon.awssdk.services.xray.model.XRayException; /** * 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 DeleteGroup { public static void main(String[] args) { final String usage = """ Usage: <groupName> Where: groupName - The name of the group to delete\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String groupName = args[0]; Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .build(); deleteSpecificGroup(xRayClient, groupName); } public static void deleteSpecificGroup(XRayClient xRayClient, String groupName) { try { DeleteGroupRequest groupRequest = DeleteGroupRequest.builder() .groupName(groupName) .build(); xRayClient.deleteGroup(groupRequest); System.out.println(groupName + " was deleted!"); } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考DeleteGroup中的。
-
以下代码示例演示了如何使用 DeleteSamplingRule。
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.DeleteSamplingRuleRequest; import software.amazon.awssdk.services.xray.model.XRayException; /** * 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 DeleteSamplingRule { public static void main(String[] args) { final String usage = """ Usage: <ruleName> Where: ruleName - The name of the rule to delete\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .build(); deleteRule(xRayClient, ruleName); } public static void deleteRule(XRayClient xRayClient, String ruleName) { try { DeleteSamplingRuleRequest ruleRequest = DeleteSamplingRuleRequest.builder() .ruleName(ruleName) .build(); xRayClient.deleteSamplingRule(ruleRequest); } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考DeleteSamplingRule中的。
-
以下代码示例演示了如何使用 GetGroups。
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.GetGroupsResponse; import software.amazon.awssdk.services.xray.model.GroupSummary; import software.amazon.awssdk.services.xray.model.XRayException; 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 */ public class GetGroups { public static void main(String[] args) { Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .build(); getAllGroups(xRayClient); } public static void getAllGroups(XRayClient xRayClient) { try { GetGroupsResponse groupsResponse = xRayClient.getGroups(); List<GroupSummary> groups = groupsResponse.groups(); for (GroupSummary group : groups) { System.out.println("The AWS XRay group name is " + group.groupName()); } } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考GetGroups中的。
-
以下代码示例演示了如何使用 GetSamplingRules。
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.GetSamplingRulesResponse; import software.amazon.awssdk.services.xray.model.SamplingRuleRecord; import software.amazon.awssdk.services.xray.model.XRayException; 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 */ public class GetSamplingRules { public static void main(String[] args) { Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); getRules(xRayClient); } public static void getRules(XRayClient xRayClient) { try { GetSamplingRulesResponse response = xRayClient.getSamplingRules(r -> r.build()); List<SamplingRuleRecord> records = response.samplingRuleRecords(); for (SamplingRuleRecord record : records) { System.out.println("The rule name is: " + record.samplingRule().ruleName()); System.out.println("The related service is: " + record.samplingRule().serviceName()); } } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考GetSamplingRules中的。
-
以下代码示例演示了如何使用 GetServiceGraph。
- 适用于 Java 的 SDK 2.x
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.GetServiceGraphRequest; import software.amazon.awssdk.services.xray.model.GetServiceGraphResponse; import software.amazon.awssdk.services.xray.model.Service; import software.amazon.awssdk.services.xray.model.XRayException; import java.time.LocalDateTime; import java.time.Instant; import java.time.ZoneId; 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 */ public class GetServiceGraph { public static void main(String[] args) { final String usage = """ Usage: <groupName> Where: groupName - The name of a group based on which you want to generate a graph. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String groupName = args[0]; Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .build(); getGraph(xRayClient, groupName); } public static void getGraph(XRayClient xRayClient, String groupName) { try { // The Instant values have to be 6 hours apart. LocalDateTime localDateTime = LocalDateTime.parse("2022-03-09T06:00:00"); Instant start = localDateTime.atZone(ZoneId.of("America/New_York")).toInstant(); LocalDateTime localDateTime2 = LocalDateTime.parse("2022-03-09T12:00:00"); Instant end = localDateTime2.atZone(ZoneId.of("America/New_York")).toInstant(); GetServiceGraphRequest getServiceGraphRequest = GetServiceGraphRequest.builder() .groupName(groupName) .startTime(start) .endTime(end) .build(); GetServiceGraphResponse graphResponse = xRayClient.getServiceGraph(getServiceGraphRequest); List<Service> services = graphResponse.services(); for (Service service : services) { System.out.println("The name of the service is " + service.name()); } } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }-
有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考GetServiceGraph中的。
-