github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/metrics/cgroups/v1/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 v1 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 // IDName is the name that is used to identify the id being collected in the metric 28 var IDName = "container_id" 29 30 type value struct { 31 v float64 32 l []string 33 } 34 35 type metric struct { 36 name string 37 help string 38 unit metrics.Unit 39 vt prometheus.ValueType 40 labels []string 41 // getValues returns the value and labels for the data 42 getValues func(stats *v1.Metrics) []value 43 } 44 45 func (m *metric) desc(ns *metrics.Namespace) *prometheus.Desc { 46 // the namespace label is for containerd namespaces 47 return ns.NewDesc(m.name, m.help, m.unit, append([]string{IDName, "namespace"}, m.labels...)...) 48 } 49 50 func (m *metric) collect(id, namespace string, stats *v1.Metrics, ns *metrics.Namespace, ch chan<- prometheus.Metric, block bool) { 51 values := m.getValues(stats) 52 for _, v := range values { 53 // block signals to block on the sending the metrics so none are missed 54 if block { 55 ch <- prometheus.MustNewConstMetric(m.desc(ns), m.vt, v.v, append([]string{id, namespace}, v.l...)...) 56 continue 57 } 58 // non-blocking metrics can be dropped if the chan is full 59 select { 60 case ch <- prometheus.MustNewConstMetric(m.desc(ns), m.vt, v.v, append([]string{id, namespace}, v.l...)...): 61 default: 62 } 63 } 64 }