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 14, 2024
1 parent f78d6ba commit edcee16
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 321 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.

9 changes: 9 additions & 0 deletions pkg/resmgr/nri.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ func (p *nriPlugin) StopPodSandbox(ctx context.Context, podSandbox *api.PodSandb

m := p.resmgr

// TODO(klihub): shouldn't we m.Lock()/defer m.Unlock() here?
metrics.Block()
defer metrics.Unblock()

released := []cache.Container{}
pod, _ := m.cache.LookupPod(podSandbox.GetId())

Expand Down Expand Up @@ -391,6 +395,8 @@ func (p *nriPlugin) CreateContainer(ctx context.Context, podSandbox *api.PodSand
m := p.resmgr
m.Lock()
defer m.Unlock()
metrics.Block()
defer metrics.Unblock()

c, err := m.cache.InsertContainer(container)
if err != nil {
Expand Down Expand Up @@ -612,6 +618,9 @@ func (p *nriPlugin) RemoveContainer(ctx context.Context, pod *api.PodSandbox, co
func (p *nriPlugin) updateContainers() (retErr error) {
// Notes: must be called with p.resmgr lock held.

metrics.Block()
defer metrics.Unblock()

updates := p.getPendingUpdates(nil)

event := UpdateContainers
Expand Down
55 changes: 55 additions & 0 deletions pkg/resmgr/policy/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 (
"github.com/prometheus/client_golang/prometheus"

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

type PolicyCollector struct {
policy *policy
}

func (p *policy) newPolicyCollector() *PolicyCollector {
return &PolicyCollector{
policy: p,
}
}

func (c *PolicyCollector) register() error {
return metrics.Register(c.policy.ActivePolicy(), c, metrics.WithGroup("policy"))
}

func (c *PolicyCollector) Describe(ch chan<- *prometheus.Desc) {
for _, d := range c.policy.active.DescribeMetrics() {
ch <- d
}
}

func (c *PolicyCollector) Collect(ch chan<- prometheus.Metric) {
polled := c.policy.active.PollMetrics()

collected, err := c.policy.active.CollectMetrics(polled)
if err != nil {
log.Error("failed to collect metrics: %v", err)
return
}

for _, m := range collected {
ch <- m
}
}
Loading

0 comments on commit edcee16

Please sign in to comment.