There are more AWS SDK examples available in the AWS Doc SDK Examples
Use CreateSamplingRule with an AWS SDK or CLI
The following code examples show how to use CreateSamplingRule.
- CLI
-
- AWS CLI
-
To create a sampling rule
The following
create-sampling-ruleexample creates a rule to control sampling behavior for instrumented applications. The rules are provided by a JSON file. The majority of the sampling rule fields are required to create the rule.aws xray create-sampling-rule \ --cli-input-jsonfile://9000-base-scorekeep.jsonContents of
9000-base-scorekeep.json:{ "SamplingRule": { "RuleName": "base-scorekeep", "ResourceARN": "*", "Priority": 9000, "FixedRate": 0.1, "ReservoirSize": 5, "ServiceName": "Scorekeep", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1 } }Output:
{ "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 } }For more information, see Configuring Sampling, Groups, and Encryption Settings with the AWS X-Ray API in the AWS X-Ray Developer Guide.
-
For API details, see CreateSamplingRule
in AWS CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. 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); } } }-
For API details, see CreateSamplingRule in AWS SDK for Java 2.x API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. 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}") } }-
For API details, see CreateSamplingRule
in AWS SDK for Kotlin API reference.
-