Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stacked 2/5] metrics: add de-facto standard collectors. #404

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/crd/bases/config.nri_balloonspolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ spec:
default:
enabled:
- policy
- buildinfo
description: Metrics defines which metrics to collect.
properties:
enabled:
Expand Down
1 change: 1 addition & 0 deletions config/crd/bases/config.nri_templatepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ spec:
default:
enabled:
- policy
- buildinfo
description: Metrics defines which metrics to collect.
properties:
enabled:
Expand Down
1 change: 1 addition & 0 deletions config/crd/bases/config.nri_topologyawarepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ spec:
default:
enabled:
- policy
- buildinfo
description: Metrics defines which metrics to collect.
properties:
enabled:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ spec:
default:
enabled:
- policy
- buildinfo
description: Metrics defines which metrics to collect.
properties:
enabled:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ spec:
default:
enabled:
- policy
- buildinfo
description: Metrics defines which metrics to collect.
properties:
enabled:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ spec:
default:
enabled:
- policy
- buildinfo
description: Metrics defines which metrics to collect.
properties:
enabled:
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={"enabled": {"policy"}}
// +kubebuilder:default={"enabled": {"policy", "buildinfo"}}
Metrics *metrics.Config `json:"metrics,omitempty"`
}
66 changes: 66 additions & 0 deletions pkg/metrics/collectors/collectors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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"
"github.com/containers/nri-plugins/pkg/version"
)

var (
log = logger.Get("metrics")
)

func NewVersionInfoCollector(v, b string) prometheus.Collector {
return prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: "version_info",
Help: "A metric with constant '1' value labeled by version and build info.",
ConstLabels: prometheus.Labels{
"version": v,
"build": b,
},
},
func() float64 { return 1 },
)
}

func init() {
var (
collectors = map[string]prometheus.Collector{
"buildinfo": collectors.NewBuildInfoCollector(),
"golang": collectors.NewGoCollector(),
"process": collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
"versioninfo": NewVersionInfoCollector(version.Version, version.Build),
}
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)
}
}
}
1 change: 1 addition & 0 deletions pkg/resmgr/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/containers/nri-plugins/pkg/agent"
"github.com/containers/nri-plugins/pkg/instrumentation"
_ "github.com/containers/nri-plugins/pkg/metrics/collectors"
"github.com/containers/nri-plugins/pkg/resmgr"
"github.com/containers/nri-plugins/pkg/resmgr/policy"

Expand Down