-
Notifications
You must be signed in to change notification settings - Fork 57
/
metrics_query.go
107 lines (91 loc) · 2.78 KB
/
metrics_query.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
package main
import (
"context"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/webdevops/go-common/prometheus/collector"
"go.uber.org/zap"
)
type MetricsCollectorQuery struct {
collector.Processor
prometheus struct {
workItemCount *prometheus.GaugeVec
workItemData *prometheus.GaugeVec
}
}
func (m *MetricsCollectorQuery) Setup(collector *collector.Collector) {
m.Processor.Setup(collector)
m.prometheus.workItemCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azure_devops_query_result",
Help: "Azure DevOps Query Result",
},
[]string{
// We use this only for bugs. Add more fields as needed.
"projectId",
"queryPath",
},
)
m.Collector.RegisterMetricList("workItemCount", m.prometheus.workItemCount, true)
m.prometheus.workItemData = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azure_devops_workitem_data",
Help: "Azure DevOps WorkItems",
},
[]string{
"projectId",
"queryPath",
"id",
"title",
"path",
"createdDate",
"acceptedDate",
"resolvedDate",
"closedDate",
},
)
m.Collector.RegisterMetricList("workItemData", m.prometheus.workItemData, true)
}
func (m *MetricsCollectorQuery) Reset() {}
func (m *MetricsCollectorQuery) Collect(callback chan<- func()) {
ctx := m.Context()
logger := m.Logger()
for _, project := range AzureDevopsServiceDiscovery.ProjectList() {
projectLogger := logger.With(zap.String("project", project.Name))
for _, query := range Opts.AzureDevops.QueriesWithProjects {
queryPair := strings.Split(query, "@")
m.collectQueryResults(ctx, projectLogger, callback, queryPair[0], queryPair[1])
}
}
}
func (m *MetricsCollectorQuery) collectQueryResults(ctx context.Context, logger *zap.SugaredLogger, callback chan<- func(), queryPath string, projectID string) {
workItemsMetric := m.Collector.GetMetricList("workItemCount")
workItemsDataMetric := m.Collector.GetMetricList("workItemData")
workItemInfoList, err := AzureDevopsClient.QueryWorkItems(queryPath, projectID)
if err != nil {
logger.Error(err)
return
}
workItemsMetric.Add(prometheus.Labels{
"projectId": projectID,
"queryPath": queryPath,
}, float64(len(workItemInfoList.List)))
for _, workItemInfo := range workItemInfoList.List {
workItem, err := AzureDevopsClient.GetWorkItem(workItemInfo.Url)
if err != nil {
logger.Error(err)
return
}
workItemsDataMetric.AddInfo(prometheus.Labels{
"projectId": projectID,
"queryPath": queryPath,
"id": int64ToString(workItem.Id),
"title": workItem.Fields.Title,
"path": workItem.Fields.Path,
"createdDate": workItem.Fields.CreatedDate,
"acceptedDate": workItem.Fields.AcceptedDate,
"resolvedDate": workItem.Fields.ResolvedDate,
"closedDate": workItem.Fields.ClosedDate,
})
}
}