-
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
* Followup to 12606 (#18316) * Adding developer CHANGELOG entry * Refactoring: extracting helper method * Adding unit tests * Consolidate event metadata field constants * Use events.GetMetaStringValue * Implement op_type values as enum * Add doc strings * Deference event pointer * Renaming op type consts and breaking them out into own block * Renaming type * Using stringer * Using go idiom instead of if-else * Adding default op type * Empty string for default * Store op type enum, not string, in event metadata * Using events.GetMetaStringValue * Updating dev CHANGELOG entry * Allow for op_type metadata field to be set as either string or enum * No need for .String() * Handle missing key case gracefully * Update unit tests * Update developer CHANGELOG entry * Fixing up CHANGELOG
- Loading branch information
1 parent
7933e01
commit 1795efd
Showing
17 changed files
with
311 additions
and
73 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
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,28 @@ | ||
// 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 events | ||
|
||
type OpType int | ||
|
||
//go:generate stringer -linecomment -type OpType | ||
const ( | ||
OpTypeDefault OpType = iota // | ||
OpTypeCreate //create | ||
OpTypeIndex // index | ||
OpTypeDelete // delete | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,84 @@ | ||
// 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 events | ||
|
||
import "github.com/elastic/beats/v7/libbeat/beat" | ||
|
||
const ( | ||
// FieldMetaID defines the ID for the event. Also see FieldMetaOpType. | ||
FieldMetaID = "_id" | ||
|
||
// FieldMetaAlias defines the index alias to use for the event. If set, it takes | ||
// precedence over values defined using FieldMetaIndex or FieldMetaRawIndex. | ||
FieldMetaAlias = "alias" | ||
|
||
// FieldMetaIndex defines the base index name to use for the event. The value is suffixed | ||
// with a Y-m-d value based on the event's timestamp. If set, it takes precedence over the | ||
// value defined using FieldMetaRawIndex. | ||
FieldMetaIndex = "index" | ||
|
||
// FieldMetaRawIndex defines the raw index name to use for the event. It is used as-is, without | ||
// any additional manipulation. | ||
FieldMetaRawIndex = "raw_index" | ||
|
||
// FieldMetaPipeline defines the ingest node pipeline to use for this event. | ||
FieldMetaPipeline = "pipeline" | ||
|
||
// FieldMetaOpType defines the metadata key name for event operation type to use with the Elasticsearch | ||
// Bulk API encoding of the event. The key's value can be an empty string, `create`, `index`, or `delete`. | ||
// If empty, `create` will be used if FieldMetaID is set; otherwise `index` will be used. | ||
FieldMetaOpType = "op_type" | ||
) | ||
|
||
// GetMetaStringValue returns the value of the given event metadata string field | ||
func GetMetaStringValue(e beat.Event, key string) (string, error) { | ||
tmp, err := e.Meta.GetValue(key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if s, ok := tmp.(string); ok { | ||
return s, nil | ||
} | ||
|
||
return "", nil | ||
} | ||
|
||
// GetOpType returns the event's op_type, if set | ||
func GetOpType(e beat.Event) OpType { | ||
tmp, err := e.Meta.GetValue(FieldMetaOpType) | ||
if err != nil { | ||
return OpTypeDefault | ||
} | ||
|
||
switch v := tmp.(type) { | ||
case OpType: | ||
return v | ||
case string: | ||
switch v { | ||
case "create": | ||
return OpTypeCreate | ||
case "index": | ||
return OpTypeIndex | ||
case "delete": | ||
return OpTypeDelete | ||
} | ||
} | ||
|
||
return OpTypeDefault | ||
} |
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,90 @@ | ||
// 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 events | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/elastic/beats/v7/libbeat/common" | ||
) | ||
|
||
func TestGetMetaStringValue(t *testing.T) { | ||
tests := map[string]struct { | ||
event beat.Event | ||
metaFieldPath string | ||
expectedValue string | ||
expectedErr error | ||
}{ | ||
"nonexistent_field": { | ||
beat.Event{ | ||
Meta: common.MapStr{ | ||
"foo": "bar", | ||
}, | ||
}, | ||
"nonexistent", | ||
"", | ||
common.ErrKeyNotFound, | ||
}, | ||
"root": { | ||
beat.Event{ | ||
Meta: common.MapStr{ | ||
"foo": "bar", | ||
"baz": "hello", | ||
}, | ||
}, | ||
"baz", | ||
"hello", | ||
nil, | ||
}, | ||
"nested": { | ||
beat.Event{ | ||
Meta: common.MapStr{ | ||
"foo": "bar", | ||
"baz": common.MapStr{ | ||
"qux": "hello", | ||
}, | ||
}, | ||
}, | ||
"baz.qux", | ||
"hello", | ||
nil, | ||
}, | ||
"non_string": { | ||
beat.Event{ | ||
Meta: common.MapStr{ | ||
"foo": "bar", | ||
"baz": 17, | ||
}, | ||
}, | ||
"baz", | ||
"", | ||
nil, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
value, err := GetMetaStringValue(test.event, test.metaFieldPath) | ||
require.Equal(t, test.expectedValue, value) | ||
require.Equal(t, test.expectedErr, err) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.