github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/agent/metric/helper/container.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package helper
    18  
    19  import (
    20  	"strconv"
    21  
    22  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/types"
    23  	"github.com/kubewharf/katalyst-core/pkg/metrics"
    24  	"github.com/kubewharf/katalyst-core/pkg/util/general"
    25  )
    26  
    27  func GetContainerMetric(metricsFetcher types.MetricsFetcher, emitter metrics.MetricEmitter, podUID, containerName, metricName string, numaID int) (float64, error) {
    28  	if numaID >= 0 {
    29  		data, err := metricsFetcher.GetContainerNumaMetric(podUID, containerName, strconv.Itoa(numaID), metricName)
    30  		if err != nil {
    31  			general.Errorf(errMsgGetContainerNumaMetrics, metricName, podUID, containerName, numaID, err)
    32  			return 0, err
    33  		}
    34  		containerMetricValue := data.Value
    35  		_ = emitter.StoreFloat64(metricsNameContainerMetric, containerMetricValue, metrics.MetricTypeNameRaw,
    36  			metrics.ConvertMapToTags(map[string]string{
    37  				metricsTagKeyPodUID:        podUID,
    38  				metricsTagKeyContainerName: containerName,
    39  				metricsTagKeyNumaID:        strconv.Itoa(numaID),
    40  				metricsTagKeyMetricName:    metricName,
    41  			})...)
    42  		return containerMetricValue, nil
    43  	}
    44  	data, err := metricsFetcher.GetContainerMetric(podUID, containerName, metricName)
    45  	if err != nil {
    46  		general.Errorf(errMsgGetContainerSystemMetrics, metricName, podUID, containerName, err)
    47  		return 0, err
    48  	}
    49  	containerMetricValue := data.Value
    50  	_ = emitter.StoreFloat64(metricsNameContainerMetric, containerMetricValue, metrics.MetricTypeNameRaw,
    51  		metrics.ConvertMapToTags(map[string]string{
    52  			metricsTagKeyPodUID:        podUID,
    53  			metricsTagKeyContainerName: containerName,
    54  			metricsTagKeyNumaID:        strconv.Itoa(numaID),
    55  			metricsTagKeyMetricName:    metricName,
    56  		})...)
    57  	return containerMetricValue, nil
    58  }