跳到主要内容

GitLab 监控体系

GitLab Omnibus 内置 Prometheus + Alertmanager + Grafana(可选) 监控栈。生产环境建议将指标接入现有 Prometheus/Grafana,而不是仅依赖 GitLab 自带 UI。

1. GitLab监控介绍

GitLab 各组件均暴露 Prometheus metrics:

组件默认端口指标前缀示例
gitlab-workhorse9229gitlab_workhorse_*
Rails (Puma)8080/metricshttp_*, ruby_*
Sidekiq8082sidekiq_*
Gitaly9236gitaly_*
PostgreSQL (postgres_exporter)9187pg_*
Redis (redis_exporter)9121redis_*
Node Exporter9100node_*

查看 Omnibus 内置监控服务状态:

sudo gitlab-ctl status prometheus
sudo gitlab-ctl status grafana
sudo gitlab-ctl status alertmanager
sudo gitlab-ctl status node-exporter

Admin Area → Monitoring → System information 可快速确认各 exporter 是否在线。

2. Prometheus监控

2.1 启用内置 Prometheus

编辑 /etc/gitlab/gitlab.rb

prometheus['enable'] = true
prometheus['listen_address'] = '127.0.0.1:9090'

# 允许外部 Prometheus 抓取(按需限制 IP)
prometheus['monitor_kubernetes'] = false
sudo gitlab-ctl reconfigure
curl -s http://127.0.0.1:9090/-/healthy

2.2 外部 Prometheus 抓取

在独立 Prometheus 的 scrape_configs 中添加(通过防火墙或 Nginx 反代暴露 metrics 端点):

scrape_configs:
- job_name: gitlab-rails
static_configs:
- targets: ['gitlab.example.com:8080']
metrics_path: /-/metrics
scheme: http

- job_name: gitlab-sidekiq
static_configs:
- targets: ['gitlab.example.com:8082']

- job_name: gitlab-gitaly
static_configs:
- targets: ['gitlab.example.com:9236']

- job_name: gitlab-node
static_configs:
- targets: ['gitlab.example.com:9100']

验证抓取目标:

curl -s 'http://127.0.0.1:9090/api/v1/targets' | jq '.data.activeTargets[] | {job: .labels.job, health: .health}'

3. Grafana Dashboard

3.1 启用内置 Grafana

grafana['enable'] = true
grafana['admin_password'] = 'ChangeMeStrong!'
sudo gitlab-ctl reconfigure
# 默认通过 GitLab 代理访问:Admin → Monitoring → Dashboards

3.2 导入社区 Dashboard

常用 Dashboard ID(Grafana.com):

ID名称
18901GitLab Omnibus Overview
18902GitLab Gitaly
9628PostgreSQL Database

导入步骤:Grafana → Dashboards → Import → 输入 ID → 选择 Prometheus 数据源。

3.3 接入外部 Grafana

grafana['enable'] = false # 关闭内置,使用外部实例
prometheus['enable'] = true

在外部 Grafana 添加 Data Source:http://<gitlab-host>:9090(需网络可达)。

4. GitLab关键指标

指标PromQL 示例告警阈值建议
HTTP 5xx 比例rate(http_requests_total{status=~"5.."}[5m]) / rate(http_requests_total[5m])> 1% 持续 5min
Puma 队列深度puma_running / puma_max_threads线程利用率 > 80%
Sidekiq 队列积压sidekiq_queue_size{queue="default"}> 1000 持续 10min
Gitaly RPC 延迟histogram_quantile(0.99, rate(gitaly_service_client_duration_seconds_bucket[5m]))P99 > 1s
磁盘使用率100 - (node_filesystem_avail_bytes{mountpoint="/var/opt/gitlab"} / node_filesystem_size_bytes * 100)> 85%
备份状态自定义脚本 + gitlab_backup_success pushgateway24h 内无成功备份

Rails 健康检查:

sudo gitlab-rake gitlab:check SANITIZE=true
curl -sf http://127.0.0.1/-/health | jq .
curl -sf http://127.0.0.1/-/readiness | jq .

5. PostgreSQL监控

GitLab 内置 postgres_exporter,端口 9187。

关键指标:

# 连接数
pg_stat_activity_count

# 复制延迟(HA 环境)
pg_replication_lag_seconds

# 事务 ID wraparound 风险
pg_database_xid_age{datname="gitlabhq_production"}

命令行快速检查:

sudo gitlab-psql -c "SELECT count(*) FROM pg_stat_activity WHERE state = 'active';"
sudo gitlab-psql -c "SELECT pg_size_pretty(pg_database_size('gitlabhq_production'));"
sudo gitlab-psql -c "SELECT schemaname, relname, n_live_tup FROM pg_stat_user_tables ORDER BY n_live_tup DESC LIMIT 10;"

长事务排查:

sudo gitlab-psql -c "
SELECT pid, now() - xact_start AS duration, query
FROM pg_stat_activity
WHERE state != 'idle' AND xact_start IS NOT NULL
ORDER BY duration DESC LIMIT 10;"

6. Redis监控

GitLab 使用 Redis 存储缓存、会话与 ActionCable。内置 redis_exporter 端口 9121。

sudo gitlab-redis-cli INFO memory
sudo gitlab-redis-cli INFO stats
sudo gitlab-redis-cli INFO replication
指标说明关注阈值
redis_memory_used_bytes内存占用> maxmemory 80%
redis_connected_clients连接数突增可能泄漏
redis_evicted_keys_total淘汰 key 数持续 > 0 需扩容
redis_master_link_status主从状态(Sentinel HA)!= up

7. Sidekiq监控

Sidekiq 处理异步任务(邮件、Webhook、CI 状态同步等),积压直接影响用户体验。

# 查看队列深度
sudo gitlab-rails runner "Sidekiq::Queue.all.each { |q| puts \"#{q.name}: #{q.size}\" }"

# Sidekiq 进程状态
sudo gitlab-ctl status sidekiq
curl -s http://127.0.0.1:8082/metrics | grep sidekiq_queue_size

高负载调优(gitlab.rb):

sidekiq['max_concurrency'] = 25
sidekiq['queue_groups'] = ['*']

修改后 gitlab-ctl reconfigure。若 mailerspipeline_processing 队列持续积压,优先排查下游 SMTP、Runner 或 Gitaly 性能。

8. 告警规则设计

8.1 Alertmanager 配置

alertmanager['enable'] = true
alertmanager['flags'] = {
'web.external-url' => 'https://gitlab.example.com/alertmanager'
}

/var/opt/gitlab/prometheus/rules/ 添加自定义规则文件,例如 gitlab-alerts.yml

groups:
- name: gitlab
rules:
- alert: GitLabSidekiqQueueHigh
expr: sidekiq_queue_size{queue="default"} > 1000
for: 10m
labels:
severity: warning
annotations:
summary: "Sidekiq default 队列积压 {{ $value }}"

- alert: GitLabDiskSpaceLow
expr: (node_filesystem_avail_bytes{mountpoint="/var/opt/gitlab"} / node_filesystem_size_bytes{mountpoint="/var/opt/gitlab"}) < 0.15
for: 5m
labels:
severity: critical
annotations:
summary: "GitLab 数据盘剩余空间不足 15%"

- alert: GitLabBackupMissing
expr: time() - gitlab_backup_last_success_timestamp > 86400
for: 1h
labels:
severity: critical
annotations:
summary: "超过 24 小时未成功备份"
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart prometheus

8.2 告警分级建议

级别场景响应时间
P1 CriticalWeb 不可用、数据库 down、磁盘 > 95%15 分钟
P2 WarningSidekiq 积压、Gitaly 延迟高、备份延迟1 小时
P3 Info证书 30 天内过期、Runner 离线下一工作日

备份成功时间戳可通过 cron 脚本在备份成功后写入 Pushgateway:

echo "gitlab_backup_last_success $(date +%s)" | curl --data-binary @- http://pushgateway:9091/metrics/job/gitlab_backup