-
Notifications
You must be signed in to change notification settings - Fork 5k
Followup to 12606 #18316
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
Followup to 12606 #18316
Changes from all commits
a60f5fb
f2f3b65
cc9c097
ff11264
611c877
5ef7bd9
8914d75
4d0259a
fcda77d
5eb058e
583b42e
50f3ee5
8551c2c
8b1f6ea
49acde0
9b0debc
16a9eaa
1f4fd5d
5c02b87
0a7e6b5
440e8c9
85c6e54
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 |
|---|---|---|
| @@ -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.
| 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 | ||
| } | ||
| 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) | ||
| }) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.