Skip to content

Commit

Permalink
feat(output/prometheus-sd): add ip-publish as option
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Apr 13, 2023
1 parent 7da4dc7 commit 6b4b954
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
38 changes: 26 additions & 12 deletions output/prometheus-sd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package prometheus_sd
import (
"errors"
"fmt"
"net"
"strconv"

"github.com/FreifunkBremen/yanic/output"
Expand All @@ -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
},
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -136,7 +150,7 @@ func toTargets(n *runtime.Node, defaultLabels map[string]interface{}, targetFunc
}
}
return &Targets{
Targets: []string{target},
Targets: target,
Labels: labels,
}
}
Expand Down
26 changes: 20 additions & 6 deletions output/prometheus-sd/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ func TestOutput(t *testing.T) {
},
})

// NodeID
out, err = Register(map[string]interface{}{
"target_address": "node_id",
"path": "/tmp/prometheus_sd.json",
"labels": map[string]interface{}{
"hosts": "ffhb",
"service": "yanic",
},
})
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
out, err = Register(map[string]interface{}{
"path": "/tmp/prometheus_sd.json",
Expand All @@ -71,14 +88,10 @@ func TestOutput(t *testing.T) {
_, err = os.Stat("/tmp/prometheus_sd.json")
assert.NoError(err)

// NodeID
// IP-Publish
out, err = Register(map[string]interface{}{
"target_address": "node_id",
"path": "/tmp/prometheus_sd.json",
"labels": map[string]interface{}{
"hosts": "ffhb",
"service": "yanic",
},
"target_address": "ip-publish",
})
os.Remove("/tmp/prometheus_sd.json")
assert.NoError(err)
Expand All @@ -87,4 +100,5 @@ func TestOutput(t *testing.T) {
out.Save(nodes)
_, err = os.Stat("/tmp/prometheus_sd.json")
assert.NoError(err)

}

0 comments on commit 6b4b954

Please sign in to comment.