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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package pulsar
     4  
     5  import (
     6  	_ "embed"
     7  	"errors"
     8  	"sync"
     9  	"time"
    10  
    11  	"github.com/netdata/go.d.plugin/pkg/matcher"
    12  	"github.com/netdata/go.d.plugin/pkg/prometheus"
    13  	"github.com/netdata/go.d.plugin/pkg/web"
    14  
    15  	"github.com/netdata/go.d.plugin/agent/module"
    16  )
    17  
    18  //go:embed "config_schema.json"
    19  var configSchema string
    20  
    21  func init() {
    22  	module.Register("pulsar", module.Creator{
    23  		JobConfigSchema: configSchema,
    24  		Defaults: module.Defaults{
    25  			UpdateEvery: 60,
    26  		},
    27  		Create: func() module.Module { return New() },
    28  	})
    29  }
    30  
    31  func New() *Pulsar {
    32  	config := Config{
    33  		HTTP: web.HTTP{
    34  			Request: web.Request{
    35  				URL: "http://127.0.0.1:8080/metrics",
    36  			},
    37  			Client: web.Client{
    38  				Timeout: web.Duration{Duration: time.Second},
    39  			},
    40  		},
    41  		TopicFiler: matcher.SimpleExpr{
    42  			Includes: nil,
    43  			Excludes: []string{"*"},
    44  		},
    45  	}
    46  	return &Pulsar{
    47  		Config:             config,
    48  		once:               &sync.Once{},
    49  		charts:             summaryCharts.Copy(),
    50  		nsCharts:           namespaceCharts.Copy(),
    51  		topicChartsMapping: topicChartsMapping(),
    52  		cache:              newCache(),
    53  		curCache:           newCache(),
    54  	}
    55  }
    56  
    57  type (
    58  	Config struct {
    59  		web.HTTP   `yaml:",inline"`
    60  		TopicFiler matcher.SimpleExpr `yaml:"topic_filter"`
    61  	}
    62  
    63  	Pulsar struct {
    64  		module.Base
    65  		Config `yaml:",inline"`
    66  
    67  		prom               prometheus.Prometheus
    68  		topicFilter        matcher.Matcher
    69  		cache              *cache
    70  		curCache           *cache
    71  		once               *sync.Once
    72  		charts             *Charts
    73  		nsCharts           *Charts
    74  		topicChartsMapping map[string]string
    75  	}
    76  
    77  	namespace struct{ name string }
    78  	topic     struct{ namespace, name string }
    79  	cache     struct {
    80  		namespaces map[namespace]bool
    81  		topics     map[topic]bool
    82  	}
    83  )
    84  
    85  func newCache() *cache {
    86  	return &cache{
    87  		namespaces: make(map[namespace]bool),
    88  		topics:     make(map[topic]bool),
    89  	}
    90  }
    91  
    92  func (p Pulsar) validateConfig() error {
    93  	if p.URL == "" {
    94  		return errors.New("URL is not set")
    95  	}
    96  	return nil
    97  }
    98  
    99  func (p *Pulsar) initClient() error {
   100  	client, err := web.NewHTTPClient(p.Client)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	p.prom = prometheus.New(client, p.Request)
   106  	return nil
   107  }
   108  
   109  func (p *Pulsar) initTopicFiler() error {
   110  	if p.TopicFiler.Empty() {
   111  		p.topicFilter = matcher.TRUE()
   112  		return nil
   113  	}
   114  
   115  	m, err := p.TopicFiler.Parse()
   116  	if err != nil {
   117  		return err
   118  	}
   119  	p.topicFilter = m
   120  	return nil
   121  }
   122  
   123  func (p *Pulsar) Init() bool {
   124  	if err := p.validateConfig(); err != nil {
   125  		p.Errorf("config validation: %v", err)
   126  		return false
   127  	}
   128  	if err := p.initClient(); err != nil {
   129  		p.Errorf("client initializing: %v", err)
   130  		return false
   131  	}
   132  	if err := p.initTopicFiler(); err != nil {
   133  		p.Errorf("topic filer initialization: %v", err)
   134  		return false
   135  	}
   136  	return true
   137  }
   138  
   139  func (p *Pulsar) Check() bool {
   140  	return len(p.Collect()) > 0
   141  }
   142  
   143  func (p *Pulsar) Charts() *Charts {
   144  	return p.charts
   145  }
   146  
   147  func (p *Pulsar) Collect() map[string]int64 {
   148  	mx, err := p.collect()
   149  	if err != nil {
   150  		p.Error(err)
   151  	}
   152  
   153  	if len(mx) == 0 {
   154  		return nil
   155  	}
   156  	return mx
   157  }
   158  
   159  func (Pulsar) Cleanup() {}