在過去的幾天裡,我們已經深入了解了日誌 (Loki) 和追蹤 (Tempo)。今天,我們將補全可觀測性 (Observability) 的最後一塊拼圖:指標 (Metrics)。我們將學習 Grafana Mimir,一個由 Grafana Labs 開發的、可無限擴展的開源指標後端。
在我們深入 Mimir 之前,讓我們先搞清楚什麼是「指標」。
User A logged in
。cpu_usage = 80%
, http_requests_total = 1024
。指標是高度聚合的數據,非常適合用來製作儀表板、設定告警,以及觀察系統的長期趨勢。Prometheus 是目前收集指標最主流的開源工具。
Prometheus 非常優秀,但它本身是一個單體的應用程式。當你的系統規模擴大,需要監控的服務越來越多時,單一的 Prometheus 實例會遇到瓶頸:
Grafana Mimir 就是為了解決這些問題而生的。
Mimir 是一個水平擴展的指標後端。你可以將它看作一個「超級 Prometheus」。它完全相容 Prometheus 的 API,你可以將來自多個 Prometheus 實例的指標數據集中儲存到 Mimir,然後透過單一的查詢端點進行全局查詢。
核心優勢:
今天我們將建立一個如下的迷你架構:
+------------+ (Scrapes) +-----------------+ (Remote Write) +-------+
| Prometheus |------------------>| Your App (mock) |---------------------->| Mimir |
+------------+ +-----------------+ +-------+
^ ^
| (Data Source) | (Data Source)
| |
+-----v-----+
| Grafana |
+-----------+
remote_write
數據流,並將其儲存起來。我們已經為您準備好了所有設定檔。
mimir-config.yaml
)這個設定檔告訴 Mimir 如何運行。為了簡單起見,我們使用 inmemory
作為協調儲存 (kvstore),並使用本地檔案系統 (filesystem
) 作為指標的塊儲存 (blocks_storage)。在生產環境中,這裡通常會換成 etcd
和 S3
。
target: all
auth_enabled: false
server:
http_listen_port: 9009
grpc_listen_port: 9095
distributor:
ring:
instance_addr: 127.0.0.1
kvstore:
store: inmemory
ingester:
ring:
instance_addr: 127.0.0.1
kvstore:
store: inmemory
replication_factor: 1
lifecycler:
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
# Disable chunk transfer, which is not supported with inmemory store
max_transfer_retries: 0
ruler:
alertmanager_url: http://localhost # Does not have to be a real alertmanager
ring:
kvstore:
store: inmemory
# S3, GCS, Azure, etc. are supported.
# For the sake of simplicity, we use the local filesystem.
blocks_storage:
backend: filesystem
filesystem:
dir: /data/mimir/blocks
compactor:
data_dir: /data/mimir/compactor
sharding_ring:
kvstore:
store: inmemory
store_gateway:
sharding_ring:
kvstore:
store: inmemory
prometheus.yml
)這個設定檔有兩個重點:
scrape_configs
: 設定 Prometheus 要抓取哪些目標的指標,這裡我們設定了 prometheus
自己和 mimir
。remote_write
: 這是最重要的部分。它告訴 Prometheus 將所有收集到的指標數據,透過 HTTP push
的方式,發送到 Mimir 的 /api/v1/push
端點。global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'mimir'
static_configs:
- targets: ['mimir:9009']
# This is the most important part.
# It tells Prometheus to send all its collected metrics to Mimir.
remote_write:
- url: "http://mimir:9009/api/v1/push"
docker-compose.yml
)這個檔案整合了我們需要的所有服務:loki
, grafana
, mimir
, prometheus
。
version: '3.8'
services:
loki:
image: grafana/loki:2.9.0
ports:
- "3100:3100"
volumes:
- ./loki-config.yaml:/etc/loki/local-config.yaml
command: -config.file=/etc/loki/local-config.yaml
grafana:
image: grafana/grafana:10.0.3
ports:
- "3000:3000"
volumes:
- ./grafana-provisioning/datasources:/etc/grafana/provisioning/datasources
mimir:
image: grafana/mimir:2.9.0
ports:
- "9009:9009"
volumes:
- ./mimir-config.yaml:/etc/mimir.yaml
- mimir-data:/data/mimir
command: -config.file=/etc/mimir.yaml
prometheus:
image: prom/prometheus:v2.47.0
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command: --config.file=/etc/prometheus/prometheus.yml
volumes:
mimir-data:
在 day26
目錄下,執行以下命令:
docker-compose up -d
開啟 Grafana: 瀏覽器訪問 http://localhost:3000
。
設定資料來源:
Configuration > Data Sources
。Add data source
,選擇 Prometheus
。Prometheus
http://prometheus:9090
Save & test
。Add data source
,選擇 Prometheus
。Mimir
http://mimir:9009/prometheus
(注意 Mimir 的查詢路徑)Save & test
。查詢驗證:
Explore
頁面。Prometheus
,輸入查詢 up
,你應該能看到 Prometheus 從自己和 Mimir 抓取到的 up
指標。Mimir
,再次輸入查詢 up
。你應該能看到完全相同的結果!這證明了 Prometheus 已經成功地將它抓取到的所有指標都遠端寫入到了 Mimir 中。
今天我們學習了指標系統 Mimir 的基本概念和用途,並成功地搭建了一個 Prometheus + Mimir
的整合環境。我們理解了 Mimir 作為一個集中式、可擴展的指標後端,如何解決單體 Prometheus 的擴展性問題。
有了 Mimir,我們就擁有了處理大規模指標數據的能力。明天,我們將把日誌、追蹤和指標這三者結合起來,在 Grafana 中建立一個真正融會貫通的儀表板!