Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting the guage all at once when all pages traversed #1530

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading