Skip to content

Commit

Permalink
[Misc] Operator: minor fixes
Browse files Browse the repository at this point in the history
Correct minor typos
  • Loading branch information
Pavan-SAP committed Nov 14, 2024
1 parent 93aa3cd commit 3724fd9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion internal/controller/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var (
}, []string{"name"})
)

// Create a varaible to hold all the collectors
// Create a variable to hold all the collectors
var collectors = []prometheus.Collector{ReconcileErrors, Panics, TenantOperations, depth, adds, latency, workDuration, unfinished, longestRunningProcessor, retries}

// #region capOperatorMetricsProvider
Expand Down
110 changes: 55 additions & 55 deletions internal/util/metrics-server.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
/*
SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and cap-operator contributors
SPDX-License-Identifier: Apache-2.0
*/

package util

import (
"net/http"
"os"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"
)

// Initalizes the metrics server with default port 9090 and path /metrics based on default prometheus client
func InitMetricsServer() {
// Expose /metrics HTTP endpoint
go func() {
// Default port
metricsPort := "9090"

// Get Port from env
portEnv := os.Getenv("METRICS_PORT")
if portEnv != "" {
metricsPort = portEnv
}
http.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(":"+metricsPort, nil))
}()
}

// Instruments the given HTTP handler with counter (total requests) and gauge (in flight requests) metrics
func InstrumentHttpHandler(handler func(http.ResponseWriter, *http.Request), metricNamePrefix string, helpTextSuffix string) http.HandlerFunc {
klog.InfoS("Instrumenting HTTP handler", "metricPrefix", metricNamePrefix, "helpSuffix", helpTextSuffix)
return promhttp.InstrumentHandlerCounter(
promauto.NewCounterVec(
prometheus.CounterOpts{
Name: metricNamePrefix + "_total",
Help: "Total " + helpTextSuffix,
},
[]string{"code", "method"},
),
promhttp.InstrumentHandlerInFlight(promauto.NewGauge(
prometheus.GaugeOpts{
Name: metricNamePrefix + "_in_flight",
Help: "Current " + helpTextSuffix,
},
),
http.HandlerFunc(handler),
),
)
}
/*
SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and cap-operator contributors
SPDX-License-Identifier: Apache-2.0
*/

package util

import (
"net/http"
"os"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"
)

// Initializes the metrics server with default port 9090 and path /metrics based on default prometheus client
func InitMetricsServer() {
// Expose /metrics HTTP endpoint
go func() {
// Default port
metricsPort := "9090"

// Get Port from env
portEnv := os.Getenv("METRICS_PORT")
if portEnv != "" {
metricsPort = portEnv
}
http.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(":"+metricsPort, nil))
}()
}

// Instruments the given HTTP handler with counter (total requests) and gauge (in flight requests) metrics
func InstrumentHttpHandler(handler func(http.ResponseWriter, *http.Request), metricNamePrefix string, helpTextSuffix string) http.HandlerFunc {
klog.InfoS("Instrumenting HTTP handler", "metricPrefix", metricNamePrefix, "helpSuffix", helpTextSuffix)
return promhttp.InstrumentHandlerCounter(
promauto.NewCounterVec(
prometheus.CounterOpts{
Name: metricNamePrefix + "_total",
Help: "Total " + helpTextSuffix,
},
[]string{"code", "method"},
),
promhttp.InstrumentHandlerInFlight(promauto.NewGauge(
prometheus.GaugeOpts{
Name: metricNamePrefix + "_in_flight",
Help: "Current " + helpTextSuffix,
},
),
http.HandlerFunc(handler),
),
)
}

0 comments on commit 3724fd9

Please sign in to comment.