-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-exporter.py
58 lines (48 loc) · 1.81 KB
/
git-exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
from prometheus_client import start_http_server, Gauge
GITLAB_API_URL = "https://gitlab.wikimedia.org/api/v4"
GITLAB_TOKEN = "your_private_token"
PROJECT_ID = "2244"
commit_gauge = Gauge('gitlab_commits_total', 'Total number of commits', ['project_id'])
merge_request_gauge = Gauge('gitlab_merge_requests_total', 'Total number of merge requests')
project_counts = Gauge('gitlab_project_count', 'Total number of projects')
# Create a session object
session = requests.Session()
def get_projects():
try:
response = session.get(f"{GITLAB_API_URL}/projects")
response.raise_for_status()
projects = response.json()
project_ids = [project['id'] for project in projects]
#print(project_id)
#return len(response.json())
return project_ids
except Exception as e:
print(f"Error fetching commits: {e}")
return 0
def get_commits(project_id):
try:
response = session.get(f"{GITLAB_API_URL}/projects/{project_id}/repository/commits")
response.raise_for_status()
return len(response.json())
except Exception as e:
print(f"Error fetching commits: {e}")
return 0
def get_merge_requests(project_id):
try:
response = session.get(f"{GITLAB_API_URL}/projects/{project_id}/merge_requests")
response.raise_for_status()
return len(response.json())
except Exception as e:
print(f"Error fetching merge requests: {e}")
return 0
def collect_metrics():
#project_counts.set(get_projects())
project_ids = get_projects()
for project_id in project_ids:
num_commits = get_commits(project_id)
commit_gauge.labels(project_id=project_id).set(num_commits)
if __name__ == '__main__':
start_http_server(8000)
while True:
collect_metrics()