forked from c4po/harbor_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_repositories.go
74 lines (67 loc) · 1.89 KB
/
metrics_repositories.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"strconv"
"time"
)
func (e *Exporter) collectRepositoriesMetric(ch chan<- prometheus.Metric) bool {
type projectsMetrics []struct {
Project_id float64
Owner_id float64
Name string
Repo_count float64
Chart_count float64
}
type repositoriesMetric []struct {
Id float64
Name string
Project_id float64
Description string
Pull_count float64
Star_count float64
Tags_count float64
Creation_time time.Time
Update_time time.Time
labels []struct {
Id float64
Name string
Project_id float64
Description string
Color string
Deleted bool
Scope string
Creation_time time.Time
Update_time time.Time
}
}
projectsBody := e.client.request("/api/projects")
var projectsData projectsMetrics
if err := json.Unmarshal(projectsBody, &projectsData); err != nil {
level.Error(e.logger).Log(err.Error())
return false
}
for i := range projectsData {
projectId := strconv.FormatFloat(projectsData[i].Project_id, 'f', 0, 32)
body := e.client.request("/api/repositories?project_id=" + projectId)
var data repositoriesMetric
if err := json.Unmarshal(body, &data); err != nil {
level.Error(e.logger).Log(err.Error())
return false
}
for i := range data {
repoId := strconv.FormatFloat(data[i].Id, 'f', 0, 32)
ch <- prometheus.MustNewConstMetric(
repositoriesPullCount, prometheus.GaugeValue, data[i].Pull_count, data[i].Name, repoId,
)
ch <- prometheus.MustNewConstMetric(
repositoriesStarCount, prometheus.GaugeValue, data[i].Star_count, data[i].Name, repoId,
)
ch <- prometheus.MustNewConstMetric(
repositoriesTagsCount, prometheus.GaugeValue, data[i].Tags_count, data[i].Name, repoId,
)
}
}
return true
}