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 11, 2024
1 parent 2337c88 commit 71a1b62
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 322 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.

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
}
}
62 changes: 27 additions & 35 deletions pkg/resmgr/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ type Policy interface {
HandleEvent(*events.Policy) (bool, error)
// ExportResourceData exports/updates resource data for the container.
ExportResourceData(cache.Container)
// DescribeMetrics generates policy-specific prometheus metrics data descriptors.
DescribeMetrics() []*prometheus.Desc
// PollMetrics provides policy metrics for monitoring.
PollMetrics() Metrics
// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
CollectMetrics(Metrics) ([]prometheus.Metric, error)
// GetTopologyZones returns the policy/pool data for 'topology zone' CRDs.
GetTopologyZones() []*TopologyZone
}
Expand Down Expand Up @@ -204,11 +198,12 @@ type ZoneAttribute struct {

// Policy instance/state.
type policy struct {
options Options // policy options
cache cache.Cache // system state cache
active Backend // our active backend
system system.System // system/HW/topology info
sendEvent SendEventFn // function to send event up to the resource manager
options Options // policy options
cache cache.Cache // system state cache
active Backend // our active backend
system system.System // system/HW/topology info
sendEvent SendEventFn // function to send event up to the resource manager
pcollect *PolicyCollector // policy metrics collector
}

// backend is a registered Backend.
Expand All @@ -225,11 +220,25 @@ var log logger.Logger = logger.NewLogger("policy")
func NewPolicy(backend Backend, cache cache.Cache, o *Options) (Policy, error) {
log.Info("creating '%s' policy...", backend.Name())

return &policy{
p := &policy{
cache: cache,
options: *o,
active: backend,
}, nil
}

sys, err := system.DiscoverSystem()
if err != nil {
return nil, policyError("failed to discover system topology: %v", err)
}
p.system = sys

pcollect := p.newPolicyCollector()
if err := pcollect.register(); err != nil {
return nil, policyError("failed to register policy collector: %v", err)
}
p.pcollect = pcollect

return p, nil
}

func (p *policy) ActivePolicy() string {
Expand All @@ -241,12 +250,6 @@ func (p *policy) ActivePolicy() string {

// Start starts up policy, preparing it for serving requests.
func (p *policy) Start(cfg interface{}) error {
sys, err := system.DiscoverSystem()
if err != nil {
return policyError("failed to discover system topology: %v", err)
}
p.system = sys

log.Info("activating '%s' policy...", p.active.Name())

if err := p.active.Setup(&BackendOptions{
Expand All @@ -258,7 +261,11 @@ func (p *policy) Start(cfg interface{}) error {
return err
}

return p.active.Start()
if err := p.active.Start(); err != nil {
return err
}

return nil
}

// Reconfigure the policy.
Expand Down Expand Up @@ -315,21 +322,6 @@ func (p *policy) ExportResourceData(c cache.Container) {
p.cache.WriteFile(c.GetID(), ExportedResources, 0644, buf.Bytes())
}

// PollMetrics provides policy metrics for monitoring.
func (p *policy) PollMetrics() Metrics {
return p.active.PollMetrics()
}

// DescribeMetrics generates policy-specific prometheus metrics data descriptors.
func (p *policy) DescribeMetrics() []*prometheus.Desc {
return p.active.DescribeMetrics()
}

// CollectMetrics generates prometheus metrics from cached/polled policy-specific metrics data.
func (p *policy) CollectMetrics(m Metrics) ([]prometheus.Metric, error) {
return p.active.CollectMetrics(m)
}

// GetTopologyZones returns the policy/pool data for 'topology zone' CRDs.
func (p *policy) GetTopologyZones() []*TopologyZone {
return p.active.GetTopologyZones()
Expand Down
Loading

0 comments on commit 71a1b62

Please sign in to comment.