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

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