Skip to content

Commit

Permalink
resmgr: remove old scattered bits of metrics polling.
Browse files Browse the repository at this point in the history
Remove the old resmgr-triggered polling of policy metrics
and the old resmgr-level polling policy metrics collector.
Implement policy metrics collection in the policy package
itself.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Nov 10, 2024
1 parent 0deae44 commit 1219e69
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 310 deletions.
22 changes: 0 additions & 22 deletions pkg/resmgr/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,21 @@ package resmgr
import (
logger "github.com/containers/nri-plugins/pkg/log"
"github.com/containers/nri-plugins/pkg/resmgr/cache"
"github.com/containers/nri-plugins/pkg/resmgr/metrics"
)

// Our logger instance for events.
var evtlog = logger.NewLogger("events")

// setupEventProcessing sets up event and metrics processing.
func (m *resmgr) setupEventProcessing() error {
var err error

m.events = make(chan interface{}, 8)
m.stop = make(chan interface{})
options := metrics.Options{
PollInterval: opt.MetricsTimer,
}
if m.metrics, err = metrics.NewMetrics(options); err != nil {
return resmgrError("failed to create metrics (pre)processor: %v", err)
}

return nil
}

func (m *resmgr) startMetricsProcessing() error {
if err := m.metrics.Start(); err != nil {
return resmgrError("failed to start metrics (pre)processor: %v", err)
}

return nil
}

// startEventProcessing starts event and metrics processing.
func (m *resmgr) startEventProcessing() error {
if err := m.startMetricsProcessing(); err != nil {
return resmgrError("failed to start metrics (pre)processor: %v", err)
}

stop := m.stop
go func() {
for {
Expand All @@ -73,7 +52,6 @@ func (m *resmgr) startEventProcessing() error {
func (m *resmgr) stopEventProcessing() {
if m.stop != nil {
close(m.stop)
m.metrics.Stop()
m.stop = nil
}
}
Expand Down
149 changes: 0 additions & 149 deletions pkg/resmgr/metrics/metrics.go

This file was deleted.

34 changes: 0 additions & 34 deletions pkg/resmgr/metrics/prometheus.go

This file was deleted.

68 changes: 68 additions & 0 deletions pkg/resmgr/policy/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 policy

import (
"sync"

"github.com/prometheus/client_golang/prometheus"

"github.com/containers/nri-plugins/pkg/metrics"
)

type Collector struct {
policy *policy
block sync.Mutex
}

var _ prometheus.Collector = &Collector{}

func (p *policy) registerCollector() error {
collector := &Collector{policy: p}
p.collector = collector

return metrics.Register(p.ActivePolicy(), collector, metrics.WithGroup("policy"))
}

func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
log.Debug("describing metrics")
for _, d := range c.policy.active.DescribeMetrics() {
ch <- d
}
}

func (c *Collector) Collect(ch chan<- prometheus.Metric) {
c.Block()
log.Debug("collecting metrics")
polled := c.policy.active.PollMetrics()
c.Unblock()

pm, err := c.policy.active.CollectMetrics(polled)
if err != nil {
log.Error("failed to collect metrics: %v", err)
return
}
for _, m := range pm {
ch <- m
}
}

func (c *Collector) Block() {
c.block.Lock()
}

func (c *Collector) Unblock() {
c.block.Unlock()
}
Loading

0 comments on commit 1219e69

Please sign in to comment.