Skip to content

Commit

Permalink
add output prometheus-sd (service discovery)
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Apr 13, 2022
1 parent 64b9cfe commit 62d708b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestReadConfig(t *testing.T) {
assert.Contains(config.Respondd.Sites["ffhb"].Domains, "city")

// Test output plugins
assert.Len(config.Nodes.Output, 5)
assert.Len(config.Nodes.Output, 6)
outputs := config.Nodes.Output["meshviewer"].([]map[string]interface{})
assert.Len(outputs, 1)
meshviewer := outputs[0]
Expand Down
10 changes: 10 additions & 0 deletions config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ path = "/var/www/html/meshviewer/data/nodelist.json"
# WARNING: if it is not set, it will publish contact information of other persons
no_owner = true

# definition for prometheus-sd.json
[[nodes.output.prometheus-sd]]
enable = true
path = "/var/www/html/meshviewer/data/prometheus-sd.json"
# ip = lates recieved ip, node_id = node id from host
target_address = "ip"

[nodes.output.prometheus-sd.labels]
hosts = "ffhb"
service = "yanic"

# definition for raw.json
[[nodes.output.raw]]
Expand Down
1 change: 1 addition & 0 deletions output/all/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "yanic/output/meshviewer"
_ "yanic/output/meshviewer-ffrgb"
_ "yanic/output/nodelist"
_ "yanic/output/prometheus-sd"
_ "yanic/output/raw"
_ "yanic/output/raw-jsonl"
)
93 changes: 93 additions & 0 deletions output/prometheus-sd/output.go
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)
}
28 changes: 28 additions & 0 deletions output/prometheus-sd/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package prometheus_sd

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"yanic/runtime"
)

func TestOutput(t *testing.T) {
assert := assert.New(t)

out, err := Register(map[string]interface{}{})
assert.Error(err)
assert.Nil(out)

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(&runtime.Nodes{})
_, err = os.Stat("/tmp/prometheus_sd.json")
assert.NoError(err)
}

0 comments on commit 62d708b

Please sign in to comment.