Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
efd6 committed Aug 30, 2023
1 parent 39c1659 commit b5ca67b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 11 deletions.
10 changes: 5 additions & 5 deletions libbeat/processors/add_remote_metadata/add_remote_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package add_process_metadata
package add_remote_metadata

import (
"context"
Expand Down Expand Up @@ -69,7 +69,7 @@ func New(cfg *conf.C) (beat.Processor, error) {
}
src, cancel, err := new(cfg)
if err != nil {
return nil, fmt.Errorf("fail to unpack the %s configuration for provider %s: %w", name, config.Provider, err)
return nil, fmt.Errorf("failed to unpack the %s configuration for provider %s: %w", name, config.Provider, err)
}

// Logging (each processor instance has a unique ID).
Expand All @@ -91,7 +91,7 @@ func New(cfg *conf.C) (beat.Processor, error) {
return &p, nil
}

// MetadatGetter is the interface implemented by metadata providers.
// MetadataGetter is the interface implemented by metadata providers.
type MetadataGetter interface {
GetMetadata(key string) (interface{}, error)
}
Expand All @@ -100,7 +100,7 @@ var providers = map[string]func(*conf.C) (MetadataGetter, context.CancelFunc, er
"map": newMapProvider,
}

// mapProvider is a simple providers based on a static map look-up.
// mapProvider is a simple provider based on a static map look-up.
type mapProvider map[string]interface{}

func newMapProvider(cfg *conf.C) (MetadataGetter, context.CancelFunc, error) {
Expand All @@ -126,7 +126,7 @@ func (p mapProvider) GetMetadata(k string) (interface{}, error) {
// noop is a no-op context.CancelFunc.
func noop() {}

// Run enriches the given event with the host meta data.
// Run enriches the given event with the host metadata.
func (p *addRemoteMetadata) Run(event *beat.Event) (*beat.Event, error) {
for _, k := range p.config.MatchKeys {
result, err := p.enrich(event, k)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package add_process_metadata
package add_remote_metadata

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion libbeat/processors/add_remote_metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package add_process_metadata
package add_remote_metadata

import (
"fmt"
Expand Down
107 changes: 107 additions & 0 deletions libbeat/processors/add_remote_metadata/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 add_remote_metadata

import (
"errors"
"testing"
)

var validateTests = []struct {
name string
cfg config
want error
}{
{
name: "invalid_provider",
cfg: config{Provider: "invalid_provider"},
want: errors.New("unknown provider: invalid_provider"),
},
{
name: "missing_fields_and_target",
cfg: config{Provider: "map"},
want: errors.New("no target field and no source fields specified"),
},
{
name: "missing_fields",
cfg: config{Provider: "map", Fields: []string{"src"}},
want: nil,
},
{
name: "missing_target",
cfg: config{Provider: "map", Target: "dst"},
want: nil,
},
}

func TestValidate(t *testing.T) {
for _, test := range validateTests {
t.Run(test.name, func(t *testing.T) {
got := test.cfg.Validate()
if !sameError(got, test.want) {
t.Errorf("unexpected error: got:%v want:%v", got, test.want)
}
})
}
}

var getMappingsTests = []struct {
name string
cfg config
want error
}{
{
name: "valid",
cfg: config{Provider: "map", Fields: []string{"src.field"}},
want: nil,
},
{
name: "type_conflict",
// Note that the order of fields here matters; it probably
// should not, but this is current mapstr.M.Put behaviour.
cfg: config{Provider: "map", Fields: []string{"src", "src.field"}},
want: errors.New("failed to set mapping 'src.field' -> 'src.field': expected map but type is string"),
},
{
name: "repeated_field",
cfg: config{Provider: "map", Fields: []string{"src.field", "src.field"}},
want: errors.New("field 'src.field' repeated"),
},
}

func TestGetMappings(t *testing.T) {
for _, test := range getMappingsTests {
t.Run(test.name, func(t *testing.T) {
_, got := test.cfg.getMappings()
if !sameError(got, test.want) {
t.Errorf("unexpected error: got:%v want:%v", got, test.want)
}
})
}
}

func sameError(a, b error) bool {
switch {
case a == nil && b == nil:
return true
case a == nil, b == nil:
return false
default:
return a.Error() == b.Error()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ It has the following settings:
`provider`:: The name of the provider used to collect and apply the metadata.
Currently the only provider is the `map` provider.

`match_pids`:: List of fields to lookup for a PID. The processor will
`match_keys`:: List of fields to lookup for a match. The processor will
search the list sequentially until the field is found in the current event, and
the PID lookup will be applied to the value of this field.
the key lookup will be applied to the value of this field.

`target`:: (Optional) Destination prefix where the `remote` object will be
`target`:: (Optional) Destination prefix where the metadata object will be
created. The default is the event's root.

`include_fields`:: (Optional) List of fields to add. By default, the processor
will add all the available fields except `remote.env`.

`ignore_missing`:: (Optional) When set to `false`, events that don't contain any
of the fields in match_pids will be discarded and an error will be generated. By
of the fields in `match_keys` will be discarded and an error will be generated. By
default, this condition is ignored.

`overwrite_keys`:: (Optional) By default, if a target field already exists, it
Expand Down

0 comments on commit b5ca67b

Please sign in to comment.