refactor(metrics): use const label for hpa, ensure provider label is set

This commit is contained in:
Trong Huu Nguyen
2022-07-20 14:50:13 +02:00
parent 13f1713fc2
commit b4e6e97448
4 changed files with 80 additions and 51 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ func run() error {
r := router.New(h)
go func() {
err := metrics.Handle(cfg.MetricsBindAddress)
err := metrics.Handle(cfg.MetricsBindAddress, openidConfig)
if err != nil {
log.Fatalf("fatal: metrics server error: %s", err)
}
+3 -10
View File
@@ -34,18 +34,11 @@ func NewHandler(
crypter crypto.Crypter,
sessionStore session.Store,
) (*Handler, error) {
loginstatusClient := loginstatus.NewClient(cfg.Loginstatus, http.DefaultClient)
cookiePath := config.ParseIngress(cfg.Ingress)
cookieOpts := cookie.DefaultOptions().WithPath(cookiePath)
openidProvider, err := provider.NewProvider(ctx, openidConfig)
if err != nil {
return nil, err
}
openidClient := client.NewClient(openidConfig)
autoLogin, err := autologin.NewOptions(cfg)
if err != nil {
return nil, err
@@ -53,11 +46,11 @@ func NewHandler(
return &Handler{
AutoLogin: autoLogin,
Client: openidClient,
Client: client.NewClient(openidConfig),
Config: cfg,
CookieOptions: cookieOpts,
CookieOptions: cookie.DefaultOptions().WithPath(config.ParseIngress(cfg.Ingress)),
Crypter: crypter,
Loginstatus: loginstatusClient,
Loginstatus: loginstatus.NewClient(cfg.Loginstatus, http.DefaultClient),
OpenIDConfig: openidConfig,
Provider: openidProvider,
Sessions: sessionStore,
+72 -38
View File
@@ -6,13 +6,16 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
openidconfig "github.com/nais/wonderwall/pkg/openid/config"
)
const (
Namespace = "wonderwall"
LabelOperation = "operation"
LabelHpa = "hpa"
LabelOperation = "operation"
LabelProvider = "provider"
)
type Hpa = string
@@ -37,56 +40,90 @@ const (
)
var (
RedisLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
RedisLatency = redisLatency()
Logins = logins()
Logouts = logouts()
)
func redisLatency(constLabels ...prometheus.Labels) *prometheus.HistogramVec {
opts := prometheus.HistogramOpts{
Name: "redis_latency",
Namespace: Namespace,
Help: "latency in redis operations",
Buckets: prometheus.ExponentialBuckets(0.02, 2, 14),
}, []string{LabelOperation})
Help: "latency in redis operations, in seconds",
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16),
}
Logins = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "logins",
Namespace: Namespace,
Help: "cumulative number of successful logins",
},
[]string{
LabelHpa,
},
)
if len(constLabels) > 0 {
opts.ConstLabels = constLabels[0]
}
Logouts = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "logouts",
Namespace: Namespace,
Help: "cumulative number of successful logouts",
return prometheus.NewHistogramVec(opts, []string{LabelOperation})
}
func logins(constLabels ...prometheus.Labels) prometheus.Counter {
opts := prometheus.CounterOpts{
Name: "logins",
Namespace: Namespace,
Help: "cumulative number of successful logins",
ConstLabels: prometheus.Labels{
LabelHpa: HpaRate,
},
[]string{
LabelOperation,
LabelHpa,
}
if len(constLabels) > 0 {
opts.ConstLabels = constLabels[0]
}
return prometheus.NewCounter(opts)
}
func logouts(constLabels ...prometheus.Labels) *prometheus.CounterVec {
opts := prometheus.CounterOpts{
Name: "logouts",
Namespace: Namespace,
Help: "cumulative number of successful logouts",
ConstLabels: prometheus.Labels{
LabelHpa: HpaRate,
},
)
)
}
if len(constLabels) > 0 {
opts.ConstLabels = constLabels[0]
}
return prometheus.NewCounterVec(opts, []string{LabelOperation})
}
func WithProvider(provider string) {
RedisLatency = redisLatency(prometheus.Labels{
LabelProvider: provider,
})
Logins = logins(prometheus.Labels{
LabelHpa: HpaRate,
LabelProvider: provider,
})
Logouts = logouts(prometheus.Labels{
LabelHpa: HpaRate,
LabelProvider: provider,
})
}
// InitLabels zeroes out all possible label combinations
func InitLabels() {
logoutOperations := []LogoutOperation{LogoutOperationSelfInitiated, LogoutOperationFrontChannel}
for _, operation := range logoutOperations {
Logouts.With(prometheus.Labels{
LabelOperation: operation,
LabelHpa: HpaRate,
})
Logouts.With(prometheus.Labels{LabelOperation: operation})
}
Logins.With(prometheus.Labels{
LabelHpa: HpaRate,
})
}
func Handle(address string) error {
func Handle(address string, openidConfig openidconfig.Config) error {
WithProvider(openidConfig.Provider().Name())
Register(prometheus.DefaultRegisterer)
InitLabels()
handler := promhttp.Handler()
return http.ListenAndServe(address, handler)
}
@@ -110,14 +147,11 @@ func ObserveRedisLatency(operation string, fun func() error) error {
}
func ObserveLogin() {
Logins.With(prometheus.Labels{
LabelHpa: HpaRate,
}).Inc()
Logins.Inc()
}
func ObserveLogout(operation LogoutOperation) {
Logouts.With(prometheus.Labels{
LabelOperation: operation,
LabelHpa: HpaRate,
}).Inc()
}
+4 -2
View File
@@ -10,6 +10,8 @@ import (
chi_middleware "github.com/go-chi/chi/v5/middleware"
"github.com/prometheus/client_golang/prometheus"
"github.com/nais/wonderwall/pkg/metrics"
)
var (
@@ -35,7 +37,7 @@ func NewPrometheusMiddleware(name, provider string, buckets ...float64) *Prometh
prometheus.CounterOpts{
Name: reqsName,
Help: "How many HTTP requests processed, partitioned by status code, method and HTTP path.",
ConstLabels: prometheus.Labels{"service": name, "provider": provider},
ConstLabels: prometheus.Labels{"service": name, metrics.LabelProvider: provider},
},
[]string{"code", "method", "path", "host"},
)
@@ -46,7 +48,7 @@ func NewPrometheusMiddleware(name, provider string, buckets ...float64) *Prometh
m.latency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: latencyName,
Help: "How long it took to process the request, partitioned by status code, method and HTTP path.",
ConstLabels: prometheus.Labels{"service": name, "provider": provider},
ConstLabels: prometheus.Labels{"service": name, metrics.LabelProvider: provider},
Buckets: buckets,
},
[]string{"code", "method", "path", "host"},