Skip to content

Commit

Permalink
multiple labels support for net_response (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongfei605 authored Jun 13, 2023
1 parent 2677b22 commit 6b6883e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions conf/input.net_response/net_response.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# # collect interval
# interval = 15

[mappings]
# "127.0.0.1:22"= {region="local",ssh="test"}
# "127.0.0.1:22"= {region="local",ssh="redis"}

[[instances]]
targets = [
# "127.0.0.1:22",
Expand Down
21 changes: 21 additions & 0 deletions inputs/net_response/net_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Instance struct {
ReadTimeout config.Duration `toml:"read_timeout"`
Send string `toml:"send"`
Expect string `toml:"expect"`

Mappings map[string]map[string]string `toml:"mappings"`
}

func (ins *Instance) Init() error {
Expand Down Expand Up @@ -85,6 +87,8 @@ func (ins *Instance) Init() error {
type NetResponse struct {
config.PluginConfig
Instances []*Instance `toml:"instances"`

Mappings map[string]map[string]string `toml:"mappings"`
}

func init() {
Expand All @@ -104,6 +108,18 @@ func (n *NetResponse) Name() string {
func (n *NetResponse) GetInstances() []inputs.Instance {
ret := make([]inputs.Instance, len(n.Instances))
for i := 0; i < len(n.Instances); i++ {
if len(n.Instances[i].Mappings) == 0 {
n.Instances[i].Mappings = n.Mappings
} else {
m := make(map[string]map[string]string)
for k, v := range n.Mappings {
m[k] = v
}
for k, v := range n.Instances[i].Mappings {
m[k] = v
}
n.Instances[i].Mappings = m
}
ret[i] = n.Instances[i]
}
return ret
Expand Down Expand Up @@ -132,6 +148,11 @@ func (ins *Instance) gather(slist *types.SampleList, target string) {

labels := map[string]string{"target": target}
fields := map[string]interface{}{}
if m, ok := ins.Mappings[target]; ok {
for k, v := range m {
labels[k] = v
}
}

defer func() {
for field, value := range fields {
Expand Down

0 comments on commit 6b6883e

Please sign in to comment.