Skip to content

Commit

Permalink
simplify parse method and update README.md
Browse files Browse the repository at this point in the history
As danielnelson noticed, there is no need to use relfect and we can simplify whole parser using just Unmarshal method from json library, what I have done.
  • Loading branch information
Daniel Salbert committed Jul 13, 2017
1 parent ffd0b35 commit b71af40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 69 deletions.
25 changes: 12 additions & 13 deletions plugins/inputs/fluentd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ This plugin understands data provided by /api/plugin.json resource (/api/config.
```toml
# Read metrics exposed by fluentd in_monitor plugin
[[inputs.fluentd]]
## This plugin only reads information exposed by fluentd using /api/plugins.json.
## Tested using 'fluentd' version '0.14.9'
##
## Endpoint:
## - only one URI is allowed
## - https is not supported
endpoint = "http://localhost:24220/api/plugins.json"

## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
exclude = [
"monitor_agent",
"dummy",
]
## This plugin reads information exposed by fluentd (using /api/plugins.json endpoint).
##
## Endpoint:
## - only one URI is allowed
## - https is not supported
endpoint = "http://localhost:24220/api/plugins.json"

## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
exclude = [
"monitor_agent",
"dummy",
]
```

### Measurements & Fields:
Expand Down
81 changes: 25 additions & 56 deletions plugins/inputs/fluentd/fluentd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"net/http"
"net/url"
"reflect"
"time"

"github.com/influxdata/telegraf"
Expand All @@ -17,18 +16,18 @@ const (
measurement = "fluentd"
description = "Read metrics exposed by fluentd in_monitor plugin"
sampleConfig = `
## This plugin only reads information exposed by fluentd using /api/plugins.json.
##
## Endpoint:
## - only one URI is allowed
## - https is not supported
endpoint = "http://localhost:24220/api/plugins.json"
## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
exclude = [
"monitor_agent",
"dummy",
]
## This plugin reads information exposed by fluentd (using /api/plugins.json endpoint).
##
## Endpoint:
## - only one URI is allowed
## - https is not supported
endpoint = "http://localhost:24220/api/plugins.json"
## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
exclude = [
"monitor_agent",
"dummy",
]
`
)

Expand All @@ -39,6 +38,10 @@ type Fluentd struct {
client *http.Client
}

type endpointInfo struct {
Payload []pluginData `json:"plugins"`
}

type pluginData struct {
PluginID string `json:"plugin_id"`
PluginType string `json:"type"`
Expand All @@ -55,53 +58,19 @@ type pluginData struct {
// Returns:
// pluginData: slice that contains parsed plugins
// error: error that may have occurred
func parse(data []byte) ([]pluginData, error) {
var (
pdPoint pluginData
pdPointArray []pluginData
parsed map[string]interface{}
err error
)

if err = json.Unmarshal(data, &parsed); err != nil {
return pdPointArray, err
}

switch parsed["plugins"].(type) {
case []interface{}:
// Iterate through all plugins in array
for _, plugin := range parsed["plugins"].([]interface{}) {

tmpInterface := make(map[string]interface{})

// Go through all fields in plugin
for name, value := range plugin.(map[string]interface{}) {

tags := reflect.ValueOf(pdPoint)
// Iterate through pluginData structure and assign field in case
// when we have field that name is coresponing with field tagged in JSON structure
for i := 0; i < tags.Type().NumField(); i++ {
if tag, ok := tags.Type().Field(i).Tag.Lookup("json"); ok {
if tag == name && value != nil {
tmpInterface[tag] = value
}
}
}
}
func parse(data []byte) (datapointArray []pluginData, err error) {
var endpointData endpointInfo

// Marshal each plugin and Unmarshal it to fit into pluginData structure
tmpByte, err := json.Marshal(tmpInterface)
if err = json.Unmarshal(tmpByte, &pdPoint); err != nil {
return pdPointArray, fmt.Errorf("Processing JSON structure")
}
if err = json.Unmarshal(data, &endpointData); err != nil {
err = fmt.Errorf("Processing JSON structure")
return
}

pdPointArray = append(pdPointArray, pdPoint)
}
default:
return pdPointArray, fmt.Errorf("Unknown JSON structure")
for _, point := range endpointData.Payload {
datapointArray = append(datapointArray, point)
}

return pdPointArray, err
return
}

// Description - display description
Expand Down

0 comments on commit b71af40

Please sign in to comment.