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

#6066: Extract json deDot feature from metricbeat/http module to libbeat/common #6067

Merged
merged 2 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions libbeat/common/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,24 @@ func (f Float) MarshalJSON() ([]byte, error) {
func DeDot(s string) string {
return strings.Replace(s, ".", "_", -1)
}

// DeDotJSON replaces in keys all . with _
// This helps when sending data to Elasticsearch to prevent object and key collisions.
func DeDotJSON(json interface{}) interface{} {
switch json.(type) {
case map[string]interface{}:
result := map[string]interface{}{}
for key, value := range json.(map[string]interface{}) {
result[DeDot(key)] = DeDotJSON(value)
}
return result
case []interface{}:
result := make([]interface{}, len(json.([]interface{})))
for i, value := range json.([]interface{}) {
result[i] = DeDotJSON(value)
}
return result
default:
return json
}
}
49 changes: 49 additions & 0 deletions libbeat/common/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (

"github.com/stretchr/testify/assert"

"io/ioutil"
"path/filepath"

"github.com/elastic/beats/libbeat/logp"
)

Expand Down Expand Up @@ -379,3 +382,49 @@ func BenchmarkConvertToGenericEventStringPointer(b *testing.B) {
ConvertToGenericEvent(MapStr{"key": &val})
}
}

func TestDeDotJsonMap(t *testing.T) {
var actualJSONBody map[string]interface{}
var expectedJSONBody map[string]interface{}

absPath, err := filepath.Abs("./testdata")
assert.NotNil(t, absPath)
assert.Nil(t, err)

actualJSONResponse, err := ioutil.ReadFile(absPath + "/json_map_with_dots.json")
assert.Nil(t, err)
err = json.Unmarshal(actualJSONResponse, &actualJSONBody)
assert.Nil(t, err)

dedottedJSONResponse, err := ioutil.ReadFile(absPath + "/json_map_dedot.json")
assert.Nil(t, err)
err = json.Unmarshal(dedottedJSONResponse, &expectedJSONBody)
assert.Nil(t, err)

actualJSONBody = DeDotJSON(actualJSONBody).(map[string]interface{})

assert.Equal(t, expectedJSONBody, actualJSONBody)
}

func TestDeDotJsonArray(t *testing.T) {
var actualJSONBody []interface{}
var expectedJSONBody []interface{}

absPath, err := filepath.Abs("./testdata")
assert.NotNil(t, absPath)
assert.Nil(t, err)

actualJSONResponse, err := ioutil.ReadFile(absPath + "/json_array_with_dots.json")
assert.Nil(t, err)
err = json.Unmarshal(actualJSONResponse, &actualJSONBody)
assert.Nil(t, err)

dedottedJSONResponse, err := ioutil.ReadFile(absPath + "/json_array_dedot.json")
assert.Nil(t, err)
err = json.Unmarshal(dedottedJSONResponse, &expectedJSONBody)
assert.Nil(t, err)

actualJSONBody = DeDotJSON(actualJSONBody).([]interface{})

assert.Equal(t, expectedJSONBody, actualJSONBody)
}
5 changes: 5 additions & 0 deletions libbeat/common/testdata/json_array_dedot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"key_with_dot_1":"value1_1"},
{"key_without_dot_2":"value1_2"},
{"key_with_multiple_dots_3": {"key_with_dot_2":"value2_1"}}
]
5 changes: 5 additions & 0 deletions libbeat/common/testdata/json_array_with_dots.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"key_with_dot.1":"value1_1"},
{"key_without_dot_2":"value1_2"},
{"key_with_multiple.dots.3": {"key_with_dot.2":"value2_1"}}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"key_with_dot_l2": 1,
"key_with_multiple_dots_l2": 2,
"key_without_dot_l2": {
"key_with_dot_l2": 3,
"key_with_multiple_dots_l2": 4
"key_with_dot_l3": 3,
"key_with_multiple_dots_l3": 4
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"key_without_dot_l1": {
"key_with_dot_l2": 1,
"key_with_dot.l2": 1,
"key.with.multiple.dots_l2": 2,
"key_without_dot_l2": {
"key_with_dot.l2": 3,
"key.with.multiple.dots_l2": 4
"key_with_dot.l3": 3,
"key.with.multiple.dots_l3": 4
}
}
}
15 changes: 1 addition & 14 deletions metricbeat/module/http/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m *MetricSet) Fetch() (common.MapStr, error) {
}

if m.deDotEnabled {
event = replaceDots(jsonBody).(map[string]interface{})
event = common.DeDotJSON(jsonBody).(map[string]interface{})
} else {
event = jsonBody
}
Expand Down Expand Up @@ -162,16 +162,3 @@ func (m *MetricSet) getHeaders(header http.Header) map[string]string {
}
return headers
}

func replaceDots(data interface{}) interface{} {
switch data.(type) {
case map[string]interface{}:
result := map[string]interface{}{}
for key, value := range data.(map[string]interface{}) {
result[common.DeDot(key)] = replaceDots(value)
}
return result
default:
return data
}
}
34 changes: 0 additions & 34 deletions metricbeat/module/http/json/json_test.go

This file was deleted.