feat(output/prometheus-sd): add ip-publish as option

This commit is contained in:
genofire 2023-04-13 08:46:11 +02:00
parent 7da4dc7d7f
commit 6b4b9545b1
2 changed files with 52 additions and 24 deletions

View File

@ -3,6 +3,7 @@ package prometheus_sd
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"strconv" "strconv"
"github.com/FreifunkBremen/yanic/output" "github.com/FreifunkBremen/yanic/output"
@ -28,25 +29,38 @@ func (c Config) Path() string {
type TargetAddressType string type TargetAddressType string
const ( 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{ 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 { if addr := n.Address; addr != nil {
return addr.IP.String() return []string{addr.IP.String()}
} }
return "" return []string{}
}, },
TargetAddressNodeID: func(n *runtime.Node) string { TargetAddressIPPublish: func(n *runtime.Node) []string {
if ni := n.Nodeinfo; ni != nil { addresses := []string{}
return ni.NodeID 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 { func toTargets(n *runtime.Node, defaultLabels map[string]interface{}, targetFunc TargetAddressFunc) *Targets {
target := targetFunc(n) target := targetFunc(n)
if target == "" { if len(target) <= 0 {
return nil return nil
} }
@ -136,7 +150,7 @@ func toTargets(n *runtime.Node, defaultLabels map[string]interface{}, targetFunc
} }
} }
return &Targets{ return &Targets{
Targets: []string{target}, Targets: target,
Labels: labels, Labels: labels,
} }
} }

View File

@ -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 // NodeID
out, err = Register(map[string]interface{}{ out, err = Register(map[string]interface{}{
"target_address": "node_id", "target_address": "node_id",
@ -87,4 +75,30 @@ func TestOutput(t *testing.T) {
out.Save(nodes) out.Save(nodes)
_, err = os.Stat("/tmp/prometheus_sd.json") _, err = os.Stat("/tmp/prometheus_sd.json")
assert.NoError(err) 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)
} }