Skip to content

Commit c1518e7

Browse files
committed
refactor: move metrics funcs to own file
1 parent fc7abaa commit c1518e7

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package metrics
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
"github.com/prometheus/client_golang/prometheus/collectors"
8+
"github.com/prometheus/client_golang/prometheus/promhttp"
9+
)
10+
11+
// Global Prometheus Registry
12+
var globalPromRegistry = prometheus.NewRegistry()
13+
14+
// metricsHandler returns a http.Handler that serves the prometheus metrics
15+
func PrometheusHandler() http.Handler {
16+
// Add Go module build info.
17+
globalPromRegistry.MustRegister(collectors.NewBuildInfoCollector())
18+
globalPromRegistry.MustRegister(collectors.NewGoCollector())
19+
globalPromRegistry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
20+
21+
handlerOpts := promhttp.HandlerOpts{
22+
ErrorHandling: promhttp.HTTPErrorOnError,
23+
}
24+
25+
return promhttp.HandlerFor(globalPromRegistry, handlerOpts)
26+
}

cmd/kar-controllers/app/server.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ import (
2626
"k8s.io/client-go/rest"
2727
"k8s.io/client-go/tools/clientcmd"
2828

29+
"github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/metrics"
2930
"github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/options"
3031
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/queuejob"
3132
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/health"
32-
"github.com/prometheus/client_golang/prometheus"
33-
"github.com/prometheus/client_golang/prometheus/collectors"
34-
"github.com/prometheus/client_golang/prometheus/promhttp"
3533

3634
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3735

@@ -40,8 +38,6 @@ import (
4038
"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/config"
4139
)
4240

43-
// Global Prometheus Registry
44-
var globalPromRegistry = prometheus.NewRegistry()
4541

4642
func buildConfig(master, kubeconfig string) (*rest.Config, error) {
4743
if master != "" || kubeconfig != "" {
@@ -87,20 +83,6 @@ func Run(ctx context.Context, opt *options.ServerOption) error {
8783
return nil
8884
}
8985

90-
// metricsHandler returns a http.Handler that serves the prometheus metrics
91-
func prometheusHandler() http.Handler {
92-
// Add Go module build info.
93-
globalPromRegistry.MustRegister(collectors.NewBuildInfoCollector())
94-
globalPromRegistry.MustRegister(collectors.NewGoCollector())
95-
globalPromRegistry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
96-
97-
handlerOpts := promhttp.HandlerOpts{
98-
ErrorHandling: promhttp.HTTPErrorOnError,
99-
}
100-
101-
return promhttp.HandlerFor(globalPromRegistry, handlerOpts)
102-
}
103-
10486
func healthHandler() http.Handler {
10587
healthHandler := http.NewServeMux()
10688
healthHandler.Handle("/healthz", &health.Handler{})
@@ -112,7 +94,7 @@ func startHealthAndMetricsServers(ctx context.Context, opt *options.ServerOption
11294
g, ctx := errgroup.WithContext(ctx)
11395

11496
// metrics server
115-
metricsServer, err := NewServer(opt.MetricsListenPort, "/metrics", prometheusHandler())
97+
metricsServer, err := NewServer(opt.MetricsListenPort, "/metrics", metrics.PrometheusHandler())
11698
if err != nil {
11799
return err
118100
}

0 commit comments

Comments
 (0)