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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package cassandra
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/agent/module"
    10  	"github.com/netdata/go.d.plugin/pkg/prometheus"
    11  	"github.com/netdata/go.d.plugin/pkg/web"
    12  )
    13  
    14  //go:embed "config_schema.json"
    15  var configSchema string
    16  
    17  func init() {
    18  	module.Register("cassandra", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Defaults: module.Defaults{
    21  			UpdateEvery: 5,
    22  		},
    23  		Create: func() module.Module { return New() },
    24  	})
    25  }
    26  
    27  func New() *Cassandra {
    28  	return &Cassandra{
    29  		Config: Config{
    30  			HTTP: web.HTTP{
    31  				Request: web.Request{
    32  					URL: "http://127.0.0.1:7072/metrics",
    33  				},
    34  				Client: web.Client{
    35  					Timeout: web.Duration{Duration: time.Second * 5},
    36  				},
    37  			},
    38  		},
    39  		charts:          baseCharts.Copy(),
    40  		validateMetrics: true,
    41  		mx:              newCassandraMetrics(),
    42  	}
    43  }
    44  
    45  type Config struct {
    46  	web.HTTP `yaml:",inline"`
    47  }
    48  
    49  type Cassandra struct {
    50  	module.Base
    51  	Config `yaml:",inline"`
    52  
    53  	charts *module.Charts
    54  
    55  	prom prometheus.Prometheus
    56  
    57  	validateMetrics bool
    58  	mx              *cassandraMetrics
    59  }
    60  
    61  func (c *Cassandra) Init() bool {
    62  	if err := c.validateConfig(); err != nil {
    63  		c.Errorf("error on validating config: %v", err)
    64  		return false
    65  	}
    66  
    67  	prom, err := c.initPrometheusClient()
    68  	if err != nil {
    69  		c.Errorf("error on init prometheus client: %v", err)
    70  		return false
    71  	}
    72  	c.prom = prom
    73  
    74  	return true
    75  }
    76  
    77  func (c *Cassandra) Check() bool {
    78  	return len(c.Collect()) > 0
    79  }
    80  
    81  func (c *Cassandra) Charts() *module.Charts {
    82  	return c.charts
    83  }
    84  
    85  func (c *Cassandra) Collect() map[string]int64 {
    86  	mx, err := c.collect()
    87  	if err != nil {
    88  		c.Error(err)
    89  	}
    90  
    91  	if len(mx) == 0 {
    92  		return nil
    93  	}
    94  	return mx
    95  }
    96  
    97  func (c *Cassandra) Cleanup() {}