From 0ce588f703c050b59abbed19ac872c435ce8371e Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Mon, 11 Nov 2024 10:38:20 +0200 Subject: [PATCH] metrics: add standard collectors. Add a metrics/collectors subpackage. When imported it pulls in and registers the fairly standard buildinfo, process and golang runtime collectors. Turn on the build info collector by default. Signed-off-by: Krisztian Litkey --- .../bases/config.nri_balloonspolicies.yaml | 2 + .../bases/config.nri_templatepolicies.yaml | 2 + .../config.nri_topologyawarepolicies.yaml | 2 + .../crds/config.nri_balloonspolicies.yaml | 2 + .../crds/config.nri_templatepolicies.yaml | 2 + .../config.nri_topologyawarepolicies.yaml | 2 + .../config/v1alpha1/instrumentation/config.go | 2 +- pkg/metrics/collectors/collectors.go | 50 +++++++++++++++++++ 8 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 pkg/metrics/collectors/collectors.go diff --git a/config/crd/bases/config.nri_balloonspolicies.yaml b/config/crd/bases/config.nri_balloonspolicies.yaml index 54de71ee4..d5a6ec6dd 100644 --- a/config/crd/bases/config.nri_balloonspolicies.yaml +++ b/config/crd/bases/config.nri_balloonspolicies.yaml @@ -322,6 +322,8 @@ spec: type: string metrics: default: + enabled: + - standard/buildinfo polled: - policy description: Metrics defines which metrics to collect. diff --git a/config/crd/bases/config.nri_templatepolicies.yaml b/config/crd/bases/config.nri_templatepolicies.yaml index cc6e20378..162b6ede8 100644 --- a/config/crd/bases/config.nri_templatepolicies.yaml +++ b/config/crd/bases/config.nri_templatepolicies.yaml @@ -94,6 +94,8 @@ spec: type: string metrics: default: + enabled: + - standard/buildinfo polled: - policy description: Metrics defines which metrics to collect. diff --git a/config/crd/bases/config.nri_topologyawarepolicies.yaml b/config/crd/bases/config.nri_topologyawarepolicies.yaml index 52e19c166..9cd66a22d 100644 --- a/config/crd/bases/config.nri_topologyawarepolicies.yaml +++ b/config/crd/bases/config.nri_topologyawarepolicies.yaml @@ -121,6 +121,8 @@ spec: type: string metrics: default: + enabled: + - standard/buildinfo polled: - policy description: Metrics defines which metrics to collect. diff --git a/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml b/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml index 54de71ee4..d5a6ec6dd 100644 --- a/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml +++ b/deployment/helm/balloons/crds/config.nri_balloonspolicies.yaml @@ -322,6 +322,8 @@ spec: type: string metrics: default: + enabled: + - standard/buildinfo polled: - policy description: Metrics defines which metrics to collect. diff --git a/deployment/helm/template/crds/config.nri_templatepolicies.yaml b/deployment/helm/template/crds/config.nri_templatepolicies.yaml index cc6e20378..162b6ede8 100644 --- a/deployment/helm/template/crds/config.nri_templatepolicies.yaml +++ b/deployment/helm/template/crds/config.nri_templatepolicies.yaml @@ -94,6 +94,8 @@ spec: type: string metrics: default: + enabled: + - standard/buildinfo polled: - policy description: Metrics defines which metrics to collect. diff --git a/deployment/helm/topology-aware/crds/config.nri_topologyawarepolicies.yaml b/deployment/helm/topology-aware/crds/config.nri_topologyawarepolicies.yaml index 52e19c166..9cd66a22d 100644 --- a/deployment/helm/topology-aware/crds/config.nri_topologyawarepolicies.yaml +++ b/deployment/helm/topology-aware/crds/config.nri_topologyawarepolicies.yaml @@ -121,6 +121,8 @@ spec: type: string metrics: default: + enabled: + - standard/buildinfo polled: - policy description: Metrics defines which metrics to collect. diff --git a/pkg/apis/config/v1alpha1/instrumentation/config.go b/pkg/apis/config/v1alpha1/instrumentation/config.go index c9f04971f..67a48210d 100644 --- a/pkg/apis/config/v1alpha1/instrumentation/config.go +++ b/pkg/apis/config/v1alpha1/instrumentation/config.go @@ -49,6 +49,6 @@ type Config struct { // +optional PrometheusExport bool `json:"prometheusExport,omitempty"` // Metrics defines which metrics to collect. - // +kubebuilder:default={"polled": {"policy"}} + // +kubebuilder:default={"enabled": {"standard/buildinfo"}, "polled": {"policy"}} Metrics *metrics.Config `json:"metrics,omitempty"` } diff --git a/pkg/metrics/collectors/collectors.go b/pkg/metrics/collectors/collectors.go new file mode 100644 index 000000000..68518757d --- /dev/null +++ b/pkg/metrics/collectors/collectors.go @@ -0,0 +1,50 @@ +// Copyright The NRI Plugins Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package collectors + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" + + logger "github.com/containers/nri-plugins/pkg/log" + "github.com/containers/nri-plugins/pkg/metrics" +) + +var ( + log = logger.Get("metrics") +) + +func init() { + var ( + collectors = map[string]prometheus.Collector{ + "buildinfo": collectors.NewBuildInfoCollector(), + "golang": collectors.NewGoCollector(), + "process": collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), + } + options = []metrics.RegisterOption{ + metrics.WithGroup("standard"), + metrics.WithCollectorOptions( + metrics.WithoutNamespace(), + metrics.WithoutSubsystem(), + ), + } + ) + + for name, collector := range collectors { + if err := metrics.Register(name, collector, options...); err != nil { + log.Error("failed to register %s collector: %v", name, err) + } + } +}