View a markdown version of this page

Gunakan GetSamplingRules dengan AWS SDK atau CLI - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

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

Gunakan GetSamplingRules dengan AWS SDK atau CLI

Contoh kode berikut menunjukkan cara menggunakanGetSamplingRules.

CLI
AWS CLI

Untuk mengambil semua aturan pengambilan sampel

get-sampling-rulesContoh berikut menampilkan rincian untuk semua aturan sampling yang tersedia. :

aws xray get-sampling-rules

Output:

{ "SamplingRuleRecords": [ { "SamplingRule": { "RuleName": "Default", "RuleARN": "arn:aws:xray:us-east-1::sampling-rule/Default", "ResourceARN": "*", "Priority": 10000, "FixedRate": 0.01, "ReservoirSize": 0, "ServiceName": "*", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1, "Attributes": {} }, "CreatedAt": 0.0, "ModifiedAt": 1530558121.0 }, { "SamplingRule": { "RuleName": "base-scorekeep", "RuleARN": "arn:aws:xray:us-east-1::sampling-rule/base-scorekeep", "ResourceARN": "*", "Priority": 9000, "FixedRate": 0.1, "ReservoirSize": 2, "ServiceName": "Scorekeep", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1, "Attributes": {} }, "CreatedAt": 1530573954.0, "ModifiedAt": 1530920505.0 }, { "SamplingRule": { "RuleName": "polling-scorekeep", "RuleARN": "arn:aws:xray:us-east-1::sampling-rule/polling-scorekeep", "ResourceARN": "*", "Priority": 5000, "FixedRate": 0.003, "ReservoirSize": 0, "ServiceName": "Scorekeep", "ServiceType": "*", "Host": "*", "HTTPMethod": "GET", "URLPath": "/api/state/*", "Version": 1, "Attributes": {} }, "CreatedAt": 1530918163.0, "ModifiedAt": 1530918163.0 } ] }

Untuk informasi selengkapnya, lihat Menggunakan Aturan Pengambilan Sampel dengan X-Ray API di Panduan Pengembang AWS X-Ray.

Java
SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode 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); } } }
  • Untuk detail API, lihat GetSamplingRulesdi Referensi AWS SDK for Java 2.x API.

Kotlin
SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di Repositori Contoh Kode AWS.

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 GetSamplingRulesdi AWS SDK untuk referensi API Kotlin.