Fix ip_protocol_fallback default value (#436)
authorPascal Gauthier <pgauthier@nihilisme.ca>
Thu, 14 Mar 2019 14:21:37 +0000 (10:21 -0400)
committerBrian Brazil <brian.brazil@robustperception.io>
Thu, 14 Mar 2019 14:21:37 +0000 (14:21 +0000)
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 <pgauthier@nihilisme.ca>
config/config.go
prober/http_test.go
prober/tcp_test.go
prober/utils.go

index 3652f86f8f0da65e1112f118b1778958e68b2040..6877997136f2f0239fa5113f57f63bb7b4fb9592 100644 (file)
@@ -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
index b45c96b20348095a3131cf85b4c6e149be78d3ac..5dc86579a07002706d26a954f90bcbb1c5207e75 100644 (file)
@@ -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.")
index 0e3709c161bb6556c92bbc88379bb6d873f727e1..480d1f5896a81eefd31aa65932a01f7b2a197a54 100644 (file)
@@ -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",
                },
        }
 
index 12e3ad38661b96fa2e91a3ba1cb6b464b6a32b75..971a25e333a50fac07f56bbffea44d21d7a89651 100644 (file)
@@ -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
                }
        }