1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/metrics/init.go
Charles-Edouard Brétéché 0c189b9bcb
fix: panic when disable metrics is true (#5366)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2022-11-17 13:29:32 +08:00

61 lines
1.5 KiB
Go

package metrics
import (
"net/http"
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/pkg/config"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
"k8s.io/client-go/kubernetes"
)
func InitMetrics(
disableMetricsExport bool,
otel string,
metricsAddr string,
otelCollector string,
metricsConfigData *config.MetricsConfigData,
transportCreds string,
kubeClient kubernetes.Interface,
log logr.Logger,
) (*MetricsConfig, *http.ServeMux, *controller.Controller, error) {
var err error
var metricsServerMux *http.ServeMux
var pusher *controller.Controller
metricsConfig := new(MetricsConfig)
metricsConfig.Log = log
metricsConfig.Config = metricsConfigData
metricsConfig, err = initializeMetrics(metricsConfig)
if err != nil {
log.Error(err, "Failed initializing metrics")
return nil, nil, nil, err
}
if !disableMetricsExport {
if otel == "grpc" {
// Otlpgrpc metrics will be served on port 4317: default port for otlpgrpcmetrics
log.V(2).Info("Enabling Metrics for Kyverno", "address", metricsAddr)
endpoint := otelCollector + metricsAddr
pusher, err = NewOTLPGRPCConfig(
endpoint,
transportCreds,
kubeClient,
log,
)
if err != nil {
return nil, nil, nil, err
}
} else if otel == "prometheus" {
// Prometheus Server will serve metrics on metrics-port
metricsServerMux, err = NewPrometheusConfig(log)
if err != nil {
return nil, nil, pusher, err
}
}
}
return metricsConfig, metricsServerMux, pusher, nil
}