From 2e3500e4fb4d933d7120ff8b0661ff2b2a394ad4 Mon Sep 17 00:00:00 2001 From: Hamid Emamian Date: Sun, 5 Nov 2023 15:09:08 +0300 Subject: [PATCH] added new approach for collecting the metrics --- scripts/jobs_status_exporter/Dockerfile | 13 +++++ scripts/jobs_status_exporter/deployment.yaml | 30 ++++++++++++ scripts/jobs_status_exporter/metrics.py | 48 +++++++++++++++++++ .../{requirement.txt => requirements.txt} | 0 scripts/jobs_status_exporter/runner_id | 1 + .../jobs_status_exporter/service-monitor.yaml | 13 +++++ scripts/jobs_status_exporter/service.yaml | 12 +++++ 7 files changed, 117 insertions(+) create mode 100644 scripts/jobs_status_exporter/Dockerfile create mode 100644 scripts/jobs_status_exporter/deployment.yaml create mode 100644 scripts/jobs_status_exporter/metrics.py rename scripts/jobs_status_exporter/{requirement.txt => requirements.txt} (100%) create mode 100644 scripts/jobs_status_exporter/runner_id create mode 100644 scripts/jobs_status_exporter/service-monitor.yaml create mode 100644 scripts/jobs_status_exporter/service.yaml diff --git a/scripts/jobs_status_exporter/Dockerfile b/scripts/jobs_status_exporter/Dockerfile new file mode 100644 index 000000000..1f15a3217 --- /dev/null +++ b/scripts/jobs_status_exporter/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.10-slim + +WORKDIR /usr/src/app + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY metrics.py . + +CMD ["python", "./metrics.py"] diff --git a/scripts/jobs_status_exporter/deployment.yaml b/scripts/jobs_status_exporter/deployment.yaml new file mode 100644 index 000000000..bd2b2fe0d --- /dev/null +++ b/scripts/jobs_status_exporter/deployment.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: metrics-deployment + labels: + app: metrics +spec: + replicas: 1 + selector: + matchLabels: + app: metrics + template: + metadata: + labels: + app: metrics + spec: + containers: + - name: metrics + image: emamihe/gitlab-runner-metrics:1.0 + ports: + - containerPort: 8000 + name: http-metrics + env: + - name: GITLAB_PRIVATE_TOKEN + valueFrom: + secretKeyRef: + name: gitlab-token + key: token + - name: RUNNER_ID + value: "RkrwHxX5" diff --git a/scripts/jobs_status_exporter/metrics.py b/scripts/jobs_status_exporter/metrics.py new file mode 100644 index 000000000..252495271 --- /dev/null +++ b/scripts/jobs_status_exporter/metrics.py @@ -0,0 +1,48 @@ +import os +import requests +import time +from prometheus_client import start_http_server, Gauge + +GITLAB_API_ENDPOINT = 'https://gitlab.parity.io/api/v4/runners/{}/jobs' +GITLAB_PRIVATE_TOKEN = os.getenv('GITLAB_PRIVATE_TOKEN') +RUNNER_ID = os.getenv('RUNNER_ID') + +if not GITLAB_PRIVATE_TOKEN or not RUNNER_ID: + raise EnvironmentError('The environment variables GITLAB_PRIVATE_TOKEN and RUNNER_ID must be set.') + +status_gauges = { + 'created': Gauge('gitlab_runner_jobs_created', 'Number of created jobs'), + 'pending': Gauge('gitlab_runner_jobs_pending', 'Number of pending jobs'), + 'running': Gauge('gitlab_runner_jobs_running', 'Number of running jobs'), + 'failed': Gauge('gitlab_runner_jobs_failed', 'Number of failed jobs'), + 'success': Gauge('gitlab_runner_jobs_success', 'Number of successful jobs'), + 'canceled': Gauge('gitlab_runner_jobs_canceled', 'Number of canceled jobs'), + 'skipped': Gauge('gitlab_runner_jobs_skipped', 'Number of skipped jobs'), + 'manual': Gauge('gitlab_runner_jobs_manual', 'Number of manual jobs'), +} + +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() + +def main(): + start_http_server(8000) + print("Metrics server running on port 8000") + + while True: + fetch_jobs_by_runner(RUNNER_ID) + time.sleep(60) + +if __name__ == '__main__': + main() diff --git a/scripts/jobs_status_exporter/requirement.txt b/scripts/jobs_status_exporter/requirements.txt similarity index 100% rename from scripts/jobs_status_exporter/requirement.txt rename to scripts/jobs_status_exporter/requirements.txt diff --git a/scripts/jobs_status_exporter/runner_id b/scripts/jobs_status_exporter/runner_id new file mode 100644 index 000000000..5798a3632 --- /dev/null +++ b/scripts/jobs_status_exporter/runner_id @@ -0,0 +1 @@ +175886 diff --git a/scripts/jobs_status_exporter/service-monitor.yaml b/scripts/jobs_status_exporter/service-monitor.yaml new file mode 100644 index 000000000..8a9794161 --- /dev/null +++ b/scripts/jobs_status_exporter/service-monitor.yaml @@ -0,0 +1,13 @@ +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + name: metrics-servicemonitor + labels: + app: metrics +spec: + selector: + matchLabels: + app: metrics + endpoints: + - port: http-metrics + interval: 15s diff --git a/scripts/jobs_status_exporter/service.yaml b/scripts/jobs_status_exporter/service.yaml new file mode 100644 index 000000000..bb70f09fb --- /dev/null +++ b/scripts/jobs_status_exporter/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: metrics-service + labels: + app: metrics +spec: + selector: + app: metrics + ports: + - port: 8000 + targetPort: http-metrics