Skip to content

Commit b6b457f

Browse files
mahipdeora25claude
andcommitted
[gitlab_runner] Add gitlab_runner_job_queue_duration_seconds metric
Add support for the `gitlab_runner_job_queue_duration_seconds` histogram metric which tracks how long jobs wait in the queue before being picked up by a runner. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7cb894d commit b6b457f

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

gitlab_runner/assets/configuration/spec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ files:
2121
- gitlab_runner_autoscaling_machine_creation_duration_seconds
2222
- gitlab_runner_autoscaling_machine_states
2323
- gitlab_runner_errors_total
24+
- gitlab_runner_job_queue_duration_seconds
2425
- gitlab_runner_jobs
2526
- gitlab_runner_version_info
2627
- go_gc_duration_seconds

gitlab_runner/metadata.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ gitlab_runner.ci_ssh_docker_machines_provider_machine_states,gauge,,request,seco
1313
gitlab_runner.gitlab_runner_autoscaling_machine_creation_duration_seconds,gauge,,request,second,A histogram of Docker machine creation time. Applies to GitLab Runner 1.11.0+,0,gitlab_runner,auto docker machine creation time,
1414
gitlab_runner.gitlab_runner_autoscaling_machine_states,gauge,,request,second,The current number of machines per state in this provider. Applies to GitLab Runner 1.11.0+,0,gitlab_runner,auto total docker machines per state,
1515
gitlab_runner.gitlab_runner_errors_total,count,,request,second,The number of caught errors. Applies to GitLab Runner 1.11.0+,0,gitlab_runner,errors,
16+
gitlab_runner.gitlab_runner_job_queue_duration_seconds.count,gauge,,,,Histogram bucket and count of job queue duration observations.,0,gitlab_runner,job queue duration count,
17+
gitlab_runner.gitlab_runner_job_queue_duration_seconds.sum,gauge,,second,,Sum of job queue duration in seconds.,0,gitlab_runner,job queue duration sum,
1618
gitlab_runner.gitlab_runner_jobs,gauge,,,,The current number of running builds. Applies to GitLab Runner 1.11.0+,0,gitlab_runner,current running builds,
1719
gitlab_runner.gitlab_runner_jobs_total,count,,,,The total number of jobs executed.,1,gitlab_runner,total jobs,
1820
gitlab_runner.gitlab_runner_version_info,gauge,,request,,A metric with a constant '1' value labeled by different build stats fields. Applies to GitLab Runner 1.11.0+,0,gitlab_runner,version info,

gitlab_runner/tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66

7+
import mock
78
import pytest
89

910
from datadog_checks.dev import docker_run
@@ -17,6 +18,7 @@
1718
GITLAB_RUNNER_URL,
1819
GITLAB_TEST_TOKEN,
1920
HERE,
21+
HOST,
2022
)
2123

2224
# Needed to mount volume for logging
@@ -57,3 +59,30 @@ def dd_environment():
5759
conditions=conditions,
5860
):
5961
yield CONFIG, E2E_METADATA
62+
63+
64+
def _mocked_requests_get(*args, **kwargs):
65+
url = args[0]
66+
67+
if url == 'http://{}:{}/metrics'.format(HOST, GITLAB_LOCAL_RUNNER_PORT):
68+
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'metrics.txt')
69+
with open(fixtures_path, 'r') as f:
70+
text_data = f.read()
71+
return mock.MagicMock(
72+
status_code=200,
73+
iter_lines=lambda **kwargs: text_data.split("\n"),
74+
headers={'Content-Type': "text/plain"},
75+
)
76+
elif url == 'http://{}:{}/ci'.format(HOST, GITLAB_LOCAL_MASTER_PORT):
77+
return mock.MagicMock(status_code=200)
78+
79+
return mock.MagicMock(status_code=404)
80+
81+
82+
@pytest.fixture()
83+
def mock_data():
84+
with mock.patch(
85+
'requests.Session.get',
86+
side_effect=_mocked_requests_get,
87+
):
88+
yield
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# HELP ci_runner_version_info A metric with a constant '1' value labeled by different build stats fields
2+
# TYPE ci_runner_version_info gauge
3+
ci_runner_version_info{architecture="amd64",branch="main",built_at="2023-01-01T00:00:00+0000",go_version="go1.20",name="gitlab-runner",os="linux",revision="abc123",version="10.8.0"} 1
4+
# HELP ci_runner_errors The number of caught errors
5+
# TYPE ci_runner_errors counter
6+
ci_runner_errors{level="warning"} 2
7+
# HELP process_max_fds Maximum number of open file descriptors
8+
# TYPE process_max_fds gauge
9+
process_max_fds 1.048576e+06
10+
# HELP process_open_fds Number of open file descriptors
11+
# TYPE process_open_fds gauge
12+
process_open_fds 12
13+
# HELP process_resident_memory_bytes Resident memory size in bytes
14+
# TYPE process_resident_memory_bytes gauge
15+
process_resident_memory_bytes 2.7394048e+07
16+
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds
17+
# TYPE process_start_time_seconds gauge
18+
process_start_time_seconds 1.67281956837e+09
19+
# HELP process_virtual_memory_bytes Virtual memory size in bytes
20+
# TYPE process_virtual_memory_bytes gauge
21+
process_virtual_memory_bytes 7.61856e+08
22+
# HELP gitlab_runner_job_queue_duration_seconds Histogram of the time in seconds a job is queued before being picked up by a runner
23+
# TYPE gitlab_runner_job_queue_duration_seconds histogram
24+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="0.3"} 5
25+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="0.6"} 8
26+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="1"} 10
27+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="5"} 12
28+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="10"} 14
29+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="30"} 15
30+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="60"} 15
31+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="300"} 15
32+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="600"} 15
33+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="1800"} 15
34+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="3600"} 15
35+
gitlab_runner_job_queue_duration_seconds_bucket{runner="test-runner",le="+Inf"} 15
36+
gitlab_runner_job_queue_duration_seconds_sum{runner="test-runner"} 45.5
37+
gitlab_runner_job_queue_duration_seconds_count{runner="test-runner"} 15

gitlab_runner/tests/test_unit.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import mock
88
import pytest
99

10+
from datadog_checks.dev.utils import get_metadata_metrics
1011
from datadog_checks.gitlab_runner import GitlabRunnerCheck
1112

1213
from . import common
@@ -44,3 +45,25 @@ def test_timeout(test_case, timeout_config, expected_timeout):
4445
verify=mock.ANY,
4546
allow_redirects=mock.ANY,
4647
)
48+
49+
50+
@pytest.mark.unit
51+
def test_job_queue_duration_metric(aggregator, dd_run_check, mock_data):
52+
"""
53+
Test that the gitlab_runner_job_queue_duration_seconds histogram metric
54+
is properly collected from the Prometheus endpoint.
55+
"""
56+
allowed_metrics = list(common.ALLOWED_METRICS) + ['gitlab_runner_job_queue_duration_seconds']
57+
config = deepcopy(common.CONFIG)
58+
config['init_config']['allowed_metrics'] = allowed_metrics
59+
60+
check = GitlabRunnerCheck('gitlab_runner', config['init_config'], instances=config['instances'])
61+
dd_run_check(check)
62+
dd_run_check(check)
63+
64+
# Histogram buckets and count (reported as .count with upper_bound tags)
65+
aggregator.assert_metric('gitlab_runner.gitlab_runner_job_queue_duration_seconds.count')
66+
# Histogram sum
67+
aggregator.assert_metric('gitlab_runner.gitlab_runner_job_queue_duration_seconds.sum')
68+
69+
aggregator.assert_metrics_using_metadata(get_metadata_metrics())

0 commit comments

Comments
 (0)