diff --git a/cmd/x509-certificate-exporter/main.go b/cmd/x509-certificate-exporter/main.go index 3fc3d38..01fdb87 100644 --- a/cmd/x509-certificate-exporter/main.go +++ b/cmd/x509-certificate-exporter/main.go @@ -16,7 +16,7 @@ import ( func main() { help := getopt.BoolLong("help", 'h', "show this help message and exit") version := getopt.BoolLong("version", 'v', "show version info and exit") - port := getopt.IntLong("port", 'p', 9793, "prometheus exporter listening port") + listenAddress := getopt.StringLong("listen-address", 'b', ":9793", "address on which to bind and expose metrics") debug := getopt.BoolLong("debug", 0, "enable debug mode") trimPathComponents := getopt.IntLong("trim-path-components", 0, 0, "remove leading component(s) from path(s) in label(s)") exposeRelativeMetrics := getopt.BoolLong("expose-relative-metrics", 0, "expose additionnal metrics with relative durations instead of absolute timestamps") @@ -69,7 +69,7 @@ func main() { } exporter := internal.Exporter{ - Port: *port, + ListenAddress: *listenAddress, Files: files, Directories: directories, YAMLs: yamls, diff --git a/internal/exporter.go b/internal/exporter.go index 5a937b3..699f95f 100644 --- a/internal/exporter.go +++ b/internal/exporter.go @@ -18,7 +18,7 @@ import ( // Exporter : Configuration (from command-line) type Exporter struct { - Port int + ListenAddress string Files []string Directories []string YAMLs []string @@ -65,10 +65,9 @@ func (exporter *Exporter) Listen() error { } } - listen := fmt.Sprintf(":%d", exporter.Port) - log.Infof("listening on %s", listen) + log.Infof("listening on %s", exporter.ListenAddress) - listener, err := net.Listen("tcp", listen) + listener, err := net.Listen("tcp", exporter.ListenAddress) if err != nil { return err } diff --git a/internal/exporter_test.go b/internal/exporter_test.go index 1702a4a..a93d220 100644 --- a/internal/exporter_test.go +++ b/internal/exporter_test.go @@ -28,14 +28,15 @@ import ( "github.com/stretchr/testify/assert" ) +const listenAddress = "0.0.0.0:9793" const port = 9793 func TestRegularStartup(t *testing.T) { _, filename, _, _ := runtime.Caller(0) e := &Exporter{ - Port: port, - Files: []string{path.Join(filepath.Dir(filename), "../test/basic.pem")}, + ListenAddress: listenAddress, + Files: []string{path.Join(filepath.Dir(filename), "../test/basic.pem")}, } go e.ListenAndServe() @@ -357,8 +358,8 @@ func TestErrorMetrics(t *testing.T) { } func TestBindAddrAlreadyInUse(t *testing.T) { - listener, _ := net.Listen("tcp", ":9793") - e := &Exporter{Port: 9793} + listener, _ := net.Listen("tcp", listenAddress) + e := &Exporter{ListenAddress: listenAddress} err := e.ListenAndServe() listener.Close() assert.NotNil(t, err, "no error was returned for bind failure") @@ -707,7 +708,7 @@ func checkLabels(t *testing.T, labels []*model.LabelPair, path string, isKube bo } func testRequest(t *testing.T, exporter *Exporter, cb func(metrics []model.MetricFamily)) { - exporter.Port = port + exporter.ListenAddress = listenAddress if exporter.KubeSecretTypes == nil { exporter.KubeSecretTypes = []string{"kubernetes.io/tls:tls.crt"} }