From b781b5cac5ebd678bc27bb1ab2509eb44e8cbb7f Mon Sep 17 00:00:00 2001 From: songjiayang Date: Fri, 4 Jun 2021 20:43:24 +0800 Subject: [PATCH] Refactor file discovery init function (#8891) * Refactor file discovery init function Combine to one init function like other discovery. Signed-off-by: songjiayang --- discovery/consul/consul.go | 3 +-- discovery/dns/dns.go | 3 +-- discovery/file/file.go | 35 ++++++++++++++--------------------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/discovery/consul/consul.go b/discovery/consul/consul.go index fa3d32303..7401989bc 100644 --- a/discovery/consul/consul.go +++ b/discovery/consul/consul.go @@ -102,8 +102,7 @@ var ( func init() { discovery.RegisterConfig(&SDConfig{}) - prometheus.MustRegister(rpcFailuresCount) - prometheus.MustRegister(rpcDuration) + prometheus.MustRegister(rpcFailuresCount, rpcDuration) } // SDConfig is the configuration for Consul service discovery. diff --git a/discovery/dns/dns.go b/discovery/dns/dns.go index 7b388ca71..931c04934 100644 --- a/discovery/dns/dns.go +++ b/discovery/dns/dns.go @@ -68,8 +68,7 @@ var ( func init() { discovery.RegisterConfig(&SDConfig{}) - prometheus.MustRegister(dnsSDLookupFailuresCount) - prometheus.MustRegister(dnsSDLookupsCount) + prometheus.MustRegister(dnsSDLookupFailuresCount, dnsSDLookupsCount) } // SDConfig is the configuration for DNS based service discovery. diff --git a/discovery/file/file.go b/discovery/file/file.go index be5dee5c5..a7b046877 100644 --- a/discovery/file/file.go +++ b/discovery/file/file.go @@ -39,6 +39,19 @@ import ( ) var ( + fileSDScanDuration = prometheus.NewSummary( + prometheus.SummaryOpts{ + Name: "prometheus_sd_file_scan_duration_seconds", + Help: "The duration of the File-SD scan in seconds.", + Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, + }) + fileSDReadErrorsCount = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "prometheus_sd_file_read_errors_total", + Help: "The number of File-SD read errors.", + }) + fileSDTimeStamp = NewTimestampCollector() + patFileSDName = regexp.MustCompile(`^[^*]*(\*[^/]*)?\.(json|yml|yaml|JSON|YML|YAML)$`) // DefaultSDConfig is the default file SD configuration. @@ -49,6 +62,7 @@ var ( func init() { discovery.RegisterConfig(&SDConfig{}) + prometheus.MustRegister(fileSDScanDuration, fileSDReadErrorsCount, fileSDTimeStamp) } // SDConfig is the configuration for file based discovery. @@ -153,27 +167,6 @@ func NewTimestampCollector() *TimestampCollector { } } -var ( - fileSDScanDuration = prometheus.NewSummary( - prometheus.SummaryOpts{ - Name: "prometheus_sd_file_scan_duration_seconds", - Help: "The duration of the File-SD scan in seconds.", - Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, - }) - fileSDReadErrorsCount = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "prometheus_sd_file_read_errors_total", - Help: "The number of File-SD read errors.", - }) - fileSDTimeStamp = NewTimestampCollector() -) - -func init() { - prometheus.MustRegister(fileSDScanDuration) - prometheus.MustRegister(fileSDReadErrorsCount) - prometheus.MustRegister(fileSDTimeStamp) -} - // Discovery provides service discovery functionality based // on files that contain target groups in JSON or YAML format. Refreshing // happens using file watches and periodic refreshes.