1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/metrics/init.go
Charles-Edouard Brétéché e03f48128a
refactor: metrics configuration code (#5475)
* refactor: metrics configuration

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

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-25 13:14:55 +00:00

62 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,
metricsConfiguration config.MetricsConfiguration,
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 := MetricsConfig{
Log: log,
Config: metricsConfiguration,
}
err = metricsConfig.initializeMetrics()
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
}