|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package events |
| 19 | + |
| 20 | +import "github.com/elastic/beats/v7/libbeat/beat" |
| 21 | + |
| 22 | +const ( |
| 23 | + // FieldMetaID defines the ID for the event. Also see FieldMetaOpType. |
| 24 | + FieldMetaID = "_id" |
| 25 | + |
| 26 | + // FieldMetaAlias defines the index alias to use for the event. If set, it takes |
| 27 | + // precedence over values defined using FieldMetaIndex or FieldMetaRawIndex. |
| 28 | + FieldMetaAlias = "alias" |
| 29 | + |
| 30 | + // FieldMetaIndex defines the base index name to use for the event. The value is suffixed |
| 31 | + // with a Y-m-d value based on the event's timestamp. If set, it takes precedence over the |
| 32 | + // value defined using FieldMetaRawIndex. |
| 33 | + FieldMetaIndex = "index" |
| 34 | + |
| 35 | + // FieldMetaRawIndex defines the raw index name to use for the event. It is used as-is, without |
| 36 | + // any additional manipulation. |
| 37 | + FieldMetaRawIndex = "raw_index" |
| 38 | + |
| 39 | + // FieldMetaPipeline defines the ingest node pipeline to use for this event. |
| 40 | + FieldMetaPipeline = "pipeline" |
| 41 | + |
| 42 | + // FieldMetaOpType defines the metadata key name for event operation type to use with the Elasticsearch |
| 43 | + // Bulk API encoding of the event. The key's value can be an empty string, `create`, `index`, or `delete`. |
| 44 | + // If empty, `create` will be used if FieldMetaID is set; otherwise `index` will be used. |
| 45 | + FieldMetaOpType = "op_type" |
| 46 | +) |
| 47 | + |
| 48 | +// GetMetaStringValue returns the value of the given event metadata string field |
| 49 | +func GetMetaStringValue(e beat.Event, key string) (string, error) { |
| 50 | + tmp, err := e.Meta.GetValue(key) |
| 51 | + if err != nil { |
| 52 | + return "", err |
| 53 | + } |
| 54 | + |
| 55 | + if s, ok := tmp.(string); ok { |
| 56 | + return s, nil |
| 57 | + } |
| 58 | + |
| 59 | + return "", nil |
| 60 | +} |
| 61 | + |
| 62 | +// GetOpType returns the event's op_type, if set |
| 63 | +func GetOpType(e beat.Event) OpType { |
| 64 | + tmp, err := e.Meta.GetValue(FieldMetaOpType) |
| 65 | + if err != nil { |
| 66 | + return OpTypeDefault |
| 67 | + } |
| 68 | + |
| 69 | + switch v := tmp.(type) { |
| 70 | + case OpType: |
| 71 | + return v |
| 72 | + case string: |
| 73 | + switch v { |
| 74 | + case "create": |
| 75 | + return OpTypeCreate |
| 76 | + case "index": |
| 77 | + return OpTypeIndex |
| 78 | + case "delete": |
| 79 | + return OpTypeDelete |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return OpTypeDefault |
| 84 | +} |
0 commit comments