feat: set a bind address and port with --listen-address

Allow providing a listening address to bind to as well as the port number with
a single new argument.

This is breaking the command line API if `--port` was used as this argument was
removed. For consistency with other exporters and general practices this approach
appeared more robust.
This commit is contained in:
Thibault VINCENT
2021-08-10 17:22:23 +02:00
parent 0b4f573c91
commit 4a1bece219
3 changed files with 11 additions and 11 deletions
+2 -2
View File
@@ -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 <n> 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,
+3 -4
View File
@@ -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
}
+6 -5
View File
@@ -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"}
}