|
1 |
| -/* |
2 |
| -SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and cap-operator contributors |
3 |
| -SPDX-License-Identifier: Apache-2.0 |
4 |
| -*/ |
5 |
| - |
6 |
| -package util |
7 |
| - |
8 |
| -import ( |
9 |
| - "net/http" |
10 |
| - "os" |
11 |
| - |
12 |
| - "github.com/prometheus/client_golang/prometheus" |
13 |
| - "github.com/prometheus/client_golang/prometheus/promauto" |
14 |
| - "github.com/prometheus/client_golang/prometheus/promhttp" |
15 |
| - "k8s.io/klog/v2" |
16 |
| -) |
17 |
| - |
18 |
| -// Initalizes the metrics server with default port 9090 and path /metrics based on default prometheus client |
19 |
| -func InitMetricsServer() { |
20 |
| - // Expose /metrics HTTP endpoint |
21 |
| - go func() { |
22 |
| - // Default port |
23 |
| - metricsPort := "9090" |
24 |
| - |
25 |
| - // Get Port from env |
26 |
| - portEnv := os.Getenv("METRICS_PORT") |
27 |
| - if portEnv != "" { |
28 |
| - metricsPort = portEnv |
29 |
| - } |
30 |
| - http.Handle("/metrics", promhttp.Handler()) |
31 |
| - klog.Fatal(http.ListenAndServe(":"+metricsPort, nil)) |
32 |
| - }() |
33 |
| -} |
34 |
| - |
35 |
| -// Instruments the given HTTP handler with counter (total requests) and gauge (in flight requests) metrics |
36 |
| -func InstrumentHttpHandler(handler func(http.ResponseWriter, *http.Request), metricNamePrefix string, helpTextSuffix string) http.HandlerFunc { |
37 |
| - klog.InfoS("Instrumenting HTTP handler", "metricPrefix", metricNamePrefix, "helpSuffix", helpTextSuffix) |
38 |
| - return promhttp.InstrumentHandlerCounter( |
39 |
| - promauto.NewCounterVec( |
40 |
| - prometheus.CounterOpts{ |
41 |
| - Name: metricNamePrefix + "_total", |
42 |
| - Help: "Total " + helpTextSuffix, |
43 |
| - }, |
44 |
| - []string{"code", "method"}, |
45 |
| - ), |
46 |
| - promhttp.InstrumentHandlerInFlight(promauto.NewGauge( |
47 |
| - prometheus.GaugeOpts{ |
48 |
| - Name: metricNamePrefix + "_in_flight", |
49 |
| - Help: "Current " + helpTextSuffix, |
50 |
| - }, |
51 |
| - ), |
52 |
| - http.HandlerFunc(handler), |
53 |
| - ), |
54 |
| - ) |
55 |
| -} |
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and cap-operator contributors |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +package util |
| 7 | + |
| 8 | +import ( |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + |
| 12 | + "github.com/prometheus/client_golang/prometheus" |
| 13 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 14 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 15 | + "k8s.io/klog/v2" |
| 16 | +) |
| 17 | + |
| 18 | +// Initializes the metrics server with default port 9090 and path /metrics based on default prometheus client |
| 19 | +func InitMetricsServer() { |
| 20 | + // Expose /metrics HTTP endpoint |
| 21 | + go func() { |
| 22 | + // Default port |
| 23 | + metricsPort := "9090" |
| 24 | + |
| 25 | + // Get Port from env |
| 26 | + portEnv := os.Getenv("METRICS_PORT") |
| 27 | + if portEnv != "" { |
| 28 | + metricsPort = portEnv |
| 29 | + } |
| 30 | + http.Handle("/metrics", promhttp.Handler()) |
| 31 | + klog.Fatal(http.ListenAndServe(":"+metricsPort, nil)) |
| 32 | + }() |
| 33 | +} |
| 34 | + |
| 35 | +// Instruments the given HTTP handler with counter (total requests) and gauge (in flight requests) metrics |
| 36 | +func InstrumentHttpHandler(handler func(http.ResponseWriter, *http.Request), metricNamePrefix string, helpTextSuffix string) http.HandlerFunc { |
| 37 | + klog.InfoS("Instrumenting HTTP handler", "metricPrefix", metricNamePrefix, "helpSuffix", helpTextSuffix) |
| 38 | + return promhttp.InstrumentHandlerCounter( |
| 39 | + promauto.NewCounterVec( |
| 40 | + prometheus.CounterOpts{ |
| 41 | + Name: metricNamePrefix + "_total", |
| 42 | + Help: "Total " + helpTextSuffix, |
| 43 | + }, |
| 44 | + []string{"code", "method"}, |
| 45 | + ), |
| 46 | + promhttp.InstrumentHandlerInFlight(promauto.NewGauge( |
| 47 | + prometheus.GaugeOpts{ |
| 48 | + Name: metricNamePrefix + "_in_flight", |
| 49 | + Help: "Current " + helpTextSuffix, |
| 50 | + }, |
| 51 | + ), |
| 52 | + http.HandlerFunc(handler), |
| 53 | + ), |
| 54 | + ) |
| 55 | +} |
0 commit comments