Skip to content
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 Elasticsearch 5.x output #2332

Merged
merged 19 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions plugins/outputs/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ func (a *Elasticsearch) Write(metrics []telegraf.Metric) error {
m := make(map[string]interface{})
mfields := make(map[string]interface{})

// Elasticsearch/java/json quirks for correct field mapping
// value as integer cannot be bigger than MaxInt64 or smaller than MinInt64; as a float it cannot be bigger than float32
for k, v := range metric.Fields() {
switch x := v.(type) {
// Truncate values too big/small for Elasticsearch to not complain when creating a dynamic field mapping
case float64:
if (x > 0 && x > math.MaxFloat32) || (x < 0 && x < math.SmallestNonzeroFloat32) {
v = float32(x)
log.Printf("W! Elasticsearch output metric %s truncated (value %v is too big or too small, truncating to %v)", k, x, v)
}
switch {
case v.(float64) > math.MaxInt64:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work if v is a float32?

Not sure I understand how this compiles, since it's comparing a float64 to an int64?

v = math.MaxInt64
case v.(float64) < math.MinInt64:
v = math.MinInt64
default:
v = float32(v.(float64))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

casting everything to float32?

}
mfields[k] = v
}
Expand Down Expand Up @@ -181,7 +183,7 @@ func (a *Elasticsearch) Write(metrics []telegraf.Metric) error {

if res.Errors {
for id, err := range res.Failed() {
log.Printf("E! Elasticsearch indexing failure, id: %d, error: %s", id, err.Error.Reason)
log.Printf("E! Elasticsearch indexing failure, id: %d, error: %s, caused by: %s, %s", id, err.Error.Reason, err.Error.CausedBy["reason"], err.Error.CausedBy["type"])
}
return fmt.Errorf("W! Elasticsearch failed to index %d metrics", len(res.Failed()))
}
Expand Down
10 changes: 9 additions & 1 deletion plugins/outputs/elasticsearch/elasticsearch_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package elasticsearch

import (
"math"
"testing"
"time"

Expand Down Expand Up @@ -55,7 +56,14 @@ func TestBigValue(t *testing.T) {
m1, _ := metric.New(
"mymeasurement",
map[string]string{"host": "192.168.0.1"},
map[string]interface{}{"mymeasurement": float64(-9223372036854776000)},
map[string]interface{}{
"myvalue1": float64(-9223372036854776000),
"myvalue2": float64(math.MaxUint64 * -10),
"myvalue3": float64(math.MaxUint64 * 10),
"myvalue4": float64(math.MaxFloat64),
"myvalue5": float64(0.000000000000000000000000000000000000000000000001),
"myvalue6": float64(-0.000000000000000000000000000000000000000000000001),
},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
// Prepare point list
Expand Down