跳至內容

如何對 Amazon OpenSearch Service 中的慢速日誌進行疑難排解?

2 分的閱讀內容
0

當我在 Amazon OpenSearch Service 網域中啟用搜尋慢速日誌時,出現錯誤。或者,我在 Amazon CloudWatch 日誌群組中看不到慢速日誌。

解決方法

無法建立資源存取政策錯誤

如果您的 AWS 帳戶在單一 AWS 區域內有超過 10 個資源政策,則您會在 CloudWatch 日誌中收到以下錯誤:

「Unable to create the Resource Access Policy - You have reached the maximum limit for number of Resource Access Policies for CloudWatch Logs.Please select an existing policy and edit it or delete an older policy and try again.」

若要解決此錯誤,請建立一個包含多個日誌群組的資源政策:


{  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "es.amazonaws.com"
      },
      "Action": [
        "logs:PutLogEvents",
        "logs:CreateLogStream"
      ],
      "Resource": [
        "ARN-Log-Group-1",
        "ARN-Log-Group-2",
        "ARN-Log-Group-3",
        "ARN-Log-Group-4"
      ]
    }
  ]
}

**注意:**請將 region 替換為您的地區。將 account-id 替換為您的帳戶 ID。

**重要:**您無法增加 AWS Identity and Access Management (IAM) 政策的配額。

慢速日誌未傳送

如果您未看到慢速日誌傳送到 CloudWatch,請檢查您的 IAM 政策或 OpenSearch Service 閾值。

您必須設定 IAM 政策以記錄查詢,因為 OpenSearch Service 需要寫入 CloudWatch 日誌的權限。

若要更新您的 IAM 政策,請完成以下步驟:

  1. 開啟 OpenSearch Service console (OpenSearch Service 主控台)。
  2. 選擇您的網域。
  3. 選擇 Logs (日誌) 索引標籤,然後選擇 Search Slow Logs (搜尋慢速日誌)。
  4. 選擇 Select Setup (選取設定)。
  5. 使用您的資源值更新 IAM 政策:
    
    {  "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "Service": "es.amazonaws.com"
          },
          "Action": [
            "logs:PutLogEvents",
            "logs:CreateLogStream"
          ],
          "Resource": "arn:aws:region:account_id:log-group:/aws/aes/domains/myes/search-logs:*"
        }
      ]
    }
    **注意:**請將 region 替換為您的地區。將 account-id 替換為您的帳戶 ID。
  6. (選用) 為了增強安全性,請將 aws:SourceAccountaws:SourceArn 條件索引鍵新增至您的政策:
    {
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "account-id"
        },
        "ArnLike": {
          "aws:SourceArn": "arn:aws:es:region:account-id:domain/domain-name"
        }
      }
    }
    **注意:**請將 region 替換為您的地區。將 account-id 替換為您的帳戶 ID。將 domain-name 替換為您的網域名稱。
  7. 選擇 Enable (啟用)。

如需詳細資訊,請參閱啟用日誌發佈 (主控台)

預設情況下,OpenSearch Service 的記錄功能為關閉狀態。您可以使用 API 呼叫來設定日誌層級與時間閾值。這些設定是動態的,因此您可以在執行中的叢集上更新設定,OpenSearch Service 會立即套用變更。

請為您的網域設定適當的時間閾值。如果所有請求都在設定閾值前完成,則 CloudWatch 不會將日誌傳送到您的日誌群組。您可以為每個偵錯層級 (TRACEDEBUGINFOWARN) 個別設定索引層級閾值。

例如,在 OpenSearch 儀表板中,將 WARN 偵錯層級的閾值設定為 10 秒:

PUT /YOURINDEXNAME/_settings
{
  "index.search.slowlog.threshold.query.warn": "10s"
}

**注意:**請將 YOURINDEXNAME 替換為您的索引名稱。

若使用 curl,請執行以下命令:

curl -XPUT http://your_domain_endpoint/index/_settings -d '{"index.search.slowlog.threshold.query.level":"10s"}'

**注意:**請將 your_domain_endpoint 替換為您的網域端點。將 10s 替換為您要使用的日誌閾值。

若要記錄您網域的所有查詢,可以將 TRACE 設為 0 毫秒。但是,記錄所有查詢會消耗大量資源,可能會影響網域效能。若要關閉記錄,請將閾值設定為 -1

若要檢查您的閾值,請執行以下命令:

GET /YOURINDEXNAME/_settings?pretty

**注意:**請將 YOURINDEXNAME 替換為您的索引名稱。

OpenSearch Service 會記錄任何超過定義閾值的查詢。

將慢速日誌設定套用至叢集層級的所有新索引

若要將慢速日誌設定套用至叢集層級的所有新索引,請使用索引範本。在 OpenSearch Service 中,沒有針對索引慢速日誌的叢集範圍設定。

請建立一個包含慢速日誌設定的索引範本:


PUT _template/all_indices_template
{
  "index_patterns": ["*"], // This applies to all indices
  "settings": {
    "index.indexing.slowlog.threshold.index.warn": "10s",
    "index.indexing.slowlog.threshold.index.info": "5s",
    "index.indexing.slowlog.threshold.index.debug": "2s",
    "index.indexing.slowlog.threshold.index.trace": "500ms"
  }
}

上述範本會自動將設定套用至範本設定完成後所建立的所有新索引。

最佳實務

請執行下列動作:

  • 逐一進行組態更新
  • 若要識別慢速搜尋查詢,請同時為查詢階段與擷取階段設定閾值。
  • 先以較低的閾值測試。再逐步增加閾值,只記錄影響效能或需要最佳化的查詢。
  • 選擇正確的碎片數量,以符合叢集與使用案例,從而最佳化叢集效能。如需詳細資訊,請參閱碎片策略
  • 針對慢速日誌,在 TRACEDEBUGINFOWARN 偵錯層級啟用記錄。每個偵錯層級會記錄不同類型的資訊。請根據請求狀態啟用適當的記錄。

相關資訊

使用 Amazon CloudWatch Logs 串流與 Kibana 分析 Amazon Elasticsearch Service 慢速日誌

如何對 CloudWatch Logs 進行疑難排解,讓它能串流到我的 OpenSearch Service 網域?

檢視 Amazon OpenSearch Service 錯誤日誌

AWS 官方已更新 9 個月前