• 让你的 Runner 可视化,使用 Prometheus + Grafana 实现极狐GitLab Runner 可视化


    极狐GitLab Runner 从 1.8.0 引入了对 Prometheus metrics 的原生支持。Runner 目前对外输出的信息有:

    • Runner 的业务逻辑指标(诸如当前运行 Job 的数量)

    • Go 相关的进程指标(诸如 gc、goroutines、memstats 等)

    • 通用的进程指标(比如 memory usage、 CPU usage、file descriptor usage 等)

    • 构建的版本信息

    下面使用 Prometheus + Grafana来实现 Runner 指标的监控

    前提条件


    Runner 的安装

    Runner 的安装有多种方法,papckage、docker、helm 等,详细的安装使用可以查看 Runner 的花式玩法

    本文用 docker 的方式拉起了一个 Runner 实例,为了获取 metrics,同步开启了 9252 端口:

    1. $ docker run -d --name gitlab-runner-docker  \
    2.     --restart always -v $PWD:/etc/gitlab-runner  \
    3.     -p 9252:9252    -v /var/run/docker.sock:/var/run/docker.sock   \
    4.     gitlab/gitlab-runner:latest

    为了测试用,使用 gitlab-runenr register 注册了 6 次:

    1. $ gitlab-runner list
    2. Runtime platform                                    arch=amd64 os=linux pid=353 revision=43b2dc3d version=15.4.0
    3. Listing configured runners                          ConfigFile=/etc/gitlab-runner/config.toml
    4. runner-1                                            Executor=docker Token=rGSszVLSiYxQzcpVm8ud URL=https://jihulab.com/
    5. runner-2                                            Executor=docker Token=7r8bfWJRVaeMsCCQ3R2m URL=https://jihulab.com/
    6. runner-3                                            Executor=docker Token=3xqRCdMdo9VYuwb92Gfr URL=https://jihulab.com/
    7. runner-4                                            Executor=docker Token=jAeBYLSNCCkaspAbmTsA URL=https://jihulab.com/
    8. runner-5                                            Executor=docker Token=DAGXrJoNHYKNuwwNUVD5 URL=https://jihulab.com/
    9. runner-6                                            Executor=docker Token=V_sAUz2C-fVba5CfQxjA URL=https://jihulab.com/

    在极狐GitLab 的项目 → 设置 → CI/CD → Runner 里面可以看到 6 个 Runner 处于 active 状态:

     

    由于 Runner 的 metrics 是通过 9252 端口输出的,需要在 Runner 的 config.toml 文件中配置 listen_address 参数,本文的配置为:

    $ listen_address = "0.0.0.0:9252"

    (可以根据自身实际情况进行配置)

    然后通过下面的命令查看可用的 metrics:

    1. $ curl -s "http://localhost:9252/metrics" | grep -E "# HELP"
    2. # HELP gitlab_runner_api_request_statuses_total The total number of api requests, partitioned by runner, endpoint and status.
    3. # HELP gitlab_runner_autoscaling_machine_creation_duration_seconds Histogram of machine creation time.
    4. # HELP gitlab_runner_autoscaling_machine_failed_creation_duration_seconds Histogram of machine failed creation timings
    5. # HELP gitlab_runner_autoscaling_machine_removal_duration_seconds Histogram of machine removal time.
    6. # HELP gitlab_runner_autoscaling_machine_states The current number of machines per state in this provider.
    7. # HELP gitlab_runner_autoscaling_machine_stopping_duration_seconds Histogram of machine stopping time.
    8. # HELP gitlab_runner_concurrent The current value of concurrent setting
    9. # HELP gitlab_runner_errors_total The number of caught errors.
    10. # HELP gitlab_runner_job_duration_seconds Histogram of job durations
    11. # HELP gitlab_runner_jobs The current number of running builds.
    12. # HELP gitlab_runner_jobs_total Total number of handled jobs
    13. # HELP gitlab_runner_limit The current value of concurrent setting
    14. # HELP gitlab_runner_request_concurrency The current number of concurrent requests for a new job
    15. # HELP gitlab_runner_request_concurrency_exceeded_total Counter tracking exceeding of request concurrency
    16. # HELP gitlab_runner_version_info A metric with a constant '1' value labeled by different build stats fields.
    17. # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
    18. # HELP go_goroutines Number of goroutines that currently exist.
    19. # HELP go_info Information about the Go environment.
    20. # HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
    21. # HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
    22. # HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
    23. # HELP go_memstats_frees_total Total number of frees.
    24. # HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
    25. # HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
    26. # HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
    27. # HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
    28. # HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
    29. # HELP go_memstats_heap_objects Number of allocated objects.
    30. # HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.
    31. # HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
    32. # HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
    33. # HELP go_memstats_lookups_total Total number of pointer lookups.
    34. # HELP go_memstats_mallocs_total Total number of mallocs.
    35. # HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
    36. # HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
    37. # HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
    38. # HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
    39. # HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
    40. # HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
    41. # HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
    42. # HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
    43. # HELP go_memstats_sys_bytes Number of bytes obtained from system.
    44. # HELP go_threads Number of OS threads created.
    45. # HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
    46. # HELP process_max_fds Maximum number of open file descriptors.
    47. # HELP process_open_fds Number of open file descriptors.
    48. # HELP process_resident_memory_bytes Resident memory size in bytes.
    49. # HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
    50. # HELP process_virtual_memory_bytes Virtual memory size in bytes.
    51. # HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.

    这些指标就是可以被 prometheus 抓取并且通过 Grafana 进行展示的。

    Prometheus 的安装

    Prometheus 的安装可以参考 Prometheus 官网文档,本文用 docker 拉起一个 prometheus 实例:

    $ docker run -d -p 9090:9090 -v $PWD:/etc/prometheus prom/prometheus
    

    使用 localhost:9090 可访问 prometheus 实例:

    Grafana 的安装

    同理,用 docker 拉起一个 Grafana 实例:

    $ docker run -d -p 3000:3000 grafana/grafana-enterprise
    

    使用 admin/admin 登陆 localhost:3030:

     

    建议安装成功之后立即修改 grafana 的初始密码,比如集成 LDAP,成为强密码,防止被破解进而遭受攻击。

    Runner 的监控配置


    Prometheus & Grafana 的配置

    通过 Grafana 来实现极狐GitLab Runner 监控和可观测性的原理图如下:

    Runner 通过 9252 端口输出 metrics,Prometheus 通过 Job 来抓取(scrape) Runner 的 metrics,最后 Grafana 通过数据源(Prometheus)来完成 metrics 的展示。

    配置层面,在 Runner 上需要开启 9252 端口,配置 listen_address 端口即可。对于 Prometheus 需要配置一个抓取 Runner metrics 的 Job 即可:

    1. scrape_configs:
    2.   - job_name: "jh gitlab runner"
    3.     static_configs:
    4.       - targets: ["YOUR-Runner-IP:9252"]
    5.   - job_name: "prometheus"

    在 Grafana 上面需要添加一个 Prometheus 的 Data Source,Configuration → Data Source 中选择 Add data source:

    选择 Prometheus 即可:

    接着配置 Promehteus 的信息,诸如 URL、Auth(如果有需要)等信息,最后选择 Save & Test:

     

    Runner Dashboard 的配置

    Grafana Lab 有极狐GitLab Runner Dashboard,可以直接导入 Grafana,Dashboard → Import:

    点击 Import:

    输入极狐GitLab Runner Dashboard ID(9631)导入即可,接着就可以看到 Runner 的 Dashboard 呈现界面了:

    由于极狐GitLab Runner 的 metrics 数量较多,Dashboard 导入之后只有一部分可见,这种情况可以通过自己添加 Panel 来实现。在 Dashboard 上点击 Add panel,选择 Add a new panel:

    在 Metric 中选择需要想要监控的指标,诸如选择 gitlab_runner_request_concurrency,在 label 中选择抓取 metrics 的 job 名称即可:

    点击 Svae 即可。如果想要其他的 metrics 并进行监控和展示,用同样的方法添加 Panel 即可。

  • 相关阅读:
    Python之列表
    [ESP32 IDF+Vscode]OLED多级菜单显示(摇杆控制)
    vue+elementui表单数组对象深层嵌套之自定义验证规则
    C++ 内联函数的作用
    ECharts多个数据视图进行自适应大小的解决方案
    Android 10.0 Launcher3定制化之folder文件夹文件居中显示的功能实现
    网络安全深入学习第七课——热门框架漏洞(RCE— Fastjson反序列化漏洞)
    vue3事务管理案例、组件基础总结案例
    计算机毕业设计ssm高校会议室管理系统w169g系统+程序+源码+lw+远程部署
    NX二次开发-调内部函数SEL_set_type_filter_index_by_label设置类型过滤器例子剖析怎么查找内部函数调用内部函数
  • 原文地址:https://blog.csdn.net/weixin_44749269/article/details/127620541