-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathevents.go
More file actions
170 lines (151 loc) · 5.66 KB
/
events.go
File metadata and controls
170 lines (151 loc) · 5.66 KB
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
package collectors
import (
"time"
"github.com/cloudfoundry/cf_exporter/models"
"github.com/prometheus/client_golang/prometheus"
)
type EventsCollector struct {
namespace string
environment string
deployment string
eventsInfoMetric *prometheus.GaugeVec
eventsScrapesTotalMetric prometheus.Counter
eventsScrapeErrorsTotalMetric prometheus.Counter
lastEventsScrapeErrorMetric prometheus.Gauge
lastEventsScrapeTimestampMetric prometheus.Gauge
lastEventsScrapeDurationSecondsMetric prometheus.Gauge
lastCheckFilter time.Time
timeLocation *time.Location
}
func NewEventsCollector(
namespace string,
environment string,
deployment string,
) *EventsCollector {
eventsInfoMetric := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "events",
Name: "info",
Help: "Labeled Cloud Foundry Events information with a constant '1' value.",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
[]string{"type", "actor", "actor_type", "actor_name", "actor_username", "actee", "actee_type", "actee_name", "space_id", "organization_id"},
)
eventsScrapesTotalMetric := prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "events_scrapes",
Name: "total",
Help: "Total number of scrapes for Cloud Foundry Events.",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
)
eventsScrapeErrorsTotalMetric := prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "events_scrape_errors",
Name: "total",
Help: "Total number of scrape errors of Cloud Foundry Events.",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
)
lastEventsScrapeErrorMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_events_scrape_error",
Help: "Whether the last scrape of Event metrics from Cloud Foundry resulted in an error (1 for error, 0 for success).",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
)
lastEventsScrapeTimestampMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_events_scrape_timestamp",
Help: "Number of seconds since 1970 since last scrape of Event metrics from Cloud Foundry.",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
)
lastEventsScrapeDurationSecondsMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_events_scrape_duration_seconds",
Help: "Duration of the last scrape of Events metrics from Cloud Foundry.",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
)
timeLocation, _ := time.LoadLocation("UTC")
now := time.Now().In(timeLocation)
return &EventsCollector{
namespace: namespace,
environment: environment,
deployment: deployment,
eventsInfoMetric: eventsInfoMetric,
eventsScrapesTotalMetric: eventsScrapesTotalMetric,
eventsScrapeErrorsTotalMetric: eventsScrapeErrorsTotalMetric,
lastEventsScrapeErrorMetric: lastEventsScrapeErrorMetric,
lastEventsScrapeTimestampMetric: lastEventsScrapeTimestampMetric,
lastEventsScrapeDurationSecondsMetric: lastEventsScrapeDurationSecondsMetric,
lastCheckFilter: now,
timeLocation: timeLocation,
}
}
func (c *EventsCollector) Collect(objs *models.CFObjects, ch chan<- prometheus.Metric) {
errorMetric := float64(0)
if objs.Error != nil {
errorMetric = float64(1)
c.eventsScrapeErrorsTotalMetric.Inc()
} else {
c.reportEventsMetrics(objs, ch)
}
c.eventsScrapeErrorsTotalMetric.Collect(ch)
c.eventsScrapesTotalMetric.Inc()
c.eventsScrapesTotalMetric.Collect(ch)
c.lastEventsScrapeErrorMetric.Set(errorMetric)
c.lastEventsScrapeErrorMetric.Collect(ch)
c.lastEventsScrapeTimestampMetric.Set(float64(time.Now().Unix()))
c.lastEventsScrapeTimestampMetric.Collect(ch)
c.lastEventsScrapeDurationSecondsMetric.Set(objs.Took)
c.lastEventsScrapeDurationSecondsMetric.Collect(ch)
}
func (c *EventsCollector) Describe(ch chan<- *prometheus.Desc) {
c.eventsInfoMetric.Describe(ch)
c.eventsScrapesTotalMetric.Describe(ch)
c.eventsScrapeErrorsTotalMetric.Describe(ch)
c.lastEventsScrapeErrorMetric.Describe(ch)
c.lastEventsScrapeTimestampMetric.Describe(ch)
c.lastEventsScrapeDurationSecondsMetric.Describe(ch)
}
// reportEventsMetrics
// 1. find user's username in user map if available
func (c *EventsCollector) reportEventsMetrics(objs *models.CFObjects, ch chan<- prometheus.Metric) {
c.eventsInfoMetric.Reset()
for _, event := range objs.Events {
if event.CreatedAt.Before(c.lastCheckFilter) {
continue
}
// 1.
actorUsername := ""
if user, ok := objs.Users[event.Actor.GUID]; ok {
actorUsername = user.Username
}
c.eventsInfoMetric.WithLabelValues(
event.Type,
event.Actor.GUID,
event.Actor.Type,
event.Actor.Name,
actorUsername,
event.Target.GUID,
event.Target.Type,
event.Target.Name,
event.Space.GUID,
event.Org.GUID,
).Set(float64(1))
}
timeLocation, _ := time.LoadLocation("UTC")
c.lastCheckFilter = time.Now().In(timeLocation)
c.eventsInfoMetric.Collect(ch)
}