github.com/verrazzano/verrazzano@v1.7.1/tools/psr/backend/metrics/server.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package metrics 5 6 import ( 7 "github.com/prometheus/client_golang/prometheus" 8 "github.com/prometheus/client_golang/prometheus/collectors" 9 "github.com/prometheus/client_golang/prometheus/promhttp" 10 "github.com/verrazzano/verrazzano/tools/psr/backend/spi" 11 "go.uber.org/zap" 12 "net/http" 13 "os" 14 "time" 15 ) 16 17 // StartMetricsServerOrDie starts the metrics server. If there is an error the code exits 18 func StartMetricsServerOrDie(providers []spi.WorkerMetricsProvider) { 19 reg := prometheus.NewPedanticRegistry() 20 rc := workerCollector{providers: providers} 21 reg.MustRegister( 22 rc, 23 ) 24 // Add the standard process and Go metrics to the custom registry. 25 reg.MustRegister( 26 collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), 27 collectors.NewGoCollector(), 28 ) 29 30 // Instrument the default metrics 31 h1 := promhttp.HandlerFor(reg, promhttp.HandlerOpts{}) 32 h2 := promhttp.InstrumentMetricHandler(reg, h1) 33 http.Handle("/metrics", h2) 34 35 server := http.Server{ 36 ReadTimeout: 10 * time.Second, 37 WriteTimeout: 10 * time.Second, 38 Addr: "0.0.0.0:9090"} 39 40 if err := server.ListenAndServe(); err != nil { 41 zap.S().Errorf("Failed to start metrics server: %v", err) 42 os.Exit(1) 43 } 44 }