@@ -17,128 +17,48 @@ limitations under the License.
1717package metrics
1818
1919import (
20- "context"
21- "fmt"
22- "time"
23-
2420 "github.com/prometheus/client_golang/prometheus"
25- "sigs.k8s.io/controller-runtime/pkg/client"
26- "sigs.k8s.io/controller-runtime/pkg/manager"
27- "sigs.k8s.io/controller-runtime/pkg/metrics"
2821
22+ "github.com/timebertt/kubernetes-controller-sharding/pkg/metrics/exporter"
2923 webhostingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/webhosting-operator/pkg/apis/webhosting/v1alpha1"
3024)
3125
32- const themeSubsystem = "theme"
33-
34- type ThemeExporter struct {
35- client.Reader
36- }
37-
38- func (e * ThemeExporter ) AddToManager (mgr manager.Manager ) error {
39- if e .Reader == nil {
40- e .Reader = mgr .GetCache ()
41- }
42-
43- return mgr .Add (e )
44- }
45-
46- // NeedLeaderElection tells the manager to run the exporter in all instances.
47- func (e * ThemeExporter ) NeedLeaderElection () bool {
48- return false
49- }
50-
51- // Start registers this collector in the controller-runtime metrics registry.
52- // When Start runs, caches have already been started, so we are ready to export metrics.
53- func (e * ThemeExporter ) Start (_ context.Context ) error {
54- if err := metrics .Registry .Register (e ); err != nil {
55- return fmt .Errorf ("failed to register theme exporter: %w" , err )
56- }
57-
58- return nil
59- }
60-
61- func (e * ThemeExporter ) Describe (ch chan <- * prometheus.Desc ) {
62- for _ , desc := range themeMetrics {
63- ch <- desc .desc
64- }
65- }
66-
67- func (e * ThemeExporter ) Collect (ch chan <- prometheus.Metric ) {
68- ctx , cancel := context .WithTimeout (context .Background (), 100 * time .Millisecond )
69- defer cancel ()
70-
71- themeList := & webhostingv1alpha1.ThemeList {}
72- if err := e .List (ctx , themeList ); err != nil {
73- for _ , desc := range themeMetrics {
74- ch <- prometheus .NewInvalidMetric (desc .desc , fmt .Errorf ("error listing themes: %w" , err ))
75- }
76-
77- return
78- }
79-
80- for _ , theme := range themeList .Items {
81- staticLabels := generateThemeStaticLabels (& theme )
82-
83- for _ , desc := range themeMetrics {
84- desc .generate (desc .desc , & theme , staticLabels , ch )
85- }
86- }
87- }
88-
89- var (
90- themeMetrics = []metric [* webhostingv1alpha1.Theme ]{
91- themeInfo ,
92- themeGeneration ,
93- }
94-
95- themeStaticLabels = []string {
96- "theme" ,
97- "uid" ,
98- }
99- )
100-
101- func generateThemeStaticLabels (theme * webhostingv1alpha1.Theme ) []string {
102- return []string {
103- theme .Name ,
104- string (theme .UID ),
105- }
106- }
107-
108- var (
109- themeInfo = metric [* webhostingv1alpha1.Theme ]{
110- desc : prometheus .NewDesc (
111- prometheus .BuildFQName (namespace , themeSubsystem , "info" ),
112- "Information about a Theme" ,
113- append (themeStaticLabels , "color" , "font_family" ),
114- nil ,
115- ),
116-
117- generate : func (desc * prometheus.Desc , theme * webhostingv1alpha1.Theme , staticLabels []string , ch chan <- prometheus.Metric ) {
118- ch <- prometheus .MustNewConstMetric (
119- desc ,
120- prometheus .GaugeValue ,
121- 1 ,
122- append (staticLabels , theme .Spec .Color , theme .Spec .FontFamily )... ,
123- )
26+ var ThemeExporter = exporter.Exporter [* webhostingv1alpha1.Theme , * webhostingv1alpha1.ThemeList ]{
27+ Namespace : namespace ,
28+ Subsystem : "theme" ,
29+
30+ StaticLabelKeys : []string {"theme" , "uid" },
31+ GenerateStaticLabelValues : func (theme * webhostingv1alpha1.Theme ) []string {
32+ return []string {theme .Name , string (theme .UID )}
33+ },
34+
35+ Metrics : []exporter.Metric [* webhostingv1alpha1.Theme ]{
36+ {
37+ Name : "info" ,
38+ Help : "Information about a Theme" ,
39+ LabelKeys : []string {"color" , "font_family" },
40+
41+ Generate : func (desc * prometheus.Desc , theme * webhostingv1alpha1.Theme , staticLabelValues []string , ch chan <- prometheus.Metric ) {
42+ ch <- prometheus .MustNewConstMetric (
43+ desc ,
44+ prometheus .GaugeValue ,
45+ 1 ,
46+ append (staticLabelValues , theme .Spec .Color , theme .Spec .FontFamily )... ,
47+ )
48+ },
12449 },
125- }
126-
127- themeGeneration = metric [* webhostingv1alpha1.Theme ]{
128- desc : prometheus .NewDesc (
129- prometheus .BuildFQName (namespace , themeSubsystem , "metadata_generation" ),
130- "The generation of a Theme" ,
131- themeStaticLabels ,
132- nil ,
133- ),
134-
135- generate : func (desc * prometheus.Desc , theme * webhostingv1alpha1.Theme , staticLabels []string , ch chan <- prometheus.Metric ) {
136- ch <- prometheus .MustNewConstMetric (
137- desc ,
138- prometheus .GaugeValue ,
139- float64 (theme .Generation ),
140- staticLabels ... ,
141- )
50+ {
51+ Name : "metadata_generation" ,
52+ Help : "The generation of a Theme" ,
53+
54+ Generate : func (desc * prometheus.Desc , theme * webhostingv1alpha1.Theme , staticLabelValues []string , ch chan <- prometheus.Metric ) {
55+ ch <- prometheus .MustNewConstMetric (
56+ desc ,
57+ prometheus .GaugeValue ,
58+ float64 (theme .Generation ),
59+ staticLabelValues ... ,
60+ )
61+ },
14262 },
143- }
144- )
63+ },
64+ }
0 commit comments