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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package tengine
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/pkg/web"
    10  
    11  	"github.com/netdata/go.d.plugin/agent/module"
    12  )
    13  
    14  //go:embed "config_schema.json"
    15  var configSchema string
    16  
    17  func init() {
    18  	module.Register("tengine", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  const (
    25  	defaultURL         = "http://127.0.0.1/us"
    26  	defaultHTTPTimeout = time.Second * 2
    27  )
    28  
    29  // New creates Tengine with default values.
    30  func New() *Tengine {
    31  	config := Config{
    32  		HTTP: web.HTTP{
    33  			Request: web.Request{
    34  				URL: defaultURL,
    35  			},
    36  			Client: web.Client{
    37  				Timeout: web.Duration{Duration: defaultHTTPTimeout},
    38  			},
    39  		},
    40  	}
    41  	return &Tengine{Config: config}
    42  }
    43  
    44  // Config is the Tengine module configuration.
    45  type Config struct {
    46  	web.HTTP `yaml:",inline"`
    47  }
    48  
    49  // Tengine Tengine module.
    50  type Tengine struct {
    51  	module.Base
    52  	Config `yaml:",inline"`
    53  
    54  	apiClient *apiClient
    55  }
    56  
    57  // Cleanup makes cleanup.
    58  func (Tengine) Cleanup() {}
    59  
    60  // Init makes initialization.
    61  func (t *Tengine) Init() bool {
    62  	if t.URL == "" {
    63  		t.Error("URL not set")
    64  		return false
    65  	}
    66  
    67  	client, err := web.NewHTTPClient(t.Client)
    68  	if err != nil {
    69  		t.Errorf("error on creating http client : %v", err)
    70  		return false
    71  	}
    72  
    73  	t.apiClient = newAPIClient(client, t.Request)
    74  
    75  	t.Debugf("using URL: %s", t.URL)
    76  	t.Debugf("using timeout: %s", t.Timeout.Duration)
    77  	return true
    78  }
    79  
    80  // Check makes check
    81  func (t *Tengine) Check() bool {
    82  	return len(t.Collect()) > 0
    83  }
    84  
    85  // Charts returns Charts.
    86  func (t Tengine) Charts() *module.Charts {
    87  	return charts.Copy()
    88  }
    89  
    90  // Collect collects metrics.
    91  func (t *Tengine) Collect() map[string]int64 {
    92  	mx, err := t.collect()
    93  
    94  	if err != nil {
    95  		t.Error(err)
    96  		return nil
    97  	}
    98  
    99  	return mx
   100  }