Skip to content

Commit

Permalink
setting the guage all at once when all pages traversed
Browse files Browse the repository at this point in the history
  • Loading branch information
emamihe authored and pepoviola committed Nov 27, 2023
1 parent 3814f16 commit 91fd6de
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions scripts/jobs_status_exporter/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,36 @@
}

def fetch_jobs_by_runner(runner_id):
"""Fetch jobs from a specific GitLab Runner and update Prometheus metrics."""
status_counts = {status: 0 for status in status_gauges}

page = 1
while True:
headers = {'PRIVATE-TOKEN': GITLAB_PRIVATE_TOKEN}
params = {'page': page, 'per_page': 100}
params = {'page': page, 'per_page': 100}
response = requests.get(GITLAB_API_ENDPOINT.format(runner_id), headers=headers, params=params)
response.raise_for_status()
response.raise_for_status()

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

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

page += 1
page += 1

for status, count in status_counts.items():
status_gauges[status].set(count)

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)
time.sleep(60)

if __name__ == '__main__':
main()

0 comments on commit 91fd6de

Please sign in to comment.