Skip to content
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

[Libbeat][New Processor] XML Decode #23678

Merged
merged 25 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d08af8d
stashing before initial commit
P1llus Jan 26, 2021
cf5c3bd
Initial commit
P1llus Jan 26, 2021
7fd6b90
updating go.sum
P1llus Jan 26, 2021
66dd30e
updating it again
P1llus Jan 26, 2021
3849b84
adding feedback from PR comments and removing expandkeys config entry
P1llus Jan 26, 2021
12620d2
Updating changelog
P1llus Jan 26, 2021
244a97a
removing expanded_keys from allowed fields
P1llus Jan 27, 2021
32442b9
adding new changes based on PR comments, a few more changes remains
P1llus Feb 5, 2021
cab945d
moving the xml decoder to its own subpackage based on PR comments
P1llus Feb 8, 2021
7c24a56
reverting back to Target being a string pointer, to be able to differ…
P1llus Feb 8, 2021
99f7ad0
Updating certain tests to fit the new ignore_failure and ignore_missi…
P1llus Feb 8, 2021
a816681
Updating unit test to test with missing field
P1llus Feb 8, 2021
7e05b48
updating license headers
P1llus Feb 8, 2021
d61d3d5
adding benchmark test
P1llus Feb 8, 2021
8085f42
benchmark, now also with allocation results
P1llus Feb 8, 2021
52d9936
updating changelog entry
P1llus Feb 8, 2021
fab3f86
removing duplicate Changelog entry
P1llus Feb 8, 2021
3078147
changing changelog entry name to new name
P1llus Feb 8, 2021
fd0a8da
Simplify error handling and fix race
andrewkroh Feb 11, 2021
03e4e3c
internal xml to json implementation
andrewkroh Feb 14, 2021
d64e261
Use internal xml to json decoder
andrewkroh Feb 14, 2021
bf9c310
changelog fix
andrewkroh Feb 14, 2021
e5567e3
Update docs
andrewkroh Feb 14, 2021
67ee664
Add godoc example of xml to json
andrewkroh Feb 14, 2021
76f6582
updating test name to fit Example naming convention
P1llus Feb 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Update the baseline version of Sarama (Kafka support library) to 1.27.2. {pull}23595[23595]
- Add kubernetes.volume.fs.used.pct field. {pull}23564[23564]
- Add the `enable_krb5_fast` flag to the Kafka output to explicitly opt-in to FAST authentication. {pull}23629[23629]
- Added new decode_xml processor to libbeat that is available to all beat types. {pull}23678[23678]
- Add deployment name in pod's meta. {pull}23610[23610]
- Add `selector` information in kubernetes services' metadata. {pull}23730[23730]

Expand Down
1 change: 1 addition & 0 deletions libbeat/cmd/instance/imports_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
_ "github.com/elastic/beats/v7/libbeat/processors/add_process_metadata"
_ "github.com/elastic/beats/v7/libbeat/processors/communityid"
_ "github.com/elastic/beats/v7/libbeat/processors/convert"
_ "github.com/elastic/beats/v7/libbeat/processors/decode_xml"
_ "github.com/elastic/beats/v7/libbeat/processors/dissect"
_ "github.com/elastic/beats/v7/libbeat/processors/dns"
_ "github.com/elastic/beats/v7/libbeat/processors/extract_array"
Expand Down
120 changes: 120 additions & 0 deletions libbeat/common/encoding/xml/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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 xml

import (
"bytes"
"encoding/xml"
"io"
"strings"
)

// A Decoder reads and decodes XML from an input stream.
type Decoder struct {
prependHyphenToAttr bool
lowercaseKeys bool
xmlDec *xml.Decoder
}

// NewDecoder returns a new decoder that reads from r.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{xmlDec: xml.NewDecoder(r)}
}

// PrependHyphenToAttr causes the Decoder to prepend a hyphen ('-') to to all
// XML attribute names.
func (d *Decoder) PrependHyphenToAttr() { d.prependHyphenToAttr = true }

// LowercaseKeys causes the Decoder to transform all key name to lowercase.
func (d *Decoder) LowercaseKeys() { d.lowercaseKeys = true }

// Decode reads XML from the input stream and return a map containing the data.
func (d *Decoder) Decode() (map[string]interface{}, error) {
_, m, err := d.decode(nil)
return m, err
}

func (d *Decoder) decode(attrs []xml.Attr) (string, map[string]interface{}, error) {
elements := map[string]interface{}{}
var cdata string

for {
t, err := d.xmlDec.Token()
if err != nil {
if err == io.EOF {
return "", elements, nil
}
return "", nil, err
}

switch elem := t.(type) {
case xml.StartElement:
cdata, subElements, err := d.decode(elem.Attr)
if err != nil {
return "", nil, err
}

// Combine sub-elements and cdata.
var add interface{} = subElements
if len(subElements) == 0 {
add = cdata
} else if len(cdata) > 0 {
subElements["#text"] = cdata
}

// Add the data to the current object while taking into account
// if the current key already exists (in the case of lists).
key := d.key(elem.Name.Local)
value := elements[elem.Name.Local]
switch v := value.(type) {
case nil:
elements[key] = add
case []interface{}:
elements[key] = append(v, add)
default:
elements[key] = []interface{}{v, add}
}
case xml.CharData:
cdata = string(bytes.TrimSpace(elem.Copy()))
case xml.EndElement:
d.addAttributes(attrs, elements)
return cdata, elements, nil
}
}
}

func (d *Decoder) addAttributes(attrs []xml.Attr, m map[string]interface{}) {
for _, attr := range attrs {
key := d.attrKey(attr.Name.Local)
m[key] = attr.Value
}
}

func (d *Decoder) key(in string) string {
if d.lowercaseKeys {
return strings.ToLower(in)
}
return in
}

func (d *Decoder) attrKey(in string) string {
if d.prependHyphenToAttr {
return d.key("-" + in)
}
return d.key(in)
}
Loading