

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用適用於 Kotlin 的 SDK 的 X-Ray 範例
<a name="kotlin_1_xray_code_examples"></a>

下列程式碼範例示範如何使用適用於 Kotlin 的 AWS SDK 搭配 X-Ray 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `CreateGroup`
<a name="xray_CreateGroup_kotlin_1_topic"></a>

以下程式碼範例顯示如何使用 `CreateGroup`。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/xray#code-examples)中設定和執行。

```
suspend fun createNewGroup(groupNameVal: String?) {
    val groupRequest =
        CreateGroupRequest {
            filterExpression = "fault = true AND http.url CONTAINS \"example/game\" AND responsetime >= 5"
            groupName = groupNameVal
        }

    XRayClient.fromEnvironment { region = "us-east-1" }.use { xRayClient ->
        val groupResponse = xRayClient.createGroup(groupRequest)
        println("The Group ARN is " + (groupResponse.group?.groupArn))
    }
}
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS Kotlin 的 SDK API 參考*》中的 [CreateGroup](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `CreateSamplingRule`
<a name="xray_CreateSamplingRule_kotlin_1_topic"></a>

以下程式碼範例顯示如何使用 `CreateSamplingRule`。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/xray#code-examples)中設定和執行。

```
suspend fun createRule(ruleNameVal: String?) {
    val rule =
        SamplingRule {
            ruleName = ruleNameVal
            priority = 1
            httpMethod = "*"
            serviceType = "*"
            serviceName = "*"
            urlPath = "*"
            version = 1
            host = "*"
            resourceArn = "*"
        }

    val ruleRequest =
        CreateSamplingRuleRequest {
            samplingRule = rule
        }

    XRayClient.fromEnvironment { region = "us-east-1" }.use { xRayClient ->
        val ruleResponse: CreateSamplingRuleResponse = xRayClient.createSamplingRule(ruleRequest)
        println("The ARN of the new rule is ${ruleResponse.samplingRuleRecord?.samplingRule?.ruleArn}")
    }
}
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS Kotlin 的 SDK API 參考*》中的 [CreateSamplingRule](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DeleteGroup`
<a name="xray_DeleteGroup_kotlin_1_topic"></a>

以下程式碼範例顯示如何使用 `DeleteGroup`。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/xray#code-examples)中設定和執行。

```
suspend fun deleteSpecificGroup(groupNameVal: String) {
    val groupRequest =
        DeleteGroupRequest {
            groupName = groupNameVal
        }

    XRayClient.fromEnvironment { region = "us-east-1" }.use { xRayClient ->
        xRayClient.deleteGroup(groupRequest)
        println("$groupNameVal was deleted!")
    }
}
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS Kotlin 的 SDK API 參考*》中的 [DeleteGroup](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DeleteSamplingRule`
<a name="xray_DeleteSamplingRule_kotlin_1_topic"></a>

以下程式碼範例顯示如何使用 `DeleteSamplingRule`。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/xray#code-examples)中設定和執行。

```
suspend fun deleteRule(ruleNameVal: String?) {
    val ruleRequest =
        DeleteSamplingRuleRequest {
            ruleName = ruleNameVal
        }

    XRayClient.fromEnvironment { region = "us-east-1" }.use { xRayClient ->
        xRayClient.deleteSamplingRule(ruleRequest)
        println("$ruleNameVal was deleted")
    }
}
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS Kotlin 的 SDK API 參考*》中的 [DeleteSamplingRule](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `GetGroups`
<a name="xray_GetGroups_kotlin_1_topic"></a>

以下程式碼範例顯示如何使用 `GetGroups`。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/xray#code-examples)中設定和執行。

```
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](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `GetSamplingRules`
<a name="xray_GetSamplingRules_kotlin_1_topic"></a>

以下程式碼範例顯示如何使用 `GetSamplingRules`。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/xray#code-examples)中設定和執行。

```
suspend fun getRules() {
    XRayClient.fromEnvironment { region = "us-east-1" }.use { xRayClient ->
        val response = xRayClient.getSamplingRules(GetSamplingRulesRequest {})
        response.samplingRuleRecords?.forEach { record ->
            println("The rule name is ${record.samplingRule?.ruleName}")
            println("The related service is: ${record.samplingRule?.serviceName}")
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS Kotlin 的 SDK API 參考*》中的 [GetSamplingRules](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `GetServiceGraph`
<a name="xray_GetServiceGraph_kotlin_1_topic"></a>

以下程式碼範例顯示如何使用 `GetServiceGraph`。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/xray#code-examples)中設定和執行。

```
suspend fun getGraph(groupNameVal: String?) {
    val time = aws.smithy.kotlin.runtime.time.Instant
    val getServiceGraphRequest =
        GetServiceGraphRequest {
            groupName = groupNameVal
            this.startTime = time.now()
            endTime = time.now()
        }
    XRayClient.fromEnvironment { region = "us-east-1" }.use { xRayClient ->
        val response = xRayClient.getServiceGraph(getServiceGraphRequest)
        response.services?.forEach { service ->
            println("The name of the service is  ${service.name}")
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS Kotlin 的 SDK API 參考*》中的 [GetServiceGraph](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。