-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Deep merge event fields and metadata maps (#17958) * Deep merge event fields and metadata maps * Add CHANGELOG entry * Use loop to remove keys; extract into function * Relocating CHANGELOG entry * Rewording and moving to bugfix section per review feedback * Adapting other CHANGELOG entry * Fix comparison in test * Fixing up CHANGELOG * Fixing up CHANGELOG
- Loading branch information
1 parent
87fe454
commit 6524eaa
Showing
3 changed files
with
157 additions
and
9 deletions.
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,136 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package jsontransform | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/elastic/beats/v7/libbeat/common" | ||
) | ||
|
||
func TestWriteJSONKeys(t *testing.T) { | ||
now := time.Now() | ||
now = now.Round(time.Second) | ||
|
||
eventTimestamp := time.Date(2020, 01, 01, 01, 01, 00, 0, time.UTC) | ||
eventMetadata := common.MapStr{ | ||
"foo": "bar", | ||
"baz": common.MapStr{ | ||
"qux": 17, | ||
}, | ||
} | ||
eventFields := common.MapStr{ | ||
"top_a": 23, | ||
"top_b": common.MapStr{ | ||
"inner_c": "see", | ||
"inner_d": "dee", | ||
}, | ||
} | ||
|
||
tests := map[string]struct { | ||
keys map[string]interface{} | ||
overwriteKeys bool | ||
expectedMetadata common.MapStr | ||
expectedTimestamp time.Time | ||
expectedFields common.MapStr | ||
}{ | ||
"overwrite_true": { | ||
overwriteKeys: true, | ||
keys: map[string]interface{}{ | ||
"@metadata": map[string]interface{}{ | ||
"foo": "NEW_bar", | ||
"baz": map[string]interface{}{ | ||
"qux": "NEW_qux", | ||
"durrr": "COMPLETELY_NEW", | ||
}, | ||
}, | ||
"@timestamp": now.Format(time.RFC3339), | ||
"top_b": map[string]interface{}{ | ||
"inner_d": "NEW_dee", | ||
"inner_e": "COMPLETELY_NEW_e", | ||
}, | ||
"top_c": "COMPLETELY_NEW_c", | ||
}, | ||
expectedMetadata: common.MapStr{ | ||
"foo": "NEW_bar", | ||
"baz": common.MapStr{ | ||
"qux": "NEW_qux", | ||
"durrr": "COMPLETELY_NEW", | ||
}, | ||
}, | ||
expectedTimestamp: now, | ||
expectedFields: common.MapStr{ | ||
"top_a": 23, | ||
"top_b": common.MapStr{ | ||
"inner_c": "see", | ||
"inner_d": "NEW_dee", | ||
"inner_e": "COMPLETELY_NEW_e", | ||
}, | ||
"top_c": "COMPLETELY_NEW_c", | ||
}, | ||
}, | ||
"overwrite_false": { | ||
overwriteKeys: false, | ||
keys: map[string]interface{}{ | ||
"@metadata": map[string]interface{}{ | ||
"foo": "NEW_bar", | ||
"baz": map[string]interface{}{ | ||
"qux": "NEW_qux", | ||
"durrr": "COMPLETELY_NEW", | ||
}, | ||
}, | ||
"@timestamp": now.Format(time.RFC3339), | ||
"top_b": map[string]interface{}{ | ||
"inner_d": "NEW_dee", | ||
"inner_e": "COMPLETELY_NEW_e", | ||
}, | ||
"top_c": "COMPLETELY_NEW_c", | ||
}, | ||
expectedMetadata: eventMetadata.Clone(), | ||
expectedTimestamp: eventTimestamp, | ||
expectedFields: common.MapStr{ | ||
"top_a": 23, | ||
"top_b": common.MapStr{ | ||
"inner_c": "see", | ||
"inner_d": "dee", | ||
"inner_e": "COMPLETELY_NEW_e", | ||
}, | ||
"top_c": "COMPLETELY_NEW_c", | ||
}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
event := &beat.Event{ | ||
Timestamp: eventTimestamp, | ||
Meta: eventMetadata.Clone(), | ||
Fields: eventFields.Clone(), | ||
} | ||
|
||
WriteJSONKeys(event, test.keys, test.overwriteKeys, false) | ||
require.Equal(t, test.expectedMetadata, event.Meta) | ||
require.Equal(t, test.expectedTimestamp.UnixNano(), event.Timestamp.UnixNano()) | ||
require.Equal(t, test.expectedFields, event.Fields) | ||
}) | ||
} | ||
} |