...

Source file src/k8s.io/kubernetes/pkg/kubelet/metrics/collectors/log_metrics.go

Documentation: k8s.io/kubernetes/pkg/kubelet/metrics/collectors

     1  /*
     2  Copyright 2018 The Kubernetes 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 collectors
    18  
    19  import (
    20  	"context"
    21  
    22  	"k8s.io/component-base/metrics"
    23  	"k8s.io/klog/v2"
    24  	statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
    25  )
    26  
    27  var (
    28  	descLogSize = metrics.NewDesc(
    29  		"kubelet_container_log_filesystem_used_bytes",
    30  		"Bytes used by the container's logs on the filesystem.",
    31  		[]string{
    32  			"uid",
    33  			"namespace",
    34  			"pod",
    35  			"container",
    36  		}, nil,
    37  		metrics.ALPHA,
    38  		"",
    39  	)
    40  )
    41  
    42  type logMetricsCollector struct {
    43  	metrics.BaseStableCollector
    44  
    45  	podStats func(ctx context.Context) ([]statsapi.PodStats, error)
    46  }
    47  
    48  // Check if logMetricsCollector implements necessary interface
    49  var _ metrics.StableCollector = &logMetricsCollector{}
    50  
    51  // NewLogMetricsCollector implements the metrics.StableCollector interface and
    52  // exposes metrics about container's log volume size.
    53  func NewLogMetricsCollector(podStats func(ctx context.Context) ([]statsapi.PodStats, error)) metrics.StableCollector {
    54  	return &logMetricsCollector{
    55  		podStats: podStats,
    56  	}
    57  }
    58  
    59  // DescribeWithStability implements the metrics.StableCollector interface.
    60  func (c *logMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
    61  	ch <- descLogSize
    62  }
    63  
    64  // CollectWithStability implements the metrics.StableCollector interface.
    65  func (c *logMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
    66  	podStats, err := c.podStats(context.Background())
    67  	if err != nil {
    68  		klog.ErrorS(err, "Failed to get pod stats")
    69  		return
    70  	}
    71  
    72  	for _, ps := range podStats {
    73  		for _, c := range ps.Containers {
    74  			if c.Logs != nil && c.Logs.UsedBytes != nil {
    75  				ch <- metrics.NewLazyConstMetric(
    76  					descLogSize,
    77  					metrics.GaugeValue,
    78  					float64(*c.Logs.UsedBytes),
    79  					ps.PodRef.UID,
    80  					ps.PodRef.Namespace,
    81  					ps.PodRef.Name,
    82  					c.Name,
    83  				)
    84  			}
    85  		}
    86  	}
    87  }
    88  

View as plain text