-
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
Implement Elasticsearch plugin #55
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d38f222
Implement Elasticsearch plugin (indices stats).
brocaar 55cfd5c
Check that API reponse is 200.
brocaar 3f6c46e
Add node_name to tags.
brocaar 6c87148
Add node-id and node attributes to tags.
brocaar c6a9335
Refactor parsing "indices" stats.
brocaar 9cd1344
Implement os stats.
brocaar d900266
Implement process stats.
brocaar 10c4ec7
Implement JVM stats.
brocaar ac54b7c
Implement thread-pool stats.
brocaar 4743c9a
Implement network stats.
brocaar ec40797
Implement fs stats.
brocaar cb839d0
Implement transport stats.
brocaar 0faa1c8
Implement http stats.
brocaar d799011
Implement breakers stats.
brocaar 986b89f
Cleanup tests.
brocaar ec138ca
Remove indices filter.
brocaar e2d48f4
Cleanup repeated logic.
brocaar f76f99e
Merge remote-tracking branch 'upstream/master' into elasticsearch_plugin
brocaar 0f6664b
Remove that it only reads indices stats.
brocaar 22d4d1f
Fix typo (tranport > transport).
brocaar 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
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,154 @@ | ||
package elasticsearch | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/influxdb/telegraf/plugins" | ||
) | ||
|
||
const statsPath = "/_nodes/stats" | ||
const statsPathLocal = "/_nodes/_local/stats" | ||
|
||
type node struct { | ||
Host string `json:"host"` | ||
Name string `json:"name"` | ||
Attributes map[string]string `json:"attributes"` | ||
Indices interface{} `json:"indices"` | ||
OS interface{} `json:"os"` | ||
Process interface{} `json:"process"` | ||
JVM interface{} `json:"jvm"` | ||
ThreadPool interface{} `json:"thread_pool"` | ||
Network interface{} `json:"network"` | ||
FS interface{} `json:"fs"` | ||
Transport interface{} `json:"transport"` | ||
HTTP interface{} `json:"http"` | ||
Breakers interface{} `json:"breakers"` | ||
} | ||
|
||
const sampleConfig = ` | ||
# specify a list of one or more Elasticsearch servers | ||
servers = ["http://localhost:9200"] | ||
|
||
# set local to false when you want to read the indices stats from all nodes | ||
# within the cluster | ||
local = true | ||
` | ||
|
||
// Elasticsearch is a plugin to read stats from one or many Elasticsearch | ||
// servers. | ||
type Elasticsearch struct { | ||
Local bool | ||
Servers []string | ||
client *http.Client | ||
} | ||
|
||
// NewElasticsearch return a new instance of Elasticsearch | ||
func NewElasticsearch() *Elasticsearch { | ||
return &Elasticsearch{client: http.DefaultClient} | ||
} | ||
|
||
// SampleConfig returns sample configuration for this plugin. | ||
func (e *Elasticsearch) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
// Description returns the plugin description. | ||
func (e *Elasticsearch) Description() string { | ||
return "Read stats from one or more Elasticsearch servers or clusters" | ||
} | ||
|
||
// Gather reads the stats from Elasticsearch and writes it to the | ||
// Accumulator. | ||
func (e *Elasticsearch) Gather(acc plugins.Accumulator) error { | ||
for _, serv := range e.Servers { | ||
var url string | ||
if e.Local { | ||
url = serv + statsPathLocal | ||
} else { | ||
url = serv + statsPath | ||
} | ||
if err := e.gatherUrl(url, acc); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *Elasticsearch) gatherUrl(url string, acc plugins.Accumulator) error { | ||
r, err := e.client.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
if r.StatusCode != http.StatusOK { | ||
return fmt.Errorf("elasticsearch: API responded with status-code %d, expected %d", r.StatusCode, http.StatusOK) | ||
} | ||
d := json.NewDecoder(r.Body) | ||
esRes := &struct { | ||
ClusterName string `json:"cluster_name"` | ||
Nodes map[string]*node `json:"nodes"` | ||
}{} | ||
if err = d.Decode(esRes); err != nil { | ||
return err | ||
} | ||
|
||
for id, n := range esRes.Nodes { | ||
tags := map[string]string{ | ||
"node_id": id, | ||
"node_host": n.Host, | ||
"node_name": n.Name, | ||
"cluster_name": esRes.ClusterName, | ||
} | ||
|
||
for k, v := range n.Attributes { | ||
tags["node_attribute_"+k] = v | ||
} | ||
|
||
stats := map[string]interface{}{ | ||
"indices": n.Indices, | ||
"os": n.OS, | ||
"process": n.Process, | ||
"jvm": n.JVM, | ||
"thread_pool": n.ThreadPool, | ||
"network": n.Network, | ||
"fs": n.FS, | ||
"transport": n.Transport, | ||
"http": n.HTTP, | ||
"breakers": n.Breakers, | ||
} | ||
|
||
for p, s := range stats { | ||
if err := e.parseInterface(acc, p, tags, s); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (e *Elasticsearch) parseInterface(acc plugins.Accumulator, prefix string, tags map[string]string, v interface{}) error { | ||
switch t := v.(type) { | ||
case map[string]interface{}: | ||
for k, v := range t { | ||
if err := e.parseInterface(acc, prefix+"_"+k, tags, v); err != nil { | ||
return err | ||
} | ||
} | ||
case float64: | ||
acc.Add(prefix, t, tags) | ||
case bool, string, []interface{}: | ||
// ignored types | ||
return nil | ||
default: | ||
return fmt.Errorf("elasticsearch: got unexpected type %T with value %v (%s)", t, t, prefix) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
plugins.Add("elasticsearch", func() plugins.Plugin { | ||
return NewElasticsearch() | ||
}) | ||
} |
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,72 @@ | ||
package elasticsearch | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/influxdb/telegraf/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type transportMock struct { | ||
statusCode int | ||
body string | ||
} | ||
|
||
func newTransportMock(statusCode int, body string) http.RoundTripper { | ||
return &transportMock{ | ||
statusCode: statusCode, | ||
body: body, | ||
} | ||
} | ||
|
||
func (t *transportMock) RoundTrip(r *http.Request) (*http.Response, error) { | ||
res := &http.Response{ | ||
Header: make(http.Header), | ||
Request: r, | ||
StatusCode: t.statusCode, | ||
} | ||
res.Header.Set("Content-Type", "application/json") | ||
res.Body = ioutil.NopCloser(strings.NewReader(t.body)) | ||
return res, nil | ||
} | ||
|
||
func TestElasticsearch(t *testing.T) { | ||
es := NewElasticsearch() | ||
es.Servers = []string{"http://example.com:9200"} | ||
es.client.Transport = newTransportMock(http.StatusOK, statsResponse) | ||
|
||
var acc testutil.Accumulator | ||
if err := es.Gather(&acc); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tags := map[string]string{ | ||
"cluster_name": "es-testcluster", | ||
"node_attribute_master": "true", | ||
"node_id": "SDFsfSDFsdfFSDSDfSFDSDF", | ||
"node_name": "test.host.com", | ||
"node_host": "test", | ||
} | ||
|
||
testTables := []map[string]float64{ | ||
indicesExpected, | ||
osExpected, | ||
processExpected, | ||
jvmExpected, | ||
threadPoolExpected, | ||
networkExpected, | ||
fsExpected, | ||
transportExpected, | ||
httpExpected, | ||
breakersExpected, | ||
} | ||
|
||
for _, testTable := range testTables { | ||
for k, v := range testTable { | ||
assert.NoError(t, acc.ValidateTaggedValue(k, v, tags)) | ||
} | ||
} | ||
} |
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.
node_name
should be added to tags.node_uuid
perhaps, but I'm not sure if it would be very useful.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.
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.
+1. Please do make good use of tags. Node name, hostname if possible, etc.
On Wednesday, July 8, 2015, Alvaro Morales [email protected] wrote:
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.
Will add the following too to the tags: