-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmetrics_pullrequest.go
141 lines (121 loc) · 3.95 KB
/
metrics_pullrequest.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
135
136
137
138
139
140
141
package main
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/webdevops/go-common/prometheus/collector"
"github.com/webdevops/go-common/utils/to"
"go.uber.org/zap"
devopsClient "github.com/webdevops/azure-devops-exporter/azure-devops-client"
)
type MetricsCollectorPullRequest struct {
collector.Processor
prometheus struct {
pullRequest *prometheus.GaugeVec
pullRequestStatus *prometheus.GaugeVec
pullRequestLabel *prometheus.GaugeVec
}
}
func (m *MetricsCollectorPullRequest) Setup(collector *collector.Collector) {
m.Processor.Setup(collector)
m.prometheus.pullRequest = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azure_devops_pullrequest_info",
Help: "Azure DevOps pullrequest",
},
[]string{
"projectID",
"repositoryID",
"pullrequestID",
"pullrequestTitle",
"sourceBranch",
"targetBranch",
"status",
"isDraft",
"voteStatus",
"creator",
},
)
m.Collector.RegisterMetricList("pullRequest", m.prometheus.pullRequest, true)
m.prometheus.pullRequestStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azure_devops_pullrequest_status",
Help: "Azure DevOps pullrequest status",
},
[]string{
"projectID",
"repositoryID",
"pullrequestID",
"type",
},
)
m.Collector.RegisterMetricList("pullRequestStatus", m.prometheus.pullRequestStatus, true)
m.prometheus.pullRequestLabel = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azure_devops_pullrequest_label",
Help: "Azure DevOps pullrequest labels",
},
[]string{
"projectID",
"repositoryID",
"pullrequestID",
"label",
"active",
},
)
m.Collector.RegisterMetricList("pullRequestLabel", m.prometheus.pullRequestLabel, true)
}
func (m *MetricsCollectorPullRequest) Reset() {}
func (m *MetricsCollectorPullRequest) Collect(callback chan<- func()) {
ctx := m.Context()
logger := m.Logger()
for _, project := range AzureDevopsServiceDiscovery.ProjectList() {
projectLogger := logger.With(zap.String("project", project.Name))
for _, repository := range project.RepositoryList.List {
if repository.Disabled() {
continue
}
repoLogger := projectLogger.With(zap.String("repository", repository.Name))
m.collectPullRequests(ctx, repoLogger, callback, project, repository)
}
}
}
func (m *MetricsCollectorPullRequest) collectPullRequests(ctx context.Context, logger *zap.SugaredLogger, callback chan<- func(), project devopsClient.Project, repository devopsClient.Repository) {
list, err := AzureDevopsClient.ListPullrequest(project.Id, repository.Id)
if err != nil {
logger.Error(err)
return
}
pullRequestMetric := m.Collector.GetMetricList("pullRequest")
pullRequestStatusMetric := m.Collector.GetMetricList("pullRequestStatus")
pullRequestLabelMetric := m.Collector.GetMetricList("pullRequestLabel")
for _, pullRequest := range list.List {
voteSummary := pullRequest.GetVoteSummary()
pullRequestMetric.AddInfo(prometheus.Labels{
"projectID": project.Id,
"repositoryID": repository.Id,
"pullrequestID": int64ToString(pullRequest.Id),
"pullrequestTitle": pullRequest.Title,
"status": pullRequest.Status,
"voteStatus": voteSummary.HumanizeString(),
"creator": pullRequest.CreatedBy.DisplayName,
"isDraft": to.BoolString(pullRequest.IsDraft),
"sourceBranch": pullRequest.SourceRefName,
"targetBranch": pullRequest.TargetRefName,
})
pullRequestStatusMetric.AddTime(prometheus.Labels{
"projectID": project.Id,
"repositoryID": repository.Id,
"pullrequestID": int64ToString(pullRequest.Id),
"type": "created",
}, pullRequest.CreationDate)
for _, label := range pullRequest.Labels {
pullRequestLabelMetric.AddInfo(prometheus.Labels{
"projectID": project.Id,
"repositoryID": repository.Id,
"pullrequestID": int64ToString(pullRequest.Id),
"label": label.Name,
"active": to.BoolString(label.Active),
})
}
}
}