forked from c4po/harbor_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_repositories.go
134 lines (121 loc) · 3.67 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"strconv"
"time"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
func (h *HarborExporter) collectRepositoriesMetric(ch chan<- prometheus.Metric) bool {
start := time.Now()
type projectsMetrics []struct {
ProjectID float64
OwnerID float64
Name string
RepoCount float64
ChartCount float64
}
type repositoriesMetric []struct {
ID float64
Name string
ProjectID float64
Description string
PullCount float64
StarCount float64
TagsCount float64
CreationTime time.Time
UpdateTime time.Time
labels []struct {
ID float64
Name string
ProjectID float64
Description string
Color string
Deleted bool
Scope string
CreationTime time.Time
UpdateTime time.Time
}
}
type repositoriesMetricV2 []struct {
ID float64
Name string
ProjectID float64
Description string
PullCount float64
ArtifactCount float64
CreationTime time.Time
UpdateTime time.Time
}
var projectsData projectsMetrics
err := h.requestAll("/projects", func(pageBody []byte) error {
var pageData projectsMetrics
if err := json.Unmarshal(pageBody, &pageData); err != nil {
return err
}
projectsData = append(projectsData, pageData...)
return nil
})
if err != nil {
level.Error(h.logger).Log(err.Error())
return false
}
for i := range projectsData {
projectID := strconv.FormatFloat(projectsData[i].ProjectID, 'f', 0, 32)
if h.isV2 {
var data repositoriesMetricV2
err := h.requestAll("/projects/"+projectsData[i].Name+"/repositories", func(pageBody []byte) error {
var pageData repositoriesMetricV2
if err := json.Unmarshal(pageBody, &pageData); err != nil {
return err
}
data = append(data, pageData...)
return nil
})
if err != nil {
level.Error(h.logger).Log(err.Error())
return false
}
for i := range data {
repoID := strconv.FormatFloat(data[i].ID, 'f', 0, 32)
ch <- prometheus.MustNewConstMetric(
allMetrics["repositories_pull_total"].Desc, allMetrics["repositories_pull_total"].Type, data[i].PullCount, data[i].Name, repoID,
)
// ch <- prometheus.MustNewConstMetric(
// allMetrics["repositories_star_total"].Desc, allMetrics["repositories_star_total"].Type, data[i].Star_count, data[i].Name, repoId,
// )
ch <- prometheus.MustNewConstMetric(
allMetrics["repositories_tags_total"].Desc, allMetrics["repositories_tags_total"].Type, data[i].ArtifactCount, data[i].Name, repoID,
)
}
} else {
var data repositoriesMetric
err := h.requestAll("/repositories?project_id="+projectID, func(pageBody []byte) error {
var pageData repositoriesMetric
if err := json.Unmarshal(pageBody, &pageData); err != nil {
return err
}
data = append(data, pageData...)
return nil
})
if err != nil {
level.Error(h.logger).Log(err.Error())
return false
}
for i := range data {
repoID := strconv.FormatFloat(data[i].ID, 'f', 0, 32)
ch <- prometheus.MustNewConstMetric(
allMetrics["repositories_pull_total"].Desc, allMetrics["repositories_pull_total"].Type, data[i].PullCount, data[i].Name, repoID,
)
ch <- prometheus.MustNewConstMetric(
allMetrics["repositories_star_total"].Desc, allMetrics["repositories_star_total"].Type, data[i].StarCount, data[i].Name, repoID,
)
ch <- prometheus.MustNewConstMetric(
allMetrics["repositories_tags_total"].Desc, allMetrics["repositories_tags_total"].Type, data[i].TagsCount, data[i].Name, repoID,
)
}
}
}
reportLatency(start, "repositories_latency", ch)
return true
}