

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 [AWS](https://github.com/awsdocs/aws-doc-sdk-examples)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# SDK for Kotlin を使用した X-Ray の例
<a name="kotlin_1_xray_code_examples"></a>

次のコード例は、X-Ray で AWS SDK for Kotlin を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

**Topics**
+ [アクション](#actions)

## アクション
<a name="actions"></a>

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

次の例は、`CreateGroup` を使用する方法を説明しています。

**SDK for Kotlin**  
 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 SDK for Kotlin API リファレンス*の「[CreateGroup](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

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

次の例は、`CreateSamplingRule` を使用する方法を説明しています。

**SDK for Kotlin**  
 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 SDK for Kotlin API リファレンス*の「[CreateSamplingRule](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

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

次の例は、`DeleteGroup` を使用する方法を説明しています。

**SDK for Kotlin**  
 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 SDK for Kotlin API リファレンス*の[DeleteGroup](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

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

次の例は、`DeleteSamplingRule` を使用する方法を説明しています。

**SDK for Kotlin**  
 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 SDK for Kotlin API リファレンス*の[DeleteSamplingRule](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

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

次の例は、`GetGroups` を使用する方法を説明しています。

**SDK for Kotlin**  
 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 SDK for Kotlin API リファレンス*の[GetGroups](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

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

次の例は、`GetSamplingRules` を使用する方法を説明しています。

**SDK for Kotlin**  
 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 SDK for Kotlin API リファレンス*の[GetSamplingRules](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

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

次の例は、`GetServiceGraph` を使用する方法を説明しています。

**SDK for Kotlin**  
 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 SDK for Kotlin API リファレンス*の[GetServiceGraph](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。