-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Cherry-pick #10945 to 7.0: [Filebeat] NetFlow input support for custom field definitions #11223
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ package fields | |
|
||
import "fmt" | ||
|
||
var Fields = FieldDict{} | ||
var GlobalFields = FieldDict{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported var GlobalFields should have comment or be unexported |
||
|
||
type Key struct { | ||
EnterpriseID uint32 | ||
|
@@ -20,12 +20,20 @@ type Field struct { | |
|
||
type FieldDict map[Key]*Field | ||
|
||
func RegisterFields(dict FieldDict) error { | ||
func RegisterGlobalFields(dict FieldDict) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported function RegisterGlobalFields should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported function RegisterGlobalFields should have comment or be unexported |
||
for key, value := range dict { | ||
if _, found := Fields[key]; found { | ||
if _, found := GlobalFields[key]; found { | ||
return fmt.Errorf("field %+v is duplicated", key) | ||
} | ||
Fields[key] = value | ||
GlobalFields[key] = value | ||
} | ||
return nil | ||
} | ||
|
||
// Merge merges the passed fields into the dictionary, overwriting existing | ||
// fields if duplicated. | ||
func (f FieldDict) Merge(otherFields FieldDict) { | ||
for key, value := range otherFields { | ||
f[key] = value | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package fields | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFieldDict_Merge(t *testing.T) { | ||
a := FieldDict{ | ||
Key{1, 2}: &Field{"field1", String}, | ||
Key{2, 3}: &Field{"field2", Unsigned32}, | ||
} | ||
b := FieldDict{ | ||
Key{3, 4}: &Field{"field3", MacAddress}, | ||
Key{4, 5}: &Field{"field4", Ipv4Address}, | ||
Key{5, 6}: &Field{"field5", Ipv6Address}, | ||
} | ||
c := FieldDict{ | ||
Key{3, 4}: &Field{"field3v2", OctetArray}, | ||
Key{0, 0}: &Field{"field0", DateTimeMicroseconds}, | ||
} | ||
|
||
f := FieldDict{} | ||
|
||
f.Merge(a) | ||
|
||
assert.Len(t, f, len(a)) | ||
if !checkContains(t, f, a) { | ||
t.FailNow() | ||
} | ||
|
||
f.Merge(b) | ||
assert.Len(t, f, len(a)+len(b)) | ||
if !checkContains(t, f, b) { | ||
t.FailNow() | ||
} | ||
|
||
f.Merge(c) | ||
assert.Len(t, f, len(a)+len(b)+len(c)-1) | ||
if !checkContains(t, f, c) { | ||
t.FailNow() | ||
} | ||
|
||
} | ||
|
||
func checkContains(t testing.TB, dest FieldDict, contains FieldDict) bool { | ||
for k, v := range contains { | ||
if !assert.Contains(t, dest, k) || !assert.Equal(t, *v, *dest[k]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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.
exported var GlobalFields should have comment or be unexported