forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (91 loc) · 3.52 KB
/
Copy pathmain.go
File metadata and controls
112 lines (91 loc) · 3.52 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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/pjutil/pprof"
prowjobinformer "k8s.io/test-infra/prow/client/informers/externalversions"
prowflagutil "k8s.io/test-infra/prow/flagutil"
configflagutil "k8s.io/test-infra/prow/flagutil/config"
"k8s.io/test-infra/prow/interrupts"
"k8s.io/test-infra/prow/logrusutil"
"k8s.io/test-infra/prow/metrics"
"k8s.io/test-infra/prow/metrics/prowjobs"
"k8s.io/test-infra/prow/pjutil"
)
type options struct {
config configflagutil.ConfigOptions
kubernetes prowflagutil.KubernetesOptions
instrumentationOptions prowflagutil.InstrumentationOptions
}
func gatherOptions(fs *flag.FlagSet, args ...string) options {
var o options
o.config.AddFlags(fs)
o.kubernetes.AddFlags(fs)
o.instrumentationOptions.AddFlags(fs)
if err := fs.Parse(os.Args[1:]); err != nil {
logrus.WithError(err).Fatalf("cannot parse args: '%s'", os.Args[1:])
}
return o
}
func (o *options) Validate() error {
for _, fs := range []interface{ Validate(bool) error }{&o.config, &o.kubernetes, &o.instrumentationOptions} {
if err := fs.Validate(false); err != nil {
return err
}
}
return nil
}
func mustRegister(component string, lister lister) *prometheus.Registry {
registry := prometheus.NewRegistry()
prometheus.WrapRegistererWith(prometheus.Labels{"collector_name": component}, registry).MustRegister(&prowJobCollector{
lister: lister,
})
registry.MustRegister(
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
collectors.NewGoCollector(),
)
return registry
}
func main() {
logrusutil.ComponentInit()
o := gatherOptions(flag.NewFlagSet(os.Args[0], flag.ExitOnError), os.Args[1:]...)
if err := o.Validate(); err != nil {
logrus.WithError(err).Fatal("Invalid options")
}
defer interrupts.WaitForGracefulShutdown()
pprof.Instrument(o.instrumentationOptions)
health := pjutil.NewHealthOnPort(o.instrumentationOptions.HealthPort)
configAgent, err := o.config.ConfigAgent()
if err != nil {
logrus.WithError(err).Fatal("Error starting config agent.")
}
cfg := configAgent.Config
pjClientset, err := o.kubernetes.ProwJobClientset(false)
if err != nil {
logrus.WithError(err).Fatal("Failed to create prowjob client set")
}
informerFactory := prowjobinformer.NewSharedInformerFactoryWithOptions(pjClientset, 0, prowjobinformer.WithNamespace(cfg().ProwJobNamespace))
pjLister := informerFactory.Prow().V1().ProwJobs().Lister()
go informerFactory.Start(interrupts.Context().Done())
registry := mustRegister("exporter", pjLister)
registry.MustRegister(prowjobs.NewProwJobLifecycleHistogramVec(informerFactory.Prow().V1().ProwJobs().Informer()))
// Expose prometheus metrics
metrics.ExposeMetricsWithRegistry("exporter", cfg().PushGateway, o.instrumentationOptions.MetricsPort, registry, nil)
logrus.Info("exporter is running ...")
health.ServeReady()
}