-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
217 lines (189 loc) · 8.83 KB
/
collector.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)
const CLUSTER_NAME string = "local_demo_cluster"
const APP_LABEL string = "app.kubernetes.io/instance"
// Define a struct for you collector that contains pointers to prometheus descriptors for each metric you wish to expose.
// You can also include fields of other types if they provide utility
type CommitTimeCollector struct {
commitTimeMetric *prometheus.Desc
deployTimeMetric *prometheus.Desc
activeDeploymentMetric *prometheus.Desc
githubClient *GithubClient
kubeClient *KubeClients
commitHashSet map[string]bool
gitCache map[string]*time.Time
searchLabel string
imageFilter []string
}
// You must create a constructor for you collector that initializes every descriptor and returns a pointer to the collector
func NewCommitTimeCollector() (*CommitTimeCollector, error) {
// Initialize the github client
gh, err := NewGithubClient()
if err != nil {
return nil, err
}
// Initialize the kubernetes clients (clientset and rest)
kubeClient, err := NewKubeClient()
if err != nil {
return nil, err
}
// Get configmap and set config parameters
// default values: if not specified in the config, use these values
searchLabel := "app.kubernetes.io/instance"
imageFilters := []string{"quay.io/redhat-appstudio/", "quay.io/redhat-appstudio-qe/", "quay.io/stolostron/", "quay.io/abarbaro/"}
configMap, err := kubeClient.GetConfigMap("exporters-config", "dora-metrics")
if err == nil {
filtersJson := configMap.Data["imageFilters"]
var imageFilters_ []string
err = json.Unmarshal([]byte(filtersJson), &imageFilters_)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal config from configmap")
}
if label, ok := configMap.Data["searchLabel"]; ok {
searchLabel = label
}
} else {
klog.Error("no configmap found")
}
klog.V(3).Infof("Using label ", searchLabel)
klog.V(3).Infof("Using image filters: ", imageFilters)
return &CommitTimeCollector{
commitTimeMetric: prometheus.NewDesc("dora:committime",
"Shows timestamp for a specific commit",
[]string{"app", "commit_hash", "image", "namespace"}, nil,
),
deployTimeMetric: prometheus.NewDesc("dora:deploytime",
"Shows deployment timestamp for a specific commit",
[]string{"app", "commit_hash", "image", "namespace"}, nil,
),
activeDeploymentMetric: prometheus.NewDesc("dora:deployactive",
"Shows the active deplyment's timestamp in time",
[]string{"app", "commit_hash", "image", "namespace"}, nil,
),
githubClient: gh,
kubeClient: kubeClient,
commitHashSet: map[string]bool{},
gitCache: map[string]*time.Time{},
searchLabel: searchLabel,
imageFilter: imageFilters,
}, nil
}
//Each and every collector must implement the Describe function. It essentially writes all descriptors to the prometheus desc channel.
func (collector *CommitTimeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.commitTimeMetric
ch <- collector.deployTimeMetric
}
//Collect implements required collect function for all promehteus collectors
func (collector *CommitTimeCollector) Collect(ch chan<- prometheus.Metric) {
// List all deployments having argocd label app.kubernetes.io/instance
// Use these deployments to get images and gather deploytime and commit time
// this map is used at each scraping to avoid parsing an image twice: duplicate metrics are not allowed by the collector and will throw an error
collector.commitHashSet = map[string]bool{}
deploymentList, err := collector.kubeClient.ListDeploymentsByLabels(collector.searchLabel)
if err != nil {
klog.Error(err)
return
}
// loop through all deployments found
for _, depl := range deploymentList.Items {
// and get all container's images
for _, cont := range depl.Spec.Template.Spec.Containers {
// filter the images to only use the appstudio ones
if isOk := filterImage(collector.imageFilter, cont.Image); isOk {
collector.CollectCommitTime(ch, &depl, &cont)
collector.CollectDeployTime(ch, &depl, &cont)
collector.commitHashSet[cont.Image] = true
}
}
}
}
func (collector *CommitTimeCollector) CollectCommitTime(ch chan<- prometheus.Metric, depl *appsv1.Deployment, cont *v1.Container) {
// check we have not parsed this image already
_, ok := collector.commitHashSet[cont.Image]
if !ok {
// get data needed for prometheus labels
namespace := depl.Namespace
component := depl.Labels[collector.searchLabel]
// parse the image url to extract organization, repository and commit hash
fields := reSubMatchMap(imageRegex, cont.Image)
// check if we have already searched for this commit, before requesting github apis
// if yes, use that value and return
commitTimeValue, commitCached := collector.gitCache[cont.Image]
fmt.Println(commitTimeValue, commitCached)
if !commitCached {
klog.V(3).Infof("Commit time is not cached yet: %s %s", fields["repo"], fields["hash"])
} else {
m1 := prometheus.MustNewConstMetric(collector.commitTimeMetric, prometheus.GaugeValue, float64(commitTimeValue.Unix()), component, fields["hash"], cont.Image, namespace)
// We let prometheus set the scraping timestamp; if we force-set it to the commit time we risk losing old out-of-bound data
ch <- m1
klog.V(3).Infof("collected (from cache) committime for %s", cont.Image)
return
}
// if the data is not cached, look in github: first try is using org+repo+hash to directly get the data from the repo (we want to avoid searching for a generic hash)
commit, err := collector.githubClient.GetCommitFromOrgAndRepo(fields["org"], fields["repo"], fields["hash"])
if err != nil {
klog.V(2).Infof("Can't find commit time using %s, %s and %s", fields["org"], fields["repo"], fields["hash"])
} else {
m1 := prometheus.MustNewConstMetric(collector.commitTimeMetric, prometheus.GaugeValue, float64(commit.Author.Date.Unix()), component, fields["hash"], cont.Image, namespace)
// We let prometheus set the scraping timestamp; if we force-set it to the commit time we risk losing old out-of-bound data
ch <- m1
klog.V(3).Infof("collected committime for %s", cont.Image)
collector.gitCache[cont.Image] = commit.Author.Date
return
}
commit, err = collector.githubClient.SearchCommit(fields["hash"])
if err != nil {
klog.V(1).Infof("Can't find commit either by get or search: %s - %s", fields["repo"], fields["hash"])
} else {
m1 := prometheus.MustNewConstMetric(collector.commitTimeMetric, prometheus.GaugeValue, float64(commit.Author.Date.Unix()), component, fields["hash"], cont.Image, namespace)
// We let prometheus set the scraping timestamp; if we force-set it to the commit time we risk losing old out-of-bound data
ch <- m1
klog.V(3).Infof("collected committime for %s", cont.Image, ": ", err)
collector.gitCache[cont.Image] = commit.Author.Date
return
}
}
}
func (collector *CommitTimeCollector) CollectDeployTime(ch chan<- prometheus.Metric, depl *appsv1.Deployment, cont *v1.Container) {
// check we have not parsed this image already
_, ok := collector.commitHashSet[cont.Image]
if !ok {
// get data needed for prometheus labels
namespace := depl.Namespace
component := depl.Labels[collector.searchLabel]
// parse the image url to extract organization, repository and commit hash
fields := reSubMatchMap(imageRegex, cont.Image)
// If the deployment is active we also collect the deploy time metric using the deployment creation timestamp
isActive, _ := collector.kubeClient.IsDeploymentActiveSince(depl)
if isActive {
creationTime, err := collector.kubeClient.GetDeploymentReplicaSetCreationTime(namespace, depl.Name, cont.Image)
if err != nil {
klog.Error(err)
} else {
isOkToIngest := creationTime.After(time.Now().Add(-1 * time.Hour))
if isOkToIngest {
m1 := prometheus.MustNewConstMetric(collector.deployTimeMetric, prometheus.GaugeValue, float64(creationTime.Unix()), component, fields["hash"], cont.Image, namespace)
// We care only deployments collected after install time, so we can force-set the timestamp to the deplytime without loosing any data
// This will simplify building the metrics in Grafana.
// Active deplyments with current timestamp are anyways collected by the activeDeploymentMetric right after
m1 = prometheus.NewMetricWithTimestamp(creationTime.Time, m1)
ch <- m1
klog.V(3).Infof("collected deploytime for %s", cont.Image)
}
m2 := prometheus.MustNewConstMetric(collector.activeDeploymentMetric, prometheus.GaugeValue, float64(creationTime.Unix()), component, fields["hash"], cont.Image, namespace)
ch <- m2
klog.V(3).Infof("collected active deployment for %s", cont.Image)
}
} else {
klog.V(1).Infof("%s deploy time not collected because deployment is not in active state.\n", cont.Image)
}
}
}