influxdb/telegraf/plugins/inputs/prometheus.go

48 lines
1.0 KiB
Go
Raw Normal View History

2018-10-05 21:43:01 +00:00
package inputs
import (
2018-10-17 19:51:35 +00:00
"errors"
2018-10-05 21:43:01 +00:00
"fmt"
"strconv"
"strings"
)
// Prometheus is based on telegraf Prometheus plugin.
type Prometheus struct {
2018-10-16 00:38:36 +00:00
baseInput
2018-10-17 19:51:35 +00:00
URLs []string `json:"urls"`
2018-10-05 21:43:01 +00:00
}
2018-10-16 00:38:36 +00:00
// PluginName is based on telegraf plugin name.
func (p *Prometheus) PluginName() string {
return "prometheus"
}
2018-10-05 21:43:01 +00:00
// TOML encodes to toml string
func (p *Prometheus) TOML() string {
s := make([]string, len(p.URLs))
2018-10-05 21:43:01 +00:00
for k, v := range p.URLs {
2018-10-05 21:43:01 +00:00
s[k] = strconv.Quote(v)
}
2018-10-16 00:38:36 +00:00
return fmt.Sprintf(`[[inputs.%s]]
2018-10-05 21:43:01 +00:00
## An array of urls to scrape metrics from.
urls = [%s]
2018-10-16 00:38:36 +00:00
`, p.PluginName(), strings.Join(s, ", "))
2018-10-05 21:43:01 +00:00
}
2018-10-17 19:51:35 +00:00
// UnmarshalTOML decodes the parsed data to the object
func (p *Prometheus) UnmarshalTOML(data interface{}) error {
dataOK, ok := data.(map[string]interface{})
if !ok {
return errors.New("bad urls for prometheus input plugin")
}
urls, ok := dataOK["urls"].([]interface{})
if !ok {
return errors.New("urls is not an array for prometheus input plugin")
}
for _, url := range urls {
p.URLs = append(p.URLs, url.(string))
}
return nil
}