go fmt run over jolokia.go

This commit is contained in:
saiello 2015-10-29 14:48:39 +01:00 committed by Cameron Sparr
parent 40d8aeecb0
commit 62270a3697
1 changed files with 71 additions and 78 deletions

View File

@ -13,29 +13,26 @@ import (
"github.com/influxdb/telegraf/plugins" "github.com/influxdb/telegraf/plugins"
) )
type Server struct { type Server struct {
Name string Name string
Host string Host string
Port string Port string
} }
type Metric struct { type Metric struct {
Name string Name string
Jmx string Jmx string
Pass []string Pass []string
Drop []string Drop []string
} }
type Jolokia struct { type Jolokia struct {
Context string
Context string Servers []Server
Servers []Server Metrics []Metric
Metrics []Metric Tags map[string]string
Tags map[string]string
} }
func (j *Jolokia) SampleConfig() string { func (j *Jolokia) SampleConfig() string {
return `[jolokia] return `[jolokia]
context = "/jolokia/read" context = "/jolokia/read"
@ -70,100 +67,96 @@ func (j *Jolokia) Description() string {
return "Read JMX metrics through Jolokia" return "Read JMX metrics through Jolokia"
} }
func getAttr(requestUrl *url.URL) (map[string]interface{}, error) { func getAttr(requestUrl *url.URL) (map[string]interface{}, error) {
//make request //make request
resp, err := http.Get(requestUrl.String()) resp, err := http.Get(requestUrl.String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
// Process response // Process response
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)", err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)",
requestUrl, requestUrl,
resp.StatusCode, resp.StatusCode,
http.StatusText(resp.StatusCode), http.StatusText(resp.StatusCode),
http.StatusOK, http.StatusOK,
http.StatusText(http.StatusOK)) http.StatusText(http.StatusOK))
return nil, err return nil, err
} }
// read body // read body
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Unmarshal json // Unmarshal json
var jsonOut map[string]interface{} var jsonOut map[string]interface{}
if err = json.Unmarshal([]byte(body), &jsonOut); err != nil { if err = json.Unmarshal([]byte(body), &jsonOut); err != nil {
return nil, errors.New("Error decoding JSON response") return nil, errors.New("Error decoding JSON response")
} }
return jsonOut, nil return jsonOut, nil
} }
func (m *Metric) shouldPass(field string) bool { func (m *Metric) shouldPass(field string) bool {
if m.Pass != nil { if m.Pass != nil {
for _, pass := range m.Pass{ for _, pass := range m.Pass {
if strings.HasPrefix(field, pass) { if strings.HasPrefix(field, pass) {
return true return true
} }
} }
return false return false
} }
if m.Drop != nil { if m.Drop != nil {
for _, drop := range m.Drop{ for _, drop := range m.Drop {
if strings.HasPrefix(field, drop) { if strings.HasPrefix(field, drop) {
return false return false
} }
} }
return true return true
} }
return true return true
} }
func (m *Metric) filterFields(fields map[string]interface{}) map[string]interface{} { func (m *Metric) filterFields(fields map[string]interface{}) map[string]interface{} {
for field, _ := range fields{ for field, _ := range fields {
if !m.shouldPass(field) { if !m.shouldPass(field) {
delete(fields, field) delete(fields, field)
} }
} }
return fields return fields
} }
func (j *Jolokia) Gather(acc plugins.Accumulator) error { func (j *Jolokia) Gather(acc plugins.Accumulator) error {
context := j.Context //"/jolokia/read" context := j.Context //"/jolokia/read"
servers := j.Servers servers := j.Servers
metrics := j.Metrics metrics := j.Metrics
tags := j.Tags tags := j.Tags
if tags == nil{ if tags == nil {
tags = map[string]string{} tags = map[string]string{}
} }
for _, server := range servers {
for _, metric := range metrics {
for _, server := range servers { measurement := metric.Name
for _, metric := range metrics { jmxPath := metric.Jmx
measurement := metric.Name tags["server"] = server.Name
jmxPath := metric.Jmx
tags["server"] = server.Name
tags["port"] = server.Port tags["port"] = server.Port
tags["host"] = server.Host tags["host"] = server.Host
@ -173,22 +166,22 @@ func (j *Jolokia) Gather(acc plugins.Accumulator) error {
return err return err
} }
out, _ := getAttr(requestUrl) out, _ := getAttr(requestUrl)
if values, ok := out["value"]; ok { if values, ok := out["value"]; ok {
switch values.(type) { switch values.(type) {
case map[string]interface{}: case map[string]interface{}:
acc.AddFields(measurement, metric.filterFields(values.(map[string]interface{})), tags) acc.AddFields(measurement, metric.filterFields(values.(map[string]interface{})), tags)
case interface{}: case interface{}:
acc.Add(measurement, values.(interface{}), tags) acc.Add(measurement, values.(interface{}), tags)
} }
}else{ } else {
fmt.Printf("Missing key 'value' in '%s' output response\n", requestUrl.String()) fmt.Printf("Missing key 'value' in '%s' output response\n", requestUrl.String())
} }
} }
} }
return nil return nil
} }
func init() { func init() {