Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow specification of source address for probes (http, grpc) #1097

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ modules:
[ preferred_ip_protocol: <string> | default = "ip6" ]
[ ip_protocol_fallback: <boolean> | default = true ]

# The source IP address.
[ source_ip_address: <string> ]

# The body of the HTTP request used in probe.
[ body: <string> ]

Expand Down Expand Up @@ -310,6 +313,9 @@ validate_additional_rrs:
[ preferred_ip_protocol: <string> ]
[ ip_protocol_fallback: <boolean> | default = true ]

# The source IP address.
[ source_ip_address: <string> ]

# Whether to connect to the endpoint with TLS.
[ tls: <boolean | default = false> ]

Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type HTTPProbe struct {
ValidHTTPVersions []string `yaml:"valid_http_versions,omitempty"`
IPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
SourceIPAddress string `yaml:"source_ip_address,omitempty"`
SkipResolvePhaseWithProxy bool `yaml:"skip_resolve_phase_with_proxy,omitempty"`
NoFollowRedirects *bool `yaml:"no_follow_redirects,omitempty"`
FailIfSSL bool `yaml:"fail_if_ssl,omitempty"`
Expand All @@ -230,6 +231,7 @@ type GRPCProbe struct {
TLS bool `yaml:"tls,omitempty"`
TLSConfig config.TLSConfig `yaml:"tls_config,omitempty"`
IPProtocolFallback bool `yaml:"ip_protocol_fallback,omitempty"`
SourceIPAddress string `yaml:"source_ip_address,omitempty"`
PreferredIPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
}

Expand Down
24 changes: 20 additions & 4 deletions prober/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ package prober

import (
"context"
"net"
"net/url"
"strings"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/blackbox_exporter/config"
Expand All @@ -27,10 +32,6 @@ import (
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"net"
"net/url"
"strings"
"time"
)

type GRPCHealthCheck interface {
Expand Down Expand Up @@ -182,6 +183,21 @@ func ProbeGRPC(ctx context.Context, target string, module config.Module, registr
}
}

// Use configured SourceIPAddress.
if len(module.GRPC.SourceIPAddress) > 0 {
srcIP := net.ParseIP(module.GRPC.SourceIPAddress)
if srcIP == nil {
level.Error(logger).Log("msg", "Error parsing source ip address", "srcIP", module.GRPC.SourceIPAddress)
return false
}
level.Info(logger).Log("msg", "Using local address", "srcIP", srcIP)
opts = append(opts, grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{
LocalAddr: &net.IPAddr{IP: srcIP},
}).DialContext(ctx, "tcp", addr)
}))
}

conn, err := grpc.Dial(target, opts...)

if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
level.Error(logger).Log("msg", "Error generating HTTP client", "err", err)
return false
}
// Use configured SourceIPAddress.
if len(module.HTTP.SourceIPAddress) > 0 {
srcIP := net.ParseIP(module.HTTP.SourceIPAddress)
if srcIP == nil {
level.Error(logger).Log("msg", "Error parsing source ip address", "srcIP", module.HTTP.SourceIPAddress)
return false
}
level.Info(logger).Log("msg", "Using local address", "srcIP", srcIP)
client, err = pconfig.NewClientFromConfig(httpClientConfig, "http_probe", pconfig.WithKeepAlivesDisabled(),
pconfig.WithDialContextFunc((&net.Dialer{
LocalAddr: &net.IPAddr{IP: srcIP},
}).DialContext))
if err != nil {
level.Error(logger).Log("msg", "Error generating HTTP client", "err", err)
return false
}
}

httpClientConfig.TLSConfig.ServerName = ""
noServerName, err := pconfig.NewRoundTripperFromConfig(httpClientConfig, "http_probe", pconfig.WithKeepAlivesDisabled())
Expand Down