Skip to content

Commit

Permalink
metrics: add standard collectors.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
klihub committed Nov 11, 2024
1 parent ff456df commit df856a6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/crd/bases/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ spec:
type: string
metrics:
default:
enabled:
- standard/buildinfo
polled:
- policy
description: Metrics defines which metrics to collect.
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/config.nri_templatepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ spec:
type: string
metrics:
default:
enabled:
- standard/buildinfo
polled:
- policy
description: Metrics defines which metrics to collect.
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/config.nri_topologyawarepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ spec:
type: string
metrics:
default:
enabled:
- standard/buildinfo
polled:
- policy
description: Metrics defines which metrics to collect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ spec:
type: string
metrics:
default:
enabled:
- standard/buildinfo
polled:
- policy
description: Metrics defines which metrics to collect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ spec:
type: string
metrics:
default:
enabled:
- standard/buildinfo
polled:
- policy
description: Metrics defines which metrics to collect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ spec:
type: string
metrics:
default:
enabled:
- standard/buildinfo
polled:
- policy
description: Metrics defines which metrics to collect.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/config/v1alpha1/instrumentation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
50 changes: 50 additions & 0 deletions pkg/metrics/collectors/collectors.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit df856a6

Please sign in to comment.