2022-07-11 17:49:47 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2022-07-25 09:25:26 +00:00
|
|
|
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
|
2022-07-11 17:49:47 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitMetrics(
|
|
|
|
disableMetricsExport bool,
|
|
|
|
otel string,
|
|
|
|
metricsAddr string,
|
|
|
|
otelCollector string,
|
|
|
|
metricsConfigData *config.MetricsConfigData,
|
|
|
|
transportCreds string,
|
|
|
|
kubeClient kubernetes.Interface,
|
2022-08-24 13:08:24 +00:00
|
|
|
log logr.Logger,
|
|
|
|
) (*MetricsConfig, *http.ServeMux, *controller.Controller, error) {
|
2022-07-11 17:49:47 +00:00
|
|
|
var err error
|
|
|
|
var metricsServerMux *http.ServeMux
|
2022-07-25 09:25:26 +00:00
|
|
|
var pusher *controller.Controller
|
2022-11-17 05:29:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:49:47 +00:00
|
|
|
if !disableMetricsExport {
|
|
|
|
if otel == "grpc" {
|
|
|
|
// Otlpgrpc metrics will be served on port 4317: default port for otlpgrpcmetrics
|
2022-08-18 13:24:59 +00:00
|
|
|
log.V(2).Info("Enabling Metrics for Kyverno", "address", metricsAddr)
|
2022-07-11 17:49:47 +00:00
|
|
|
|
|
|
|
endpoint := otelCollector + metricsAddr
|
2022-11-17 05:29:32 +00:00
|
|
|
pusher, err = NewOTLPGRPCConfig(
|
2022-07-11 17:49:47 +00:00
|
|
|
endpoint,
|
|
|
|
transportCreds,
|
|
|
|
kubeClient,
|
|
|
|
log,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-11-17 05:29:32 +00:00
|
|
|
return nil, nil, nil, err
|
2022-07-11 17:49:47 +00:00
|
|
|
}
|
|
|
|
} else if otel == "prometheus" {
|
|
|
|
// Prometheus Server will serve metrics on metrics-port
|
2022-11-17 05:29:32 +00:00
|
|
|
metricsServerMux, err = NewPrometheusConfig(log)
|
2022-07-11 17:49:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-07-25 09:25:26 +00:00
|
|
|
return nil, nil, pusher, err
|
2022-07-11 17:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 09:25:26 +00:00
|
|
|
return metricsConfig, metricsServerMux, pusher, nil
|
2022-07-11 17:49:47 +00:00
|
|
|
}
|