View a markdown version of this page

CreateSamplingRule与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

CreateSamplingRule与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 CreateSamplingRule

CLI
AWS CLI

创建采样规则

以下 create-sampling-rule 示例创建规则以控制仪器应用程序的采样行为。这些规则由 JSON 文件提供。大多数采样规则字段都是创建规则所必需的。

aws xray create-sampling-rule \ --cli-input-json file://9000-base-scorekeep.json

9000-base-scorekeep.json 的内容:

{ "SamplingRule": { "RuleName": "base-scorekeep", "ResourceARN": "*", "Priority": 9000, "FixedRate": 0.1, "ReservoirSize": 5, "ServiceName": "Scorekeep", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1 } }

输出:

{ "SamplingRuleRecord": { "SamplingRule": { "RuleName": "base-scorekeep", "RuleARN": "arn:aws:xray:us-west-2:123456789012:sampling-rule/base-scorekeep", "ResourceARN": "*", "Priority": 9000, "FixedRate": 0.1, "ReservoirSize": 5, "ServiceName": "Scorekeep", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1, "Attributes": {} }, "CreatedAt": 1530574410.0, "ModifiedAt": 1530574410.0 } }

有关更多信息,请参阅《X-Ray 开发人员指南》中的 “使用 AWS X-Ray API 配置采样、分组和加密设置”。AWS

Java
适用于 Java 的 SDK 2.x
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.xray.XRayClient; import software.amazon.awssdk.services.xray.model.CreateSamplingRuleRequest; import software.amazon.awssdk.services.xray.model.SamplingRule; import software.amazon.awssdk.services.xray.model.XRayException; import software.amazon.awssdk.services.xray.model.CreateSamplingRuleResponse; /** * 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 CreateSamplingRule { public static void main(String[] args) { final String usage = """ Usage: <ruleName> Where: ruleName - The name of the rule\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; Region region = Region.US_EAST_1; XRayClient xRayClient = XRayClient.builder() .region(region) .build(); createRule(xRayClient, ruleName); } public static void createRule(XRayClient xRayClient, String ruleName) { try { SamplingRule rule = SamplingRule.builder() .ruleName(ruleName) .priority(1) .httpMethod("*") .serviceType("*") .serviceName("*") .urlPath("*") .version(1) .host("*") .resourceARN("*") .build(); CreateSamplingRuleRequest ruleRequest = CreateSamplingRuleRequest.builder() .samplingRule(rule) .build(); CreateSamplingRuleResponse ruleResponse = xRayClient.createSamplingRule(ruleRequest); System.out.println( "The ARN of the new rule is " + ruleResponse.samplingRuleRecord().samplingRule().ruleARN()); } catch (XRayException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考CreateSamplingRule中的。

Kotlin
适用于 Kotlin 的 SDK
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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 的详细信息,请参阅适用CreateSamplingRule于 K otlin 的AWS SDK API 参考