github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/exporter/main.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  
    23  	"github.com/prometheus/client_golang/prometheus"
    24  	"github.com/prometheus/client_golang/prometheus/collectors"
    25  	"github.com/sirupsen/logrus"
    26  	"sigs.k8s.io/prow/pkg/pjutil/pprof"
    27  
    28  	prowjobinformer "sigs.k8s.io/prow/pkg/client/informers/externalversions"
    29  	prowflagutil "sigs.k8s.io/prow/pkg/flagutil"
    30  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    31  	"sigs.k8s.io/prow/pkg/interrupts"
    32  	"sigs.k8s.io/prow/pkg/logrusutil"
    33  	"sigs.k8s.io/prow/pkg/metrics"
    34  	"sigs.k8s.io/prow/pkg/metrics/prowjobs"
    35  	"sigs.k8s.io/prow/pkg/pjutil"
    36  )
    37  
    38  type options struct {
    39  	config                 configflagutil.ConfigOptions
    40  	kubernetes             prowflagutil.KubernetesOptions
    41  	instrumentationOptions prowflagutil.InstrumentationOptions
    42  }
    43  
    44  func gatherOptions(fs *flag.FlagSet, args ...string) options {
    45  	var o options
    46  
    47  	o.config.AddFlags(fs)
    48  	o.kubernetes.AddFlags(fs)
    49  	o.instrumentationOptions.AddFlags(fs)
    50  	if err := fs.Parse(os.Args[1:]); err != nil {
    51  		logrus.WithError(err).Fatalf("cannot parse args: '%s'", os.Args[1:])
    52  	}
    53  	return o
    54  }
    55  
    56  func (o *options) Validate() error {
    57  	for _, fs := range []interface{ Validate(bool) error }{&o.config, &o.kubernetes, &o.instrumentationOptions} {
    58  		if err := fs.Validate(false); err != nil {
    59  			return err
    60  		}
    61  	}
    62  	return nil
    63  }
    64  
    65  func mustRegister(component string, lister lister) *prometheus.Registry {
    66  	registry := prometheus.NewRegistry()
    67  	prometheus.WrapRegistererWith(prometheus.Labels{"collector_name": component}, registry).MustRegister(&prowJobCollector{
    68  		lister: lister,
    69  	})
    70  	registry.MustRegister(
    71  		collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
    72  		collectors.NewGoCollector(),
    73  	)
    74  	return registry
    75  }
    76  
    77  func main() {
    78  	logrusutil.ComponentInit()
    79  	o := gatherOptions(flag.NewFlagSet(os.Args[0], flag.ExitOnError), os.Args[1:]...)
    80  	if err := o.Validate(); err != nil {
    81  		logrus.WithError(err).Fatal("Invalid options")
    82  	}
    83  
    84  	defer interrupts.WaitForGracefulShutdown()
    85  
    86  	pprof.Instrument(o.instrumentationOptions)
    87  	health := pjutil.NewHealthOnPort(o.instrumentationOptions.HealthPort)
    88  
    89  	configAgent, err := o.config.ConfigAgent()
    90  	if err != nil {
    91  		logrus.WithError(err).Fatal("Error starting config agent.")
    92  	}
    93  	cfg := configAgent.Config
    94  
    95  	pjClientset, err := o.kubernetes.ProwJobClientset(false)
    96  	if err != nil {
    97  		logrus.WithError(err).Fatal("Failed to create prowjob client set")
    98  	}
    99  	informerFactory := prowjobinformer.NewSharedInformerFactoryWithOptions(pjClientset, 0, prowjobinformer.WithNamespace(cfg().ProwJobNamespace))
   100  	pjLister := informerFactory.Prow().V1().ProwJobs().Lister()
   101  
   102  	go informerFactory.Start(interrupts.Context().Done())
   103  
   104  	registry := mustRegister("exporter", pjLister)
   105  	registry.MustRegister(prowjobs.NewProwJobLifecycleHistogramVec(informerFactory.Prow().V1().ProwJobs().Informer()))
   106  
   107  	// Expose prometheus metrics
   108  	metrics.ExposeMetricsWithRegistry("exporter", cfg().PushGateway, o.instrumentationOptions.MetricsPort, registry, nil)
   109  
   110  	logrus.Info("exporter is running ...")
   111  	health.ServeReady()
   112  }