github.com/demonoid81/containerd@v1.3.4/metrics/cgroups/metric.go (about)

     1  // +build linux
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package cgroups
    20  
    21  import (
    22  	v1 "github.com/containerd/containerd/metrics/types/v1"
    23  	metrics "github.com/docker/go-metrics"
    24  	"github.com/prometheus/client_golang/prometheus"
    25  )
    26  
    27  type value struct {
    28  	v float64
    29  	l []string
    30  }
    31  
    32  type metric struct {
    33  	name   string
    34  	help   string
    35  	unit   metrics.Unit
    36  	vt     prometheus.ValueType
    37  	labels []string
    38  	// getValues returns the value and labels for the data
    39  	getValues func(stats *v1.Metrics) []value
    40  }
    41  
    42  func (m *metric) desc(ns *metrics.Namespace) *prometheus.Desc {
    43  	// the namespace label is for containerd namespaces
    44  	return ns.NewDesc(m.name, m.help, m.unit, append([]string{"container_id", "namespace"}, m.labels...)...)
    45  }
    46  
    47  func (m *metric) collect(id, namespace string, stats *v1.Metrics, ns *metrics.Namespace, ch chan<- prometheus.Metric, block bool) {
    48  	values := m.getValues(stats)
    49  	for _, v := range values {
    50  		// block signals to block on the sending the metrics so none are missed
    51  		if block {
    52  			ch <- prometheus.MustNewConstMetric(m.desc(ns), m.vt, v.v, append([]string{id, namespace}, v.l...)...)
    53  			continue
    54  		}
    55  		// non-blocking metrics can be dropped if the chan is full
    56  		select {
    57  		case ch <- prometheus.MustNewConstMetric(m.desc(ns), m.vt, v.v, append([]string{id, namespace}, v.l...)...):
    58  		default:
    59  		}
    60  	}
    61  }