-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fluentd input plugin #2661
Merged
danielnelson
merged 7 commits into
influxdata:master
from
dsalbert:input-fluentd-plugin
Jul 13, 2017
Merged
Add fluentd input plugin #2661
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
01fbca0
Add fluentd input plugin
dsalbert 6c71de8
Simplify error for slice comparision
dsalbert 115d268
Fix formatting
dsalbert 2ecbdc1
apply required changes
24cf872
simplify parse method and update README.md
ce9d8df
add information about @id parameter for fluentd
9ee35df
change logic for adding metric to accumulator
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Fluentd Input Plugin | ||
|
||
The fluentd plugin gathers metrics from plugin endpoint provided by [in_monitor plugin](http://docs.fluentd.org/v0.12/articles/monitoring). | ||
This plugin understands data provided by /api/plugin.json resource (/api/config.json is not covered). | ||
|
||
### Configuration: | ||
|
||
```toml | ||
# Read metrics exposed by fluentd in_monitor plugin | ||
[[inputs.fluentd]] | ||
## 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: | ||
|
||
Fields may vary depends on type of the plugin | ||
|
||
- fluentd | ||
- retry_count (float, unit) | ||
- buffer_queue_length (float, unit) | ||
- buffer_total_queued_size (float, unit) | ||
|
||
### Tags: | ||
|
||
- All measurements have the following tags: | ||
- plugin_id (unique plugin id) | ||
- plugin_type (type of the plugin e.g. s3) | ||
- plugin_category (plugin category e.g. output) | ||
|
||
### Example Output: | ||
|
||
``` | ||
$ telegraf --config fluentd.conf --input-filter fluentd --test | ||
* Plugin: inputs.fluentd, Collection 1 | ||
> fluentd,host=T440s,plugin_id=object:9f748c,plugin_category=input,plugin_type=dummy buffer_total_queued_size=0,buffer_queue_length=0,retry_count=0 1492006105000000000 | ||
> fluentd,plugin_category=input,plugin_type=dummy,host=T440s,plugin_id=object:8da98c buffer_queue_length=0,retry_count=0,buffer_total_queued_size=0 1492006105000000000 | ||
> fluentd,plugin_id=object:820190,plugin_category=input,plugin_type=monitor_agent,host=T440s retry_count=0,buffer_total_queued_size=0,buffer_queue_length=0 1492006105000000000 | ||
> fluentd,plugin_id=object:c5e054,plugin_category=output,plugin_type=stdout,host=T440s buffer_queue_length=0,retry_count=0,buffer_total_queued_size=0 1492006105000000000 | ||
> fluentd,plugin_type=s3,host=T440s,plugin_id=object:bd7a90,plugin_category=output buffer_queue_length=0,retry_count=0,buffer_total_queued_size=0 1492006105000000000 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package fluentd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
) | ||
|
||
const ( | ||
measurement = "fluentd" | ||
description = "Read metrics exposed by fluentd in_monitor plugin" | ||
sampleConfig = ` | ||
## 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", | ||
] | ||
` | ||
) | ||
|
||
// Fluentd - plugin main structure | ||
type Fluentd struct { | ||
Endpoint string | ||
Exclude []string | ||
client *http.Client | ||
} | ||
|
||
type endpointInfo struct { | ||
Payload []pluginData `json:"plugins"` | ||
} | ||
|
||
type pluginData struct { | ||
PluginID string `json:"plugin_id"` | ||
PluginType string `json:"type"` | ||
PluginCategory string `json:"plugin_category"` | ||
RetryCount float64 `json:"retry_count"` | ||
BufferQueueLength float64 `json:"buffer_queue_length"` | ||
BufferTotalQueuedSize float64 `json:"buffer_total_queued_size"` | ||
} | ||
|
||
// parse JSON from fluentd Endpoint | ||
// Parameters: | ||
// data: unprocessed json recivied from endpoint | ||
// | ||
// Returns: | ||
// pluginData: slice that contains parsed plugins | ||
// error: error that may have occurred | ||
func parse(data []byte) (datapointArray []pluginData, err error) { | ||
var endpointData endpointInfo | ||
|
||
if err = json.Unmarshal(data, &endpointData); err != nil { | ||
err = fmt.Errorf("Processing JSON structure") | ||
return | ||
} | ||
|
||
for _, point := range endpointData.Payload { | ||
datapointArray = append(datapointArray, point) | ||
} | ||
|
||
return | ||
} | ||
|
||
// Description - display description | ||
func (h *Fluentd) Description() string { return description } | ||
|
||
// SampleConfig - generate configuretion | ||
func (h *Fluentd) SampleConfig() string { return sampleConfig } | ||
|
||
// Gather - Main code responsible for gathering, processing and creating metrics | ||
func (h *Fluentd) Gather(acc telegraf.Accumulator) error { | ||
|
||
_, err := url.Parse(h.Endpoint) | ||
if err != nil { | ||
return fmt.Errorf("Invalid URL \"%s\"", h.Endpoint) | ||
} | ||
|
||
if h.client == nil { | ||
|
||
tr := &http.Transport{ | ||
ResponseHeaderTimeout: time.Duration(3 * time.Second), | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: tr, | ||
Timeout: time.Duration(4 * time.Second), | ||
} | ||
|
||
h.client = client | ||
} | ||
|
||
resp, err := h.client.Get(h.Endpoint) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Unable to perform HTTP client GET on \"%s\": %s", h.Endpoint, err) | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Unable to read the HTTP body \"%s\": %s", string(body), err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("http status ok not met") | ||
} | ||
|
||
dataPoints, err := parse(body) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Problem with parsing") | ||
} | ||
|
||
// Go through all plugins one by one | ||
for _, p := range dataPoints { | ||
|
||
skip := false | ||
|
||
// Check if this specific type was excluded in configuration | ||
for _, exclude := range h.Exclude { | ||
if exclude == p.PluginType { | ||
skip = true | ||
} | ||
} | ||
|
||
// If not, create new metric and add it to Accumulator | ||
if !skip { | ||
tmpFields := make(map[string]interface{}) | ||
|
||
tmpTags := map[string]string{ | ||
"plugin_id": p.PluginID, | ||
"plugin_category": p.PluginCategory, | ||
"plugin_type": p.PluginType, | ||
} | ||
|
||
tmpFields["buffer_queue_length"] = p.BufferQueueLength | ||
tmpFields["retry_count"] = p.RetryCount | ||
tmpFields["buffer_total_queued_size"] = p.BufferTotalQueuedSize | ||
|
||
acc.AddFields(measurement, tmpFields, tmpTags) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
inputs.Add("fluentd", func() telegraf.Input { return &Fluentd{} }) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One difference in this revision of your plugin is that it will report 0 for these fields if they do not exist. In order to handle this you could have these be
*float64
and test for nil before assigning the field.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. According to documentation we can setup this field for all plugins:
@id: Specify plugin id. in_monitor_agent uses this value for plugin_id field
. I'm going to add this to README.mdFor second thing I will change this to pointer and add some checks around:
Thanks!