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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package cockroachdb
     4  
     5  import (
     6  	_ "embed"
     7  	"errors"
     8  	"time"
     9  
    10  	"github.com/netdata/go.d.plugin/pkg/prometheus"
    11  	"github.com/netdata/go.d.plugin/pkg/web"
    12  
    13  	"github.com/netdata/go.d.plugin/agent/module"
    14  )
    15  
    16  // DefaultMetricsSampleInterval hard coded to 10
    17  // https://github.com/cockroachdb/cockroach/blob/d5ffbf76fb4c4ef802836529188e4628476879bd/pkg/server/config.go#L56-L58
    18  const cockroachDBSamplingInterval = 10
    19  
    20  //go:embed "config_schema.json"
    21  var configSchema string
    22  
    23  func init() {
    24  	module.Register("cockroachdb", module.Creator{
    25  		JobConfigSchema: configSchema,
    26  		Defaults: module.Defaults{
    27  			UpdateEvery: cockroachDBSamplingInterval,
    28  		},
    29  		Create: func() module.Module { return New() },
    30  	})
    31  }
    32  
    33  func New() *CockroachDB {
    34  	config := Config{
    35  		HTTP: web.HTTP{
    36  			Request: web.Request{
    37  				URL: "http://127.0.0.1:8080/_status/vars",
    38  			},
    39  			Client: web.Client{
    40  				Timeout: web.Duration{Duration: time.Second},
    41  			},
    42  		},
    43  	}
    44  
    45  	return &CockroachDB{
    46  		Config: config,
    47  		charts: charts.Copy(),
    48  	}
    49  }
    50  
    51  type (
    52  	Config struct {
    53  		web.HTTP    `yaml:",inline"`
    54  		UpdateEvery int `yaml:"update_every"`
    55  	}
    56  
    57  	CockroachDB struct {
    58  		module.Base
    59  		Config `yaml:",inline"`
    60  
    61  		prom   prometheus.Prometheus
    62  		charts *Charts
    63  	}
    64  )
    65  
    66  func (c *CockroachDB) validateConfig() error {
    67  	if c.URL == "" {
    68  		return errors.New("URL is not set")
    69  	}
    70  	return nil
    71  }
    72  
    73  func (c *CockroachDB) initClient() error {
    74  	client, err := web.NewHTTPClient(c.Client)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	c.prom = prometheus.New(client, c.Request)
    80  	return nil
    81  }
    82  
    83  func (c *CockroachDB) Init() bool {
    84  	if err := c.validateConfig(); err != nil {
    85  		c.Errorf("error on validating config: %v", err)
    86  		return false
    87  	}
    88  	if err := c.initClient(); err != nil {
    89  		c.Errorf("error on initializing client: %v", err)
    90  		return false
    91  	}
    92  	if c.UpdateEvery < cockroachDBSamplingInterval {
    93  		c.Warningf("'update_every'(%d) is lower then CockroachDB default sampling interval (%d)",
    94  			c.UpdateEvery, cockroachDBSamplingInterval)
    95  	}
    96  	return true
    97  }
    98  
    99  func (c *CockroachDB) Check() bool {
   100  	return len(c.Collect()) > 0
   101  }
   102  
   103  func (c *CockroachDB) Charts() *Charts {
   104  	return c.charts
   105  }
   106  
   107  func (c *CockroachDB) Collect() map[string]int64 {
   108  	mx, err := c.collect()
   109  	if err != nil {
   110  		c.Error(err)
   111  	}
   112  
   113  	if len(mx) == 0 {
   114  		return nil
   115  	}
   116  	return mx
   117  }
   118  
   119  func (CockroachDB) Cleanup() {}