From 6b4b9545b19d53d989e2c734dd59e85d000bf1d9 Mon Sep 17 00:00:00 2001 From: genofire Date: Thu, 13 Apr 2023 08:46:11 +0200 Subject: [PATCH] feat(output/prometheus-sd): add ip-publish as option --- output/prometheus-sd/output.go | 38 ++++++++++++++++++++--------- output/prometheus-sd/output_test.go | 38 ++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/output/prometheus-sd/output.go b/output/prometheus-sd/output.go index 696908b..46c22fe 100644 --- a/output/prometheus-sd/output.go +++ b/output/prometheus-sd/output.go @@ -3,6 +3,7 @@ package prometheus_sd import ( "errors" "fmt" + "net" "strconv" "github.com/FreifunkBremen/yanic/output" @@ -28,25 +29,38 @@ func (c Config) Path() string { type TargetAddressType string const ( - TargetAddressIP TargetAddressType = "ip" - TargetAddressNodeID TargetAddressType = "node_id" + TargetAddressNodeID TargetAddressType = "node_id" + TargetAddressIP TargetAddressType = "ip" + TargetAddressIPPublish TargetAddressType = "ip-publish" ) -type TargetAddressFunc func(*runtime.Node) string +type TargetAddressFunc func(*runtime.Node) []string var TargetAddressTypeFuncs = map[TargetAddressType]TargetAddressFunc{ - TargetAddressIP: func(n *runtime.Node) string { + TargetAddressNodeID: func(n *runtime.Node) []string { + if ni := n.Nodeinfo; ni != nil { + return []string{ni.NodeID} + } + return []string{} + }, + TargetAddressIP: func(n *runtime.Node) []string { if addr := n.Address; addr != nil { - return addr.IP.String() + return []string{addr.IP.String()} } - return "" + return []string{} }, - TargetAddressNodeID: func(n *runtime.Node) string { - if ni := n.Nodeinfo; ni != nil { - return ni.NodeID + TargetAddressIPPublish: func(n *runtime.Node) []string { + addresses := []string{} + if nodeinfo := n.Nodeinfo; nodeinfo != nil { + for _, addr := range nodeinfo.Network.Addresses { + if net.ParseIP(addr).IsGlobalUnicast() { + addresses = append(addresses, addr) + } + } + } - return "" + return addresses }, } @@ -89,7 +103,7 @@ type Targets struct { func toTargets(n *runtime.Node, defaultLabels map[string]interface{}, targetFunc TargetAddressFunc) *Targets { target := targetFunc(n) - if target == "" { + if len(target) <= 0 { return nil } @@ -136,7 +150,7 @@ func toTargets(n *runtime.Node, defaultLabels map[string]interface{}, targetFunc } } return &Targets{ - Targets: []string{target}, + Targets: target, Labels: labels, } } diff --git a/output/prometheus-sd/output_test.go b/output/prometheus-sd/output_test.go index 69af53d..8cbfe9a 100644 --- a/output/prometheus-sd/output_test.go +++ b/output/prometheus-sd/output_test.go @@ -59,18 +59,6 @@ func TestOutput(t *testing.T) { }, }) - // IP - out, err = Register(map[string]interface{}{ - "path": "/tmp/prometheus_sd.json", - }) - os.Remove("/tmp/prometheus_sd.json") - assert.NoError(err) - assert.NotNil(out) - - out.Save(nodes) - _, err = os.Stat("/tmp/prometheus_sd.json") - assert.NoError(err) - // NodeID out, err = Register(map[string]interface{}{ "target_address": "node_id", @@ -87,4 +75,30 @@ func TestOutput(t *testing.T) { out.Save(nodes) _, err = os.Stat("/tmp/prometheus_sd.json") assert.NoError(err) + + // IP + out, err = Register(map[string]interface{}{ + "path": "/tmp/prometheus_sd.json", + }) + os.Remove("/tmp/prometheus_sd.json") + assert.NoError(err) + assert.NotNil(out) + + out.Save(nodes) + _, err = os.Stat("/tmp/prometheus_sd.json") + assert.NoError(err) + + // IP-Publish + out, err = Register(map[string]interface{}{ + "path": "/tmp/prometheus_sd.json", + "target_address": "ip-publish", + }) + os.Remove("/tmp/prometheus_sd.json") + assert.NoError(err) + assert.NotNil(out) + + out.Save(nodes) + _, err = os.Stat("/tmp/prometheus_sd.json") + assert.NoError(err) + }