View a markdown version of this page

GetGroups 搭配 AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

GetGroups 搭配 AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 GetGroups

CLI
AWS CLI

擷取所有群組

下列範例會顯示所有作用中群組的詳細資訊。

aws xray get-groups

輸出:

{ "Groups": [ { "GroupName": "AdminGroup", "GroupARN": "arn:aws:xray:us-west-2:123456789012:group/AdminGroup/123456789", "FilterExpression": "service(\"example.com\") {fault OR error}" }, { "GroupName": "SDETGroup", "GroupARN": "arn:aws:xray:us-west-2:123456789012:group/SDETGroup/987654321", "FilterExpression": "responsetime > 2" } ] }

如需詳細資訊,請參閱《AWS X-Ray 開發人員指南》中的使用 X-Ray API 設定取樣、群組和加密設定AWS

  • 如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 GetGroups

Java
SDK for Java 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

Kotlin
適用於 Kotlin 的 SDK
注意

GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun getAllGroups() { XRayClient.fromEnvironment { region = "us-east-1" }.use { xRayClient -> val response = xRayClient.getGroups(GetGroupsRequest {}) response.groups?.forEach { group -> println("The AWS X-Ray group name is ${group.groupName}") } } }
  • 如需 API 詳細資訊,請參閱《適用於 AWS Kotlin 的 SDK API 參考》中的 GetGroups