

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh [SDK AWS Doc](https://github.com/awsdocs/aws-doc-sdk-examples). GitHub 

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Contoh X-Ray menggunakan SDK untuk Kotlin
<a name="kotlin_1_xray_code_examples"></a>

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK untuk Kotlin dengan X-Ray.

*Tindakan* merupakan kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

**Topics**
+ [Tindakan](#actions)

## Tindakan
<a name="actions"></a>

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

Contoh kode berikut menunjukkan cara menggunakan`CreateGroup`.

**SDK untuk Kotlin**  
 Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di [Repositori Contoh Kode 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))
    }
}
```
+  Untuk detail API, lihat [CreateGroup](https://sdk.amazonaws.com/kotlin/api/latest/index.html)di *AWS SDK untuk referensi API Kotlin*. 

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

Contoh kode berikut menunjukkan cara menggunakan`CreateSamplingRule`.

**SDK untuk Kotlin**  
 Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di [Repositori Contoh Kode 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}")
    }
}
```
+  Untuk detail API, lihat [CreateSamplingRule](https://sdk.amazonaws.com/kotlin/api/latest/index.html)di *AWS SDK untuk referensi API Kotlin*. 

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

Contoh kode berikut menunjukkan cara menggunakan`DeleteGroup`.

**SDK untuk Kotlin**  
 Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di [Repositori Contoh Kode 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!")
    }
}
```
+  Untuk detail API, lihat [DeleteGroup](https://sdk.amazonaws.com/kotlin/api/latest/index.html)di *AWS SDK untuk referensi API Kotlin*. 

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

Contoh kode berikut menunjukkan cara menggunakan`DeleteSamplingRule`.

**SDK untuk Kotlin**  
 Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di [Repositori Contoh Kode 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")
    }
}
```
+  Untuk detail API, lihat [DeleteSamplingRule](https://sdk.amazonaws.com/kotlin/api/latest/index.html)di *AWS SDK untuk referensi API Kotlin*. 

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

Contoh kode berikut menunjukkan cara menggunakan`GetGroups`.

**SDK untuk Kotlin**  
 Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di [Repositori Contoh Kode 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}")
        }
    }
}
```
+  Untuk detail API, lihat [GetGroups](https://sdk.amazonaws.com/kotlin/api/latest/index.html)di *AWS SDK untuk referensi API Kotlin*. 

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

Contoh kode berikut menunjukkan cara menggunakan`GetSamplingRules`.

**SDK untuk Kotlin**  
 Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di [Repositori Contoh Kode 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}")
        }
    }
}
```
+  Untuk detail API, lihat [GetSamplingRules](https://sdk.amazonaws.com/kotlin/api/latest/index.html)di *AWS SDK untuk referensi API Kotlin*. 

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

Contoh kode berikut menunjukkan cara menggunakan`GetServiceGraph`.

**SDK untuk Kotlin**  
 Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di [Repositori Contoh Kode 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}")
        }
    }
}
```
+  Untuk detail API, lihat [GetServiceGraph](https://sdk.amazonaws.com/kotlin/api/latest/index.html)di *AWS SDK untuk referensi API Kotlin*. 