From 52e046c92181f3f504e993947c39baa089a6de36 Mon Sep 17 00:00:00 2001 From: Pascal Gauthier Date: Thu, 14 Mar 2019 10:21:37 -0400 Subject: [PATCH] Fix ip_protocol_fallback default value (#436) Fix for https://github.com/prometheus/blackbox_exporter/issues/424 Also fix an undefined probeIPProtocolGauge in case of no IP found. Signed-off-by: Pascal Gauthier --- config/config.go | 33 +++++++++++++++++++++++++++++++++ prober/http_test.go | 3 ++- prober/tcp_test.go | 13 +++++-------- prober/utils.go | 7 ++++++- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 3652f86..6877997 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,34 @@ var ( Name: "config_last_reload_success_timestamp_seconds", Help: "Timestamp of the last successful configuration reload.", }) + + // DefaultModule set default configuration for the Module + DefaultModule = Module{ + HTTP: DefaultHTTPProbe, + TCP: DefaultTCPProbe, + ICMP: DefaultICMPProbe, + DNS: DefaultDNSProbe, + } + + // DefaultHTTPProbe set default value for HTTPProbe + DefaultHTTPProbe = HTTPProbe{ + IPProtocolFallback: true, + } + + // DefaultTCPProbe set default value for TCPProbe + DefaultTCPProbe = TCPProbe{ + IPProtocolFallback: true, + } + + // DefaultICMPProbe set default value for ICMPProbe + DefaultICMPProbe = ICMPProbe{ + IPProtocolFallback: true, + } + + // DefaultDNSProbe set default value for DNSProbe + DefaultDNSProbe = DNSProbe{ + IPProtocolFallback: true, + } ) func init() { @@ -155,6 +183,7 @@ func (s *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { // UnmarshalYAML implements the yaml.Unmarshaler interface. func (s *Module) UnmarshalYAML(unmarshal func(interface{}) error) error { + *s = DefaultModule type plain Module if err := unmarshal((*plain)(s)); err != nil { return err @@ -164,6 +193,7 @@ func (s *Module) UnmarshalYAML(unmarshal func(interface{}) error) error { // UnmarshalYAML implements the yaml.Unmarshaler interface. func (s *HTTPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error { + *s = DefaultHTTPProbe type plain HTTPProbe if err := unmarshal((*plain)(s)); err != nil { return err @@ -176,6 +206,7 @@ func (s *HTTPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error { // UnmarshalYAML implements the yaml.Unmarshaler interface. func (s *DNSProbe) UnmarshalYAML(unmarshal func(interface{}) error) error { + *s = DefaultDNSProbe type plain DNSProbe if err := unmarshal((*plain)(s)); err != nil { return err @@ -188,6 +219,7 @@ func (s *DNSProbe) UnmarshalYAML(unmarshal func(interface{}) error) error { // UnmarshalYAML implements the yaml.Unmarshaler interface. func (s *TCPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error { + *s = DefaultTCPProbe type plain TCPProbe if err := unmarshal((*plain)(s)); err != nil { return err @@ -206,6 +238,7 @@ func (s *DNSRRValidator) UnmarshalYAML(unmarshal func(interface{}) error) error // UnmarshalYAML implements the yaml.Unmarshaler interface. func (s *ICMPProbe) UnmarshalYAML(unmarshal func(interface{}) error) error { + *s = DefaultICMPProbe type plain ICMPProbe if err := unmarshal((*plain)(s)); err != nil { return err diff --git a/prober/http_test.go b/prober/http_test.go index b45c96b..5dc8657 100644 --- a/prober/http_test.go +++ b/prober/http_test.go @@ -496,7 +496,8 @@ func TestHTTPHeaders(t *testing.T) { testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() result := ProbeHTTP(testCTX, ts.URL, config.Module{Timeout: time.Second, HTTP: config.HTTPProbe{ - Headers: headers, IPProtocolFallback: true, + IPProtocolFallback: true, + Headers: headers, }}, registry, log.NewNopLogger()) if !result { t.Fatalf("Probe failed unexpectedly.") diff --git a/prober/tcp_test.go b/prober/tcp_test.go index 0e3709c..480d1f5 100644 --- a/prober/tcp_test.go +++ b/prober/tcp_test.go @@ -62,7 +62,7 @@ func TestTCPConnectionFails(t *testing.T) { registry := prometheus.NewRegistry() testCTX, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - if ProbeTCP(testCTX, ":0", config.Module{TCP: config.TCPProbe{IPProtocolFallback: true}}, registry, log.NewNopLogger()) { + if ProbeTCP(testCTX, ":0", config.Module{TCP: config.TCPProbe{}}, registry, log.NewNopLogger()) { t.Fatalf("TCP module suceeded, expected failure.") } } @@ -129,9 +129,8 @@ func TestTCPConnectionWithTLS(t *testing.T) { // Expect name-verified TLS connection. module := config.Module{ TCP: config.TCPProbe{ - IPProtocol: "ipv4", - IPProtocolFallback: true, - TLS: true, + IPProtocol: "ipv4", + TLS: true, TLSConfig: pconfig.TLSConfig{ CAFile: tmpCaFile.Name(), InsecureSkipVerify: false, @@ -437,8 +436,7 @@ func TestTCPConnectionProtocol(t *testing.T) { // Force IPv4 module := config.Module{ TCP: config.TCPProbe{ - IPProtocol: "ip4", - IPProtocolFallback: true, + IPProtocol: "ip4", }, } @@ -499,8 +497,7 @@ func TestTCPConnectionProtocol(t *testing.T) { // Prefer IPv6 module = config.Module{ TCP: config.TCPProbe{ - IPProtocol: "ip6", - IPProtocolFallback: true, + IPProtocol: "ip6", }, } diff --git a/prober/utils.go b/prober/utils.go index 12e3ad3..971a25e 100644 --- a/prober/utils.go +++ b/prober/utils.go @@ -50,13 +50,18 @@ func chooseProtocol(IPProtocol string, fallbackIPProtocol bool, target string, r ip, err = net.ResolveIPAddr(IPProtocol, target) if err != nil { if !fallbackIPProtocol { - level.Error(logger).Log("msg", "Resolution with IP protocol failed (fallback_ip_protocol is false): err", err) + level.Error(logger).Log("msg", "Resolution with IP protocol failed (fallback_ip_protocol is false):", "err", err) } else { level.Warn(logger).Log("msg", "Resolution with IP protocol failed, attempting fallback protocol", "fallback_protocol", fallbackProtocol, "err", err) ip, err = net.ResolveIPAddr(fallbackProtocol, target) } if err != nil { + if IPProtocol == "ip6" { + probeIPProtocolGauge.Set(6) + } else { + probeIPProtocolGauge.Set(4) + } return ip, 0.0, err } } -- 2.25.1