Skip to content

Commit b71af40

Browse files
author
Daniel Salbert
committed
simplify parse method and update README.md
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.
1 parent ffd0b35 commit b71af40

File tree

2 files changed

+37
-69
lines changed

2 files changed

+37
-69
lines changed

plugins/inputs/fluentd/README.md

+12-13
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ This plugin understands data provided by /api/plugin.json resource (/api/config.
88
```toml
99
# Read metrics exposed by fluentd in_monitor plugin
1010
[[inputs.fluentd]]
11-
## This plugin only reads information exposed by fluentd using /api/plugins.json.
12-
## Tested using 'fluentd' version '0.14.9'
13-
##
14-
## Endpoint:
15-
## - only one URI is allowed
16-
## - https is not supported
17-
endpoint = "http://localhost:24220/api/plugins.json"
18-
19-
## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
20-
exclude = [
21-
"monitor_agent",
22-
"dummy",
23-
]
11+
## This plugin reads information exposed by fluentd (using /api/plugins.json endpoint).
12+
##
13+
## Endpoint:
14+
## - only one URI is allowed
15+
## - https is not supported
16+
endpoint = "http://localhost:24220/api/plugins.json"
17+
18+
## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
19+
exclude = [
20+
"monitor_agent",
21+
"dummy",
22+
]
2423
```
2524

2625
### Measurements & Fields:

plugins/inputs/fluentd/fluentd.go

+25-56
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io/ioutil"
77
"net/http"
88
"net/url"
9-
"reflect"
109
"time"
1110

1211
"github.com/influxdata/telegraf"
@@ -17,18 +16,18 @@ const (
1716
measurement = "fluentd"
1817
description = "Read metrics exposed by fluentd in_monitor plugin"
1918
sampleConfig = `
20-
## This plugin only reads information exposed by fluentd using /api/plugins.json.
21-
##
22-
## Endpoint:
23-
## - only one URI is allowed
24-
## - https is not supported
25-
endpoint = "http://localhost:24220/api/plugins.json"
26-
27-
## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
28-
exclude = [
29-
"monitor_agent",
30-
"dummy",
31-
]
19+
## This plugin reads information exposed by fluentd (using /api/plugins.json endpoint).
20+
##
21+
## Endpoint:
22+
## - only one URI is allowed
23+
## - https is not supported
24+
endpoint = "http://localhost:24220/api/plugins.json"
25+
26+
## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
27+
exclude = [
28+
"monitor_agent",
29+
"dummy",
30+
]
3231
`
3332
)
3433

@@ -39,6 +38,10 @@ type Fluentd struct {
3938
client *http.Client
4039
}
4140

41+
type endpointInfo struct {
42+
Payload []pluginData `json:"plugins"`
43+
}
44+
4245
type pluginData struct {
4346
PluginID string `json:"plugin_id"`
4447
PluginType string `json:"type"`
@@ -55,53 +58,19 @@ type pluginData struct {
5558
// Returns:
5659
// pluginData: slice that contains parsed plugins
5760
// error: error that may have occurred
58-
func parse(data []byte) ([]pluginData, error) {
59-
var (
60-
pdPoint pluginData
61-
pdPointArray []pluginData
62-
parsed map[string]interface{}
63-
err error
64-
)
65-
66-
if err = json.Unmarshal(data, &parsed); err != nil {
67-
return pdPointArray, err
68-
}
69-
70-
switch parsed["plugins"].(type) {
71-
case []interface{}:
72-
// Iterate through all plugins in array
73-
for _, plugin := range parsed["plugins"].([]interface{}) {
74-
75-
tmpInterface := make(map[string]interface{})
76-
77-
// Go through all fields in plugin
78-
for name, value := range plugin.(map[string]interface{}) {
79-
80-
tags := reflect.ValueOf(pdPoint)
81-
// Iterate through pluginData structure and assign field in case
82-
// when we have field that name is coresponing with field tagged in JSON structure
83-
for i := 0; i < tags.Type().NumField(); i++ {
84-
if tag, ok := tags.Type().Field(i).Tag.Lookup("json"); ok {
85-
if tag == name && value != nil {
86-
tmpInterface[tag] = value
87-
}
88-
}
89-
}
90-
}
61+
func parse(data []byte) (datapointArray []pluginData, err error) {
62+
var endpointData endpointInfo
9163

92-
// Marshal each plugin and Unmarshal it to fit into pluginData structure
93-
tmpByte, err := json.Marshal(tmpInterface)
94-
if err = json.Unmarshal(tmpByte, &pdPoint); err != nil {
95-
return pdPointArray, fmt.Errorf("Processing JSON structure")
96-
}
64+
if err = json.Unmarshal(data, &endpointData); err != nil {
65+
err = fmt.Errorf("Processing JSON structure")
66+
return
67+
}
9768

98-
pdPointArray = append(pdPointArray, pdPoint)
99-
}
100-
default:
101-
return pdPointArray, fmt.Errorf("Unknown JSON structure")
69+
for _, point := range endpointData.Payload {
70+
datapointArray = append(datapointArray, point)
10271
}
10372

104-
return pdPointArray, err
73+
return
10574
}
10675

10776
// Description - display description

0 commit comments

Comments
 (0)