Skip to content

Commit

Permalink
bug fix for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
emamihe authored and pepoviola committed Nov 13, 2023
1 parent deeacaf commit 8357429
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions scripts/jobs_status_exporter/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,32 @@

def fetch_jobs_by_runner(runner_id):
"""Fetch jobs from a specific GitLab Runner and update Prometheus metrics."""
headers = {'PRIVATE-TOKEN': GITLAB_PRIVATE_TOKEN}
response = requests.get(GITLAB_API_ENDPOINT.format(runner_id), headers=headers)
response.raise_for_status()
jobs = response.json()

for gauge in status_gauges.values():
gauge.set(0)

for job in jobs:
status = job.get('status')
if status in status_gauges:
status_gauges[status].inc()
page = 1
while True:
headers = {'PRIVATE-TOKEN': GITLAB_PRIVATE_TOKEN}
params = {'page': page, 'per_page': 100}
response = requests.get(GITLAB_API_ENDPOINT.format(runner_id), headers=headers, params=params)
response.raise_for_status()

jobs = response.json()
if not jobs:
break

for job in jobs:
status = job.get('status')
if status in status_gauges:
status_gauges[status].inc()

page += 1

def main():
start_http_server(8000)
print("Metrics server running on port 8000")

while True:
for gauge in status_gauges.values():
gauge.set(0)

fetch_jobs_by_runner(RUNNER_ID)
time.sleep(60)

Expand Down

0 comments on commit 8357429

Please sign in to comment.