-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] add output prometheus-sd (service discovery) (#213)
- Loading branch information
Showing
6 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package prometheus_sd | ||
|
||
import ( | ||
"errors" | ||
|
||
"yanic/output" | ||
"yanic/runtime" | ||
) | ||
|
||
type Output struct { | ||
output.Output | ||
path string | ||
targetType TargetAddressType | ||
labels map[string]interface{} | ||
} | ||
|
||
type Config map[string]interface{} | ||
|
||
func (c Config) Path() string { | ||
if path, ok := c["path"]; ok { | ||
return path.(string) | ||
} | ||
return "" | ||
} | ||
|
||
type TargetAddressType string | ||
|
||
const ( | ||
TargetAddressIP TargetAddressType = "ip" | ||
TargetAddressNodeID TargetAddressType = "node_id" | ||
) | ||
|
||
func (c Config) TargetAddress() TargetAddressType { | ||
if v, ok := c["target_address"]; ok { | ||
return TargetAddressType(v.(string)) | ||
} | ||
return TargetAddressIP | ||
} | ||
|
||
func (c Config) Labels() map[string]interface{} { | ||
if v, ok := c["labels"]; ok { | ||
return v.(map[string]interface{}) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
output.RegisterAdapter("prometheus-sd", Register) | ||
} | ||
|
||
func Register(configuration map[string]interface{}) (output.Output, error) { | ||
config := Config(configuration) | ||
|
||
if path := config.Path(); path != "" { | ||
return &Output{ | ||
path: path, | ||
targetType: config.TargetAddress(), | ||
labels: config.Labels(), | ||
}, nil | ||
} | ||
return nil, errors.New("no path given") | ||
|
||
} | ||
|
||
type Targets struct { | ||
Targets []string `json:"targets"` | ||
Labels map[string]interface{} `json:"labels,omitempty"` | ||
} | ||
|
||
func (o *Output) Save(nodes *runtime.Nodes) { | ||
nodes.RLock() | ||
defer nodes.RUnlock() | ||
|
||
targets := &Targets{ | ||
Targets: []string{}, | ||
Labels: o.labels, | ||
} | ||
if o.targetType == TargetAddressNodeID { | ||
for _, n := range nodes.List { | ||
if ni := n.Nodeinfo; ni != nil { | ||
targets.Targets = append(targets.Targets, ni.NodeID) | ||
} | ||
} | ||
} else { | ||
for _, n := range nodes.List { | ||
if addr := n.Address; addr != nil { | ||
targets.Targets = append(targets.Targets, addr.IP.String()) | ||
} | ||
} | ||
} | ||
|
||
runtime.SaveJSON([]interface{}{targets}, o.path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package prometheus_sd | ||
|
||
import ( | ||
"os" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"yanic/data" | ||
"yanic/runtime" | ||
) | ||
|
||
func TestOutput(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
out, err := Register(map[string]interface{}{}) | ||
assert.Error(err) | ||
assert.Nil(out) | ||
|
||
nodes := runtime.NewNodes(&runtime.NodesConfig{}) | ||
ipAddress, err := net.ResolveUDPAddr("udp6", "[fe80::20de:a:3ac%eth0]:1001") | ||
assert.NoError(err) | ||
nodes.AddNode(&runtime.Node{ | ||
Online: true, | ||
Address: ipAddress, | ||
Nodeinfo: &data.Nodeinfo{ | ||
NodeID: "node_a", | ||
Network: data.Network{ | ||
Mac: "node:a:mac", | ||
Mesh: map[string]*data.NetworkInterface{ | ||
"bat0": { | ||
Interfaces: struct { | ||
Wireless []string `json:"wireless,omitempty"` | ||
Other []string `json:"other,omitempty"` | ||
Tunnel []string `json:"tunnel,omitempty"` | ||
}{ | ||
Wireless: []string{"node:a:mac:wifi"}, | ||
Tunnel: []string{"node:a:mac:vpn"}, | ||
Other: []string{"node:a:mac:lan"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Neighbours: &data.Neighbours{ | ||
NodeID: "node_a", | ||
Batadv: map[string]data.BatadvNeighbours{ | ||
"node:a:mac:wifi": { | ||
Neighbours: map[string]data.BatmanLink{ | ||
"node:b:mac:wifi": {Tq: 153}, | ||
}, | ||
}, | ||
"node:a:mac:lan": { | ||
Neighbours: map[string]data.BatmanLink{ | ||
"node:b:mac:lan": {Tq: 51}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
// 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", | ||
"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) | ||
} |