github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/resource/cobalt/metrics.go (about)

     1  package cobalt
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/projecteru2/core/log"
     7  	"github.com/projecteru2/core/resource/plugins"
     8  	plugintypes "github.com/projecteru2/core/resource/plugins/types"
     9  	"github.com/projecteru2/core/types"
    10  )
    11  
    12  // GetMetricsDescription .
    13  func (m Manager) GetMetricsDescription(ctx context.Context) ([]*plugintypes.MetricsDescription, error) {
    14  	var metricsDescriptions []*plugintypes.MetricsDescription
    15  	resps, err := call(ctx, m.plugins, func(plugin plugins.Plugin) (*plugintypes.GetMetricsDescriptionResponse, error) {
    16  		resp, err := plugin.GetMetricsDescription(ctx)
    17  		return resp, err
    18  	})
    19  
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	for _, resp := range resps {
    25  		metricsDescriptions = append(metricsDescriptions, *resp...)
    26  	}
    27  
    28  	return metricsDescriptions, nil
    29  }
    30  
    31  // GetNodeMetrics .
    32  func (m Manager) GetNodeMetrics(ctx context.Context, node *types.Node) ([]*plugintypes.Metrics, error) {
    33  	logger := log.WithFunc("resource.cobalt.GetNodeMetrics").WithField("node", node.Name)
    34  
    35  	var metrics []*plugintypes.Metrics
    36  	resps, err := call(ctx, m.plugins, func(plugin plugins.Plugin) (*plugintypes.GetMetricsResponse, error) {
    37  		resp, err := plugin.GetMetrics(ctx, node.Podname, node.Name)
    38  		if err != nil {
    39  			logger.Errorf(ctx, err, "plugin %+v failed to convert node resource info to metrics", plugin.Name())
    40  		}
    41  		return resp, err
    42  	})
    43  
    44  	if err != nil {
    45  		logger.Error(ctx, err, "failed to convert node resource info to metrics")
    46  		return nil, err
    47  	}
    48  
    49  	for _, resp := range resps {
    50  		metrics = append(metrics, *resp...)
    51  	}
    52  
    53  	return metrics, nil
    54  }