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.

Also update cgroupstats collector for the reworked metrics
registry and split out automatic registration to a register
subpackage.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Nov 10, 2024
1 parent edc6064 commit 6ed3d64
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 14 deletions.
22 changes: 8 additions & 14 deletions pkg/cgroupstats/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/containers/nri-plugins/pkg/cgroups"
logger "github.com/containers/nri-plugins/pkg/log"
"github.com/containers/nri-plugins/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -42,7 +41,7 @@ const (

var descriptors = [numDescriptors]*prometheus.Desc{
numaStatsDesc: prometheus.NewDesc(
"cgroup_numa_stats",
"numa_stats",
"NUMA statistics for a given container and pod.",
[]string{
// cgroup path
Expand All @@ -54,22 +53,22 @@ var descriptors = [numDescriptors]*prometheus.Desc{
}, nil,
),
memoryUsageDesc: prometheus.NewDesc(
"cgroup_memory_usage",
"memory_usage",
"Memory usage statistics for a given container and pod.",
[]string{
"container_id",
"type",
}, nil,
),
memoryMigrateDesc: prometheus.NewDesc(
"cgroup_memory_migrate",
"memory_migrate",
"Memory migrate status for a given container and pod.",
[]string{
"container_id",
}, nil,
),
cpuAcctUsageDesc: prometheus.NewDesc(
"cgroup_cpu_acct",
"cpu_acct",
"CPU accounting for a given container and pod.",
[]string{
"container_id",
Expand All @@ -79,7 +78,7 @@ var descriptors = [numDescriptors]*prometheus.Desc{
}, nil,
),
hugeTlbUsageDesc: prometheus.NewDesc(
"cgroup_hugetlb_usage",
"hugetlb_usage",
"Hugepages usage for a given container and pod.",
[]string{
"container_id",
Expand All @@ -88,7 +87,7 @@ var descriptors = [numDescriptors]*prometheus.Desc{
}, nil,
),
blkioDeviceUsageDesc: prometheus.NewDesc(
"cgroup_blkio_device_usage",
"blkio_device_usage",
"Blkio Device bytes usage for a given container and pod.",
[]string{
"container_id",
Expand All @@ -114,8 +113,8 @@ type collector struct {
}

// NewCollector creates new Prometheus collector
func NewCollector() (prometheus.Collector, error) {
return &collector{}, nil
func NewCollector() prometheus.Collector {
return &collector{}
}

// Describe implements prometheus.Collector interface
Expand Down Expand Up @@ -405,9 +404,4 @@ func (c collector) Collect(ch chan<- prometheus.Metric) {
func init() {
flag.StringVar(&cgroupRoot, "cgroup-path", cgroupRoot,
"Path to cgroup filesystem mountpoint")

err := metrics.RegisterCollector("cgroupstats", NewCollector)
if err != nil {
log.Error("failed register cgroupstats collector: %v", err)
}
}
39 changes: 39 additions & 0 deletions pkg/cgroupstats/register/collectors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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/containers/nri-plugins/pkg/cgroupstats"
logger "github.com/containers/nri-plugins/pkg/log"
"github.com/containers/nri-plugins/pkg/metrics"
)

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

func init() {
err := metrics.Register(
cgroupstats.NewCollector(),
metrics.WithGroup("cgroup"),
metrics.WithCollectorOptions(
metrics.WithName("stats"),
metrics.WithoutNamespace(),
),
)
if err != nil {
log.Error("failed register cgroup/stats collector: %v", err)
}
}
56 changes: 56 additions & 0 deletions pkg/metrics/collectors/collectors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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("misc"),
metrics.WithCollectorOptions(
metrics.WithoutNamespace(),
metrics.WithoutSubsystem(),
),
}
)

for name, collector := range collectors {
if err := metrics.Register(
collector,
append(
append([]metrics.RegisterOption{}, options...),
metrics.WithCollectorOptions(metrics.WithName(name)),
)...,
); err != nil {
log.Error("failed to register %s collector: %v", name, err)
}
}
}

0 comments on commit 6ed3d64

Please sign in to comment.