同步网关部署:与Prometheus协作
首先
在这里,我们将介绍如何将Sync Gateway和Prometheus集成,以实现对Sync Gateway事件的有效监控和提供警报的方法。
简要介绍
Sync Gateway的指标REST API以与Prometheus兼容的JSON格式公开统计信息。
形成
默认情况下,Metrics REST API在端口4986上启用。要进行更改,需要编辑引导配置文件中的api.metricsInterface设置,以定义提供Sync Gateway URL和API的端口。
设置度量接口终端点
在启动配置文件中:
"api.metricsInterface": "127.0.0.1:4986" 
在这里定义了Sync Gateway的URL以及提供Metrics REST API所需的端口(在这种情况下是4986)。
整合
需要将Sync Gateway的度量指标流与Prometheus部署集成。为了方便Couchbase与Prometheus的集成,Couchbase提供了配置文件和示例规则文件两者。
将Prometheus配置文件(prometheus.yml)和规则文件(rules/sync-gateway.rules.yml)从Sync Gateway的发布包复制到Prometheus的/etc目录。
/etc/prometheus/prometheus.yml  ①
/etc/prometheus/rules/sync-gateway.rules.yml ② 
通过在 Prometheus 启动时使用命令行标志–config.file 来指定路径,可以更改此位置。
通过在 prometheus.yml 配置文件的 rule_files 部分编辑路径,可以指定规则文件的另一个位置。
组成
在 Sync Gateway 中运行的 Prometheus 的配置由两个文件管理,其初始副本由 Sync Gateway 提供。
普罗米修斯配置文件
提供的prometheus.yml文件指定了用于抓取Sync Gateway度量目标所需的配置。在这个例子中,我们将Sync Gateway的metricsInterface定义为sync_gateway:4986/_metrics,可通过该地址访问。如果有多个Sync Gateway,则可以在这里指定所有的终端点作为目标。
普罗米修斯规则文件 (Pǔ xiū sī guī zé
使用 Prometheus 的规则文件,可以指定记录规则和警报规则。 Sync Gateway 提供了可立即使用的规则集,作为自定义的起点,根据需要进行调整。以下是规则:
- 
- すべてのクエリ数を合計して保存する合計クエリレコード sgw::gsi::total_queries
 
アラート
样本文件的内容
組成:prometheus.yaml
prometheus.yml配置文件指定了用于启动Prometheus服务器的配置。
global:
  scrape_interval:     5s  ①
  evaluation_interval: 5s
rule_files: ②
  - '/etc/prometheus/rules/*'
scrape_configs:
  - job_name: swg
    metrics_path: /_metrics
    static_configs:
      - targets: ③
          - sync_gateway:4986
- 
- ① scrape_interval ポーリング間隔を指定します。この間隔は、Prometheusがこのエンドポイントからデータをスクレイピングする頻度を決定します。必要に応じて調整できます。
 
- 
- ② rules_filesPrometheus ルールファイルへのパスを指定します。ルールファイルは、収集された統計に基づいてカスタムアラートを定義します。
 
- ③ このtargetsプロパティは、Prometheusが統計を利用できるようにするターゲットのリストを指定します。ここでは、Sync GatewayのmetricsInterfaceを指定します。複数のSync Gatewayがある場合は、ここでそれぞれのエンドポイントを指定できます。
规则: sync-gateway-rules.yaml
groups:
  - name: sync-gateway.rules
    rules:
      - record: sgw::gsi::total_queries ① 
        expr: sum by (instance, database, job) ({__name__=~"sgw_gsi_views_.*_count"})
      - alert: TooManyAuthFailuresInLastHour
        expr: increase(sgw_security_auth_failed_count[1h]) > 1000
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: Too Many Auth Failures in Last Hour
      - alert: TooManyDocumentAccessFailuresInLastHour ②
        expr: increase(sgw_security_num_access_errors[1h]) > 1000
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: Too many Document Access Failures in last hour
      - alert: TooManyDocumentRejectionFailuresInLastHour
        expr: increase(sgw_security_num_docs_rejected[1h]) > 1000
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: Too many Document Rejection Failures in last hour
      - alert: HighRevCacheMissRate
        expr: sgw_cache_rev_cache_misses / (sgw_cache_rev_cache_misses + sgw_cache_rev_cache_hits) >= 0.8
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: High Rev Cache Miss Rate
      - alert: HighChannelCacheMissRate
        expr: sgw_cache_chan_cache_misses / (sgw_cache_chan_cache_misses + sgw_cache_chan_cache_hits) >= 0.8
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: High Channel Cache Miss Rate
      - alert: HighDeltaCacheMissRate
        expr: sgw_delta_sync_delta_sync_miss / (sgw_delta_sync_delta_sync_miss + sgw_delta_sync_delta_cache_hit) >= 0.8
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: High Delta Cache Miss Rate
      - alert: GlobalErrorCount
        expr: increase(sgw_resource_utilization_error_count[1h]) > 1
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: An error occurred in the last hour
      - alert: WarnXattrSizeCount
        expr: increase(sgw_database_warn_xattr_size_count[1h]) > 0
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: A document had larger sync data than the maximum allowed by xattrs in the last hour
      - alert: SGRNumDocsFailedToPull
        expr: increase(sgw_replication_sgr_num_docs_failed_to_pull[1h]) > 0
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: At least one document failed to be pulled with Inter Sync Gateway Replication in the last hour
      - alert: SGRNumDocsFailedToPush
        expr: increase(sgw_replication_sgr_num_docs_failed_to_push[1h]) > 0
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: At least one document failed to be pushed with Inter Sync Gateway Replication in the last hour
- 
- ① ここでは、記録ルールを定義します。記録ルールを使用すると、頻繁に使用される(または計算コストの高い)式の結果を計算して保存できます。
 
- ② ここでは、アラートルールを定義します。アラートルールを使用すると、式に基づいてアラート条件を定義し、式が満たされたときに通知を送信できます。
 
    