使用 AWS X-Ray API 設定抽樣、分組和加密設定 - AWS X-Ray

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 AWS X-Ray API 設定抽樣、分組和加密設定

AWS X-Ray 提供可設定抽樣規則、群組規則和加密設定的 API。

加密設定

使用PutEncryptionConfig來指定AWS Key Management Service(AWS KMS) 密鑰以用於加密。

注意

X-Ray 不支援非對稱 KMS 密鑰。

$ aws xray put-encryption-config --type KMS --key-id alias/aws/xray { "EncryptionConfig": { "KeyId": "arn:aws:kms:us-east-2:123456789012:key/c234g4e8-39e9-4gb0-84e2-b0ea215cbba5", "Status": "UPDATING", "Type": "KMS" } }

針對金鑰 ID,您可以使用別名 (如範例中所示)、金鑰 ID 或 Amazon Resource Name (ARN)。

使用 GetEncryptionConfig 以取得目前的組態。當 X-Ray 完成應用設定時,狀態會從UPDATINGACTIVE

$ aws xray get-encryption-config { "EncryptionConfig": { "KeyId": "arn:aws:kms:us-east-2:123456789012:key/c234g4e8-39e9-4gb0-84e2-b0ea215cbba5", "Status": "ACTIVE", "Type": "KMS" } }

若要停止使用 KMS 密鑰並使用默認加密,請將加密類型設為NONE

$ aws xray put-encryption-config --type NONE { "EncryptionConfig": { "Status": "UPDATING", "Type": "NONE" } }

抽樣規則

您可以管理抽樣規則在您的帳户中使用 X-Ray API。如需新增和管理標籤的詳細資訊,請參。標記 X-Ray 取樣規則和群組

使用 GetSamplingRules 取得所有抽樣規則。

$ aws xray get-sampling-rules { "SamplingRuleRecords": [ { "SamplingRule": { "RuleName": "Default", "RuleARN": "arn:aws:xray:us-east-2:123456789012:sampling-rule/Default", "ResourceARN": "*", "Priority": 10000, "FixedRate": 0.05, "ReservoirSize": 1, "ServiceName": "*", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1, "Attributes": {} }, "CreatedAt": 0.0, "ModifiedAt": 1529959993.0 } ] }

預設規則會套用至不符合其他規則的所有請求。這是優先順序最低的規則,且無法刪除。不過,您可以使用 UpdateSamplingRule 變更速率和儲槽大小。

範例 的 API 輸入UpdateSamplingRule— 10000-scorekee.json
{ "SamplingRuleUpdate": { "RuleName": "Default", "FixedRate": 0.01, "ReservoirSize": 0 } }

以下範例使用之前的檔案做為輸入,將預設規則變更為 1%、無儲槽。標籤是選擇性的。如果選擇添加標籤,則需要標籤鍵,標籤值是可選的。要從採樣規則中刪除現有標籤,請使用UntagResource

$ aws xray update-sampling-rule --cli-input-json file://1000-default.json --tags [{"Key": "key_name","Value": "value"},{"Key": "key_name","Value": "value"}] { "SamplingRuleRecords": [ { "SamplingRule": { "RuleName": "Default", "RuleARN": "arn:aws:xray:us-east-2:123456789012:sampling-rule/Default", "ResourceARN": "*", "Priority": 10000, "FixedRate": 0.01, "ReservoirSize": 0, "ServiceName": "*", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1, "Attributes": {} }, "CreatedAt": 0.0, "ModifiedAt": 1529959993.0 },

使用 CreateSamplingRule 建立額外的抽樣規則。當您建立規則時,大多數規則欄位都必須填寫。以下範例將建立兩個規則。第一個規則會設定 Scorekeep 範例應用程式的基本速率。此規則適用於所有 API 提供但不符合更高優先順序之規則的請求。

範例 的 API 輸入UpdateSamplingRule— 9000-scorekeep.json
{ "SamplingRule": { "RuleName": "base-scorekeep", "ResourceARN": "*", "Priority": 9000, "FixedRate": 0.1, "ReservoirSize": 5, "ServiceName": "Scorekeep", "ServiceType": "*", "Host": "*", "HTTPMethod": "*", "URLPath": "*", "Version": 1 } }

第二個規則也會套用至 Scorekeep,但它的優先順序更高且更具體。此規則會設定非常低的抽樣速率以輪詢請求。這些是用戶端每隔幾秒發出的 GET 請求,以檢查遊戲狀態的變更。

範例 的 API 輸入UpdateSamplingRule— 5000-polling-scorekeep.json
{ "SamplingRule": { "RuleName": "polling-scorekeep", "ResourceARN": "*", "Priority": 5000, "FixedRate": 0.003, "ReservoirSize": 0, "ServiceName": "Scorekeep", "ServiceType": "*", "Host": "*", "HTTPMethod": "GET", "URLPath": "/api/state/*", "Version": 1 } }

標籤是選擇性的。如果選擇添加標籤,則需要標籤鍵,標籤值是可選的。

$ aws xray create-sampling-rule --cli-input-json file://5000-polling-scorekeep.json --tags [{"Key": "key_name","Value": "value"},{"Key": "key_name","Value": "value"}] { "SamplingRuleRecord": { "SamplingRule": { "RuleName": "polling-scorekeep", "RuleARN": "arn:aws:xray:us-east-1:123456789012: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": 1530574399.0, "ModifiedAt": 1530574399.0 } } $ aws xray create-sampling-rule --cli-input-json file://9000-base-scorekeep.json { "SamplingRuleRecord": { "SamplingRule": { "RuleName": "base-scorekeep", "RuleARN": "arn:aws:xray:us-east-1: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 } }

若要刪除抽樣規則,請使用 DeleteSamplingRule

$ aws xray delete-sampling-rule --rule-name polling-scorekeep { "SamplingRuleRecord": { "SamplingRule": { "RuleName": "polling-scorekeep", "RuleARN": "arn:aws:xray:us-east-1:123456789012: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": 1530574399.0, "ModifiedAt": 1530574399.0 } }

群組

您可以使用 X-Ray API 管理您的帳户中的羣組。群組是一系列追蹤,為篩選條件表達式所定義。您可以使用羣組生成其他服務圖表並提供 Amazon CloudWatch 指標。請參閱從中獲取數據 AWS X-Ray以進一步了解透過 X-Ray API 使用服務圖表及指標的詳細資訊。如需組的詳細資訊,請參。設定群組。如需新增和管理標籤的詳細資訊,請參。標記 X-Ray 取樣規則和群組

使用 CreateGroup 建立群組 標籤是選擇性的。如果選擇添加標籤,則需要標籤鍵,標籤值是可選的。

$ aws xray create-group --group-name "TestGroup" --filter-expression "service(\"example.com\") {fault}" --tags [{"Key": "key_name","Value": "value"},{"Key": "key_name","Value": "value"}] { "GroupName": "TestGroup", "GroupARN": "arn:aws:xray:us-east-2:123456789012:group/TestGroup/UniqueID", "FilterExpression": "service(\"example.com\") {fault OR error}" }

使用 GetGroups 取得所有現有群組。

$ aws xray get-groups { "Groups": [ { "GroupName": "TestGroup", "GroupARN": "arn:aws:xray:us-east-2:123456789012:group/TestGroup/UniqueID", "FilterExpression": "service(\"example.com\") {fault OR error}" }, { "GroupName": "TestGroup2", "GroupARN": "arn:aws:xray:us-east-2:123456789012:group/TestGroup2/UniqueID", "FilterExpression": "responsetime > 2" } ], "NextToken": "tokenstring" }

使用 UpdateGroup 更新群組 標籤是選擇性的。如果選擇添加標籤,則需要標籤鍵,標籤值是可選的。若要從組中移除現有標籤,請使用UntagResource

$ aws xray update-group --group-name "TestGroup" --group-arn "arn:aws:xray:us-east-2:123456789012:group/TestGroup/UniqueID" --filter-expression "service(\"example.com\") {fault OR error}" --tags [{"Key": "Stage","Value": "Prod"},{"Key": "Department","Value": "QA"}] { "GroupName": "TestGroup", "GroupARN": "arn:aws:xray:us-east-2:123456789012:group/TestGroup/UniqueID", "FilterExpression": "service(\"example.com\") {fault OR error}" }

DeleteGroup 刪除群組。

$ aws xray delete-group --group-name "TestGroup" --group-arn "arn:aws:xray:us-east-2:123456789012:group/TestGroup/UniqueID" { }