/
exporter.go
185 lines (165 loc) · 6.78 KB
/
exporter.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package exporter
import (
"strconv"
"github.com/nikhilsbhat/gocd-prometheus-exporter/pkg/common"
"github.com/nikhilsbhat/gocd-prometheus-exporter/pkg/gocd"
goCD "github.com/nikhilsbhat/gocd-sdk-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/thoas/go-funk"
)
func (e *Exporter) collect(channel chan<- prometheus.Metric) { //nolint:funlen
// fetching server health status
if !funk.Contains(e.skipMetrics, common.MetricServerHealth) {
for _, health := range gocd.CurrentServerHealth {
e.serverHealth.WithLabelValues(health.Level, health.Message).Set(1)
}
e.serverHealth.Collect(channel)
}
// fetching agent count metrics
if !funk.Contains(e.skipMetrics, common.MetricAgentsCount) {
e.agentsCount.WithLabelValues("all").Set(float64(len(gocd.CurrentAgentsConfig)))
e.agentsCount.Collect(channel)
}
// fetching agent down and agent disk space metrics
if !funk.Contains(e.skipMetrics, common.MetricAgentDown) || !funk.Contains(e.skipMetrics, common.MetricAgentDiskSpace) {
e.agentDown.Reset()
for _, node := range gocd.CurrentAgentsConfig {
if node.CurrentState == common.GoCdDisconnectedState {
e.agentDown.WithLabelValues(node.Name, node.ID, node.Version, node.OS, node.Sandbox, node.CurrentState, node.ConfigState).Set(1)
}
if !funk.Contains(e.skipMetrics, common.MetricAgentDiskSpace) {
e.agentDisk.WithLabelValues(node.Name, node.ID, node.Version, node.OS, node.Sandbox).Set(common.Float(node.DiskSpaceAvailable))
}
}
e.agentDown.Collect(channel)
e.agentDisk.Collect(channel)
}
// fetching repo count metrics
if !funk.Contains(e.skipMetrics, common.MetricConfigRepoCount) {
e.configRepoCount.WithLabelValues("all").Set(float64(len(gocd.CurrentConfigRepos)))
e.configRepoCount.Collect(channel)
}
// fetching config repo in errored state
if !funk.Contains(e.skipMetrics, common.MetricConfigRepoFailed) {
for _, repo := range gocd.CurrentFailedConfigRepos {
if len(repo.ConfigRepoParseInfo.Error) != 0 {
e.configRepoFailure.WithLabelValues(repo.ID, repo.PluginID).Set(1)
}
}
e.configRepoFailure.Collect(channel)
}
// fetching system admins metrics
if !funk.Contains(e.skipMetrics, common.MetricSystemAdminsCount) {
e.adminCount.WithLabelValues("all").Set(float64(len(gocd.CurrentSystemAdmins.Users)))
e.adminCount.Collect(channel)
}
// fetching pipeline group metrics
if !funk.Contains(e.skipMetrics, common.MetricPipelineGroupCount) {
e.pipelineGroupCount.WithLabelValues("all").Set(float64(len(gocd.CurrentPipelineGroup)))
e.pipelineGroupCount.Collect(channel)
}
// fetching pipeline count metrics
if !funk.Contains(e.skipMetrics, common.MetricPipelineCount) {
e.pipelineCount.WithLabelValues("all").Set(float64(gocd.CurrentPipelineCount))
e.pipelineCount.Collect(channel)
}
if !funk.Contains(e.skipMetrics, common.MetricEnvironmentCountAll) {
e.environmentCount.WithLabelValues("all").Set(float64(len(gocd.CurrentEnvironments)))
e.environmentCount.Collect(channel)
}
// fetching backup metrics
if !funk.Contains(e.skipMetrics, common.MetricConfiguredBackup) {
var enabled float64
if len(gocd.CurrentBackupConfig.Schedule) != 0 {
enabled = 1.0
}
e.backupConfigured.WithLabelValues(
strconv.FormatBool(gocd.CurrentBackupConfig.EmailOnSuccess),
strconv.FormatBool(gocd.CurrentBackupConfig.EmailOnFailure),
gocd.CurrentBackupConfig.Schedule).Set(enabled)
e.backupConfigured.Collect(channel)
}
if !funk.Contains(e.skipMetrics, common.MetricVersion) {
if (gocd.CurrentVersion == goCD.VersionInfo{}) {
e.versionInfo.WithLabelValues("", "", "").Set(0)
} else {
e.versionInfo.WithLabelValues(gocd.CurrentVersion.Version, gocd.CurrentVersion.GitSHA, gocd.CurrentVersion.FullVersion).Set(1)
}
e.versionInfo.Collect(channel)
}
if !funk.Contains(e.skipMetrics, common.MetricJobStatus) {
for _, agentHistory := range gocd.CurrentAgentJobRunHistory {
if len(agentHistory.Jobs) != 0 {
switch agentHistory.Jobs[0].Result {
case common.GoCdPipelineStateFail:
e.jobStatus.WithLabelValues(agentHistory.Jobs[0].Name, agentHistory.Jobs[0].JobName,
agentHistory.Jobs[0].StageName, agentHistory.Jobs[0].Result).Set(1)
case common.GoCdPipelineStatePass:
e.jobStatus.WithLabelValues(agentHistory.Jobs[0].Name, agentHistory.Jobs[0].JobName,
agentHistory.Jobs[0].StageName, agentHistory.Jobs[0].Result).Set(1)
default:
e.jobStatus.WithLabelValues(agentHistory.Jobs[0].Name, agentHistory.Jobs[0].JobName,
agentHistory.Jobs[0].StageName, agentHistory.Jobs[0].Result).Set(1)
}
}
}
e.jobStatus.Collect(channel)
}
if !funk.Contains(e.skipMetrics, common.MetricPipelineState) {
for _, pipelineState := range gocd.CurrentPipelineState {
e.pipelineState.WithLabelValues(pipelineState.Name,
strconv.FormatBool(pipelineState.Paused),
strconv.FormatBool(pipelineState.Locked),
strconv.FormatBool(pipelineState.Schedulable),
pipelineState.PausedBy)
}
e.pipelineState.Collect(channel)
}
// fetching agent down and agent disk space metrics
if !funk.Contains(e.skipMetrics, common.MetricElasticAgentProfileUsage) {
for _, elasticProfile := range gocd.CurrentElasticProfileUsage {
for _, usage := range elasticProfile.Usage {
e.elasticProfileUsage.WithLabelValues(elasticProfile.Name, usage.PipelineName, usage.StageName, usage.JobName, usage.PipelineConfigOrigin).Set(1)
}
}
e.elasticProfileUsage.Collect(channel)
}
// fetching agent down and agent disk space metrics
if !funk.Contains(e.skipMetrics, common.MetricPlugins) {
for _, plugin := range gocd.CurrentPluginInfo {
e.plugins.WithLabelValues(plugin.ID, plugin.Status.State, strconv.FormatBool(plugin.BundledPlugin)).Set(1)
}
e.plugins.Collect(channel)
}
const intBase = 10
// fetching pipelines not run in last X days
if !funk.Contains(e.skipMetrics, common.MetricPipelineNotRun) {
e.pipelineNotRun.Reset()
for _, pipeline := range gocd.CurrentPipelineNotRun {
e.pipelineNotRun.WithLabelValues(pipeline.Usage.Name,
strconv.FormatInt(pipeline.Usage.Groups[0].History[0].ScheduledTimestamp, intBase),
pipeline.Usage.Groups[0].History[0].ScheduledDate).Set(pipeline.NotRunIn)
}
e.pipelineNotRun.Collect(channel)
}
}
func (e *Exporter) Describe(channel chan<- *prometheus.Desc) {
e.agentsCount.Describe(channel)
e.agentDown.Describe(channel)
e.agentDisk.Describe(channel)
e.serverHealth.Describe(channel)
e.configRepoCount.Describe(channel)
e.adminCount.Describe(channel)
e.backupConfigured.Describe(channel)
e.pipelineGroupCount.Describe(channel)
e.pipelineCount.Describe(channel)
e.environmentCount.Describe(channel)
e.versionInfo.Describe(channel)
e.jobStatus.Describe(channel)
e.pipelineState.Describe(channel)
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
e.collect(ch)
}