Skip to content

Commit

Permalink
Send float fields as float values to Elasticsearch, to remove definin…
Browse files Browse the repository at this point in the history
…g the mapping in advance (#2627) (#2687)
  • Loading branch information
monicasarbu authored and tsg committed Oct 5, 2016
1 parent 912186b commit caf40c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ https://github.com/elastic/beats/compare/v5.0.0-beta1...master[Check the HEAD di
==== Bugfixes

*Affecting all Beats*
- Make sure Beats sent always float values when they are defined as float by sending 5.00000 instead of 5.
{pull}2627[2627]

*Metricbeat*

Expand Down
10 changes: 9 additions & 1 deletion libbeat/common/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var eventDebugf = logp.MakeDebug(eventDebugSelector)

var textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()

type Float float64

// ConvertToGenericEvent normalizes the types contained in the given MapStr.
//
// Nil values in maps are dropped during the conversion. Any unsupported types
Expand Down Expand Up @@ -130,6 +132,7 @@ func normalizeValue(value interface{}, keys ...string) (interface{}, []error) {
case uint, uint8, uint16, uint32, uint64:
case []uint, []uint8, []uint16, []uint32, []uint64:
case float32, float64:
return Float(value.(float64)), nil
case []float32, []float64:
case complex64, complex128:
case []complex64, []complex128:
Expand All @@ -156,7 +159,7 @@ func normalizeValue(value interface{}, keys ...string) (interface{}, []error) {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint(), nil
case reflect.Float32, reflect.Float64:
return v.Float(), nil
return Float(v.Float()), nil
case reflect.Complex64, reflect.Complex128:
return v.Complex(), nil
case reflect.String:
Expand Down Expand Up @@ -221,3 +224,8 @@ func joinKeys(keys ...string) string {
}
return strings.Join(keys, ".")
}

// Defines the marshal of the Float type
func (f Float) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%.6f", f)), nil
}
18 changes: 18 additions & 0 deletions libbeat/common/event_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"encoding/json"
"testing"

"github.com/elastic/beats/libbeat/logp"
Expand Down Expand Up @@ -297,6 +298,23 @@ func TestMarshalUnmarshalArray(t *testing.T) {
}
}

func TestMarshalFloatValues(t *testing.T) {

assert := assert.New(t)

var f float64

f = 5

a := MapStr{
"f": Float(f),
}

b, err := json.Marshal(a)
assert.Nil(err)
assert.Equal(string(b), "{\"f\":5.000000}")
}

// Uses TextMarshaler interface.
func BenchmarkConvertToGenericEventNetString(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down
4 changes: 2 additions & 2 deletions libbeat/processors/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,15 @@ func (c *Condition) checkRange(event common.MapStr) bool {
return false
}

case float64, float32:
case float64, float32, common.Float:
floatValue := reflect.ValueOf(value).Float()

if !checkValue(floatValue, rangeValue) {
return false
}

default:
logp.Warn("unexpected type %T in range condition as it accepts only strings. ", value)
logp.Warn("unexpected type %T in range condition. ", value)
return false
}

Expand Down
2 changes: 1 addition & 1 deletion metricbeat/tests/system/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_dropevent_with_complex_condition(self):
output = self.read_output(
required_fields=["@timestamp", "type"],
)
assert len(output) == 1
assert len(output) >= 1


def test_include_fields(self):
Expand Down

0 comments on commit caf40c0

Please sign in to comment.