github.com/netdata/go.d.plugin@v0.58.1/modules/k8s_kubeproxy/kubeproxy.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package k8s_kubeproxy
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/pkg/prometheus"
    10  	"github.com/netdata/go.d.plugin/pkg/web"
    11  
    12  	"github.com/netdata/go.d.plugin/agent/module"
    13  )
    14  
    15  const (
    16  	defaultURL         = "http://127.0.0.1:10249/metrics"
    17  	defaultHTTPTimeout = time.Second * 2
    18  )
    19  
    20  //go:embed "config_schema.json"
    21  var configSchema string
    22  
    23  func init() {
    24  	module.Register("k8s_kubeproxy", module.Creator{
    25  		JobConfigSchema: configSchema,
    26  		Defaults: module.Defaults{
    27  			// NETDATA_CHART_PRIO_CGROUPS_CONTAINERS        40000
    28  			Priority: 50000,
    29  		},
    30  		Create: func() module.Module { return New() },
    31  	})
    32  }
    33  
    34  // New creates KubeProxy with default values.
    35  func New() *KubeProxy {
    36  	config := Config{
    37  		HTTP: web.HTTP{
    38  			Request: web.Request{
    39  				URL: defaultURL,
    40  			},
    41  			Client: web.Client{
    42  				Timeout: web.Duration{Duration: defaultHTTPTimeout},
    43  			},
    44  		},
    45  	}
    46  	return &KubeProxy{
    47  		Config: config,
    48  		charts: charts.Copy(),
    49  	}
    50  }
    51  
    52  // Config is the KubeProxy module configuration.
    53  type Config struct {
    54  	web.HTTP `yaml:",inline"`
    55  }
    56  
    57  // KubeProxy is KubeProxy module.
    58  type KubeProxy struct {
    59  	module.Base
    60  	Config `yaml:",inline"`
    61  
    62  	prom   prometheus.Prometheus
    63  	charts *Charts
    64  }
    65  
    66  // Cleanup makes cleanup.
    67  func (KubeProxy) Cleanup() {}
    68  
    69  // Init makes initialization.
    70  func (kp *KubeProxy) Init() bool {
    71  	if kp.URL == "" {
    72  		kp.Error("URL not set")
    73  		return false
    74  	}
    75  
    76  	client, err := web.NewHTTPClient(kp.Client)
    77  	if err != nil {
    78  		kp.Errorf("error on creating http client : %v", err)
    79  		return false
    80  	}
    81  
    82  	kp.prom = prometheus.New(client, kp.Request)
    83  
    84  	return true
    85  }
    86  
    87  // Check makes check.
    88  func (kp *KubeProxy) Check() bool {
    89  	return len(kp.Collect()) > 0
    90  }
    91  
    92  // Charts creates Charts.
    93  func (kp KubeProxy) Charts() *Charts {
    94  	return kp.charts
    95  }
    96  
    97  // Collect collects metrics.
    98  func (kp *KubeProxy) Collect() map[string]int64 {
    99  	mx, err := kp.collect()
   100  
   101  	if err != nil {
   102  		kp.Error(err)
   103  		return nil
   104  	}
   105  
   106  	return mx
   107  }