-
Notifications
You must be signed in to change notification settings - Fork 51
/
metrics_repositories.go
134 lines (121 loc) · 4.28 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 `json:"project_id"`
OwnerID float64 `json:"owner_id"`
Name string `json:"name"`
RepoCount float64 `json:"repo_count"`
ChartCount float64 `json:"chart_count"`
}
type repositoriesMetric []struct {
ID float64 `json:"id"`
Name string `json:"name"`
ProjectID float64 `json:"project_id"`
Description string `json:"description"`
PullCount float64 `json:"pull_count"`
StarCount float64 `json:"star_count"`
TagsCount float64 `json:"tags_count"`
CreationTime time.Time `json:"creation_time"`
UpdateTime time.Time `json:"update_time"`
labels []struct {
ID float64 `json:"id"`
Name string `json:"name"`
ProjectID float64 `json:"project_id"`
Description string `json:"description"`
Color string `json:"color"`
Deleted bool `json:"deleted"`
Scope string `json:"scope"`
CreationTime time.Time `json:"creation_time"`
UpdateTime time.Time `json:"update_time"`
}
}
type repositoriesMetricV2 []struct {
ID float64 `json:"id"`
Name string `json:"name"`
ProjectID float64 `json:"project_id"`
Description string `json:"description"`
PullCount float64 `json:"pull_count"`
ArtifactCount float64 `json:"artifact_count"`
CreationTime time.Time `json:"creation_time"`
UpdateTime time.Time `json:"update_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
}