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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package envoy
     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("envoy", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  func New() *Envoy {
    25  	return &Envoy{
    26  		Config: Config{
    27  			HTTP: web.HTTP{
    28  				Request: web.Request{
    29  					URL: "http://127.0.0.1:9091/stats/prometheus",
    30  				},
    31  				Client: web.Client{
    32  					Timeout: web.Duration{Duration: time.Second * 2},
    33  				},
    34  			},
    35  		},
    36  
    37  		charts: &module.Charts{},
    38  
    39  		servers:                 make(map[string]bool),
    40  		clusterMgrs:             make(map[string]bool),
    41  		clusterUpstream:         make(map[string]bool),
    42  		listenerMgrs:            make(map[string]bool),
    43  		listenerAdminDownstream: make(map[string]bool),
    44  		listenerDownstream:      make(map[string]bool),
    45  	}
    46  }
    47  
    48  type Config struct {
    49  	web.HTTP `yaml:",inline"`
    50  }
    51  
    52  type Envoy struct {
    53  	module.Base
    54  	Config `yaml:",inline"`
    55  
    56  	prom prometheus.Prometheus
    57  
    58  	charts *module.Charts
    59  
    60  	servers                 map[string]bool
    61  	clusterMgrs             map[string]bool
    62  	clusterUpstream         map[string]bool
    63  	listenerMgrs            map[string]bool
    64  	listenerAdminDownstream map[string]bool
    65  	listenerDownstream      map[string]bool
    66  }
    67  
    68  func (e *Envoy) Init() bool {
    69  	if err := e.validateConfig(); err != nil {
    70  		e.Errorf("config validation: %v", err)
    71  		return false
    72  	}
    73  
    74  	prom, err := e.initPrometheusClient()
    75  	if err != nil {
    76  		e.Errorf("init Prometheus client: %v", err)
    77  		return false
    78  	}
    79  	e.prom = prom
    80  
    81  	return true
    82  }
    83  
    84  func (e *Envoy) Check() bool {
    85  	return len(e.Collect()) > 0
    86  }
    87  
    88  func (e *Envoy) Charts() *module.Charts {
    89  	return e.charts
    90  }
    91  
    92  func (e *Envoy) Collect() map[string]int64 {
    93  	mx, err := e.collect()
    94  	if err != nil {
    95  		e.Error(err)
    96  	}
    97  
    98  	if len(mx) == 0 {
    99  		return nil
   100  	}
   101  	return mx
   102  }
   103  
   104  func (e *Envoy) Cleanup() {
   105  	if e.prom == nil || e.prom.HTTPClient() == nil {
   106  		return
   107  	}
   108  
   109  	e.prom.HTTPClient().CloseIdleConnections()
   110  }