ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# Monitoring GitLab with Prometheus > 原文:[https://docs.gitlab.com/ee/administration/monitoring/prometheus/](https://docs.gitlab.com/ee/administration/monitoring/prometheus/) * [Overview](#overview) * [Configuring Prometheus](#configuring-prometheus) * [Changing the port and address Prometheus listens on](#changing-the-port-and-address-prometheus-listens-on) * [Adding custom scrape configurations](#adding-custom-scrape-configurations) * [Using an external Prometheus server](#using-an-external-prometheus-server) * [Viewing performance metrics](#viewing-performance-metrics) * [Prometheus as a Grafana data source](#prometheus-as-a-grafana-data-source) * [GitLab metrics](#gitlab-metrics) * [Bundled software metrics](#bundled-software-metrics) * [Node exporter](#node-exporter) * [Redis exporter](#redis-exporter) * [PostgreSQL exporter](#postgresql-exporter) * [PgBouncer exporter](#pgbouncer-exporter) * [Registry exporter](#registry-exporter) * [GitLab exporter](#gitlab-exporter) * [Configuring Prometheus to monitor Kubernetes](#configuring-prometheus-to-monitor-kubernetes) # Monitoring GitLab with Prometheus[](#monitoring-gitlab-with-prometheus "Permalink") > **Notes:** > > * 本页中列出的 Prometheus 和各种出口商都捆绑在 Omnibus GitLab 软件包中. 查看每个出口商的文档以了解添加的时间表. 对于源安装,您必须自己安装. 在后续版本中,将捕获其他的 GitLab 指标. > * GitLab 9.0 默认情况下会启用 Prometheus 服务. > * Prometheus 及其出口商不对用户进行身份验证,任何可以访问它们的人都可以使用. [Prometheus](https://s0prometheus0io.icopy.site)是一项功能强大的时间序列监视服务,为监视 GitLab 和其他软件产品提供了灵活的平台. GitLab 提供了 Prometheus 的开箱即用监视功能,可轻松访问 GitLab 服务的高质量时间序列监视. ## Overview[](#overview "Permalink") Prometheus 的工作方式是定期连接到数据源,并通过[各种出口商](#bundled-software-metrics)收集其绩效指标. 要查看和使用监视数据,您可以[直接连接到 Prometheus](#viewing-performance-metrics)或使用仪表板工具(例如[Grafana)](https://grafana.com) . ## Configuring Prometheus[](#configuring-prometheus "Permalink") **注意:**对于源安装,您必须自己安装和配置. 从 GitLab 9.0 开始,Prometheus 及其出口商默认情况下处于打开状态. Prometheus 将以`gitlab-prometheus`用户身份运行,并监听`http://localhost:9090` . 默认情况下,只能从 GitLab 服务器本身访问 Prometheus. 除非单独禁用,否则每个出口商将自动设置为 Prometheus 的监视目标. 要禁用 Prometheus 及其所有出口商以及将来添加的任何出口商,请执行以下操作: 1. Edit `/etc/gitlab/gitlab.rb` 2. 添加或查找并取消注释以下行,确保将其设置为`false` : ``` prometheus_monitoring['enable'] = false ``` 3. 保存文件并[重新配置 GitLab,](../../restart_gitlab.html#omnibus-gitlab-reconfigure)以使更改生效. ### Changing the port and address Prometheus listens on[](#changing-the-port-and-address-prometheus-listens-on "Permalink") **注意:** [Omnibus GitLab 8.17 中](https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/1261)添加了以下更改. 尽管可能,但不建议更改 Prometheus 监听的端口,因为这可能会影响 GitLab 服务器上运行的其他服务或与之冲突. 继续需要您自担风险. 为了从 GitLab 服务器外部访问 Prometheus,您需要在`prometheus['listen_address']`设置 FQDN 或 IP. 更改 Prometheus 监听的地址/端口: 1. Edit `/etc/gitlab/gitlab.rb` 2. 添加或查找并取消注释以下行: ``` prometheus['listen_address'] = 'localhost:9090' ``` 将`localhost:9090`替换为您希望 Prometheus 监听的地址或端口. 如果要允许除`localhost`以外的其他主机访问 Prometheus,请忽略该主机,或使用`0.0.0.0`允许公共访问: ``` prometheus['listen_address'] = ':9090' # or prometheus['listen_address'] = '0.0.0.0:9090' ``` 3. 保存文件并[重新配置 GitLab,](../../restart_gitlab.html#omnibus-gitlab-reconfigure)以使更改生效 ### Adding custom scrape configurations[](#adding-custom-scrape-configurations "Permalink") 您可以使用[Prometheus 刮擦目标配置](https://s0prometheus0io.icopy.site/docs/prometheus/latest/configuration/configuration/)语法在`/etc/gitlab/gitlab.rb`编辑`prometheus['scrape_configs']` , `/etc/gitlab/gitlab.rb`为 Omnibus GitLab 捆绑的 Prometheus 配置其他刮擦目标. 这是刮取`http://1.1.1.1:8060/probe?param_a=test&param_b=additional_test`的示例配置: ``` prometheus['scrape_configs'] = [ { 'job_name': 'custom-scrape', 'metrics_path': '/probe', 'params' => { 'param_a' => ['test'], 'param_b' => ['additional_test'] }, 'static_configs' => [ 'targets' => ['1.1.1.1:8060'], ], }, ] ``` ### Using an external Prometheus server[](#using-an-external-prometheus-server "Permalink") **注意:** Prometheus 和大多数导出器不支持身份验证. 我们不建议将它们公开在本地网络之外. 需要进行一些配置更改,才能由外部 Prometheus 服务器监视 GitLab. 对于[具有多个节点的 GitLab 部署,](../../reference_architectures/index.html)建议使用外部服务器. 要使用外部 Prometheus 服务器: 1. Edit `/etc/gitlab/gitlab.rb`. 2. 禁用捆绑的 Prometheus: ``` prometheus['enable'] = false ``` 3. 将每个捆绑服务的[导出器设置](#bundled-software-metrics)为侦听网络地址,例如: ``` gitlab_exporter['listen_address'] = '0.0.0.0' sidekiq['listen_address'] = '0.0.0.0' gitlab_exporter['listen_port'] = '9168' node_exporter['listen_address'] = '0.0.0.0:9100' redis_exporter['listen_address'] = '0.0.0.0:9121' postgres_exporter['listen_address'] = '0.0.0.0:9187' gitaly['prometheus_listen_addr'] = "0.0.0.0:9236" gitlab_workhorse['prometheus_listen_addr'] = "0.0.0.0:9229" ``` 4. 如有必要,请使用[官方安装说明](https://s0prometheus0io.icopy.site/docs/prometheus/latest/installation/)安装并设置专用的 Prometheus 实例. 5. 将 Prometheus 服务器 IP 地址添加到[监视 IP 白名单](../ip_whitelist.html) . 例如: ``` gitlab_rails['monitoring_whitelist'] = ['127.0.0.0/8', '192.168.0.1'] ``` 6. 在**所有** GitLab Rails(Puma / Unicorn,Sidekiq)服务器上,设置 Prometheus 服务器 IP 地址和监听端口. 例如: ``` gitlab_rails['prometheus_address'] = '192.168.0.1:9090' ``` 7. 要抓取 NGINX 指标,您还需要配置 NGINX 以允许 Prometheus 服务器 IP. 例如: ``` nginx['status']['options'] = { "server_tokens" => "off", "access_log" => "off", "allow" => "192.168.0.1", "deny" => "all", } ``` 8. [重新配置 GitLab](../../restart_gitlab.html#omnibus-gitlab-reconfigure)以应用更改. 9. 编辑 Prometheus 服务器的配置文件. 10. 将每个节点的导出器添加到 Prometheus 服务器的[抓取目标配置中](https://s0prometheus0io.icopy.site/docs/prometheus/latest/configuration/configuration/) . 例如,使用`static_configs`的样本片段: ``` scrape_configs: - job_name: nginx static_configs: - targets: - 1.1.1.1:8060 - job_name: redis static_configs: - targets: - 1.1.1.1:9121 - job_name: postgres static_configs: - targets: - 1.1.1.1:9187 - job_name: node static_configs: - targets: - 1.1.1.1:9100 - job_name: gitlab-workhorse static_configs: - targets: - 1.1.1.1:9229 - job_name: gitlab-rails metrics_path: "/-/metrics" static_configs: - targets: - 1.1.1.1:8080 - job_name: gitlab-sidekiq static_configs: - targets: - 1.1.1.1:8082 - job_name: gitlab_exporter_database metrics_path: "/database" static_configs: - targets: - 1.1.1.1:9168 - job_name: gitlab_exporter_sidekiq metrics_path: "/sidekiq" static_configs: - targets: - 1.1.1.1:9168 - job_name: gitlab_exporter_process metrics_path: "/process" static_configs: - targets: - 1.1.1.1:9168 - job_name: gitaly static_configs: - targets: - 1.1.1.1:9236 ``` 11. 重新加载 Prometheus 服务器. ## Viewing performance metrics[](#viewing-performance-metrics "Permalink") 您可以访问`http://localhost:9090` ,以获取 Prometheus 默认提供的仪表板. > **注意:**如果在您的 GitLab 实例上启用了 SSL,由于[HSTS,](https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security)如果使用相同的 FQDN,则可能无法在与 GitLab 相同的浏览器上访问 Prometheus. 我们计划[通过 GitLab 提供访问](https://gitlab.com/gitlab-org/multi-user-prometheus) ,但是在此期间,有一些解决方法:使用单独的 FQDN,使用服务器 IP,使用单独的 Prometheus 浏览器,重置 HSTS 或使用[NGINX 代理](https://docs.gitlab.com/omnibus/settings/nginx.html) . Prometheus 收集的性能数据可以在 Prometheus 控制台中直接查看,也可以通过兼容的仪表板工具查看. Prometheus 界面提供了一种[灵活的查询语言](https://s0prometheus0io.icopy.site/docs/prometheus/latest/querying/basics/) ,可与收集的数据一起使用,您可以在其中可视化输出. 要获得功能更全面的仪表板,可以使用 Grafana 并已[对 Prometheus 进行了官方支持](https://s0prometheus0io.icopy.site/docs/visualization/grafana/) . 普罗米修斯样本查询: * **可用内存百分比:** `((node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) or ((node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes) / node_memory_MemTotal_bytes)) * 100` * **CPU 使用率百分比:** `1 - avg without (mode,cpu) (rate(node_cpu_seconds_total{mode="idle"}[5m]))` * **传输的数据:** `rate(node_network_transmit_bytes_total{device!="lo"}[5m])` * **收到的数据:** `rate(node_network_receive_bytes_total{device!="lo"}[5m])` ## Prometheus as a Grafana data source[](#prometheus-as-a-grafana-data-source "Permalink") Grafana 允许您导入 Prometheus 性能指标作为数据源,并将指标呈现为图形和仪表板,这有助于可视化. To add a Prometheus dashboard for a single server GitLab setup: 1. 在 Grafana 中创建一个新的数据源. 2. 命名您的数据源(如 GitLab). 3. 在类型下拉框中选择`Prometheus` . 4. 将您的 Prometheus 侦听地址添加为 URL,并设置对`Browser`访问权限. 5. 将 HTTP 方法设置为`GET` . 6. 保存并测试您的配置以验证其是否有效. ## GitLab metrics[](#gitlab-metrics "Permalink") 在 GitLab 9.3 中引入. GitLab 监视其自身的内部服务指标,并使其在`/-/metrics`端点可用. 与其他导出器不同,此终结点需要身份验证,因为它可以在与用户流量相同的 URL 和端口上使用. [➔ Read more about the GitLab Metrics.](gitlab_metrics.html) ## Bundled software metrics[](#bundled-software-metrics "Permalink") Omnibus GitLab 中捆绑的许多 GitLab 依赖项都已预先配置为导出 Prometheus 指标. ### Node exporter[](#node-exporter "Permalink") 节点导出器允许您测量各种机器资源,例如内存,磁盘和 CPU 利用率. [Read more about the node exporter](node_exporter.html). ### Redis exporter[](#redis-exporter "Permalink") Redis 导出器允许您测量各种 Redis 指标. [Read more about the Redis exporter](redis_exporter.html). ### PostgreSQL exporter[](#postgresql-exporter "Permalink") PostgreSQL 导出器允许您测量各种 PostgreSQL 指标. [Read more about the PostgreSQL exporter](postgres_exporter.html). ### PgBouncer exporter[](#pgbouncer-exporter "Permalink") PgBouncer 导出器允许您测量各种 PgBouncer 指标. [Read more about the PgBouncer exporter](pgbouncer_exporter.html). ### Registry exporter[](#registry-exporter "Permalink") 注册表导出器允许您测量各种注册表指标. [Read more about the Registry exporter](registry_exporter.html). ### GitLab exporter[](#gitlab-exporter "Permalink") GitLab 导出器允许您测量从 Redis 和数据库中提取的各种 GitLab 指标. [Read more about the GitLab exporter](gitlab_exporter.html). ## Configuring Prometheus to monitor Kubernetes[](#configuring-prometheus-to-monitor-kubernetes "Permalink") 版本历史 * 在 GitLab 9.0 中引入. * 在 GitLab 9.4 中引入了 Pod 监控. 如果您的 GitLab 服务器在 Kubernetes 中运行,则 Prometheus 将从群集中的节点和带[注释的](https://s0prometheus0io.icopy.site/docs/prometheus/latest/configuration/configuration/) Pod 收集指标,包括每个容器上的性能数据. 如果您的 CI / CD 环境在同一群集中运行,这将特别有用,因为您可以使用[Prometheus 项目集成](../../../user/project/integrations/prometheus.html)来监视它们. 要禁用对 Kubernetes 的监视: 1. Edit `/etc/gitlab/gitlab.rb`. 2. 添加(或查找并取消注释)以下行并将其设置为`false` : ``` prometheus['monitor_kubernetes'] = false ``` 3. 保存文件并[重新配置 GitLab,](../../restart_gitlab.html#omnibus-gitlab-reconfigure)以使更改生效.