github.com/lalkh/containerd@v1.4.3/metrics/cgroups/cgroups.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  	"github.com/containerd/cgroups"
    23  	v1 "github.com/containerd/containerd/metrics/cgroups/v1"
    24  	v2 "github.com/containerd/containerd/metrics/cgroups/v2"
    25  	"github.com/containerd/containerd/platforms"
    26  	"github.com/containerd/containerd/plugin"
    27  	"github.com/containerd/containerd/runtime"
    28  	metrics "github.com/docker/go-metrics"
    29  )
    30  
    31  // Config for the cgroups monitor
    32  type Config struct {
    33  	NoPrometheus bool `toml:"no_prometheus"`
    34  }
    35  
    36  func init() {
    37  	plugin.Register(&plugin.Registration{
    38  		Type:   plugin.TaskMonitorPlugin,
    39  		ID:     "cgroups",
    40  		InitFn: New,
    41  		Config: &Config{},
    42  	})
    43  }
    44  
    45  // New returns a new cgroups monitor
    46  func New(ic *plugin.InitContext) (interface{}, error) {
    47  	var ns *metrics.Namespace
    48  	config := ic.Config.(*Config)
    49  	if !config.NoPrometheus {
    50  		ns = metrics.NewNamespace("container", "", nil)
    51  	}
    52  	var (
    53  		tm  runtime.TaskMonitor
    54  		err error
    55  	)
    56  	if cgroups.Mode() == cgroups.Unified {
    57  		tm, err = v2.NewTaskMonitor(ic.Context, ic.Events, ns)
    58  	} else {
    59  		tm, err = v1.NewTaskMonitor(ic.Context, ic.Events, ns)
    60  	}
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	if ns != nil {
    65  		metrics.Register(ns)
    66  	}
    67  	ic.Meta.Platforms = append(ic.Meta.Platforms, platforms.DefaultSpec())
    68  	return tm, nil
    69  }