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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package coredns
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/blang/semver/v4"
    10  	"github.com/netdata/go.d.plugin/pkg/matcher"
    11  	"github.com/netdata/go.d.plugin/pkg/prometheus"
    12  	"github.com/netdata/go.d.plugin/pkg/web"
    13  
    14  	"github.com/netdata/go.d.plugin/agent/module"
    15  )
    16  
    17  const (
    18  	defaultURL         = "http://127.0.0.1:9153/metrics"
    19  	defaultHTTPTimeout = time.Second * 2
    20  )
    21  
    22  //go:embed "config_schema.json"
    23  var configSchema string
    24  
    25  func init() {
    26  	module.Register("coredns", module.Creator{
    27  		JobConfigSchema: configSchema,
    28  		Create:          func() module.Module { return New() },
    29  	})
    30  }
    31  
    32  // New creates CoreDNS with default values.
    33  func New() *CoreDNS {
    34  	config := Config{
    35  		HTTP: web.HTTP{
    36  			Request: web.Request{
    37  				URL: defaultURL,
    38  			},
    39  			Client: web.Client{
    40  				Timeout: web.Duration{Duration: defaultHTTPTimeout},
    41  			},
    42  		},
    43  	}
    44  	return &CoreDNS{
    45  		Config:           config,
    46  		charts:           summaryCharts.Copy(),
    47  		collectedServers: make(map[string]bool),
    48  		collectedZones:   make(map[string]bool),
    49  	}
    50  }
    51  
    52  // Config is the CoreDNS module configuration.
    53  type Config struct {
    54  	web.HTTP       `yaml:",inline"`
    55  	PerServerStats matcher.SimpleExpr `yaml:"per_server_stats"`
    56  	PerZoneStats   matcher.SimpleExpr `yaml:"per_zone_stats"`
    57  }
    58  
    59  // CoreDNS CoreDNS module.
    60  type CoreDNS struct {
    61  	module.Base
    62  	Config           `yaml:",inline"`
    63  	charts           *Charts
    64  	prom             prometheus.Prometheus
    65  	perServerMatcher matcher.Matcher
    66  	perZoneMatcher   matcher.Matcher
    67  	collectedServers map[string]bool
    68  	collectedZones   map[string]bool
    69  	skipVersionCheck bool
    70  	version          *semver.Version
    71  	metricNames      requestMetricsNames
    72  }
    73  
    74  // Cleanup makes cleanup.
    75  func (CoreDNS) Cleanup() {}
    76  
    77  // Init makes initialization.
    78  func (cd *CoreDNS) Init() bool {
    79  	if cd.URL == "" {
    80  		cd.Error("URL not set")
    81  		return false
    82  	}
    83  
    84  	if !cd.PerServerStats.Empty() {
    85  		m, err := cd.PerServerStats.Parse()
    86  		if err != nil {
    87  			cd.Errorf("error on creating 'per_server_stats' matcher : %v", err)
    88  			return false
    89  		}
    90  		cd.perServerMatcher = matcher.WithCache(m)
    91  	}
    92  
    93  	if !cd.PerZoneStats.Empty() {
    94  		m, err := cd.PerZoneStats.Parse()
    95  		if err != nil {
    96  			cd.Errorf("error on creating 'per_zone_stats' matcher : %v", err)
    97  			return false
    98  		}
    99  		cd.perZoneMatcher = matcher.WithCache(m)
   100  	}
   101  
   102  	client, err := web.NewHTTPClient(cd.Client)
   103  	if err != nil {
   104  		cd.Errorf("error on creating http client : %v", err)
   105  		return false
   106  	}
   107  
   108  	cd.prom = prometheus.New(client, cd.Request)
   109  
   110  	return true
   111  }
   112  
   113  // Check makes check.
   114  func (cd *CoreDNS) Check() bool {
   115  	return len(cd.Collect()) > 0
   116  }
   117  
   118  // Charts creates Charts.
   119  func (cd *CoreDNS) Charts() *Charts {
   120  	return cd.charts
   121  }
   122  
   123  // Collect collects metrics.
   124  func (cd *CoreDNS) Collect() map[string]int64 {
   125  	mx, err := cd.collect()
   126  
   127  	if err != nil {
   128  		cd.Error(err)
   129  		return nil
   130  	}
   131  
   132  	return mx
   133  }