diff --git a/CHANGELOG-developer.next.asciidoc b/CHANGELOG-developer.next.asciidoc index 4d20151d6cb..e4623de39b1 100644 --- a/CHANGELOG-developer.next.asciidoc +++ b/CHANGELOG-developer.next.asciidoc @@ -60,6 +60,7 @@ The list below covers the major changes between 7.0.0-rc2 and main only. - libbeat.logp package forces ECS compliant logs. Logs are JSON formatted. Options to enable ECS/JSON have been removed. {issue}15544[15544] {pull}28573[28573] - Removed deprecated disk spool from Beats. Use disk queue instead. {pull}28869[28869] - Wildcard fields no longer have a default ignore_above setting of 1024. {issue}30096[30096] {pull}30668[30668] +- Remove `common.MapStr` and use `mapstr.M` from `github.com/elastic/elastic-agent-libs` instead. {pull}31420[31420] ==== Bugfixes diff --git a/auditbeat/cmd/root.go b/auditbeat/cmd/root.go index 79c74cf0279..d47e49ea36b 100644 --- a/auditbeat/cmd/root.go +++ b/auditbeat/cmd/root.go @@ -24,11 +24,11 @@ import ( "github.com/elastic/beats/v7/auditbeat/core" "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/publisher/processing" "github.com/elastic/beats/v7/metricbeat/beater" "github.com/elastic/beats/v7/metricbeat/mb/module" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -46,8 +46,8 @@ var ShowCmd = &cobra.Command{ } // withECSVersion is a modifier that adds ecs.version to events. -var withECSVersion = processing.WithFields(common.MapStr{ - "ecs": common.MapStr{ +var withECSVersion = processing.WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) diff --git a/auditbeat/core/eventmod.go b/auditbeat/core/eventmod.go index 662422f7e9d..02e980bb10c 100644 --- a/auditbeat/core/eventmod.go +++ b/auditbeat/core/eventmod.go @@ -18,15 +18,15 @@ package core import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // AddDatasetToEvent adds dataset information to the event. In particular this // adds the module name under dataset.module. func AddDatasetToEvent(module, metricSet string, event *mb.Event) { if event.RootFields == nil { - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} } event.RootFields.Put("event.module", module) diff --git a/auditbeat/module/auditd/audit_linux.go b/auditbeat/module/auditd/audit_linux.go index 63febb6fcec..9a4d8b0fa33 100644 --- a/auditbeat/module/auditd/audit_linux.go +++ b/auditbeat/module/auditd/audit_linux.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/monitoring" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-libaudit/v2" "github.com/elastic/go-libaudit/v2/aucoalesce" "github.com/elastic/go-libaudit/v2/auparse" @@ -514,7 +515,7 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event auditEvent, err := aucoalesce.CoalesceMessages(msgs) if err != nil { // Add messages on error so that it's possible to debug the problem. - out := mb.Event{RootFields: common.MapStr{}, Error: err} + out := mb.Event{RootFields: mapstr.M{}, Error: err} addEventOriginal(msgs, out.RootFields) return out } @@ -529,14 +530,14 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event } out := mb.Event{ Timestamp: auditEvent.Timestamp, - RootFields: common.MapStr{ - "event": common.MapStr{ + RootFields: mapstr.M{ + "event": mapstr.M{ "category": auditEvent.Category.String(), "action": auditEvent.Summary.Action, "outcome": eventOutcome, }, }, - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "message_type": strings.ToLower(auditEvent.Type.String()), "sequence": auditEvent.Sequence, "result": auditEvent.Result, @@ -602,7 +603,7 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event } // Copy user.*/group.* fields from event - setECSEntity := func(key string, ent aucoalesce.ECSEntityData, root common.MapStr, set common.StringSet) { + setECSEntity := func(key string, ent aucoalesce.ECSEntityData, root mapstr.M, set common.StringSet) { if ent.ID == "" && ent.Name == "" { return } @@ -637,7 +638,7 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event out.RootFields.Put("related.user", userSet.ToSlice()) } } - getStringField := func(key string, m common.MapStr) (str string) { + getStringField := func(key string, m mapstr.M) (str string) { if asIf, _ := m.GetValue(key); asIf != nil { str, _ = asIf.(string) } @@ -645,7 +646,7 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event } // Remove redundant user.effective.* when it's the same as user.* - removeRedundantEntity := func(target, original string, m common.MapStr) bool { + removeRedundantEntity := func(target, original string, m mapstr.M) bool { for _, suffix := range []string{".id", ".name"} { if value := getStringField(original+suffix, m); value != "" && getStringField(target+suffix, m) == value { m.Delete(target) @@ -658,7 +659,7 @@ func buildMetricbeatEvent(msgs []*auparse.AuditMessage, config Config) mb.Event return out } -func normalizeEventFields(event *aucoalesce.Event, m common.MapStr) { +func normalizeEventFields(event *aucoalesce.Event, m mapstr.M) { m.Put("event.kind", "event") if len(event.ECS.Event.Category) > 0 { m.Put("event.category", event.ECS.Event.Category) @@ -671,8 +672,8 @@ func normalizeEventFields(event *aucoalesce.Event, m common.MapStr) { } } -func addUser(u aucoalesce.User, m common.MapStr) { - user := common.MapStr{} +func addUser(u aucoalesce.User, m mapstr.M) { + user := mapstr.M{} m.Put("user", user) for id, value := range u.IDs { @@ -733,12 +734,12 @@ func addUser(u aucoalesce.User, m common.MapStr) { } } -func addProcess(p aucoalesce.Process, m common.MapStr) { +func addProcess(p aucoalesce.Process, m mapstr.M) { if p.IsEmpty() { return } - process := common.MapStr{} + process := mapstr.M{} m.Put("process", process) if p.PID != "" { if pid, err := strconv.Atoi(p.PID); err == nil { @@ -747,7 +748,7 @@ func addProcess(p aucoalesce.Process, m common.MapStr) { } if p.PPID != "" { if ppid, err := strconv.Atoi(p.PPID); err == nil { - process["parent"] = common.MapStr{ + process["parent"] = mapstr.M{ "pid": ppid, } } @@ -769,12 +770,12 @@ func addProcess(p aucoalesce.Process, m common.MapStr) { } } -func addFile(f *aucoalesce.File, m common.MapStr) { +func addFile(f *aucoalesce.File, m mapstr.M) { if f == nil { return } - file := common.MapStr{} + file := mapstr.M{} m.Put("file", file) if f.Path != "" { file["path"] = f.Path @@ -805,12 +806,12 @@ func addFile(f *aucoalesce.File, m common.MapStr) { } } -func addAddress(addr *aucoalesce.Address, key string, m common.MapStr) { +func addAddress(addr *aucoalesce.Address, key string, m mapstr.M) { if addr == nil { return } - address := common.MapStr{} + address := mapstr.M{} m.Put(key, address) if addr.Hostname != "" { address["domain"] = addr.Hostname @@ -826,18 +827,18 @@ func addAddress(addr *aucoalesce.Address, key string, m common.MapStr) { } } -func addNetwork(net *aucoalesce.Network, m common.MapStr) { +func addNetwork(net *aucoalesce.Network, m mapstr.M) { if net == nil { return } - network := common.MapStr{ + network := mapstr.M{ "direction": net.Direction, } m.Put("network", network) } -func addEventOriginal(msgs []*auparse.AuditMessage, m common.MapStr) { +func addEventOriginal(msgs []*auparse.AuditMessage, m mapstr.M) { const key = "event.original" if len(msgs) == 0 { return @@ -853,8 +854,8 @@ func addEventOriginal(msgs []*auparse.AuditMessage, m common.MapStr) { m.Put(key, rawMsgs) } -func createAuditdData(data map[string]string) common.MapStr { - out := make(common.MapStr, len(data)) +func createAuditdData(data map[string]string) mapstr.M { + out := make(mapstr.M, len(data)) for key, v := range data { if strings.HasPrefix(key, "socket_") { out.Put("socket."+key[7:], v) diff --git a/auditbeat/module/auditd/audit_linux_test.go b/auditbeat/module/auditd/audit_linux_test.go index 437acd6df55..0711bab4f8e 100644 --- a/auditbeat/module/auditd/audit_linux_test.go +++ b/auditbeat/module/auditd/audit_linux_test.go @@ -34,11 +34,11 @@ import ( "github.com/prometheus/procfs" "github.com/elastic/beats/v7/auditbeat/core" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/mapping" "github.com/elastic/beats/v7/metricbeat/mb" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-libaudit/v2" "github.com/elastic/go-libaudit/v2/auparse" ) @@ -138,7 +138,7 @@ func TestLoginType(t *testing.T) { return events[i].ModuleFields["sequence"].(uint32) < events[j].ModuleFields["sequence"].(uint32) }) - for idx, expected := range []common.MapStr{ + for idx, expected := range []mapstr.M{ { "event.category": []string{"authentication"}, "event.type": []string{"start"}, @@ -175,7 +175,7 @@ func TestLoginType(t *testing.T) { assert.Equal(t, v, cur, msg) } else { _, err := beatEvent.GetValue(k) - assert.Equal(t, common.ErrKeyNotFound, err, msg) + assert.Equal(t, mapstr.ErrKeyNotFound, err, msg) } } } diff --git a/auditbeat/module/auditd/golden_files_test.go b/auditbeat/module/auditd/golden_files_test.go index 47e99cf6b5e..17625c0b558 100644 --- a/auditbeat/module/auditd/golden_files_test.go +++ b/auditbeat/module/auditd/golden_files_test.go @@ -35,10 +35,10 @@ import ( "github.com/stretchr/testify/assert" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-libaudit/v2" "github.com/elastic/go-libaudit/v2/aucoalesce" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" ) @@ -88,7 +88,7 @@ func readLines(path string) (lines []string, err error) { return lines, scanner.Err() } -func readGoldenFile(t testing.TB, path string) (events []common.MapStr) { +func readGoldenFile(t testing.TB, path string) (events []mapstr.M) { data, err := ioutil.ReadFile(path) if err != nil { t.Fatalf("can't read golden file '%s': %v", path, err) @@ -99,9 +99,9 @@ func readGoldenFile(t testing.TB, path string) (events []common.MapStr) { return } -func normalize(t testing.TB, events []mb.Event) (norm []common.MapStr) { +func normalize(t testing.TB, events []mb.Event) (norm []mapstr.M) { for _, ev := range events { - var output common.MapStr + var output mapstr.M data, err := json.Marshal(ev.BeatEvent(moduleName, metricsetName).Fields) if err != nil { t.Fatal(err) diff --git a/auditbeat/module/file_integrity/event.go b/auditbeat/module/file_integrity/event.go index 5e423d04fec..bf90cf16edb 100644 --- a/auditbeat/module/file_integrity/event.go +++ b/auditbeat/module/file_integrity/event.go @@ -39,9 +39,9 @@ import ( "golang.org/x/crypto/blake2b" "golang.org/x/crypto/sha3" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/file" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Source identifies the source of an event (i.e. what triggered it). @@ -243,13 +243,13 @@ func getDriveLetter(path string) string { } func buildMetricbeatEvent(e *Event, existedBefore bool) mb.Event { - file := common.MapStr{ + file := mapstr.M{ "path": e.Path, } out := mb.Event{ Timestamp: e.Timestamp, Took: e.rtt, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "file": file, }, } @@ -309,7 +309,7 @@ func buildMetricbeatEvent(e *Event, existedBefore bool) mb.Event { } if len(e.Hashes) > 0 { - hashes := make(common.MapStr, len(e.Hashes)) + hashes := make(mapstr.M, len(e.Hashes)) for hashType, digest := range e.Hashes { hashes[string(hashType)] = digest } diff --git a/auditbeat/module/file_integrity/event_test.go b/auditbeat/module/file_integrity/event_test.go index b45bd4895fc..417271ab0fe 100644 --- a/auditbeat/module/file_integrity/event_test.go +++ b/auditbeat/module/file_integrity/event_test.go @@ -31,7 +31,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var testEventTime = time.Now().UTC() @@ -520,7 +520,7 @@ func mustDecodeHex(v string) []byte { return data } -func assertHasKey(t testing.TB, m common.MapStr, key string) bool { +func assertHasKey(t testing.TB, m mapstr.M, key string) bool { t.Helper() found, err := m.HasKey(key) if err != nil || !found { diff --git a/filebeat/autodiscover/builder/hints/config_test.go b/filebeat/autodiscover/builder/hints/config_test.go index ce871a6f6cb..fc29a4adffe 100644 --- a/filebeat/autodiscover/builder/hints/config_test.go +++ b/filebeat/autodiscover/builder/hints/config_test.go @@ -23,11 +23,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestUnpackCopiesDefault(t *testing.T) { - userCfg := common.MustNewConfigFrom(common.MapStr{ - "default_config": common.MapStr{ + userCfg := common.MustNewConfigFrom(mapstr.M{ + "default_config": mapstr.M{ "type": "container", "paths": []string{ "/var/log/containers/*${data.kubernetes.container.id}.log", diff --git a/filebeat/autodiscover/builder/hints/logs.go b/filebeat/autodiscover/builder/hints/logs.go index 9c29852dff3..ee5b4ce2702 100644 --- a/filebeat/autodiscover/builder/hints/logs.go +++ b/filebeat/autodiscover/builder/hints/logs.go @@ -21,6 +21,7 @@ import ( "fmt" "regexp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" "github.com/elastic/beats/v7/filebeat/fileset" @@ -73,9 +74,9 @@ func NewLogHints(cfg *common.Config) (autodiscover.Builder, error) { // Create config based on input hints in the bus event func (l *logHints) CreateConfig(event bus.Event, options ...ucfg.Option) []*common.Config { - var hints common.MapStr + var hints mapstr.M if hintsIfc, found := event["hints"]; found { - hints, _ = hintsIfc.(common.MapStr) + hints, _ = hintsIfc.(mapstr.M) } // Hint must be explicitly enabled when default_config sets enabled=false. @@ -106,7 +107,7 @@ func (l *logHints) CreateConfig(event bus.Event, options ...ucfg.Option) []*comm config, _ := common.NewConfigFrom(l.config.DefaultConfig) config.Remove("enabled", -1) - tempCfg := common.MapStr{} + tempCfg := mapstr.M{} mline := l.getMultiline(h) if len(mline) != 0 { tempCfg.Put(multiline, mline) @@ -166,37 +167,37 @@ func (l *logHints) CreateConfig(event bus.Event, options ...ucfg.Option) []*comm return template.ApplyConfigTemplate(event, configs) } -func (l *logHints) getMultiline(hints common.MapStr) common.MapStr { +func (l *logHints) getMultiline(hints mapstr.M) mapstr.M { return builder.GetHintMapStr(hints, l.config.Key, multiline) } -func (l *logHints) getIncludeLines(hints common.MapStr) []string { +func (l *logHints) getIncludeLines(hints mapstr.M) []string { return builder.GetHintAsList(hints, l.config.Key, includeLines) } -func (l *logHints) getExcludeLines(hints common.MapStr) []string { +func (l *logHints) getExcludeLines(hints mapstr.M) []string { return builder.GetHintAsList(hints, l.config.Key, excludeLines) } -func (l *logHints) getModule(hints common.MapStr) string { +func (l *logHints) getModule(hints mapstr.M) string { module := builder.GetHintString(hints, l.config.Key, "module") // for security, strip module name return validModuleNames.ReplaceAllString(module, "") } -func (l *logHints) getInputsConfigs(hints common.MapStr) []common.MapStr { +func (l *logHints) getInputsConfigs(hints mapstr.M) []mapstr.M { return builder.GetHintAsConfigs(hints, l.config.Key) } -func (l *logHints) getProcessors(hints common.MapStr) []common.MapStr { +func (l *logHints) getProcessors(hints mapstr.M) []mapstr.M { return builder.GetProcessors(hints, l.config.Key) } -func (l *logHints) getPipeline(hints common.MapStr) string { +func (l *logHints) getPipeline(hints mapstr.M) string { return builder.GetHintString(hints, l.config.Key, "pipeline") } -func (l *logHints) getJSONOptions(hints common.MapStr) common.MapStr { +func (l *logHints) getJSONOptions(hints mapstr.M) mapstr.M { return builder.GetHintMapStr(hints, l.config.Key, json) } @@ -206,7 +207,7 @@ type filesetConfig struct { } // Return a map containing filesets -> enabled & stream (stdout, stderr, all) -func (l *logHints) getFilesets(hints common.MapStr, module string) map[string]*filesetConfig { +func (l *logHints) getFilesets(hints mapstr.M, module string) map[string]*filesetConfig { var configured bool filesets := make(map[string]*filesetConfig) @@ -251,20 +252,20 @@ func (l *logHints) getFilesets(hints common.MapStr, module string) map[string]*f return filesets } -func (l *logHints) getInputs(hints common.MapStr) []common.MapStr { +func (l *logHints) getInputs(hints mapstr.M) []mapstr.M { modules := builder.GetHintsAsList(hints, l.config.Key) - var output []common.MapStr + var output []mapstr.M for _, mod := range modules { - output = append(output, common.MapStr{ + output = append(output, mapstr.M{ l.config.Key: mod, }) } // Generate this so that no hints with completely valid templates work if len(output) == 0 { - output = append(output, common.MapStr{ - l.config.Key: common.MapStr{}, + output = append(output, mapstr.M{ + l.config.Key: mapstr.M{}, }) } diff --git a/filebeat/autodiscover/builder/hints/logs_test.go b/filebeat/autodiscover/builder/hints/logs_test.go index ae1ca208313..7ddebc59b31 100644 --- a/filebeat/autodiscover/builder/hints/logs_test.go +++ b/filebeat/autodiscover/builder/hints/logs_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/paths" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints(t *testing.T) { @@ -55,26 +56,26 @@ func TestGenerateHints(t *testing.T) { config *common.Config event bus.Event len int - result []common.MapStr + result []mapstr.M }{ { msg: "Default config is correct", config: defaultCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "paths": []interface{}{"/var/lib/docker/containers/abc/*-json.log"}, "type": "container", @@ -86,44 +87,44 @@ func TestGenerateHints(t *testing.T) { config: defaultDisabled, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, len: 0, - result: []common.MapStr{}, + result: []mapstr.M{}, }, { msg: "Hint to enable when disabled by default works", config: defaultDisabled, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "enabled": "true", "exclude_lines": "^test2, ^test3", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "type": "container", "paths": []interface{}{"/var/lib/docker/containers/abc/*-json.log"}, @@ -135,46 +136,46 @@ func TestGenerateHints(t *testing.T) { msg: "Hints without host should return nothing", config: customCfg, event: bus.Event{ - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", }, }, }, len: 0, - result: []common.MapStr{}, + result: []mapstr.M{}, }, { msg: "Hints with logs.disable should return nothing", config: customCfg, event: bus.Event{ - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "disable": "true", }, }, }, len: 0, - result: []common.MapStr{}, + result: []mapstr.M{}, }, { msg: "Empty event hints should return default config", config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "type": "docker", "containers": map[string]interface{}{ @@ -189,25 +190,25 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "include_lines": "^test, ^test1", "exclude_lines": "^test2, ^test3", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "type": "docker", "containers": map[string]interface{}{ @@ -224,29 +225,29 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ - "1": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ + "1": mapstr.M{ "exclude_lines": "^test1, ^test2", }, - "2": common.MapStr{ + "2": mapstr.M{ "include_lines": "^test1, ^test2", }, }, }, }, len: 2, - result: []common.MapStr{ + result: []mapstr.M{ { "type": "docker", "containers": map[string]interface{}{ @@ -270,19 +271,19 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", "negate": "true", }, @@ -290,7 +291,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "type": "docker", "containers": map[string]interface{}{ @@ -309,24 +310,24 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "raw": "[{\"containers\":{\"ids\":[\"${data.container.id}\"]},\"multiline\":{\"negate\":\"true\",\"pattern\":\"^test\"},\"type\":\"docker\"}]", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "type": "docker", "containers": map[string]interface{}{ @@ -344,31 +345,31 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ - "processors": common.MapStr{ - "1": common.MapStr{ - "dissect": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ + "processors": mapstr.M{ + "1": mapstr.M{ + "dissect": mapstr.M{ "tokenizer": "%{key1} %{key2}", }, }, - "drop_event": common.MapStr{}, + "drop_event": mapstr.M{}, }, }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "type": "docker", "containers": map[string]interface{}{ @@ -393,24 +394,24 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "apache", "error": map[string]interface{}{ @@ -443,25 +444,25 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", "fileset": "access", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "apache", "access": map[string]interface{}{ @@ -494,18 +495,18 @@ func TestGenerateHints(t *testing.T) { config: customCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", "fileset.stdout": "access", "fileset.stderr": "error", @@ -513,7 +514,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "apache", "access": map[string]interface{}{ @@ -546,24 +547,24 @@ func TestGenerateHints(t *testing.T) { config: defaultCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "apache", "error": map[string]interface{}{ @@ -594,25 +595,25 @@ func TestGenerateHints(t *testing.T) { config: defaultCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", "fileset": "access", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "apache", "access": map[string]interface{}{ @@ -643,18 +644,18 @@ func TestGenerateHints(t *testing.T) { config: defaultCfg, event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", "fileset.stdout": "access", "fileset.stderr": "error", @@ -662,7 +663,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "apache", "access": map[string]interface{}{ @@ -704,9 +705,9 @@ func TestGenerateHints(t *testing.T) { cfgs := l.CreateConfig(test.event) assert.Equal(t, test.len, len(cfgs), test.msg) - configs := make([]common.MapStr, 0) + configs := make([]mapstr.M, 0) for _, cfg := range cfgs { - config := common.MapStr{} + config := mapstr.M{} err := cfg.Unpack(&config) ok := assert.Nil(t, err, test.msg) if !ok { @@ -724,30 +725,30 @@ func TestGenerateHintsWithPaths(t *testing.T) { event bus.Event path string len int - result common.MapStr + result mapstr.M }{ { msg: "Empty event hints should return default config", event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "pod", "uid": "12345", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, path: "/var/lib/docker/containers/${data.kubernetes.container.id}/*-json.log", len: 1, - result: common.MapStr{ + result: mapstr.M{ "type": "docker", "containers": map[string]interface{}{ "paths": []interface{}{"/var/lib/docker/containers/abc/*-json.log"}, @@ -759,36 +760,36 @@ func TestGenerateHintsWithPaths(t *testing.T) { msg: "Hint with processors config must have a processors in the input config", event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "pod", "uid": "12345", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ - "processors": common.MapStr{ - "1": common.MapStr{ - "dissect": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ + "processors": mapstr.M{ + "1": mapstr.M{ + "dissect": mapstr.M{ "tokenizer": "%{key1} %{key2}", }, }, - "drop_event": common.MapStr{}, + "drop_event": mapstr.M{}, }, }, }, }, len: 1, path: "/var/log/pods/${data.kubernetes.pod.uid}/${data.kubernetes.container.name}/*.log", - result: common.MapStr{ + result: mapstr.M{ "type": "docker", "containers": map[string]interface{}{ "paths": []interface{}{"/var/log/pods/12345/foobar/*.log"}, @@ -810,29 +811,29 @@ func TestGenerateHintsWithPaths(t *testing.T) { msg: "Hint with module should attach input to its filesets", event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "pod", "uid": "12345", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", }, }, }, len: 1, path: "/var/log/pods/${data.kubernetes.pod.uid}/${data.kubernetes.container.name}/*.log", - result: common.MapStr{ + result: mapstr.M{ "module": "apache", "error": map[string]interface{}{ "enabled": true, @@ -862,22 +863,22 @@ func TestGenerateHintsWithPaths(t *testing.T) { msg: "Hint with module should honor defined filesets", event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "pod", "uid": "12345", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "module": "apache", "fileset": "access", }, @@ -885,7 +886,7 @@ func TestGenerateHintsWithPaths(t *testing.T) { }, len: 1, path: "/var/log/pods/${data.kubernetes.pod.uid}/${data.kubernetes.container.name}/*.log", - result: common.MapStr{ + result: mapstr.M{ "module": "apache", "access": map[string]interface{}{ "enabled": true, @@ -940,7 +941,7 @@ func TestGenerateHintsWithPaths(t *testing.T) { cfgs := l.CreateConfig(test.event) require.Equal(t, test.len, len(cfgs), test.msg) if test.len != 0 { - config := common.MapStr{} + config := mapstr.M{} err := cfgs[0].Unpack(&config) assert.Nil(t, err, test.msg) diff --git a/filebeat/channel/interface.go b/filebeat/channel/interface.go index 9df9ff584b3..4c183145172 100644 --- a/filebeat/channel/interface.go +++ b/filebeat/channel/interface.go @@ -26,7 +26,7 @@ import ( type Factory func(beat.PipelineConnector) Connector // Connector creates an Outlet connecting the event publishing with some internal pipeline. -// type Connector func(*common.Config, *common.MapStrPointer) (Outleter, error) +// type Connector func(*common.Config, *mapstr.Pointer) (Outleter, error) type Connector interface { Connect(*common.Config) (Outleter, error) ConnectWith(*common.Config, beat.ClientConfig) (Outleter, error) diff --git a/filebeat/channel/runner.go b/filebeat/channel/runner.go index 1019dc2ff27..692e467941e 100644 --- a/filebeat/channel/runner.go +++ b/filebeat/channel/runner.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/add_formatted_index" "github.com/elastic/beats/v7/libbeat/publisher/pipetool" + "github.com/elastic/elastic-agent-libs/mapstr" ) type onCreateFactory struct { @@ -38,7 +39,7 @@ type onCreateWrapper func(cfgfile.RunnerFactory, beat.PipelineConnector, *common // for the publisher pipeline. type commonInputConfig struct { // event processing - common.EventMetadata `config:",inline"` // Fields and tags to add to events. + mapstr.EventMetadata `config:",inline"` // Fields and tags to add to events. Processors processors.PluginConfig `config:"processors"` KeepNull bool `config:"keep_null"` @@ -155,7 +156,7 @@ func newCommonConfigEditor( setOptional(fields, "service.type", serviceType) setOptional(fields, "input.type", config.Type) if config.Module != "" { - event := common.MapStr{"module": config.Module} + event := mapstr.M{"module": config.Module} if config.Fileset != "" { event["dataset"] = config.Module + "." + config.Fileset } @@ -188,7 +189,7 @@ func newCommonConfigEditor( }, nil } -func setOptional(to common.MapStr, key string, value string) { +func setOptional(to mapstr.M, key string, value string) { if value != "" { to.Put(key, value) } diff --git a/filebeat/channel/runner_test.go b/filebeat/channel/runner_test.go index cf42a38e4b8..0a0fa4332ef 100644 --- a/filebeat/channel/runner_test.go +++ b/filebeat/channel/runner_test.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/actions" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestProcessorsForConfig(t *testing.T) { @@ -84,8 +85,8 @@ func TestProcessorsForConfig(t *testing.T) { "Set field in ClientConfig": { clientCfg: beat.ClientConfig{ Processing: beat.ProcessingConfig{ - Processor: makeProcessors(actions.NewAddFields(common.MapStr{ - "fields": common.MapStr{"testField": "clientConfig"}, + Processor: makeProcessors(actions.NewAddFields(mapstr.M{ + "fields": mapstr.M{"testField": "clientConfig"}, }, false, true)), }, }, @@ -97,8 +98,8 @@ func TestProcessorsForConfig(t *testing.T) { configStr: `processors: [add_fields: {fields: {testField: inputConfig}}]`, clientCfg: beat.ClientConfig{ Processing: beat.ProcessingConfig{ - Processor: makeProcessors(actions.NewAddFields(common.MapStr{ - "fields": common.MapStr{"testField": "clientConfig"}, + Processor: makeProcessors(actions.NewAddFields(mapstr.M{ + "fields": mapstr.M{"testField": "clientConfig"}, }, false, true)), }, }, @@ -109,7 +110,7 @@ func TestProcessorsForConfig(t *testing.T) { } for description, test := range testCases { if test.event.Fields == nil { - test.event.Fields = common.MapStr{} + test.event.Fields = mapstr.M{} } config, err := common.NewConfigFrom(test.configStr) if err != nil { @@ -194,7 +195,7 @@ type setRawIndex struct { func (p *setRawIndex) Run(event *beat.Event) (*beat.Event, error) { if event.Meta == nil { - event.Meta = common.MapStr{} + event.Meta = mapstr.M{} } event.Meta[events.FieldMetaRawIndex] = p.indexStr return event, nil diff --git a/filebeat/input/container/input.go b/filebeat/input/container/input.go index b208cd16013..179eae7911b 100644 --- a/filebeat/input/container/input.go +++ b/filebeat/input/container/input.go @@ -22,6 +22,7 @@ import ( "github.com/elastic/beats/v7/filebeat/input" "github.com/elastic/beats/v7/filebeat/input/log" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" ) @@ -45,7 +46,7 @@ func NewInput( return nil, errors.Wrap(err, "reading container input config") } - err := cfg.Merge(common.MapStr{ + err := cfg.Merge(mapstr.M{ "docker-json.partial": true, "docker-json.cri_flags": true, diff --git a/filebeat/input/container/input_test.go b/filebeat/input/container/input_test.go index 068f7400f98..011167c350f 100644 --- a/filebeat/input/container/input_test.go +++ b/filebeat/input/container/input_test.go @@ -26,11 +26,11 @@ import ( "testing" "github.com/elastic/beats/v7/filebeat/input/inputtest" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "paths": path.Join(os.TempDir(), "logs", "*.log"), } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) diff --git a/filebeat/input/inputtest/input.go b/filebeat/input/inputtest/input.go index 56af21bd4d0..d6ead59b44b 100644 --- a/filebeat/input/inputtest/input.go +++ b/filebeat/input/inputtest/input.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/resources" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Outlet is an empty outlet for testing. @@ -47,7 +48,7 @@ var Connector = channel.ConnectorFunc( // AssertNotStartedInputCanBeDone checks that the context of an input can be // done before starting the input, and it doesn't leak goroutines. This is // important to confirm that leaks don't happen with CheckConfig. -func AssertNotStartedInputCanBeDone(t *testing.T, factory input.Factory, configMap *common.MapStr) { +func AssertNotStartedInputCanBeDone(t *testing.T, factory input.Factory, configMap *mapstr.M) { goroutines := resources.NewGoroutinesChecker() defer goroutines.Check(t) diff --git a/filebeat/input/journald/input_filtering_test.go b/filebeat/input/journald/input_filtering_test.go index 3018133ab76..be7bca0253c 100644 --- a/filebeat/input/journald/input_filtering_test.go +++ b/filebeat/input/journald/input_filtering_test.go @@ -25,7 +25,7 @@ import ( "path" "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestInputSyslogIdentifier(t *testing.T) { @@ -51,7 +51,7 @@ func TestInputSyslogIdentifier(t *testing.T) { for name, testCase := range tests { t.Run(name, func(t *testing.T) { env := newInputTestingEnvironment(t) - inp := env.mustCreateInput(common.MapStr{ + inp := env.mustCreateInput(mapstr.M{ "paths": []string{path.Join("testdata", "input-multiline-parser.journal")}, "syslog_identifiers": testCase.identifiers, }) @@ -108,7 +108,7 @@ func TestInputUnits(t *testing.T) { for name, testCase := range tests { t.Run(name, func(t *testing.T) { env := newInputTestingEnvironment(t) - inp := env.mustCreateInput(common.MapStr{ + inp := env.mustCreateInput(mapstr.M{ "paths": []string{path.Join("testdata", "input-multiline-parser.journal")}, "units": testCase.units, "kernel": testCase.kernel, @@ -197,7 +197,7 @@ func TestInputIncludeMatches(t *testing.T) { for name, testCase := range tests { t.Run(name, func(t *testing.T) { env := newInputTestingEnvironment(t) - inp := env.mustCreateInput(common.MapStr{ + inp := env.mustCreateInput(mapstr.M{ "paths": []string{path.Join("testdata", "input-multiline-parser.journal")}, "include_matches": testCase.includeMatches, }) diff --git a/filebeat/input/journald/input_parsers_test.go b/filebeat/input/journald/input_parsers_test.go index 3c6d1ad5780..74d752d316d 100644 --- a/filebeat/input/journald/input_parsers_test.go +++ b/filebeat/input/journald/input_parsers_test.go @@ -25,7 +25,7 @@ import ( "path" "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TestInputParsers ensures journald input support parsers, @@ -35,12 +35,12 @@ func TestInputParsers(t *testing.T) { inputParsersExpected := []string{"1st line\n2nd line\n3rd line", "4th line\n5th line\n6th line"} env := newInputTestingEnvironment(t) - inp := env.mustCreateInput(common.MapStr{ + inp := env.mustCreateInput(mapstr.M{ "paths": []string{path.Join("testdata", "input-multiline-parser.journal")}, "include_matches.match": []string{"_SYSTEMD_USER_UNIT=log-service.service"}, - "parsers": []common.MapStr{ + "parsers": []mapstr.M{ { - "multiline": common.MapStr{ + "multiline": mapstr.M{ "type": "count", "count_lines": 3, }, diff --git a/filebeat/input/journald/input_test.go b/filebeat/input/journald/input_test.go index f6578ccf01b..be45dde8a3b 100644 --- a/filebeat/input/journald/input_test.go +++ b/filebeat/input/journald/input_test.go @@ -26,7 +26,7 @@ import ( "path" "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestInputFieldsTranslation(t *testing.T) { @@ -49,7 +49,7 @@ func TestInputFieldsTranslation(t *testing.T) { t.Run(name, func(t *testing.T) { env := newInputTestingEnvironment(t) - inp := env.mustCreateInput(common.MapStr{ + inp := env.mustCreateInput(mapstr.M{ "paths": []string{path.Join("testdata", "input-multiline-parser.journal")}, "include_matches.match": []string{"_SYSTEMD_USER_UNIT=log-service.service"}, "save_remote_hostname": tc.saveRemoteHostname, diff --git a/filebeat/input/journald/pkg/journalfield/conv.go b/filebeat/input/journald/pkg/journalfield/conv.go index 703a1c677f6..68d95fc9a52 100644 --- a/filebeat/input/journald/pkg/journalfield/conv.go +++ b/filebeat/input/journald/pkg/journalfield/conv.go @@ -23,8 +23,8 @@ import ( "strconv" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // FieldConversion provides the mappings and conversion rules for raw fields of journald entries. @@ -38,7 +38,7 @@ type Conversion struct { } // Converter applis configured conversion rules to journald entries, producing -// a new common.MapStr. +// a new mapstr.M. type Converter struct { log *logp.Logger conversions FieldConversion @@ -54,21 +54,21 @@ func NewConverter(log *logp.Logger, conversions FieldConversion) *Converter { return &Converter{log: log, conversions: conversions} } -// Convert creates a common.MapStr from the raw fields by applying the +// Convert creates a mapstr.M from the raw fields by applying the // configured conversion rules. // Field type conversion errors are logged to at debug level and the original // value is added to the map. -func (c *Converter) Convert(entryFields map[string]string) common.MapStr { - fields := common.MapStr{} - var custom common.MapStr +func (c *Converter) Convert(entryFields map[string]string) mapstr.M { + fields := mapstr.M{} + var custom mapstr.M for entryKey, v := range entryFields { if fieldConversionInfo, ok := c.conversions[entryKey]; !ok { if custom == nil { - custom = common.MapStr{} + custom = mapstr.M{} } normalized := strings.ToLower(strings.TrimLeft(entryKey, "_")) - custom.Put(normalized, v) + _, _ = custom.Put(normalized, v) } else if !fieldConversionInfo.Dropped { value, err := convertValue(fieldConversionInfo, v) if err != nil { @@ -76,13 +76,13 @@ func (c *Converter) Convert(entryFields map[string]string) common.MapStr { c.log.Debugf("Journald mapping error: %v", err) } for _, name := range fieldConversionInfo.Names { - fields.Put(name, value) + _, _ = fields.Put(name, value) } } } if len(custom) != 0 { - fields.Put("journald.custom", custom) + _, _ = fields.Put("journald.custom", custom) } return withECSEnrichment(fields) @@ -99,7 +99,7 @@ func convertValue(fc Conversion, value string) (interface{}, error) { s := strings.Split(value, ",") v, err = strconv.ParseInt(s[0], 10, 64) if err != nil { - return value, fmt.Errorf("failed to convert field %s \"%v\" to int: %v", fc.Names[0], value, err) + return value, fmt.Errorf("failed to convert field %s \"%v\" to int: %w", fc.Names[0], value, err) } } return v, nil @@ -107,7 +107,7 @@ func convertValue(fc Conversion, value string) (interface{}, error) { return value, nil } -func withECSEnrichment(fields common.MapStr) common.MapStr { +func withECSEnrichment(fields mapstr.M) mapstr.M { // from https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html // we see journald.object fields are populated by systemd on behalf of a different program // so we want them to favor their use in root fields as they are the from the effective program @@ -119,11 +119,11 @@ func withECSEnrichment(fields common.MapStr) common.MapStr { return fields } -func setGidUidFields(prefix string, fields common.MapStr) { +func setGidUidFields(prefix string, fields mapstr.M) { var auditLoginUid string if found, _ := fields.HasKey(prefix + ".audit.login_uid"); found { auditLoginUid = fmt.Sprint(getIntegerFromFields(prefix+".audit.login_uid", fields)) - fields.Put("user.id", auditLoginUid) + _, _ = fields.Put("user.id", auditLoginUid) } if found, _ := fields.HasKey(prefix + ".uid"); !found { @@ -143,20 +143,20 @@ func setGidUidFields(prefix string, fields common.MapStr) { var cmdlineRegexp = regexp.MustCompile(`"(\\"|[^"])*?"|[^\s]+`) -func setProcessFields(prefix string, fields common.MapStr) { +func setProcessFields(prefix string, fields mapstr.M) { if found, _ := fields.HasKey(prefix + ".pid"); found { pid := getIntegerFromFields(prefix+".pid", fields) - fields.Put("process.pid", pid) + _, _ = fields.Put("process.pid", pid) } name := getStringFromFields(prefix+".name", fields) if name != "" { - fields.Put("process.name", name) + _, _ = fields.Put("process.name", name) } executable := getStringFromFields(prefix+".executable", fields) if executable != "" { - fields.Put("process.executable", executable) + _, _ = fields.Put("process.executable", executable) } cmdline := getStringFromFields(prefix+".process.command_line", fields) @@ -164,36 +164,35 @@ func setProcessFields(prefix string, fields common.MapStr) { return } - fields.Put("process.command_line", cmdline) + _, _ = fields.Put("process.command_line", cmdline) args := cmdlineRegexp.FindAllString(cmdline, -1) if len(args) > 0 { - fields.Put("process.args", args) - fields.Put("process.args_count", len(args)) + _, _ = fields.Put("process.args", args) + _, _ = fields.Put("process.args_count", len(args)) } } -func getStringFromFields(key string, fields common.MapStr) string { +func getStringFromFields(key string, fields mapstr.M) string { value, _ := fields.GetValue(key) str, _ := value.(string) return str } -func getIntegerFromFields(key string, fields common.MapStr) int64 { +func getIntegerFromFields(key string, fields mapstr.M) int64 { value, _ := fields.GetValue(key) i, _ := value.(int64) return i } -func putStringIfNotEmtpy(k, v string, fields common.MapStr) { +func putStringIfNotEmtpy(k, v string, fields mapstr.M) { if v == "" { return } - fields.Put(k, v) + _, _ = fields.Put(k, v) } // helpers for creating a field conversion table. - var ignoredField = Conversion{Dropped: true} func text(names ...string) Conversion { diff --git a/filebeat/input/journald/pkg/journalfield/conv_test.go b/filebeat/input/journald/pkg/journalfield/conv_test.go index 9d0be66bbe5..cad405eade5 100644 --- a/filebeat/input/journald/pkg/journalfield/conv_test.go +++ b/filebeat/input/journald/pkg/journalfield/conv_test.go @@ -26,22 +26,22 @@ import ( "github.com/coreos/go-systemd/v22/sdjournal" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConversion(t *testing.T) { tests := map[string]struct { fields map[string]string - want common.MapStr + want mapstr.M }{ "field name from fields.go": { fields: map[string]string{ sdjournal.SD_JOURNAL_FIELD_BOOT_ID: "123456", }, - want: common.MapStr{ - "journald": common.MapStr{ - "host": common.MapStr{ + want: mapstr.M{ + "journald": mapstr.M{ + "host": mapstr.M{ "boot_id": "123456", }, }, @@ -51,8 +51,8 @@ func TestConversion(t *testing.T) { fields: map[string]string{ sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "123456", }, - want: common.MapStr{ - "syslog": common.MapStr{ + want: mapstr.M{ + "syslog": mapstr.M{ "pid": int64(123456), }, }, @@ -61,12 +61,12 @@ func TestConversion(t *testing.T) { fields: map[string]string{ sdjournal.SD_JOURNAL_FIELD_PRIORITY: "123456, ", }, - want: common.MapStr{ - "syslog": common.MapStr{ + want: mapstr.M{ + "syslog": mapstr.M{ "priority": int64(123456), }, - "log": common.MapStr{ - "syslog": common.MapStr{ + "log": mapstr.M{ + "syslog": mapstr.M{ "priority": int64(123456), }, }, @@ -76,8 +76,8 @@ func TestConversion(t *testing.T) { fields: map[string]string{ sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "123456,root", }, - want: common.MapStr{ - "syslog": common.MapStr{ + want: mapstr.M{ + "syslog": mapstr.M{ "pid": int64(123456), }, }, @@ -86,8 +86,8 @@ func TestConversion(t *testing.T) { fields: map[string]string{ sdjournal.SD_JOURNAL_FIELD_SYSLOG_PID: "", }, - want: common.MapStr{ - "syslog": common.MapStr{ + want: mapstr.M{ + "syslog": mapstr.M{ "pid": "", }, }, @@ -96,9 +96,9 @@ func TestConversion(t *testing.T) { fields: map[string]string{ "my_custom_field": "value", }, - want: common.MapStr{ - "journald": common.MapStr{ - "custom": common.MapStr{ + want: mapstr.M{ + "journald": mapstr.M{ + "custom": mapstr.M{ "my_custom_field": "value", }, }, @@ -108,7 +108,7 @@ func TestConversion(t *testing.T) { fields: map[string]string{ "_SOURCE_MONOTONIC_TIMESTAMP": "value", }, - want: common.MapStr{}, + want: mapstr.M{}, }, } diff --git a/filebeat/input/kafka/input.go b/filebeat/input/kafka/input.go index 058d261c855..7167123a1e4 100644 --- a/filebeat/input/kafka/input.go +++ b/filebeat/input/kafka/input.go @@ -27,6 +27,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common/atomic" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/Shopify/sarama" "github.com/pkg/errors" @@ -431,9 +432,9 @@ func (l *listFromFieldReader) parseMultipleMessages(bMessage []byte) []string { return messages } -func composeEventMetadata(claim sarama.ConsumerGroupClaim, handler *groupHandler, msg *sarama.ConsumerMessage) (time.Time, common.MapStr) { +func composeEventMetadata(claim sarama.ConsumerGroupClaim, handler *groupHandler, msg *sarama.ConsumerMessage) (time.Time, mapstr.M) { timestamp := time.Now() - kafkaFields := common.MapStr{ + kafkaFields := mapstr.M{ "topic": claim.Topic(), "partition": claim.Partition(), "offset": msg.Offset, @@ -453,11 +454,11 @@ func composeEventMetadata(claim sarama.ConsumerGroupClaim, handler *groupHandler return timestamp, kafkaFields } -func composeMessage(timestamp time.Time, content []byte, kafkaFields common.MapStr, ackHandler func()) reader.Message { +func composeMessage(timestamp time.Time, content []byte, kafkaFields mapstr.M, ackHandler func()) reader.Message { return reader.Message{ Ts: timestamp, Content: content, - Fields: common.MapStr{ + Fields: mapstr.M{ "kafka": kafkaFields, "message": string(content), }, diff --git a/filebeat/input/kafka/input_test.go b/filebeat/input/kafka/input_test.go index 66e78630200..e9ab99aae06 100644 --- a/filebeat/input/kafka/input_test.go +++ b/filebeat/input/kafka/input_test.go @@ -27,10 +27,11 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/resources" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewInputDone(t *testing.T) { - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": "localhost:9092", "topics": "messages", "group_id": "filebeat", diff --git a/filebeat/input/kafka/kafka_integration_test.go b/filebeat/input/kafka/kafka_integration_test.go index 23101afe1c6..7f47686932c 100644 --- a/filebeat/input/kafka/kafka_integration_test.go +++ b/filebeat/input/kafka/kafka_integration_test.go @@ -33,6 +33,7 @@ import ( v2 "github.com/elastic/beats/v7/filebeat/input/v2" "github.com/elastic/beats/v7/libbeat/logp" beattest "github.com/elastic/beats/v7/libbeat/publisher/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/Shopify/sarama" "github.com/stretchr/testify/assert" @@ -87,7 +88,7 @@ func TestInput(t *testing.T) { } // Setup the input config - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": getTestKafkaHost(), "topics": []string{testTopic}, "group_id": groupID, @@ -162,7 +163,7 @@ func TestInputWithMultipleEvents(t *testing.T) { writeToKafkaTopic(t, testTopic, message.message, message.headers, time.Second*20) // Setup the input config - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": getTestKafkaHost(), "topics": []string{testTopic}, "group_id": "filebeat", @@ -218,14 +219,14 @@ func TestInputWithJsonPayload(t *testing.T) { writeToKafkaTopic(t, testTopic, message.message, message.headers, time.Second*20) // Setup the input config - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": getTestKafkaHost(), "topics": []string{testTopic}, "group_id": "filebeat", "wait_close": 0, - "parsers": []common.MapStr{ + "parsers": []mapstr.M{ { - "ndjson": common.MapStr{ + "ndjson": mapstr.M{ "target": "", }, }, @@ -280,15 +281,15 @@ func TestInputWithJsonPayloadAndMultipleEvents(t *testing.T) { writeToKafkaTopic(t, testTopic, message.message, message.headers, time.Second*20) // Setup the input config - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": getTestKafkaHost(), "topics": []string{testTopic}, "group_id": "filebeat", "wait_close": 0, "expand_event_list_from_field": "records", - "parsers": []common.MapStr{ + "parsers": []mapstr.M{ { - "ndjson": common.MapStr{ + "ndjson": mapstr.M{ "target": "", }, }, @@ -346,7 +347,7 @@ func TestSASLAuthentication(t *testing.T) { } // Setup the input config - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": []string{getTestSASLKafkaHost()}, "protocol": "https", "sasl.mechanism": "SCRAM-SHA-512", @@ -429,7 +430,7 @@ func TestTest(t *testing.T) { writeToKafkaTopic(t, testTopic, message.message, message.headers, time.Second*20) // Setup the input config - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": getTestKafkaHost(), "topics": []string{testTopic}, "group_id": "filebeat", @@ -474,7 +475,7 @@ func checkMatchingHeaders( if err != nil { t.Fatal(err) } - kafkaMap, ok := kafka.(common.MapStr) + kafkaMap, ok := kafka.(mapstr.M) if !ok { t.Fatal("event.Fields.kafka isn't MapStr") } diff --git a/filebeat/input/log/harvester.go b/filebeat/input/log/harvester.go index 61a5592c72e..4dac962343f 100644 --- a/filebeat/input/log/harvester.go +++ b/filebeat/input/log/harvester.go @@ -44,6 +44,7 @@ import ( file_helper "github.com/elastic/beats/v7/libbeat/common/file" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/filebeat/channel" "github.com/elastic/beats/v7/filebeat/harvester" @@ -428,10 +429,10 @@ func (h *Harvester) onMessage( return err == nil } - fields := common.MapStr{ - "log": common.MapStr{ + fields := mapstr.M{ + "log": mapstr.M{ "offset": messageOffset, // Offset here is the offset before the starting char. - "file": common.MapStr{ + "file": mapstr.M{ "path": state.Source, }, }, @@ -439,12 +440,12 @@ func (h *Harvester) onMessage( fields.DeepUpdate(message.Fields) // Check if json fields exist - var jsonFields common.MapStr + var jsonFields mapstr.M if f, ok := fields["json"]; ok { - jsonFields = f.(common.MapStr) + jsonFields = f.(mapstr.M) } - var meta common.MapStr + var meta mapstr.M timestamp := message.Ts if h.config.JSON != nil && len(jsonFields) > 0 { id, ts := readjson.MergeJSONFields(fields, jsonFields, &text, *h.config.JSON) @@ -455,13 +456,13 @@ func (h *Harvester) onMessage( } if id != "" { - meta = common.MapStr{ + meta = mapstr.M{ "_id": id, } } } else if &text != nil { if fields == nil { - fields = common.MapStr{} + fields = mapstr.M{} } fields["message"] = text } diff --git a/filebeat/input/log/harvester_test.go b/filebeat/input/log/harvester_test.go index d95d797c129..961aa8989fa 100644 --- a/filebeat/input/log/harvester_test.go +++ b/filebeat/input/log/harvester_test.go @@ -30,11 +30,11 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/reader" "github.com/elastic/beats/v7/libbeat/reader/readfile" "github.com/elastic/beats/v7/libbeat/reader/readfile/encoding" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestReadLine(t *testing.T) { @@ -127,7 +127,7 @@ func TestReadLine(t *testing.T) { // readLine reads a full line into buffer and returns it. // In case of partial lines, readLine does return an error and an empty string // This could potentially be improved / replaced by https://github.com/elastic/beats/libbeat/tree/master/common/streambuf -func readLine(reader reader.Reader) (time.Time, string, int, common.MapStr, error) { +func readLine(reader reader.Reader) (time.Time, string, int, mapstr.M, error) { message, err := reader.Next() // Full line read to be returned diff --git a/filebeat/input/log/input_test.go b/filebeat/input/log/input_test.go index 600ed3c420f..e9a70836281 100644 --- a/filebeat/input/log/input_test.go +++ b/filebeat/input/log/input_test.go @@ -38,6 +38,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/match" "github.com/elastic/beats/v7/libbeat/tests/resources" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestInputFileExclude(t *testing.T) { @@ -144,7 +145,7 @@ func testInputLifecycle(t *testing.T, context input.Context, closer func(input.C assert.NoError(t, err) // Setup the input - config, _ := common.NewConfigFrom(common.MapStr{ + config, _ := common.NewConfigFrom(mapstr.M{ "paths": path.Join(tmpdir, "*.log"), "close_eof": true, }) @@ -187,7 +188,7 @@ func testInputLifecycle(t *testing.T, context input.Context, closer func(input.C } func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "paths": path.Join(os.TempDir(), "logs", "*.log"), } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) diff --git a/filebeat/input/mqtt/input.go b/filebeat/input/mqtt/input.go index 9266429a200..bb8ff0a7a54 100644 --- a/filebeat/input/mqtt/input.go +++ b/filebeat/input/mqtt/input.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/backoff" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -112,7 +113,7 @@ func createOnMessageHandler(logger *logp.Logger, outlet channel.Outleter, inflig logger.Debugf("Received message on topic '%s', messageID: %d, size: %d", message.Topic(), message.MessageID(), len(message.Payload())) - mqttFields := common.MapStr{ + mqttFields := mapstr.M{ "duplicate": message.Duplicate(), "message_id": message.MessageID(), "qos": message.Qos(), @@ -121,7 +122,7 @@ func createOnMessageHandler(logger *logp.Logger, outlet channel.Outleter, inflig } outlet.OnEvent(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": string(message.Payload()), "mqtt": mqttFields, }, diff --git a/filebeat/input/mqtt/input_test.go b/filebeat/input/mqtt/input_test.go index a82ed2a4237..dc6caca4d5a 100644 --- a/filebeat/input/mqtt/input_test.go +++ b/filebeat/input/mqtt/input_test.go @@ -32,12 +32,13 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/backoff" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var logger = logp.NewLogger("test") func TestNewInput_MissingConfigField(t *testing.T) { - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "topics": "#", }) connector := new(mockedConnector) @@ -51,7 +52,7 @@ func TestNewInput_MissingConfigField(t *testing.T) { func TestNewInput_ConnectWithFailed(t *testing.T) { connectWithError := errors.New("failure") - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": "tcp://mocked:1234", "topics": "#", }) @@ -67,7 +68,7 @@ func TestNewInput_ConnectWithFailed(t *testing.T) { } func TestNewInput_Run(t *testing.T) { - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": "tcp://mocked:1234", "topics": []string{"first", "second"}, "qos": 2, @@ -139,7 +140,7 @@ func TestNewInput_Run(t *testing.T) { } func TestNewInput_Run_Wait(t *testing.T) { - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": "tcp://mocked:1234", "topics": []string{"first", "second"}, "qos": 2, @@ -324,7 +325,7 @@ func TestOnCreateHandler_SubscribeMultiple_BackoffSignalDone(t *testing.T) { } func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "hosts": "tcp://:0", } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) diff --git a/filebeat/input/mqtt/mqtt_integration_test.go b/filebeat/input/mqtt/mqtt_integration_test.go index 553bd60706b..ee3fb623ca1 100644 --- a/filebeat/input/mqtt/mqtt_integration_test.go +++ b/filebeat/input/mqtt/mqtt_integration_test.go @@ -35,6 +35,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -85,7 +86,7 @@ func TestInput(t *testing.T) { logp.TestingSetup(logp.WithSelectors("mqtt input", "libmqtt")) // Setup the input config. - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "hosts": []string{hostPort}, "topics": []string{topic}, }) diff --git a/filebeat/input/redis/harvester.go b/filebeat/input/redis/harvester.go index 9c575f83665..d84579c0a11 100644 --- a/filebeat/input/redis/harvester.go +++ b/filebeat/input/redis/harvester.go @@ -26,8 +26,8 @@ import ( rd "github.com/gomodule/redigo/redis" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/filebeat/harvester" ) @@ -132,11 +132,11 @@ func (h *Harvester) Run() error { log.args = args[2:] } - slowlogEntry := common.MapStr{ + slowlogEntry := mapstr.M{ "id": log.id, "cmd": log.cmd, "key": log.key, - "duration": common.MapStr{ + "duration": mapstr.M{ "us": log.duration, }, } @@ -147,12 +147,12 @@ func (h *Harvester) Run() error { h.forwarder.Send(beat.Event{ Timestamp: time.Unix(log.timestamp, 0).UTC(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": strings.Join(args, " "), - "redis": common.MapStr{ + "redis": mapstr.M{ "slowlog": slowlogEntry, }, - "event": common.MapStr{ + "event": mapstr.M{ "created": time.Now(), }, }, diff --git a/filebeat/input/redis/input_test.go b/filebeat/input/redis/input_test.go index 54342e5d922..15e4742b09c 100644 --- a/filebeat/input/redis/input_test.go +++ b/filebeat/input/redis/input_test.go @@ -24,11 +24,11 @@ import ( "testing" "github.com/elastic/beats/v7/filebeat/input/inputtest" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "hosts": "localhost:3679", } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) diff --git a/filebeat/input/stdin/input_test.go b/filebeat/input/stdin/input_test.go index 07efc555f58..e42d65b56ab 100644 --- a/filebeat/input/stdin/input_test.go +++ b/filebeat/input/stdin/input_test.go @@ -24,11 +24,11 @@ import ( "testing" "github.com/elastic/beats/v7/filebeat/input/inputtest" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "paths": "-", } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) diff --git a/filebeat/input/syslog/input.go b/filebeat/input/syslog/input.go index b055a729050..01650a651d1 100644 --- a/filebeat/input/syslog/input.go +++ b/filebeat/input/syslog/input.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Parser is generated from a ragel state machine using the following command: @@ -204,13 +205,13 @@ func GetCbByConfig(cfg config, forwarder *harvester.Forwarder, log *logp.Logger) } func createEvent(ev *event, metadata inputsource.NetworkMetadata, timezone *time.Location, log *logp.Logger) beat.Event { - f := common.MapStr{ + f := mapstr.M{ "message": strings.TrimRight(ev.Message(), "\n"), } - syslog := common.MapStr{} - event := common.MapStr{} - process := common.MapStr{} + syslog := mapstr.M{} + event := mapstr.M{} + process := mapstr.M{} if ev.Hostname() != "" { f["hostname"] = ev.Hostname() @@ -283,7 +284,7 @@ func parseAndCreateEvent3164(data []byte, metadata inputsource.NetworkMetadata, ParserRFC3164(data, ev) if !ev.IsValid() { log.Errorw("can't parse event as syslog rfc3164", "message", string(data)) - return newBeatEvent(time.Now(), metadata, common.MapStr{ + return newBeatEvent(time.Now(), metadata, mapstr.M{ "message": string(data), }) } @@ -295,17 +296,17 @@ func parseAndCreateEvent5424(data []byte, metadata inputsource.NetworkMetadata, ParserRFC5424(data, ev) if !ev.IsValid() { log.Errorw("can't parse event as syslog rfc5424", "message", string(data)) - return newBeatEvent(time.Now(), metadata, common.MapStr{ + return newBeatEvent(time.Now(), metadata, mapstr.M{ "message": string(data), }) } return createEvent(ev, metadata, timezone, log) } -func newBeatEvent(timestamp time.Time, metadata inputsource.NetworkMetadata, fields common.MapStr) beat.Event { +func newBeatEvent(timestamp time.Time, metadata inputsource.NetworkMetadata, fields mapstr.M) beat.Event { event := beat.Event{ Timestamp: timestamp, - Meta: common.MapStr{ + Meta: mapstr.M{ "truncated": metadata.Truncated, }, Fields: fields, diff --git a/filebeat/input/syslog/input_test.go b/filebeat/input/syslog/input_test.go index 23600278672..7c8cef9a87b 100644 --- a/filebeat/input/syslog/input_test.go +++ b/filebeat/input/syslog/input_test.go @@ -26,8 +26,8 @@ import ( "github.com/elastic/beats/v7/filebeat/input/inputtest" "github.com/elastic/beats/v7/filebeat/inputsource" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestWhenPriorityIsSet(t *testing.T) { @@ -40,21 +40,21 @@ func TestWhenPriorityIsSet(t *testing.T) { m := dummyMetadata() event := createEvent(e, m, time.Local, logp.NewLogger("syslog")) - expected := common.MapStr{ - "log": common.MapStr{ - "source": common.MapStr{ + expected := mapstr.M{ + "log": mapstr.M{ + "source": mapstr.M{ "address": "127.0.0.1", }, }, "message": "hello world", "hostname": "wopr", - "process": common.MapStr{ + "process": mapstr.M{ "pid": 123, }, - "event": common.MapStr{ + "event": mapstr.M{ "severity": 5, }, - "syslog": common.MapStr{ + "syslog": mapstr.M{ "facility": 1, "severity_label": "Notice", "facility_label": "user-level", @@ -73,19 +73,19 @@ func TestWhenPriorityIsNotSet(t *testing.T) { m := dummyMetadata() event := createEvent(e, m, time.Local, logp.NewLogger("syslog")) - expected := common.MapStr{ - "log": common.MapStr{ - "source": common.MapStr{ + expected := mapstr.M{ + "log": mapstr.M{ + "source": mapstr.M{ "address": "127.0.0.1", }, }, "message": "hello world", "hostname": "wopr", - "process": common.MapStr{ + "process": mapstr.M{ "pid": 123, }, - "event": common.MapStr{}, - "syslog": common.MapStr{}, + "event": mapstr.M{}, + "syslog": mapstr.M{}, } assert.Equal(t, expected, event.Fields) @@ -102,7 +102,7 @@ func TestPid(t *testing.T) { if !assert.NoError(t, err) { return } - assert.Equal(t, common.MapStr{"pid": 123}, v) + assert.Equal(t, mapstr.M{"pid": 123}, v) }) t.Run("is not set", func(t *testing.T) { @@ -112,7 +112,7 @@ func TestPid(t *testing.T) { event := createEvent(e, m, time.Local, logp.NewLogger("syslog")) _, err := event.GetValue("process") - assert.Equal(t, common.ErrKeyNotFound, err) + assert.Equal(t, mapstr.ErrKeyNotFound, err) }) } @@ -154,7 +154,7 @@ func TestProgram(t *testing.T) { if !assert.NoError(t, err) { return } - assert.Equal(t, common.MapStr{"program": "sudo"}, v) + assert.Equal(t, mapstr.M{"program": "sudo"}, v) }) t.Run("is not set", func(t *testing.T) { @@ -164,7 +164,7 @@ func TestProgram(t *testing.T) { event := createEvent(e, m, time.Local, logp.NewLogger("syslog")) _, err := event.GetValue("process") - assert.Equal(t, common.ErrKeyNotFound, err) + assert.Equal(t, mapstr.ErrKeyNotFound, err) }) } @@ -197,21 +197,21 @@ func TestSequence(t *testing.T) { func TestParseAndCreateEvent3164(t *testing.T) { cases := map[string]struct { data []byte - expected common.MapStr + expected mapstr.M }{ "valid data": { data: []byte("<34>Oct 11 22:14:15 mymachine su[230]: 'su root' failed for lonvick on /dev/pts/8"), - expected: common.MapStr{ - "event": common.MapStr{"severity": 2}, + expected: mapstr.M{ + "event": mapstr.M{"severity": 2}, "hostname": "mymachine", - "log": common.MapStr{ - "source": common.MapStr{ + "log": mapstr.M{ + "source": mapstr.M{ "address": "127.0.0.1", }, }, "message": "'su root' failed for lonvick on /dev/pts/8", - "process": common.MapStr{"pid": 230, "program": "su"}, - "syslog": common.MapStr{ + "process": mapstr.M{"pid": 230, "program": "su"}, + "syslog": mapstr.M{ "facility": 4, "facility_label": "security/authorization", "priority": 34, @@ -222,9 +222,9 @@ func TestParseAndCreateEvent3164(t *testing.T) { "invalid data": { data: []byte("invalid"), - expected: common.MapStr{ - "log": common.MapStr{ - "source": common.MapStr{ + expected: mapstr.M{ + "log": mapstr.M{ + "source": mapstr.M{ "address": "127.0.0.1", }, }, @@ -247,7 +247,7 @@ func TestParseAndCreateEvent3164(t *testing.T) { } func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "protocol.tcp.host": "localhost:9000", } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) @@ -263,24 +263,24 @@ func dummyMetadata() inputsource.NetworkMetadata { func TestParseAndCreateEvent5424(t *testing.T) { cases := map[string]struct { data []byte - expected common.MapStr + expected mapstr.M }{ "valid data": { data: []byte(RfcDoc65Example1), - expected: common.MapStr{ - "event": common.MapStr{"severity": 2}, + expected: mapstr.M{ + "event": mapstr.M{"severity": 2}, "hostname": "mymachine.example.com", - "log": common.MapStr{ - "source": common.MapStr{ + "log": mapstr.M{ + "source": mapstr.M{ "address": "127.0.0.1", }, }, - "process": common.MapStr{ + "process": mapstr.M{ "name": "su", "entity_id": "-", }, "message": "'su root' failed for lonvick on /dev/pts/8", - "syslog": common.MapStr{ + "syslog": mapstr.M{ "facility": 4, "facility_label": "security/authorization", "priority": 34, @@ -292,20 +292,20 @@ func TestParseAndCreateEvent5424(t *testing.T) { }, "valid data2": { data: []byte(RfcDoc65Example3), - expected: common.MapStr{ - "event": common.MapStr{"severity": 5}, + expected: mapstr.M{ + "event": mapstr.M{"severity": 5}, "hostname": "mymachine.example.com", - "log": common.MapStr{ - "source": common.MapStr{ + "log": mapstr.M{ + "source": mapstr.M{ "address": "127.0.0.1", }, }, - "process": common.MapStr{ + "process": mapstr.M{ "name": "evntslog", "entity_id": "-", }, "message": "An application event log entry...", - "syslog": common.MapStr{ + "syslog": mapstr.M{ "facility": 20, "facility_label": "local4", "priority": 165, @@ -325,9 +325,9 @@ func TestParseAndCreateEvent5424(t *testing.T) { "invalid data": { data: []byte("<34>Oct 11 22:14:15 mymachine su[230]: 'su root' failed for lonvick on /dev/pts/8"), - expected: common.MapStr{ - "log": common.MapStr{ - "source": common.MapStr{ + expected: mapstr.M{ + "log": mapstr.M{ + "source": mapstr.M{ "address": "127.0.0.1", }, }, diff --git a/filebeat/input/tcp/input.go b/filebeat/input/tcp/input.go index fbf3f286ca6..b02b1457094 100644 --- a/filebeat/input/tcp/input.go +++ b/filebeat/input/tcp/input.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -129,10 +130,10 @@ func (p *Input) Wait() { func createEvent(raw []byte, metadata inputsource.NetworkMetadata) beat.Event { return beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": string(raw), - "log": common.MapStr{ - "source": common.MapStr{ + "log": mapstr.M{ + "source": mapstr.M{ "address": metadata.RemoteAddr.String(), }, }, diff --git a/filebeat/input/tcp/input_test.go b/filebeat/input/tcp/input_test.go index 47733803077..4133de6b334 100644 --- a/filebeat/input/tcp/input_test.go +++ b/filebeat/input/tcp/input_test.go @@ -25,7 +25,7 @@ import ( "github.com/elastic/beats/v7/filebeat/input/inputtest" "github.com/elastic/beats/v7/filebeat/inputsource" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestCreateEvent(t *testing.T) { @@ -48,7 +48,7 @@ func TestCreateEvent(t *testing.T) { } func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "host": ":0", } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) diff --git a/filebeat/input/udp/input.go b/filebeat/input/udp/input.go index 56941a501a6..12e7ffbb462 100644 --- a/filebeat/input/udp/input.go +++ b/filebeat/input/udp/input.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -66,13 +67,13 @@ func NewInput( callback := func(data []byte, metadata inputsource.NetworkMetadata) { forwarder.Send(beat.Event{ Timestamp: time.Now(), - Meta: common.MapStr{ + Meta: mapstr.M{ "truncated": metadata.Truncated, }, - Fields: common.MapStr{ + Fields: mapstr.M{ "message": string(data), - "log": common.MapStr{ - "source": common.MapStr{ + "log": mapstr.M{ + "source": mapstr.M{ "address": metadata.RemoteAddr.String(), }, }, diff --git a/filebeat/input/udp/input_test.go b/filebeat/input/udp/input_test.go index 3046b2d62e5..55157d3a9e1 100644 --- a/filebeat/input/udp/input_test.go +++ b/filebeat/input/udp/input_test.go @@ -24,11 +24,11 @@ import ( "testing" "github.com/elastic/beats/v7/filebeat/input/inputtest" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "hosts": ":0", } inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) diff --git a/filebeat/input/unix/input.go b/filebeat/input/unix/input.go index 7346521c6c8..590f75c5db6 100644 --- a/filebeat/input/unix/input.go +++ b/filebeat/input/unix/input.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/feature" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-concert/ctxtool" ) @@ -62,7 +63,7 @@ func newServer(config config) (*server, error) { func (s *server) Name() string { return "unix" } func (s *server) Test(_ input.TestContext) error { - l, err := net.Listen("unix", s.config.Path) + l, err := net.Listen("unix", s.config.Config.Path) if err != nil { return err } @@ -99,7 +100,7 @@ func (s *server) Run(ctx input.Context, publisher stateless.Publisher) error { func createEvent(raw []byte, metadata inputsource.NetworkMetadata) beat.Event { return beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": string(raw), }, } diff --git a/filebeat/input/v2/input-cursor/manager_test.go b/filebeat/input/v2/input-cursor/manager_test.go index cb4d813ec5e..d774d093de5 100644 --- a/filebeat/input/v2/input-cursor/manager_test.go +++ b/filebeat/input/v2/input-cursor/manager_test.go @@ -37,6 +37,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" pubtest "github.com/elastic/beats/v7/libbeat/publisher/testing" "github.com/elastic/beats/v7/libbeat/tests/resources" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-concert/unison" ) @@ -366,7 +367,7 @@ func TestManager_InputsRun(t *testing.T) { } for i := 0; i < config.Max; i++ { - event := beat.Event{Fields: common.MapStr{"n": state.N}} + event := beat.Event{Fields: mapstr.M{"n": state.N}} state.N++ pub.Publish(event, state) } @@ -414,7 +415,7 @@ func TestManager_InputsRun(t *testing.T) { manager := constInput(t, sourceList("key"), &fakeTestInput{ OnRun: func(ctx input.Context, _ Source, _ Cursor, pub Publisher) error { defer wgSend.Done() - fields := common.MapStr{"hello": "world"} + fields := mapstr.M{"hello": "world"} pub.Publish(beat.Event{Fields: fields}, "test-cursor-state1") pub.Publish(beat.Event{Fields: fields}, "test-cursor-state2") pub.Publish(beat.Event{Fields: fields}, "test-cursor-state3") diff --git a/filebeat/input/v2/input-stateless/stateless_test.go b/filebeat/input/v2/input-stateless/stateless_test.go index 9a6a0665092..242778c3119 100644 --- a/filebeat/input/v2/input-stateless/stateless_test.go +++ b/filebeat/input/v2/input-stateless/stateless_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/atomic" pubtest "github.com/elastic/beats/v7/libbeat/publisher/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) type fakeStatelessInput struct { @@ -97,7 +98,7 @@ func TestStateless_Run(t *testing.T) { for ctx.Cancelation.Err() == nil { started.Store(true) publisher.Publish(beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "hello": "world", }, }) diff --git a/filebeat/processor/add_kubernetes_metadata/matchers.go b/filebeat/processor/add_kubernetes_metadata/matchers.go index f4635f76641..1b6b1ec3ccb 100644 --- a/filebeat/processor/add_kubernetes_metadata/matchers.go +++ b/filebeat/processor/add_kubernetes_metadata/matchers.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors/add_kubernetes_metadata" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -80,7 +81,7 @@ func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, err // Docker container ID is a 64-character-long hexadecimal string const containerIdLen = 64 -func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string { +func (f *LogPathMatcher) MetadataIndex(event mapstr.M) string { value, err := event.GetValue("log.file.path") if err != nil { f.logger.Debugf("Error extracting log.file.path from the event: %s.", event) diff --git a/filebeat/processor/add_kubernetes_metadata/matchers_test.go b/filebeat/processor/add_kubernetes_metadata/matchers_test.go index f7ed7ce88f2..94e4f7a7385 100644 --- a/filebeat/processor/add_kubernetes_metadata/matchers_test.go +++ b/filebeat/processor/add_kubernetes_metadata/matchers_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // A random container ID that we use for our tests @@ -186,9 +187,9 @@ func executeTestWithResourceType(t *testing.T, cfgLogsPath string, cfgResourceTy logMatcher, err := newLogsPathMatcher(*testConfig) assert.NoError(t, err) - input := common.MapStr{ - "log": common.MapStr{ - "file": common.MapStr{ + input := mapstr.M{ + "log": mapstr.M{ + "file": mapstr.M{ "path": source, }, }, diff --git a/filebeat/scripts/tester/main.go b/filebeat/scripts/tester/main.go index 41a197ce3f0..79fee6ad541 100644 --- a/filebeat/scripts/tester/main.go +++ b/filebeat/scripts/tester/main.go @@ -35,6 +35,7 @@ import ( "github.com/elastic/beats/v7/libbeat/reader/multiline" "github.com/elastic/beats/v7/libbeat/reader/readfile" "github.com/elastic/beats/v7/libbeat/reader/readfile/encoding" + "github.com/elastic/elastic-agent-libs/mapstr" ) type logReaderConfig struct { @@ -272,19 +273,19 @@ func readPipeline(path string) (map[string]interface{}, error) { } func runSimulate(url string, pipeline map[string]interface{}, logs []string, verbose bool) (*http.Response, error) { - var sources []common.MapStr + var sources []mapstr.M now := time.Now().UTC() for _, l := range logs { - s := common.MapStr{ + s := mapstr.M{ "@timestamp": common.Time(now), "message": l, } sources = append(sources, s) } - var docs []common.MapStr + var docs []mapstr.M for _, s := range sources { - d := common.MapStr{ + d := mapstr.M{ "_index": "index", "_type": "_doc", "_id": "id", @@ -293,7 +294,7 @@ func runSimulate(url string, pipeline map[string]interface{}, logs []string, ver docs = append(docs, d) } - p := common.MapStr{ + p := mapstr.M{ "pipeline": pipeline, "docs": docs, } @@ -316,7 +317,7 @@ func showResp(resp *http.Response, verbose, simulateVerbose bool) error { b := new(bytes.Buffer) b.ReadFrom(resp.Body) - var r common.MapStr + var r mapstr.M err := json.Unmarshal(b.Bytes(), &r) if err != nil { return err @@ -337,7 +338,7 @@ func showResp(resp *http.Response, verbose, simulateVerbose bool) error { return nil } -func getDocErrors(r common.MapStr, simulateVerbose bool) ([]common.MapStr, error) { +func getDocErrors(r mapstr.M, simulateVerbose bool) ([]mapstr.M, error) { d, err := r.GetValue("docs") if err != nil { return nil, err @@ -351,11 +352,11 @@ func getDocErrors(r common.MapStr, simulateVerbose bool) ([]common.MapStr, error return getRegularErrors(docs) } -func getRegularErrors(docs []interface{}) ([]common.MapStr, error) { - var errors []common.MapStr +func getRegularErrors(docs []interface{}) ([]mapstr.M, error) { + var errors []mapstr.M for _, d := range docs { dd := d.(map[string]interface{}) - doc := common.MapStr(dd) + doc := mapstr.M(dd) hasError, err := doc.HasKey("doc._source.error") if err != nil { return nil, err @@ -368,11 +369,11 @@ func getRegularErrors(docs []interface{}) ([]common.MapStr, error) { return errors, nil } -func getErrorsSimulateVerbose(docs []interface{}) ([]common.MapStr, error) { - var errors []common.MapStr +func getErrorsSimulateVerbose(docs []interface{}) ([]mapstr.M, error) { + var errors []mapstr.M for _, d := range docs { pr := d.(map[string]interface{}) - p := common.MapStr(pr) + p := mapstr.M(pr) rr, err := p.GetValue("processor_results") if err != nil { @@ -382,7 +383,7 @@ func getErrorsSimulateVerbose(docs []interface{}) ([]common.MapStr, error) { hasError := false for _, r := range res { rres := r.(map[string]interface{}) - result := common.MapStr(rres) + result := mapstr.M(rres) hasError, _ = result.HasKey("error") if hasError { errors = append(errors, p) diff --git a/heartbeat/autodiscover/builder/hints/monitors.go b/heartbeat/autodiscover/builder/hints/monitors.go index ca182fe37f2..a02d57d7994 100644 --- a/heartbeat/autodiscover/builder/hints/monitors.go +++ b/heartbeat/autodiscover/builder/hints/monitors.go @@ -21,6 +21,7 @@ import ( "fmt" "strings" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" "github.com/elastic/beats/v7/libbeat/autodiscover" @@ -61,10 +62,10 @@ func NewHeartbeatHints(cfg *common.Config) (autodiscover.Builder, error) { // Create config based on input hints in the bus event func (hb *heartbeatHints) CreateConfig(event bus.Event, options ...ucfg.Option) []*common.Config { - var hints common.MapStr + var hints mapstr.M hIface, ok := event["hints"] if ok { - hints, _ = hIface.(common.MapStr) + hints, _ = hIface.(mapstr.M) } monitorConfig := hb.getRawConfigs(hints) @@ -94,7 +95,7 @@ func (hb *heartbeatHints) CreateConfig(event bus.Event, options ...ucfg.Option) return template.ApplyConfigTemplate(event, configs) } - tempCfg := common.MapStr{} + tempCfg := mapstr.M{} monitors := builder.GetHintsAsList(hints, hb.config.Key) var configs []*common.Config @@ -124,23 +125,23 @@ func (hb *heartbeatHints) CreateConfig(event bus.Event, options ...ucfg.Option) return template.ApplyConfigTemplate(event, configs) } -func (hb *heartbeatHints) getType(hints common.MapStr) common.MapStr { +func (hb *heartbeatHints) getType(hints mapstr.M) mapstr.M { return builder.GetHintMapStr(hints, hb.config.Key, montype) } -func (hb *heartbeatHints) getSchedule(hints common.MapStr) []string { +func (hb *heartbeatHints) getSchedule(hints mapstr.M) []string { return builder.GetHintAsList(hints, hb.config.Key, schedule) } -func (hb *heartbeatHints) getRawConfigs(hints common.MapStr) []common.MapStr { +func (hb *heartbeatHints) getRawConfigs(hints mapstr.M) []mapstr.M { return builder.GetHintAsConfigs(hints, hb.config.Key) } -func (hb *heartbeatHints) getProcessors(hints common.MapStr) []common.MapStr { +func (hb *heartbeatHints) getProcessors(hints mapstr.M) []mapstr.M { return builder.GetConfigs(hints, "", "processors") } -func (hb *heartbeatHints) getHostsWithPort(hints common.MapStr, port int) []string { +func (hb *heartbeatHints) getHostsWithPort(hints mapstr.M, port int) []string { var result []string thosts := builder.GetHintAsList(hints, "", hosts) // Only pick hosts that have ${data.port} or the port on current event. This will make diff --git a/heartbeat/autodiscover/builder/hints/monitors_test.go b/heartbeat/autodiscover/builder/hints/monitors_test.go index 82c4dc6262c..863abf997e3 100644 --- a/heartbeat/autodiscover/builder/hints/monitors_test.go +++ b/heartbeat/autodiscover/builder/hints/monitors_test.go @@ -22,9 +22,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints(t *testing.T) { @@ -32,54 +32,54 @@ func TestGenerateHints(t *testing.T) { message string event bus.Event len int - result common.MapStr + result mapstr.M }{ { message: "Empty event hints should return empty config", event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, }, len: 0, - result: common.MapStr{}, + result: mapstr.M{}, }, { message: "Hints without host should return nothing", event: bus.Event{ - "hints": common.MapStr{ - "monitor": common.MapStr{ + "hints": mapstr.M{ + "monitor": mapstr.M{ "type": "icmp", }, }, }, len: 0, - result: common.MapStr{}, + result: mapstr.M{}, }, { message: "Hints without matching port should return nothing in the hosts section", event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "monitor": common.MapStr{ + "hints": mapstr.M{ + "monitor": mapstr.M{ "type": "icmp", "hosts": "${data.host}:8888", }, }, }, len: 1, - result: common.MapStr{ + result: mapstr.M{ "schedule": "@every 5s", "type": "icmp", }, @@ -89,15 +89,15 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "monitor": common.MapStr{ + "hints": mapstr.M{ + "monitor": mapstr.M{ "type": "icmp", "hosts": "${data.host}:8888,${data.host}:9090", }, }, }, len: 1, - result: common.MapStr{ + result: mapstr.M{ "type": "icmp", "schedule": "@every 5s", "hosts": []interface{}{"1.2.3.4:9090"}, @@ -108,15 +108,15 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "monitor": common.MapStr{ + "hints": mapstr.M{ + "monitor": mapstr.M{ "type": "icmp", "hosts": "${data.host}:8888,${data.host}:${data.port}", }, }, }, len: 1, - result: common.MapStr{ + result: mapstr.M{ "type": "icmp", "schedule": "@every 5s", "hosts": []interface{}{"1.2.3.4:9090"}, @@ -126,14 +126,14 @@ func TestGenerateHints(t *testing.T) { message: "Monitor defined in monitors as a JSON string should return a config", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "monitor": common.MapStr{ + "hints": mapstr.M{ + "monitor": mapstr.M{ "raw": "{\"enabled\":true,\"type\":\"icmp\",\"schedule\":\"@every 20s\",\"timeout\":\"3s\"}", }, }, }, len: 1, - result: common.MapStr{ + result: mapstr.M{ "type": "icmp", "timeout": "3s", "schedule": "@every 20s", @@ -145,12 +145,12 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "monitor": common.MapStr{ + "hints": mapstr.M{ + "monitor": mapstr.M{ "type": "icmp", "hosts": "${data.host}:9090", - "processors": common.MapStr{ - "add_locale": common.MapStr{ + "processors": mapstr.M{ + "add_locale": mapstr.M{ "abbrevation": "MST", }, }, @@ -158,7 +158,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: common.MapStr{ + result: mapstr.M{ "type": "icmp", "hosts": []interface{}{"1.2.3.4:9090"}, "schedule": "@every 5s", @@ -176,13 +176,13 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "monitor": common.MapStr{ - "1": common.MapStr{ + "hints": mapstr.M{ + "monitor": mapstr.M{ + "1": mapstr.M{ "type": "icmp", "hosts": "${data.host}:8888,${data.host}:9090", }, - "2": common.MapStr{ + "2": mapstr.M{ "type": "icmp", "hosts": "${data.host}:8888,${data.host}:9090", }, @@ -190,7 +190,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 2, - result: common.MapStr{ + result: mapstr.M{ "type": "icmp", "schedule": "@every 5s", "hosts": []interface{}{"1.2.3.4:9090"}, @@ -207,7 +207,7 @@ func TestGenerateHints(t *testing.T) { assert.Equal(t, len(cfgs), test.len, test.message) if len(cfgs) != 0 { - config := common.MapStr{} + config := mapstr.M{} err := cfgs[0].Unpack(&config) assert.Nil(t, err, test.message) diff --git a/heartbeat/cmd/root.go b/heartbeat/cmd/root.go index 06f3961c279..d3374648414 100644 --- a/heartbeat/cmd/root.go +++ b/heartbeat/cmd/root.go @@ -21,11 +21,11 @@ import ( // include all heartbeat specific autodiscovery builders _ "github.com/elastic/beats/v7/heartbeat/autodiscover/builder/hints" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/heartbeat/beater" cmd "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/publisher/processing" @@ -44,8 +44,8 @@ const ( var RootCmd *cmd.BeatsRootCmd // withECSVersion is a modifier that adds ecs.version to events. -var withECSVersion = processing.WithFields(common.MapStr{ - "ecs": common.MapStr{ +var withECSVersion = processing.WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) diff --git a/heartbeat/eventext/eventext.go b/heartbeat/eventext/eventext.go index 122c8ae70e2..0afe3b46fb2 100644 --- a/heartbeat/eventext/eventext.go +++ b/heartbeat/eventext/eventext.go @@ -19,11 +19,11 @@ package eventext import ( "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) -// MergeEventFields merges the given common.MapStr into the given Event's Fields. -func MergeEventFields(e *beat.Event, merge common.MapStr) { +// MergeEventFields merges the given mapstr.M into the given Event's Fields. +func MergeEventFields(e *beat.Event, merge mapstr.M) { if e.Fields != nil { e.Fields.DeepUpdate(merge.Clone()) } else { @@ -38,7 +38,7 @@ const EventCancelledMetaKey = "__hb_evt_cancel__" func CancelEvent(event *beat.Event) { if event != nil { if event.Meta == nil { - event.Meta = common.MapStr{} + event.Meta = mapstr.M{} } event.Meta.Put(EventCancelledMetaKey, true) } diff --git a/heartbeat/hbtest/hbtestutil.go b/heartbeat/hbtest/hbtestutil.go index 77b12b527cf..84202ca1163 100644 --- a/heartbeat/hbtest/hbtestutil.go +++ b/heartbeat/hbtest/hbtestutil.go @@ -36,7 +36,7 @@ import ( "time" "github.com/elastic/beats/v7/heartbeat/monitors/active/dialchain/tlsmeta" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/heartbeat/hbtestllext" @@ -135,7 +135,7 @@ func ServerPort(server *httptest.Server) (uint16, error) { // TLSChecks validates the given x509 cert at the given position. func TLSChecks(chainIndex, certIndex int, certificate *x509.Certificate) validator.Validator { - expected := common.MapStr{} + expected := mapstr.M{} // This function is well tested independently, so we just test that things match up here. tlsmeta.AddTLSMetadata(expected, tls.ConnectionState{ Version: tls.VersionTLS13, @@ -162,7 +162,7 @@ func TLSChecks(chainIndex, certIndex int, certificate *x509.Certificate) validat } func TLSCertChecks(certificate *x509.Certificate) validator.Validator { - expected := common.MapStr{} + expected := mapstr.M{} tlsmeta.AddCertMetadata(expected, []*x509.Certificate{certificate}) return lookslike.MustCompile(expected) } diff --git a/heartbeat/look/look.go b/heartbeat/look/look.go index 42be922d7ef..75d23b973a1 100644 --- a/heartbeat/look/look.go +++ b/heartbeat/look/look.go @@ -23,18 +23,19 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/heartbeat/reason" ) // RTT formats a round-trip-time given as time.Duration into an // event field. The duration is stored in `{"us": rtt}`. -func RTT(rtt time.Duration) common.MapStr { +func RTT(rtt time.Duration) mapstr.M { if rtt < 0 { rtt = 0 } - return common.MapStr{ + return mapstr.M{ // cast to int64 since a go duration is a nano, but we want micros // This makes the types less confusing because other wise the duration // we get back has the wrong unit @@ -43,7 +44,7 @@ func RTT(rtt time.Duration) common.MapStr { } // Reason formats an error into an error event field. -func Reason(err error) common.MapStr { +func Reason(err error) mapstr.M { //nolint:errorlint // There are no new changes to this line but // linter has been activated in the meantime. We'll cleanup separately. if r, ok := err.(reason.Reason); ok { diff --git a/heartbeat/look/look_test.go b/heartbeat/look/look_test.go index 0cef9836cf4..ff3f9bdcc54 100644 --- a/heartbeat/look/look_test.go +++ b/heartbeat/look/look_test.go @@ -27,6 +27,7 @@ import ( reason2 "github.com/elastic/beats/v7/heartbeat/reason" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // helper @@ -48,7 +49,7 @@ func TestReason(t *testing.T) { reason := reason2.ValidateFailed(fmt.Errorf("an error")) res := Reason(reason) assert.Equal(t, - common.MapStr{ + mapstr.M{ "type": reason.Type(), "message": reason.Error(), }, res) @@ -57,7 +58,7 @@ func TestReason(t *testing.T) { func TestReasonGenericError(t *testing.T) { msg := "An error" res := Reason(fmt.Errorf(msg)) - assert.Equal(t, common.MapStr{ + assert.Equal(t, mapstr.M{ "type": "io", "message": msg, }, res) diff --git a/heartbeat/monitors/active/dialchain/dialers.go b/heartbeat/monitors/active/dialchain/dialers.go index ddd34870d8d..c3ed73731d3 100644 --- a/heartbeat/monitors/active/dialchain/dialers.go +++ b/heartbeat/monitors/active/dialchain/dialers.go @@ -26,9 +26,9 @@ import ( "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/heartbeat/look" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TCPDialer creates a new NetDialer with constant event fields and default @@ -106,9 +106,9 @@ func CreateNetDialer(timeout time.Duration) NetDialer { } end := time.Now() - eventext.MergeEventFields(event, common.MapStr{ - namespace: common.MapStr{ - "rtt": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + namespace: mapstr.M{ + "rtt": mapstr.M{ "connect": look.RTT(end.Sub(start)), }, }, diff --git a/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta.go b/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta.go index f780e903317..b643a99b748 100644 --- a/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta.go +++ b/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta.go @@ -29,14 +29,14 @@ import ( "time" "github.com/elastic/beats/v7/heartbeat/look" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/tlscommon" + "github.com/elastic/elastic-agent-libs/mapstr" ) // UnknownTLSHandshakeDuration to be used in AddTLSMetadata when the duration of the TLS handshake can't be determined. const UnknownTLSHandshakeDuration = time.Duration(-1) -func AddTLSMetadata(fields common.MapStr, connState cryptoTLS.ConnectionState, duration time.Duration) { +func AddTLSMetadata(fields mapstr.M, connState cryptoTLS.ConnectionState, duration time.Duration) { fields.Put("tls.established", true) if duration != UnknownTLSHandshakeDuration { fields.Put("tls.rtt.handshake", look.RTT(duration)) @@ -57,12 +57,12 @@ func AddTLSMetadata(fields common.MapStr, connState cryptoTLS.ConnectionState, d AddCertMetadata(fields, connState.PeerCertificates) } -func AddCertMetadata(fields common.MapStr, certs []*x509.Certificate) { +func AddCertMetadata(fields mapstr.M, certs []*x509.Certificate) { hostCert := certs[0] - x509Fields := common.MapStr{} - serverFields := common.MapStr{"x509": x509Fields} - tlsFields := common.MapStr{"server": serverFields} + x509Fields := mapstr.M{} + serverFields := mapstr.M{"x509": x509Fields} + tlsFields := mapstr.M{"server": serverFields} serverFields.Put("hash.sha1", fmt.Sprintf("%x", sha1.Sum(hostCert.Raw))) serverFields.Put("hash.sha256", fmt.Sprintf("%x", sha256.Sum256(hostCert.Raw))) @@ -98,7 +98,7 @@ func AddCertMetadata(fields common.MapStr, certs []*x509.Certificate) { x509Fields.Put("not_after", *chainNotAfter) } - fields.DeepUpdate(common.MapStr{"tls": tlsFields}) + fields.DeepUpdate(mapstr.M{"tls": tlsFields}) } func calculateCertTimestamps(certs []*x509.Certificate) (chainNotBefore time.Time, chainNotAfter *time.Time) { diff --git a/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta_test.go b/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta_test.go index 609fbbb413d..68cf34de750 100644 --- a/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta_test.go +++ b/heartbeat/monitors/active/dialchain/tlsmeta/tlsmeta_test.go @@ -26,13 +26,13 @@ import ( "testing" "time" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/heartbeat/look" - "github.com/elastic/beats/v7/libbeat/common" ) // Tests for the non-cert fields @@ -40,14 +40,14 @@ func TestAddTLSMetadata(t *testing.T) { // We always test with this one cert because addCertificateMetadata // is tested in detail elsewhere certs := []*x509.Certificate{parseCert(t, elasticCert)} - certMetadata := common.MapStr{} + certMetadata := mapstr.M{} AddCertMetadata(certMetadata, certs) scenarios := []struct { name string connState tls.ConnectionState duration time.Duration - expected common.MapStr + expected mapstr.M }{ { "simple TLSv1.1", @@ -59,9 +59,9 @@ func TestAddTLSMetadata(t *testing.T) { ServerName: "example.net", }, time.Duration(1), - common.MapStr{ + mapstr.M{ "established": true, - "rtt": common.MapStr{"handshake": look.RTT(time.Duration(1))}, + "rtt": mapstr.M{"handshake": look.RTT(time.Duration(1))}, "version_protocol": "tls", "version": "1.1", "cipher": "ECDHE-ECDSA-AES-256-CBC-SHA", @@ -78,9 +78,9 @@ func TestAddTLSMetadata(t *testing.T) { NegotiatedProtocol: "h2", }, time.Duration(1), - common.MapStr{ + mapstr.M{ "established": true, - "rtt": common.MapStr{"handshake": look.RTT(time.Duration(1))}, + "rtt": mapstr.M{"handshake": look.RTT(time.Duration(1))}, "version_protocol": "tls", "version": "1.2", "cipher": "ECDHE-ECDSA-AES-256-CBC-SHA", @@ -92,12 +92,12 @@ func TestAddTLSMetadata(t *testing.T) { for _, s := range scenarios { t.Run(s.name, func(t *testing.T) { // Nest under the TLS namespace to match actual output - expected := common.MapStr{"tls": s.expected} + expected := mapstr.M{"tls": s.expected} // Always add in the cert metadata since we test that in other test funcs, not here expected.DeepUpdate(certMetadata) - fields := common.MapStr{} + fields := mapstr.M{} AddTLSMetadata(fields, s.connState, s.duration) require.Equal(t, expected, fields) }) @@ -115,17 +115,17 @@ func TestAddCertMetadata(t *testing.T) { expectedFields := lookslike.Strict(lookslike.MustCompile(map[string]interface{}{ "certificate_not_valid_after": certNotAfter, "certificate_not_valid_before": certNotBefore, - "server": common.MapStr{ - "hash": common.MapStr{ + "server": mapstr.M{ + "hash": mapstr.M{ "sha1": "b7b4b89ef0d0caf39d223736f0fdbb03c7b426f1", "sha256": "12b00d04db0db8caa302bfde043e88f95baceb91e86ac143e93830b4bbec726d", }, - "x509": common.MapStr{ - "issuer": common.MapStr{ + "x509": mapstr.M{ + "issuer": mapstr.M{ "common_name": "GlobalSign CloudSSL CA - SHA256 - G3", "distinguished_name": "CN=GlobalSign CloudSSL CA - SHA256 - G3,O=GlobalSign nv-sa,C=BE", }, - "subject": common.MapStr{ + "subject": mapstr.M{ "common_name": "r2.shared.global.fastly.net", "distinguished_name": "CN=r2.shared.global.fastly.net,O=Fastly\\, Inc.,L=San Francisco,ST=California,C=US", }, @@ -156,7 +156,7 @@ func TestAddCertMetadata(t *testing.T) { for _, scenario := range scenarios { t.Run(scenario.name, func(t *testing.T) { - fields := common.MapStr{} + fields := mapstr.M{} AddCertMetadata(fields, scenario.certs) tls, err := fields.GetValue("tls") require.NoError(t, err) diff --git a/heartbeat/monitors/active/http/checkjson.go b/heartbeat/monitors/active/http/checkjson.go index 64b697f5376..11501a1d22a 100644 --- a/heartbeat/monitors/active/http/checkjson.go +++ b/heartbeat/monitors/active/http/checkjson.go @@ -27,10 +27,10 @@ import ( "github.com/PaesslerAG/gval" "github.com/PaesslerAG/jsonpath" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/common/jsontransform" "github.com/elastic/beats/v7/libbeat/conditions" + "github.com/elastic/elastic-agent-libs/mapstr" ) type jsonChecker func(interface{}) bool @@ -76,7 +76,7 @@ func checkJson(checks []*jsonResponseCheck) (bodyValidator, error) { checkFn := func(d interface{}) bool { ms, ok := d.(map[string]interface{}) if ok { - return cond.Check(common.MapStr(ms)) + return cond.Check(mapstr.M(ms)) } else { return false } diff --git a/heartbeat/monitors/active/http/http_test.go b/heartbeat/monitors/active/http/http_test.go index 250fe13df2d..7b69e2f5f05 100644 --- a/heartbeat/monitors/active/http/http_test.go +++ b/heartbeat/monitors/active/http/http_test.go @@ -50,6 +50,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/file" btesting "github.com/elastic/beats/v7/libbeat/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/isdef" "github.com/elastic/go-lookslike/testslike" @@ -347,7 +348,7 @@ func TestJsonBody(t *testing.T) { name string responseBody string expression string - condition common.MapStr + condition mapstr.M expectedErrMsg string expectedContentType string } @@ -373,8 +374,8 @@ func TestJsonBody(t *testing.T) { "simple condition match", "{\"foo\": \"bar\"}", "", - common.MapStr{ - "equals": common.MapStr{"foo": "bar"}, + mapstr.M{ + "equals": mapstr.M{"foo": "bar"}, }, "", "application/json", @@ -383,8 +384,8 @@ func TestJsonBody(t *testing.T) { "condition mismatch", "{\"foo\": \"bar\"}", "", - common.MapStr{ - "equals": common.MapStr{"baz": "bot"}, + mapstr.M{ + "equals": mapstr.M{"baz": "bot"}, }, "JSON body did not match", "application/json", @@ -393,8 +394,8 @@ func TestJsonBody(t *testing.T) { "condition invalid json", "notjson", "", - common.MapStr{ - "equals": common.MapStr{"foo": "bar"}, + mapstr.M{ + "equals": mapstr.M{"foo": "bar"}, }, "could not parse JSON", "text/plain; charset=utf-8", @@ -403,8 +404,8 @@ func TestJsonBody(t *testing.T) { "condition complex type match json", "{\"number\": 3, \"bool\": true}", "", - common.MapStr{ - "equals": common.MapStr{"number": 3, "bool": true}, + mapstr.M{ + "equals": mapstr.M{"number": 3, "bool": true}, }, "", "application/json", @@ -416,7 +417,7 @@ func TestJsonBody(t *testing.T) { server := httptest.NewServer(hbtest.CustomResponseHandler([]byte(tc.responseBody), 200, nil)) defer server.Close() - jsonCheck := common.MapStr{"description": tc.name} + jsonCheck := mapstr.M{"description": tc.name} if tc.expression != "" { jsonCheck["expression"] = tc.expression } @@ -428,7 +429,7 @@ func TestJsonBody(t *testing.T) { "hosts": server.URL, "timeout": "1s", "response.include_body": "never", - "check.response.json": []common.MapStr{ + "check.response.json": []mapstr.M{ jsonCheck, }, } diff --git a/heartbeat/monitors/active/http/respbody.go b/heartbeat/monitors/active/http/respbody.go index 0587c207be3..b9551b7e096 100644 --- a/heartbeat/monitors/active/http/respbody.go +++ b/heartbeat/monitors/active/http/respbody.go @@ -28,8 +28,8 @@ import ( "github.com/docker/go-units" "github.com/elastic/beats/v7/heartbeat/reason" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mime" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -42,7 +42,7 @@ const ( minBufferBodyBytes = 128 ) -func processBody(resp *http.Response, config responseConfig, validator multiValidator) (common.MapStr, string, reason.Reason) { +func processBody(resp *http.Response, config responseConfig, validator multiValidator) (mapstr.M, string, reason.Reason) { // Determine how much of the body to actually buffer in memory var bufferBodyBytes int if validator.wantsBody() { @@ -65,7 +65,7 @@ func processBody(resp *http.Response, config responseConfig, validator multiVali // Run any validations errReason := validator.validate(resp, respBody) - bodyFields := common.MapStr{ + bodyFields := mapstr.M{ "hash": bodyHash, "bytes": bodyLenBytes, } diff --git a/heartbeat/monitors/active/http/task.go b/heartbeat/monitors/active/http/task.go index 9ee5f3fa1e8..feb1a9154ec 100644 --- a/heartbeat/monitors/active/http/task.go +++ b/heartbeat/monitors/active/http/task.go @@ -32,6 +32,7 @@ import ( "time" "github.com/elastic/beats/v7/heartbeat/monitors/active/dialchain/tlsmeta" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/heartbeat/look" @@ -40,7 +41,6 @@ import ( "github.com/elastic/beats/v7/heartbeat/monitors/jobs" "github.com/elastic/beats/v7/heartbeat/reason" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/httpcommon" "github.com/elastic/beats/v7/libbeat/common/transport/tlscommon" ) @@ -175,9 +175,9 @@ func createPingFactory( defer cbMutex.Unlock() if !readStart.IsZero() { - eventext.MergeEventFields(event, common.MapStr{ - "http": common.MapStr{ - "rtt": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "http": mapstr.M{ + "rtt": mapstr.M{ "write_request": look.RTT(writeEnd.Sub(writeStart)), "response_header": look.RTT(readStart.Sub(writeStart)), }, @@ -252,7 +252,7 @@ func execPing( bodyFields, mimeType, errReason := processBody(resp, responseConfig, validator) - responseFields := common.MapStr{ + responseFields := mapstr.M{ "status_code": resp.StatusCode, "body": bodyFields, } @@ -262,7 +262,7 @@ func execPing( } if responseConfig.IncludeHeaders { - headerFields := common.MapStr{} + headerFields := mapstr.M{} for canonicalHeaderKey, vals := range resp.Header { if len(vals) > 1 { headerFields[canonicalHeaderKey] = vals @@ -273,9 +273,9 @@ func execPing( responseFields["headers"] = headerFields } - httpFields := common.MapStr{"response": responseFields} + httpFields := mapstr.M{"response": responseFields} - eventext.MergeEventFields(event, common.MapStr{"http": httpFields}) + eventext.MergeEventFields(event, mapstr.M{"http": httpFields}) // Mark the end time as now, since we've finished downloading end = time.Now() @@ -283,14 +283,14 @@ func execPing( // Enrich event with TLS information when available. This is useful when connecting to an HTTPS server through // a proxy. if resp.TLS != nil { - tlsFields := common.MapStr{} + tlsFields := mapstr.M{} tlsmeta.AddTLSMetadata(tlsFields, *resp.TLS, tlsmeta.UnknownTLSHandshakeDuration) eventext.MergeEventFields(event, tlsFields) } // Add total HTTP RTT - eventext.MergeEventFields(event, common.MapStr{"http": common.MapStr{ - "rtt": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{"http": mapstr.M{ + "rtt": mapstr.M{ "total": look.RTT(end.Sub(start)), }, }}) diff --git a/heartbeat/monitors/active/icmp/icmp.go b/heartbeat/monitors/active/icmp/icmp.go index 073660c259a..e140a467349 100644 --- a/heartbeat/monitors/active/icmp/icmp.go +++ b/heartbeat/monitors/active/icmp/icmp.go @@ -23,6 +23,7 @@ import ( "net/url" "github.com/elastic/beats/v7/heartbeat/monitors/plugin" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/heartbeat/look" @@ -119,10 +120,10 @@ func (jf *jobFactory) pingIPFactory(config *Config) func(*net.IPAddr) jobs.Job { return err } - icmpFields := common.MapStr{"requests": n} + icmpFields := mapstr.M{"requests": n} if err == nil { icmpFields["rtt"] = look.RTT(rtt) - eventext.MergeEventFields(event, common.MapStr{"icmp": icmpFields}) + eventext.MergeEventFields(event, mapstr.M{"icmp": icmpFields}) } return nil diff --git a/heartbeat/monitors/active/tcp/helpers_test.go b/heartbeat/monitors/active/tcp/helpers_test.go index b5c7aa077f3..2bbc6399321 100644 --- a/heartbeat/monitors/active/tcp/helpers_test.go +++ b/heartbeat/monitors/active/tcp/helpers_test.go @@ -32,9 +32,10 @@ import ( "github.com/elastic/beats/v7/heartbeat/scheduler/schedule" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func testTCPConfigCheck(t *testing.T, configMap common.MapStr, host string, port uint16) *beat.Event { +func testTCPConfigCheck(t *testing.T, configMap mapstr.M, host string, port uint16) *beat.Event { config, err := common.NewConfigFrom(configMap) require.NoError(t, err) diff --git a/heartbeat/monitors/active/tcp/socks5_test.go b/heartbeat/monitors/active/tcp/socks5_test.go index 0cb75c12084..bd76afe06bf 100644 --- a/heartbeat/monitors/active/tcp/socks5_test.go +++ b/heartbeat/monitors/active/tcp/socks5_test.go @@ -30,7 +30,7 @@ import ( "github.com/elastic/beats/v7/heartbeat/hbtest" "github.com/elastic/beats/v7/heartbeat/hbtestllext" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" ) @@ -65,7 +65,7 @@ func TestSocks5Job(t *testing.T) { defer closeProxy() proxyURL := &url.URL{Scheme: "socks5", Host: net.JoinHostPort(proxyIp, fmt.Sprint(proxyPort))} - configMap := common.MapStr{ + configMap := mapstr.M{ "hosts": host, "ports": port, "timeout": "1s", diff --git a/heartbeat/monitors/active/tcp/tcp.go b/heartbeat/monitors/active/tcp/tcp.go index 805e5a9a6d4..86d86c38ee2 100644 --- a/heartbeat/monitors/active/tcp/tcp.go +++ b/heartbeat/monitors/active/tcp/tcp.go @@ -37,6 +37,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/transport" "github.com/elastic/beats/v7/libbeat/common/transport/tlscommon" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -252,9 +253,9 @@ func (jf *jobFactory) execDialer( } end := time.Now() - eventext.MergeEventFields(event, common.MapStr{ - "tcp": common.MapStr{ - "rtt": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "tcp": mapstr.M{ + "rtt": mapstr.M{ "validate": look.RTT(end.Sub(validateStart)), }, }, diff --git a/heartbeat/monitors/active/tcp/tcp_test.go b/heartbeat/monitors/active/tcp/tcp_test.go index 31bfb414eb7..4ea1275f498 100644 --- a/heartbeat/monitors/active/tcp/tcp_test.go +++ b/heartbeat/monitors/active/tcp/tcp_test.go @@ -31,15 +31,15 @@ import ( "github.com/elastic/beats/v7/heartbeat/hbtest" "github.com/elastic/beats/v7/heartbeat/hbtestllext" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" btesting "github.com/elastic/beats/v7/libbeat/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" "github.com/elastic/go-lookslike/validator" ) func testTCPCheck(t *testing.T, host string, port uint16) *beat.Event { - config := common.MapStr{ + config := mapstr.M{ "hosts": host, "ports": port, "timeout": "1s", @@ -161,7 +161,7 @@ func TestCheckUp(t *testing.T) { // linter has been activated in the meantime. We'll cleanup separately. defer closeEcho() - configMap := common.MapStr{ + configMap := mapstr.M{ "hosts": host, "ports": port, "timeout": "1s", @@ -196,7 +196,7 @@ func TestCheckDown(t *testing.T) { // linter has been activated in the meantime. We'll cleanup separately. defer closeEcho() - configMap := common.MapStr{ + configMap := mapstr.M{ "hosts": host, "ports": port, "timeout": "1s", diff --git a/heartbeat/monitors/active/tcp/tls_test.go b/heartbeat/monitors/active/tcp/tls_test.go index 4307bf7cf32..4113b7d85a1 100644 --- a/heartbeat/monitors/active/tcp/tls_test.go +++ b/heartbeat/monitors/active/tcp/tls_test.go @@ -35,6 +35,7 @@ import ( "github.com/elastic/beats/v7/heartbeat/scheduler/schedule" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/require" @@ -181,10 +182,10 @@ func setupTLSTestServer(t *testing.T) (ip string, port uint16, cert *x509.Certif } func testTLSTCPCheck(t *testing.T, host string, port uint16, certFileName string, resolver monitors.Resolver) *beat.Event { - config, err := common.NewConfigFrom(common.MapStr{ + config, err := common.NewConfigFrom(mapstr.M{ "hosts": host, "ports": int64(port), - "ssl": common.MapStr{"certificate_authorities": certFileName}, + "ssl": mapstr.M{"certificate_authorities": certFileName}, "timeout": "1s", }) require.NoError(t, err) diff --git a/heartbeat/monitors/factory.go b/heartbeat/monitors/factory.go index 11a3b2d9ecd..1f3bf822dba 100644 --- a/heartbeat/monitors/factory.go +++ b/heartbeat/monitors/factory.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors/add_data_stream" "github.com/elastic/beats/v7/libbeat/processors/add_formatted_index" "github.com/elastic/beats/v7/libbeat/publisher/pipetool" + "github.com/elastic/elastic-agent-libs/mapstr" ) // RunnerFactory that can be used to create cfg.Runner cast versions of Monitor @@ -50,7 +51,7 @@ type RunnerFactory struct { type publishSettings struct { // Fields and tags to add to monitor. - EventMetadata common.EventMetadata `config:",inline"` + EventMetadata mapstr.EventMetadata `config:",inline"` Processors processors.PluginConfig `config:"processors"` PublisherPipeline struct { @@ -202,7 +203,7 @@ func preProcessors(info beat.Info, settings publishSettings, monitorType string) } // Always set event.dataset - procs.AddProcessor(actions.NewAddFields(common.MapStr{"event": common.MapStr{"dataset": dataset}}, true, true)) + procs.AddProcessor(actions.NewAddFields(mapstr.M{"event": mapstr.M{"dataset": dataset}}, true, true)) if settings.DataStream != nil { ds := *settings.DataStream diff --git a/heartbeat/monitors/factory_test.go b/heartbeat/monitors/factory_test.go index e4ff3589ccd..0952613390e 100644 --- a/heartbeat/monitors/factory_test.go +++ b/heartbeat/monitors/factory_test.go @@ -27,10 +27,10 @@ import ( "github.com/elastic/beats/v7/heartbeat/scheduler" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/beat/events" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fmtstr" "github.com/elastic/beats/v7/libbeat/monitoring" "github.com/elastic/beats/v7/libbeat/processors/add_data_stream" + "github.com/elastic/elastic-agent-libs/mapstr" ) var binfo = beat.Info{ @@ -100,7 +100,7 @@ func TestPreProcessors(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { - e := beat.Event{Meta: common.MapStr{}, Fields: common.MapStr{}} + e := beat.Event{Meta: mapstr.M{}, Fields: mapstr.M{}} procs, err := preProcessors(binfo, tt.settings, tt.monitorType) if tt.wantErr == true { require.Error(t, err) diff --git a/heartbeat/monitors/jobs/job_test.go b/heartbeat/monitors/jobs/job_test.go index dbb84c32453..c9ecf6d70c5 100644 --- a/heartbeat/monitors/jobs/job_test.go +++ b/heartbeat/monitors/jobs/job_test.go @@ -24,7 +24,7 @@ import ( "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" ) @@ -36,19 +36,19 @@ func TestWrapAll(t *testing.T) { } var basicJob Job = func(event *beat.Event) (jobs []Job, err error) { - eventext.MergeEventFields(event, common.MapStr{"basic": "job"}) + eventext.MergeEventFields(event, mapstr.M{"basic": "job"}) return nil, nil } var contJob Job = func(event *beat.Event) (js []Job, e error) { - eventext.MergeEventFields(event, common.MapStr{"cont": "job"}) + eventext.MergeEventFields(event, mapstr.M{"cont": "job"}) return []Job{basicJob}, nil } addFoo := func(job Job) Job { return func(event *beat.Event) ([]Job, error) { cont, err := job(event) - eventext.MergeEventFields(event, common.MapStr{"foo": "bar"}) + eventext.MergeEventFields(event, mapstr.M{"foo": "bar"}) return cont, err } } @@ -56,7 +56,7 @@ func TestWrapAll(t *testing.T) { addBaz := func(job Job) Job { return func(event *beat.Event) ([]Job, error) { cont, err := job(event) - eventext.MergeEventFields(event, common.MapStr{"baz": "bot"}) + eventext.MergeEventFields(event, mapstr.M{"baz": "bot"}) return cont, err } } diff --git a/heartbeat/monitors/logger/logger_test.go b/heartbeat/monitors/logger/logger_test.go index 9993defeb90..56d83e63d6f 100644 --- a/heartbeat/monitors/logger/logger_test.go +++ b/heartbeat/monitors/logger/logger_test.go @@ -28,8 +28,8 @@ import ( "go.uber.org/zap/zaptest/observer" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestLogRun(t *testing.T) { @@ -42,7 +42,7 @@ func TestLogRun(t *testing.T) { durationMs := time.Duration(durationUs * int64(time.Microsecond)).Milliseconds() steps := 1337 - fields := common.MapStr{ + fields := mapstr.M{ "monitor.id": "b0", "monitor.duration.us": durationUs, "monitor.type": "browser", diff --git a/heartbeat/monitors/mocks_test.go b/heartbeat/monitors/mocks_test.go index 3a68045971f..03758a92739 100644 --- a/heartbeat/monitors/mocks_test.go +++ b/heartbeat/monitors/mocks_test.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/atomic" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/isdef" "github.com/elastic/go-lookslike/validator" @@ -126,7 +127,7 @@ func mockEventMonitorValidator(id string, name string) validator.Validator { } func mockEventCustomFields() map[string]interface{} { - return common.MapStr{"foo": "bar"} + return mapstr.M{"foo": "bar"} } //nolint:unparam // There are no new changes to this line but diff --git a/heartbeat/monitors/monitor_test.go b/heartbeat/monitors/monitor_test.go index 8184a867eae..932e54e2d7a 100644 --- a/heartbeat/monitors/monitor_test.go +++ b/heartbeat/monitors/monitor_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/heartbeat/scheduler" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/isdef" "github.com/elastic/go-lookslike/testslike" @@ -50,8 +51,8 @@ func TestMonitorCfgError(t *testing.T) { mockInvalidPluginConfWithStdFields(t, "invalidTestId", "invalidTestName", "@every 10s"), lookslike.Compose( baseMockEventMonitorValidator("invalidTestId", "invalidTestName", "down"), - lookslike.MustCompile(common.MapStr{ - "error": common.MapStr{ + lookslike.MustCompile(mapstr.M{ + "error": mapstr.M{ "message": isdef.IsStringContaining("missing required field"), "type": "io", }, diff --git a/heartbeat/monitors/stdfields/stdfields_test.go b/heartbeat/monitors/stdfields/stdfields_test.go index c20fc28688a..503367bf2ce 100644 --- a/heartbeat/monitors/stdfields/stdfields_test.go +++ b/heartbeat/monitors/stdfields/stdfields_test.go @@ -24,13 +24,14 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestLegacyServiceNameConfig(t *testing.T) { srvName := "myService" - configBase := func() common.MapStr { - return common.MapStr{ + configBase := func() mapstr.M { + return mapstr.M{ "type": "http", "id": "myId", "schedule": "@every 1s", @@ -41,13 +42,13 @@ func TestLegacyServiceNameConfig(t *testing.T) { legacyOnly["service_name"] = srvName newOnly := configBase() - newOnly["service"] = common.MapStr{"name": srvName} + newOnly["service"] = mapstr.M{"name": srvName} mix := configBase() - mix["service"] = common.MapStr{"name": srvName} + mix["service"] = mapstr.M{"name": srvName} mix["service_name"] = "ignoreMe" - confMaps := []common.MapStr{ + confMaps := []mapstr.M{ legacyOnly, newOnly, mix, diff --git a/heartbeat/monitors/stdfields/unnest.go b/heartbeat/monitors/stdfields/unnest.go index e7fe7690608..b3a36dbcca2 100644 --- a/heartbeat/monitors/stdfields/unnest.go +++ b/heartbeat/monitors/stdfields/unnest.go @@ -21,6 +21,7 @@ import ( "fmt" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // OptionalStream represents a config that has a stream set, which in practice @@ -68,6 +69,6 @@ func UnnestStream(config *common.Config) (res *common.Config, err error) { return nil, fmt.Errorf("could not determine base stream for config: %s", id) } - err = res.Merge(common.MapStr{"id": optS.Id, "data_stream": optS.DataStream}) + err = res.Merge(mapstr.M{"id": optS.Id, "data_stream": optS.DataStream}) return } diff --git a/heartbeat/monitors/stdfields/unnest_test.go b/heartbeat/monitors/stdfields/unnest_test.go index 252f649e512..c7a2f2c8eb0 100644 --- a/heartbeat/monitors/stdfields/unnest_test.go +++ b/heartbeat/monitors/stdfields/unnest_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" "github.com/elastic/go-lookslike/validator" @@ -31,19 +32,19 @@ import ( func TestUnnestStream(t *testing.T) { type testCase struct { name string - cfg common.MapStr + cfg mapstr.M v validator.Validator } tests := []testCase{ { name: "simple", - cfg: common.MapStr{ + cfg: mapstr.M{ "id": "myuuid", - "streams": []common.MapStr{ + "streams": []mapstr.M{ { "type": "montype", "streamid": "mystreamid", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "namespace": "mynamespace", "dataset": "mydataset", "type": "mytype", @@ -51,10 +52,10 @@ func TestUnnestStream(t *testing.T) { }, }, }, - v: lookslike.MustCompile(common.MapStr{ + v: lookslike.MustCompile(mapstr.M{ "id": "myuuid", "type": "montype", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "namespace": "mynamespace", "dataset": "mydataset", "type": "mytype", @@ -63,26 +64,26 @@ func TestUnnestStream(t *testing.T) { }, { name: "split data stream", - cfg: common.MapStr{ + cfg: mapstr.M{ "id": "myuuid", "type": "montype", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "namespace": "mynamespace", }, - "streams": []common.MapStr{ + "streams": []mapstr.M{ { "type": "montype", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "type": "mytype", "dataset": "mydataset", }, }, }, }, - v: lookslike.MustCompile(common.MapStr{ + v: lookslike.MustCompile(mapstr.M{ "id": "myuuid", "type": "montype", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "namespace": "mynamespace", "dataset": "mydataset", "type": "mytype", @@ -91,14 +92,14 @@ func TestUnnestStream(t *testing.T) { }, { name: "base is last, not first stream", - cfg: common.MapStr{ + cfg: mapstr.M{ "id": "myuuid", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "namespace": "parentnamespace", }, - "streams": []common.MapStr{ + "streams": []mapstr.M{ { - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ // Intentionally missing `type` since // this is not the base dataset. // There is only one stream with `type` @@ -107,17 +108,17 @@ func TestUnnestStream(t *testing.T) { }, { "type": "montype", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "type": "basetype", "dataset": "basedataset", }, }, }, }, - v: lookslike.MustCompile(common.MapStr{ + v: lookslike.MustCompile(mapstr.M{ "id": "myuuid", "type": "montype", - "data_stream": common.MapStr{ + "data_stream": mapstr.M{ "namespace": "parentnamespace", "type": "basetype", "dataset": "basedataset", @@ -134,7 +135,7 @@ func TestUnnestStream(t *testing.T) { unnested, err := UnnestStream(src) require.NoError(t, err) - unpacked := common.MapStr{} + unpacked := mapstr.M{} err = unnested.Unpack(unpacked) require.NoError(t, err) testslike.Test(t, test.v, unpacked) diff --git a/heartbeat/monitors/task.go b/heartbeat/monitors/task.go index a7f1848b3ae..5e0382a7d04 100644 --- a/heartbeat/monitors/task.go +++ b/heartbeat/monitors/task.go @@ -26,8 +26,8 @@ import ( "github.com/elastic/beats/v7/heartbeat/scheduler" "github.com/elastic/beats/v7/heartbeat/scheduler/schedule" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // configuredJob represents a job combined with its config and any @@ -103,7 +103,7 @@ func (t *configuredJob) Stop() { func runPublishJob(job jobs.Job, client *WrappedClient) []scheduler.TaskFunc { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, } conts, err := job(event) diff --git a/heartbeat/monitors/task_test.go b/heartbeat/monitors/task_test.go index 64e86b35b88..a7316dbe239 100644 --- a/heartbeat/monitors/task_test.go +++ b/heartbeat/monitors/task_test.go @@ -21,6 +21,7 @@ import ( "context" "testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike/validator" "github.com/stretchr/testify/require" @@ -31,17 +32,16 @@ import ( "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/heartbeat/monitors/jobs" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" ) func Test_runPublishJob(t *testing.T) { - defineJob := func(fields common.MapStr) func(event *beat.Event) (j []jobs.Job, e error) { + defineJob := func(fields mapstr.M) func(event *beat.Event) (j []jobs.Job, e error) { return func(event *beat.Event) (j []jobs.Job, e error) { eventext.MergeEventFields(event, fields) return nil, nil } } - simpleJob := defineJob(common.MapStr{"foo": "bar"}) + simpleJob := defineJob(mapstr.M{"foo": "bar"}) testCases := []struct { name string @@ -71,8 +71,8 @@ func Test_runPublishJob(t *testing.T) { func(event *beat.Event) (j []jobs.Job, e error) { simpleJob(event) return []jobs.Job{ - defineJob(common.MapStr{"baz": "bot"}), - defineJob(common.MapStr{"blah": "blargh"}), + defineJob(mapstr.M{"baz": "bot"}), + defineJob(mapstr.M{"blah": "blargh"}), }, nil }, []validator.Validator{ diff --git a/heartbeat/monitors/util.go b/heartbeat/monitors/util.go index 31191e85e63..e0283eda2b9 100644 --- a/heartbeat/monitors/util.go +++ b/heartbeat/monitors/util.go @@ -28,7 +28,7 @@ import ( "github.com/elastic/beats/v7/heartbeat/monitors/jobs" "github.com/elastic/beats/v7/heartbeat/monitors/wrappers" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // IPSettings provides common configuration settings for IP resolution and ping @@ -109,8 +109,8 @@ func MakeByIPJob( return nil, err } - fields := common.MapStr{ - "monitor": common.MapStr{"ip": addr.String()}, + fields := mapstr.M{ + "monitor": mapstr.M{"ip": addr.String()}, } return wrappers.WithFields(fields, pingFactory(addr)), nil @@ -214,12 +214,12 @@ func makeByHostAllIPJob( } } -func resolveIPEvent(ip string, rtt time.Duration) common.MapStr { - return common.MapStr{ - "monitor": common.MapStr{ +func resolveIPEvent(ip string, rtt time.Duration) mapstr.M { + return mapstr.M{ + "monitor": mapstr.M{ "ip": ip, }, - "resolve": common.MapStr{ + "resolve": mapstr.M{ "ip": ip, "rtt": look.RTT(rtt), }, diff --git a/heartbeat/monitors/wrappers/util.go b/heartbeat/monitors/wrappers/util.go index 30afb29c444..831ea19bb74 100644 --- a/heartbeat/monitors/wrappers/util.go +++ b/heartbeat/monitors/wrappers/util.go @@ -24,17 +24,17 @@ import ( "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/heartbeat/monitors/jobs" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // WithFields wraps a Job and all continuations, updating all events returned with the set of // fields configured. -func WithFields(fields common.MapStr, origJob jobs.Job) jobs.Job { +func WithFields(fields mapstr.M, origJob jobs.Job) jobs.Job { return jobs.Wrap(origJob, Fields(fields)) } // Fields is a JobWrapper that adds fields to a given event -func Fields(fields common.MapStr) jobs.JobWrapper { +func Fields(fields mapstr.M) jobs.JobWrapper { return func(origJob jobs.Job) jobs.Job { return func(event *beat.Event) ([]jobs.Job, error) { cont, err := origJob(event) @@ -46,13 +46,13 @@ func Fields(fields common.MapStr) jobs.JobWrapper { // WithURLField wraps a job setting the "url" field appropriately using URLFields. func WithURLField(u *url.URL, job jobs.Job) jobs.Job { - return WithFields(common.MapStr{"url": URLFields(u)}, job) + return WithFields(mapstr.M{"url": URLFields(u)}, job) } // URLFields generates ECS compatible URL.* fields from a given url. It also sanitizes // the password making sure that, if present, it is replaced with the string ''. -func URLFields(u *url.URL) common.MapStr { - fields := common.MapStr{ +func URLFields(u *url.URL) mapstr.M { + fields := mapstr.M{ "scheme": u.Scheme, "domain": u.Hostname(), } diff --git a/heartbeat/monitors/wrappers/util_test.go b/heartbeat/monitors/wrappers/util_test.go index a681d4cedad..022fb57f5f8 100644 --- a/heartbeat/monitors/wrappers/util_test.go +++ b/heartbeat/monitors/wrappers/util_test.go @@ -23,22 +23,21 @@ import ( "github.com/stretchr/testify/require" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" - - "github.com/elastic/beats/v7/libbeat/common" ) func TestURLFields(t *testing.T) { tests := []struct { name string u string - want common.MapStr + want mapstr.M }{ { "simple-http", "http://elastic.co", - common.MapStr{ + mapstr.M{ "full": "http://elastic.co", "scheme": "http", "domain": "elastic.co", @@ -48,7 +47,7 @@ func TestURLFields(t *testing.T) { { "simple-https", "https://elastic.co", - common.MapStr{ + mapstr.M{ "full": "https://elastic.co", "scheme": "https", "domain": "elastic.co", @@ -58,7 +57,7 @@ func TestURLFields(t *testing.T) { { "fancy-proto", "tcp+ssl://elastic.co:1234", - common.MapStr{ + mapstr.M{ "full": "tcp+ssl://elastic.co:1234", "scheme": "tcp+ssl", "domain": "elastic.co", @@ -68,7 +67,7 @@ func TestURLFields(t *testing.T) { { "complex", "tcp+ssl://myuser:mypass@elastic.co:65500/foo/bar?q=dosomething&x=y", - common.MapStr{ + mapstr.M{ "full": "tcp+ssl://myuser:%3Chidden%3E@elastic.co:65500/foo/bar?q=dosomething&x=y", "scheme": "tcp+ssl", "domain": "elastic.co", diff --git a/heartbeat/monitors/wrappers/wrappers.go b/heartbeat/monitors/wrappers/wrappers.go index 60b7a8def30..75e3f9c1d40 100644 --- a/heartbeat/monitors/wrappers/wrappers.go +++ b/heartbeat/monitors/wrappers/wrappers.go @@ -36,8 +36,8 @@ import ( "github.com/elastic/beats/v7/heartbeat/monitors/stdfields" "github.com/elastic/beats/v7/heartbeat/scheduler/schedule" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // WrapCommon applies the common wrappers that all monitor jobs get. @@ -97,8 +97,8 @@ func addMonitorMeta(sf stdfields.StdMonitorFields, isMulti bool) jobs.JobWrapper id = fmt.Sprintf("%s-%x", sf.ID, urlHash) } - eventext.MergeEventFields(event, common.MapStr{ - "monitor": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "monitor": mapstr.M{ "id": id, "name": name, "type": sf.Type, @@ -114,8 +114,8 @@ func addMonitorTimespan(sf stdfields.StdMonitorFields) jobs.JobWrapper { return func(event *beat.Event) ([]jobs.Job, error) { cont, err := origJob(event) - eventext.MergeEventFields(event, common.MapStr{ - "monitor": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "monitor": mapstr.M{ "timespan": timespan(time.Now(), sf.Schedule, sf.Timeout), }, }) @@ -131,8 +131,8 @@ func addServiceName(sf stdfields.StdMonitorFields) jobs.JobWrapper { cont, err := origJob(event) if sf.Service.Name != "" { - eventext.MergeEventFields(event, common.MapStr{ - "service": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "service": mapstr.M{ "name": sf.Service.Name, }, }) @@ -142,14 +142,14 @@ func addServiceName(sf stdfields.StdMonitorFields) jobs.JobWrapper { } } -func timespan(started time.Time, sched *schedule.Schedule, timeout time.Duration) common.MapStr { +func timespan(started time.Time, sched *schedule.Schedule, timeout time.Duration) mapstr.M { maxEnd := sched.Next(started) if maxEnd.Sub(started) < timeout { maxEnd = started.Add(timeout) } - return common.MapStr{ + return mapstr.M{ "gte": started, "lt": maxEnd, } @@ -171,8 +171,8 @@ func addMonitorStatus(summaryOnly bool) jobs.JobWrapper { } } - fields := common.MapStr{ - "monitor": common.MapStr{ + fields := mapstr.M{ + "monitor": mapstr.M{ "status": look.Status(err), }, } @@ -193,8 +193,8 @@ func addMonitorDuration(job jobs.Job) jobs.Job { duration := time.Since(start) if event != nil { - eventext.MergeEventFields(event, common.MapStr{ - "monitor": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "monitor": mapstr.M{ "duration": look.RTT(duration), }, }) @@ -269,8 +269,8 @@ func makeAddSummary() jobs.JobWrapper { up := state.up down := state.down - eventext.MergeEventFields(event, common.MapStr{ - "summary": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "summary": mapstr.M{ "up": up, "down": down, }, diff --git a/heartbeat/monitors/wrappers/wrappers_test.go b/heartbeat/monitors/wrappers/wrappers_test.go index 8708ea3fe03..c7b10b5b3d4 100644 --- a/heartbeat/monitors/wrappers/wrappers_test.go +++ b/heartbeat/monitors/wrappers/wrappers_test.go @@ -37,8 +37,8 @@ import ( "github.com/elastic/beats/v7/heartbeat/monitors/stdfields" "github.com/elastic/beats/v7/heartbeat/scheduler/schedule" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/isdef" "github.com/elastic/go-lookslike/testslike" @@ -241,14 +241,14 @@ func TestMultiJobConts(t *testing.T) { makeContJob := func(t *testing.T, u string) jobs.Job { return func(event *beat.Event) ([]jobs.Job, error) { - eventext.MergeEventFields(event, common.MapStr{"cont": "1st"}) + eventext.MergeEventFields(event, mapstr.M{"cont": "1st"}) u, err := url.Parse(u) require.NoError(t, err) - eventext.MergeEventFields(event, common.MapStr{"url": URLFields(u)}) + eventext.MergeEventFields(event, mapstr.M{"url": URLFields(u)}) return []jobs.Job{ func(event *beat.Event) ([]jobs.Job, error) { - eventext.MergeEventFields(event, common.MapStr{"cont": "2nd"}) - eventext.MergeEventFields(event, common.MapStr{"url": URLFields(u)}) + eventext.MergeEventFields(event, mapstr.M{"cont": "2nd"}) + eventext.MergeEventFields(event, mapstr.M{"url": URLFields(u)}) return nil, nil }, }, nil @@ -299,15 +299,15 @@ func TestMultiJobContsCancelledEvents(t *testing.T) { makeContJob := func(t *testing.T, u string) jobs.Job { return func(event *beat.Event) ([]jobs.Job, error) { - eventext.MergeEventFields(event, common.MapStr{"cont": "1st"}) + eventext.MergeEventFields(event, mapstr.M{"cont": "1st"}) eventext.CancelEvent(event) u, err := url.Parse(u) require.NoError(t, err) - eventext.MergeEventFields(event, common.MapStr{"url": URLFields(u)}) + eventext.MergeEventFields(event, mapstr.M{"url": URLFields(u)}) return []jobs.Job{ func(event *beat.Event) ([]jobs.Job, error) { - eventext.MergeEventFields(event, common.MapStr{"cont": "2nd"}) - eventext.MergeEventFields(event, common.MapStr{"url": URLFields(u)}) + eventext.MergeEventFields(event, mapstr.M{"cont": "2nd"}) + eventext.MergeEventFields(event, mapstr.M{"url": URLFields(u)}) return nil, nil }, }, nil @@ -355,9 +355,9 @@ func TestMultiJobContsCancelledEvents(t *testing.T) { }, []validator.Validator{ metaCancelledValidator, - lookslike.MustCompile(isdef.IsEqual(common.MapStr(nil))), + lookslike.MustCompile(isdef.IsEqual(mapstr.M(nil))), metaCancelledValidator, - lookslike.MustCompile(isdef.IsEqual(common.MapStr(nil))), + lookslike.MustCompile(isdef.IsEqual(mapstr.M(nil))), }, nil, }) @@ -367,7 +367,7 @@ func makeURLJob(t *testing.T, u string) jobs.Job { parsed, err := url.Parse(u) require.NoError(t, err) return func(event *beat.Event) (i []jobs.Job, e error) { - eventext.MergeEventFields(event, common.MapStr{"url": URLFields(parsed)}) + eventext.MergeEventFields(event, mapstr.M{"url": URLFields(parsed)}) return nil, nil } } @@ -402,12 +402,12 @@ func TestTimespan(t *testing.T) { tests := []struct { name string args args - want common.MapStr + want mapstr.M }{ { "interval longer than timeout", args{now, sched10s, time.Second}, - common.MapStr{ + mapstr.M{ "gte": now, "lt": now.Add(time.Second * 10), }, @@ -415,7 +415,7 @@ func TestTimespan(t *testing.T) { { "timeout longer than interval", args{now, sched10s, time.Second * 20}, - common.MapStr{ + mapstr.M{ "gte": now, "lt": now.Add(time.Second * 20), }, @@ -446,9 +446,9 @@ func makeInlineBrowserJob(t *testing.T, u string) jobs.Job { parsed, err := url.Parse(u) require.NoError(t, err) return func(event *beat.Event) (i []jobs.Job, e error) { - eventext.MergeEventFields(event, common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ "url": URLFields(parsed), - "monitor": common.MapStr{ + "monitor": mapstr.M{ "type": "browser", "id": inlineMonitorValues.id, "name": inlineMonitorValues.name, @@ -498,9 +498,9 @@ func makeSuiteBrowserJob(t *testing.T, u string, summary bool, suiteErr error) j parsed, err := url.Parse(u) require.NoError(t, err) return func(event *beat.Event) (i []jobs.Job, e error) { - eventext.MergeEventFields(event, common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ "url": URLFields(parsed), - "monitor": common.MapStr{ + "monitor": mapstr.M{ "type": "browser", "id": suiteMonitorValues.id, "name": suiteMonitorValues.name, @@ -508,13 +508,13 @@ func makeSuiteBrowserJob(t *testing.T, u string, summary bool, suiteErr error) j }, }) if summary { - sumFields := common.MapStr{"up": 0, "down": 0} + sumFields := mapstr.M{"up": 0, "down": 0} if suiteErr == nil { sumFields["up"] = 1 } else { sumFields["down"] = 1 } - eventext.MergeEventFields(event, common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ "summary": sumFields, }) } @@ -532,7 +532,7 @@ func TestSuiteBrowserJob(t *testing.T) { "id": suiteMonitorValues.id, "name": suiteMonitorValues.name, "check_group": suiteMonitorValues.checkGroup, - "timespan": common.MapStr{ + "timespan": mapstr.M{ "gte": hbtestllext.IsTime, "lt": hbtestllext.IsTime, }, diff --git a/heartbeat/reason/reason.go b/heartbeat/reason/reason.go index ad1823af8e3..d09a2f09ad4 100644 --- a/heartbeat/reason/reason.go +++ b/heartbeat/reason/reason.go @@ -17,7 +17,7 @@ package reason -import "github.com/elastic/beats/v7/libbeat/common" +import "github.com/elastic/elastic-agent-libs/mapstr" type Reason interface { error @@ -55,21 +55,21 @@ func (e IOError) Error() string { return e.err.Error() } func (e IOError) Unwrap() error { return e.err } func (IOError) Type() string { return "io" } -func FailError(typ string, err error) common.MapStr { - return common.MapStr{ +func FailError(typ string, err error) mapstr.M { + return mapstr.M{ "type": typ, "message": err.Error(), } } -func Fail(r Reason) common.MapStr { - return common.MapStr{ +func Fail(r Reason) mapstr.M { + return mapstr.M{ "type": r.Type(), "message": r.Error(), } } -func FailIO(err error) common.MapStr { return Fail(IOError{err}) } +func FailIO(err error) mapstr.M { return Fail(IOError{err}) } // MakeValidateError creates an instance of ValidateError from the given error. func MakeValidateError(err error) ValidateError { diff --git a/libbeat/api/routes.go b/libbeat/api/routes.go index 14b213b1928..1b584fe8079 100644 --- a/libbeat/api/routes.go +++ b/libbeat/api/routes.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" ) type handlerFunc func(http.ResponseWriter, *http.Request) @@ -80,7 +81,7 @@ func makeAPIHandler(ns *monitoring.Namespace) handlerFunc { } } -func prettyPrint(w http.ResponseWriter, data common.MapStr, u *url.URL) { +func prettyPrint(w http.ResponseWriter, data mapstr.M, u *url.URL) { query := u.Query() if _, ok := query["pretty"]; ok { fmt.Fprintf(w, data.StringToPrint()) diff --git a/libbeat/autodiscover/appenders/config/config.go b/libbeat/autodiscover/appenders/config/config.go index 018ee1b587d..4e7153e593a 100644 --- a/libbeat/autodiscover/appenders/config/config.go +++ b/libbeat/autodiscover/appenders/config/config.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/conditions" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -42,7 +43,7 @@ type config struct { type configAppender struct { condition conditions.Condition - config common.MapStr + config mapstr.M } // NewConfigAppender creates a configAppender that can append templatized configs into built configs @@ -65,7 +66,7 @@ func NewConfigAppender(cfg *common.Config) (autodiscover.Appender, error) { } // Unpack the config - cf := common.MapStr{} + cf := mapstr.M{} err = config.Config.Unpack(&cf) if err != nil { return nil, errors.Wrap(err, "unable to unpack config due to error") @@ -88,10 +89,10 @@ func (c *configAppender) Append(event bus.Event) { if !ok { return } - if c.condition == nil || c.condition.Check(common.MapStr(event)) == true { + if c.condition == nil || c.condition.Check(mapstr.M(event)) == true { // Merge the template with all the configs for _, cfg := range cfgs { - cf := common.MapStr{} + cf := mapstr.M{} err := cfg.Unpack(&cf) if err != nil { logp.Debug("config", "unable to unpack config due to error: %v", err) diff --git a/libbeat/autodiscover/appenders/config/config_test.go b/libbeat/autodiscover/appenders/config/config_test.go index 8256c852318..8d63a71c9bc 100644 --- a/libbeat/autodiscover/appenders/config/config_test.go +++ b/libbeat/autodiscover/appenders/config/config_test.go @@ -24,24 +24,25 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateAppender(t *testing.T) { tests := []struct { name string - eventConfig common.MapStr + eventConfig mapstr.M event bus.Event - result common.MapStr + result mapstr.M config string }{ { name: "Appender without a condition should apply the config regardless", event: bus.Event{}, - result: common.MapStr{ + result: mapstr.M{ "test": "bar", "test1": "foo", }, - eventConfig: common.MapStr{ + eventConfig: mapstr.M{ "test": "bar", }, config: ` @@ -53,10 +54,10 @@ config: event: bus.Event{ "field": "notbar", }, - result: common.MapStr{ + result: mapstr.M{ "test": "bar", }, - eventConfig: common.MapStr{ + eventConfig: mapstr.M{ "test": "bar", }, config: ` @@ -70,11 +71,11 @@ condition.equals: event: bus.Event{ "field": "bar", }, - result: common.MapStr{ + result: mapstr.M{ "test": "bar", "test2": "foo", }, - eventConfig: common.MapStr{ + eventConfig: mapstr.M{ "test": "bar", }, config: ` @@ -104,7 +105,7 @@ condition.equals: cfgs, _ := test.event["config"].([]*common.Config) assert.Equal(t, len(cfgs), 1) - out := common.MapStr{} + out := mapstr.M{} cfgs[0].Unpack(&out) assert.Equal(t, out, test.result) diff --git a/libbeat/autodiscover/autodiscover.go b/libbeat/autodiscover/autodiscover.go index e1f1d8d2bfc..540c735186a 100644 --- a/libbeat/autodiscover/autodiscover.go +++ b/libbeat/autodiscover/autodiscover.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/reload" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -280,14 +281,14 @@ func (a *Autodiscover) handleStop(event bus.Event) bool { return updated } -func (a *Autodiscover) getMeta(event bus.Event) common.MapStr { +func (a *Autodiscover) getMeta(event bus.Event) mapstr.M { m := event["meta"] if m == nil { return nil } a.logger.Debugf("Got a meta field in the event") - meta, ok := m.(common.MapStr) + meta, ok := m.(mapstr.M) if !ok { a.logger.Errorf("Got a wrong meta field for event %v", event) return nil diff --git a/libbeat/autodiscover/autodiscover_test.go b/libbeat/autodiscover/autodiscover_test.go index 2d0ea26b689..f0e176e6685 100644 --- a/libbeat/autodiscover/autodiscover_test.go +++ b/libbeat/autodiscover/autodiscover_test.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/tests/resources" + "github.com/elastic/elastic-agent-libs/mapstr" ) type mockRunner struct { @@ -66,7 +67,7 @@ func (m *mockRunner) String() string { m.mutex.Lock() defer m.mutex.Unlock() - out := common.MapStr{} + out := mapstr.M{} m.config.Unpack(&out) return fmt.Sprintf("config: %v, started=%v, stopped=%v", out.String(), m.started, m.stopped) } @@ -191,7 +192,7 @@ func TestAutodiscover(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "bar", }, }) @@ -208,7 +209,7 @@ func TestAutodiscover(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "baz", }, }) @@ -224,7 +225,7 @@ func TestAutodiscover(t *testing.T) { "id": "foo", "provider": "mock", "stop": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "baz", }, }) @@ -232,7 +233,7 @@ func TestAutodiscover(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "baz", }, }) @@ -250,7 +251,7 @@ func TestAutodiscover(t *testing.T) { "id": "foo", "provider": "mock", "stop": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "baz", }, }) @@ -313,7 +314,7 @@ func TestAutodiscoverHash(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "bar", }, }) @@ -378,7 +379,7 @@ func TestAutodiscoverDuplicatedConfigConfigCheckCalledOnce(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "bar", }, }) @@ -437,7 +438,7 @@ func TestAutodiscoverWithConfigCheckFailures(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "bar", }, }) @@ -493,7 +494,7 @@ func TestAutodiscoverWithMutlipleEntries(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "bar", }, "config": []*common.Config{ @@ -517,7 +518,7 @@ func TestAutodiscoverWithMutlipleEntries(t *testing.T) { "id": "foo", "provider": "mock", "start": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "bar", }, "config": []*common.Config{ @@ -551,7 +552,7 @@ func TestAutodiscoverWithMutlipleEntries(t *testing.T) { "id": "foo", "provider": "mock", "stop": true, - "meta": common.MapStr{ + "meta": mapstr.M{ "foo": "bar", }, "config": []*common.Config{ @@ -596,7 +597,7 @@ func check(t *testing.T, runners []*mockRunner, expected *common.Config, started } // Fail the test case if the check fails - out := common.MapStr{} + out := mapstr.M{} expected.Unpack(&out) t.Fatalf("expected cfg %v to be started=%v stopped=%v but have %v", out, started, stopped, runners) } diff --git a/libbeat/autodiscover/builder/helper.go b/libbeat/autodiscover/builder/helper.go index 6f78973e6fb..e2b5b5ae054 100644 --- a/libbeat/autodiscover/builder/helper.go +++ b/libbeat/autodiscover/builder/helper.go @@ -24,27 +24,27 @@ import ( "strconv" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const logName = "autodiscover.builder" // GetContainerID returns the id of a container -func GetContainerID(container common.MapStr) string { +func GetContainerID(container mapstr.M) string { id, _ := container["id"].(string) return id } // GetContainerName returns the name of a container -func GetContainerName(container common.MapStr) string { +func GetContainerName(container mapstr.M) string { name, _ := container["name"].(string) return name } // GetHintString takes a hint and returns its value as a string -func GetHintString(hints common.MapStr, key, config string) string { +func GetHintString(hints mapstr.M, key, config string) string { base := config if base == "" { base = key @@ -61,7 +61,7 @@ func GetHintString(hints common.MapStr, key, config string) string { } // GetHintMapStr takes a hint and returns a MapStr -func GetHintMapStr(hints common.MapStr, key, config string) common.MapStr { +func GetHintMapStr(hints mapstr.M, key, config string) mapstr.M { base := config if base == "" { base = key @@ -69,7 +69,7 @@ func GetHintMapStr(hints common.MapStr, key, config string) common.MapStr { base = fmt.Sprint(key, ".", config) } if iface, err := hints.GetValue(base); err == nil { - if mapstr, ok := iface.(common.MapStr); ok { + if mapstr, ok := iface.(mapstr.M); ok { return mapstr } } @@ -78,7 +78,7 @@ func GetHintMapStr(hints common.MapStr, key, config string) common.MapStr { } // GetHintAsList takes a hint and returns the value as lists. -func GetHintAsList(hints common.MapStr, key, config string) []string { +func GetHintAsList(hints mapstr.M, key, config string) []string { if str := GetHintString(hints, key, config); str != "" { return getStringAsList(str) } @@ -87,12 +87,12 @@ func GetHintAsList(hints common.MapStr, key, config string) []string { } // GetProcessors gets processor definitions from the hints and returns a list of configs as a MapStr -func GetProcessors(hints common.MapStr, key string) []common.MapStr { +func GetProcessors(hints mapstr.M, key string) []mapstr.M { processors := GetConfigs(hints, key, "processors") for _, proc := range processors { for key, value := range proc { if str, ok := value.(string); ok { - cfg := common.MapStr{} + cfg := mapstr.M{} if err := json.Unmarshal([]byte(str), &cfg); err != nil { logp.NewLogger(logName).Debugw("Unable to unmarshal json due to error", "error", err) continue @@ -105,7 +105,7 @@ func GetProcessors(hints common.MapStr, key string) []common.MapStr { } // GetConfigs takes in a key and returns a list of configs as a slice of MapStr -func GetConfigs(hints common.MapStr, key, name string) []common.MapStr { +func GetConfigs(hints mapstr.M, key, name string) []mapstr.M { raw := GetHintMapStr(hints, key, name) if raw == nil { return nil @@ -124,16 +124,16 @@ func GetConfigs(hints common.MapStr, key, name string) []common.MapStr { sort.Strings(nums) - var configs []common.MapStr + var configs []mapstr.M for _, key := range nums { rawCfg := raw[key] - if config, ok := rawCfg.(common.MapStr); ok { + if config, ok := rawCfg.(mapstr.M); ok { configs = append(configs, config) } } for _, word := range words { - configs = append(configs, common.MapStr{ + configs = append(configs, mapstr.M{ word: raw[word], }) } @@ -154,20 +154,20 @@ func getStringAsList(input string) []string { return list } -// GetHintAsConfigs can read a hint in the form of a stringified JSON and return a common.MapStr -func GetHintAsConfigs(hints common.MapStr, key string) []common.MapStr { +// GetHintAsConfigs can read a hint in the form of a stringified JSON and return a mapstr.M +func GetHintAsConfigs(hints mapstr.M, key string) []mapstr.M { if str := GetHintString(hints, key, "raw"); str != "" { // check if it is a single config if str[0] != '[' { - cfg := common.MapStr{} + cfg := mapstr.M{} if err := json.Unmarshal([]byte(str), &cfg); err != nil { logp.NewLogger(logName).Debugw("Unable to unmarshal json due to error", "error", err) return nil } - return []common.MapStr{cfg} + return []mapstr.M{cfg} } - var cfg []common.MapStr + var cfg []mapstr.M if err := json.Unmarshal([]byte(str), &cfg); err != nil { logp.NewLogger(logName).Debugw("Unable to unmarshal json due to error", "error", err) return nil @@ -178,7 +178,7 @@ func GetHintAsConfigs(hints common.MapStr, key string) []common.MapStr { } // IsEnabled will return true when 'enabled' is **explicitly** set to true. -func IsEnabled(hints common.MapStr, key string) bool { +func IsEnabled(hints mapstr.M, key string) bool { if value, err := hints.GetValue(fmt.Sprintf("%s.enabled", key)); err == nil { enabled, _ := strconv.ParseBool(value.(string)) return enabled @@ -188,7 +188,7 @@ func IsEnabled(hints common.MapStr, key string) bool { } // IsDisabled will return true when 'enabled' is **explicitly** set to false. -func IsDisabled(hints common.MapStr, key string) bool { +func IsDisabled(hints mapstr.M, key string) bool { if value, err := hints.GetValue(fmt.Sprintf("%s.enabled", key)); err == nil { enabled, err := strconv.ParseBool(value.(string)) if err != nil { @@ -210,10 +210,10 @@ func IsDisabled(hints common.MapStr, key string) bool { } // GenerateHints parses annotations based on a prefix and sets up hints that can be picked up by individual Beats. -func GenerateHints(annotations common.MapStr, container, prefix string) common.MapStr { - hints := common.MapStr{} +func GenerateHints(annotations mapstr.M, container, prefix string) mapstr.M { + hints := mapstr.M{} if rawEntries, err := annotations.GetValue(prefix); err == nil { - if entries, ok := rawEntries.(common.MapStr); ok { + if entries, ok := rawEntries.(mapstr.M); ok { for key, rawValue := range entries { // If there are top level hints like co.elastic.logs/ then just add the values after the / // Only consider namespaced annotations @@ -226,9 +226,9 @@ func GenerateHints(annotations common.MapStr, container, prefix string) common.M hints.Put(hintKey, rawValue) } } else if container != "" { - // Only consider annotations that are of type common.MapStr as we are looking for + // Only consider annotations that are of type mapstr.M as we are looking for // container level nesting - builderHints, ok := rawValue.(common.MapStr) + builderHints, ok := rawValue.(mapstr.M) if !ok { continue } @@ -254,7 +254,7 @@ func GenerateHints(annotations common.MapStr, container, prefix string) common.M } // GetHintsAsList gets a set of hints and tries to convert them into a list of hints -func GetHintsAsList(hints common.MapStr, key string) []common.MapStr { +func GetHintsAsList(hints mapstr.M, key string) []mapstr.M { raw := GetHintMapStr(hints, key, "") if raw == nil { return nil @@ -273,15 +273,15 @@ func GetHintsAsList(hints common.MapStr, key string) []common.MapStr { sort.Strings(nums) - var configs []common.MapStr + var configs []mapstr.M for _, key := range nums { rawCfg := raw[key] - if config, ok := rawCfg.(common.MapStr); ok { + if config, ok := rawCfg.(mapstr.M); ok { configs = append(configs, config) } } - defaultMap := common.MapStr{} + defaultMap := mapstr.M{} for _, word := range words { defaultMap[word] = raw[word] } diff --git a/libbeat/autodiscover/builder/helper_test.go b/libbeat/autodiscover/builder/helper_test.go index dee7c95ef13..56d8cbe5ef9 100644 --- a/libbeat/autodiscover/builder/helper_test.go +++ b/libbeat/autodiscover/builder/helper_test.go @@ -22,15 +22,15 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGetProcessors(t *testing.T) { - hints := common.MapStr{ - "co": common.MapStr{ - "elastic": common.MapStr{ - "logs": common.MapStr{ - "processors": common.MapStr{ + hints := mapstr.M{ + "co": mapstr.M{ + "elastic": mapstr.M{ + "logs": mapstr.M{ + "processors": mapstr.M{ "add_fields": `{"fields": {"foo": "bar"}}`, }, }, @@ -38,9 +38,9 @@ func TestGetProcessors(t *testing.T) { }, } procs := GetProcessors(hints, "co.elastic.logs") - assert.Equal(t, []common.MapStr{ - common.MapStr{ - "add_fields": common.MapStr{ + assert.Equal(t, []mapstr.M{ + mapstr.M{ + "add_fields": mapstr.M{ "fields": map[string]interface{}{ "foo": "bar", }, @@ -52,18 +52,18 @@ func TestGetProcessors(t *testing.T) { func TestGenerateHints(t *testing.T) { tests := []struct { annotations map[string]string - result common.MapStr + result mapstr.M }{ // Empty annotations should return empty hints { annotations: map[string]string{}, - result: common.MapStr{}, + result: mapstr.M{}, }, // Scenarios being tested: - // logs/multiline.pattern must be a nested common.MapStr under hints.logs - // logs/processors.add_fields must be nested common.MapStr under hints.logs - // logs/json.keys_under_root must be a nested common.MapStr under hints.logs + // logs/multiline.pattern must be a nested mapstr.M under hints.logs + // logs/processors.add_fields must be nested mapstr.M under hints.logs + // logs/json.keys_under_root must be a nested mapstr.M under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints @@ -77,23 +77,23 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics.foobar1/period": "15s", "not.to.include": "true", }, - result: common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + result: mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, - "json": common.MapStr{ + "json": mapstr.M{ "keys_under_root": "true", }, }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, }, }, // Scenarios being tested: - // logs/multiline.pattern must be a nested common.MapStr under hints.logs + // logs/multiline.pattern must be a nested mapstr.M under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints // metrics/metrics_path must be found in hints.metrics @@ -109,13 +109,13 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics.foobar1/period": "15s", "not.to.include": "true", }, - result: common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + result: mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", "metrics_path": "/metrics/prometheus", @@ -126,7 +126,7 @@ func TestGenerateHints(t *testing.T) { }, // Scenarios being tested: // have co.elastic.logs/disable set to false. - // logs/multiline.pattern must be a nested common.MapStr under hints.logs + // logs/multiline.pattern must be a nested mapstr.M under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints @@ -139,13 +139,13 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics.foobar1/period": "15s", "not.to.include": "true", }, - result: common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + result: mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, @@ -153,7 +153,7 @@ func TestGenerateHints(t *testing.T) { }, // Scenarios being tested: // have co.elastic.logs/disable set to false. - // logs/multiline.pattern must be a nested common.MapStr under hints.logs + // logs/multiline.pattern must be a nested mapstr.M under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints @@ -167,14 +167,14 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics.foobar1/period": "15s", "not.to.include": "true", }, - result: common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + result: mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, "disable": "false", }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, @@ -182,7 +182,7 @@ func TestGenerateHints(t *testing.T) { }, // Scenarios being tested: // have co.elastic.logs/disable set to true. - // logs/multiline.pattern must be a nested common.MapStr under hints.logs + // logs/multiline.pattern must be a nested mapstr.M under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints @@ -196,14 +196,14 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics.foobar1/period": "15s", "not.to.include": "true", }, - result: common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + result: mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, "disable": "true", }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, @@ -212,7 +212,7 @@ func TestGenerateHints(t *testing.T) { } for _, test := range tests { - annMap := common.MapStr{} + annMap := mapstr.M{} for k, v := range test.annotations { annMap.Put(k, v) } @@ -221,18 +221,18 @@ func TestGenerateHints(t *testing.T) { } func TestGetHintsAsList(t *testing.T) { tests := []struct { - input common.MapStr - output []common.MapStr + input mapstr.M + output []mapstr.M message string }{ { - input: common.MapStr{ - "metrics": common.MapStr{ + input: mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, }, - output: []common.MapStr{ + output: []mapstr.M{ { "module": "prometheus", "period": "15s", @@ -241,15 +241,15 @@ func TestGetHintsAsList(t *testing.T) { message: "Single hint should return a single set of configs", }, { - input: common.MapStr{ - "metrics": common.MapStr{ - "1": common.MapStr{ + input: mapstr.M{ + "metrics": mapstr.M{ + "1": mapstr.M{ "module": "prometheus", "period": "15s", }, }, }, - output: []common.MapStr{ + output: []mapstr.M{ { "module": "prometheus", "period": "15s", @@ -258,19 +258,19 @@ func TestGetHintsAsList(t *testing.T) { message: "Single hint with numeric prefix should return a single set of configs", }, { - input: common.MapStr{ - "metrics": common.MapStr{ - "1": common.MapStr{ + input: mapstr.M{ + "metrics": mapstr.M{ + "1": mapstr.M{ "module": "prometheus", "period": "15s", }, - "2": common.MapStr{ + "2": mapstr.M{ "module": "dropwizard", "period": "20s", }, }, }, - output: []common.MapStr{ + output: []mapstr.M{ { "module": "prometheus", "period": "15s", @@ -283,9 +283,9 @@ func TestGetHintsAsList(t *testing.T) { message: "Multiple hints with numeric prefix should return configs in numeric ordering", }, { - input: common.MapStr{ - "metrics": common.MapStr{ - "1": common.MapStr{ + input: mapstr.M{ + "metrics": mapstr.M{ + "1": mapstr.M{ "module": "prometheus", "period": "15s", }, @@ -293,7 +293,7 @@ func TestGetHintsAsList(t *testing.T) { "period": "20s", }, }, - output: []common.MapStr{ + output: []mapstr.M{ { "module": "prometheus", "period": "15s", diff --git a/libbeat/autodiscover/meta/meta.go b/libbeat/autodiscover/meta/meta.go index bf1a2048cae..e8037e99407 100644 --- a/libbeat/autodiscover/meta/meta.go +++ b/libbeat/autodiscover/meta/meta.go @@ -20,35 +20,35 @@ package meta import ( "sync" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Map stores a map of id -> MapStrPointer type Map struct { mutex sync.RWMutex - meta map[uint64]common.MapStrPointer + meta map[uint64]mapstr.Pointer } // NewMap instantiates and returns a new meta.Map func NewMap() *Map { return &Map{ - meta: make(map[uint64]common.MapStrPointer), + meta: make(map[uint64]mapstr.Pointer), } } // Store inserts or updates given meta under the given id. Then it returns a MapStrPointer to it -func (m *Map) Store(id uint64, meta common.MapStr) common.MapStrPointer { +func (m *Map) Store(id uint64, meta mapstr.M) mapstr.Pointer { m.mutex.Lock() defer m.mutex.Unlock() if meta == nil { - return common.MapStrPointer{} + return mapstr.Pointer{} } p, ok := m.meta[id] if !ok { // create - p = common.NewMapStrPointer(meta) + p = mapstr.NewPointer(meta) m.meta[id] = p } else { // update diff --git a/libbeat/autodiscover/meta/meta_test.go b/libbeat/autodiscover/meta/meta_test.go index d48351324eb..82c63d0b8d1 100644 --- a/libbeat/autodiscover/meta/meta_test.go +++ b/libbeat/autodiscover/meta/meta_test.go @@ -22,24 +22,24 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestStoreNil(t *testing.T) { m := NewMap() - assert.Equal(t, common.MapStrPointer{}, m.Store(0, nil)) + assert.Equal(t, mapstr.Pointer{}, m.Store(0, nil)) } func TestStore(t *testing.T) { m := NewMap() // Store meta - res := m.Store(0, common.MapStr{"foo": "bar"}) - assert.Equal(t, res.Get(), common.MapStr{"foo": "bar"}) + res := m.Store(0, mapstr.M{"foo": "bar"}) + assert.Equal(t, res.Get(), mapstr.M{"foo": "bar"}) // Update it - res = m.Store(0, common.MapStr{"foo": "baz"}) - assert.Equal(t, res.Get(), common.MapStr{"foo": "baz"}) + res = m.Store(0, mapstr.M{"foo": "baz"}) + assert.Equal(t, res.Get(), mapstr.M{"foo": "baz"}) m.Remove(0) assert.Equal(t, len(m.meta), 0) diff --git a/libbeat/autodiscover/providers/docker/docker.go b/libbeat/autodiscover/providers/docker/docker.go index eb13f7cd2e9..5e7bbf25d55 100644 --- a/libbeat/autodiscover/providers/docker/docker.go +++ b/libbeat/autodiscover/providers/docker/docker.go @@ -34,9 +34,10 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/common/docker" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) func init() { @@ -163,14 +164,14 @@ type dockerContainerMetadata struct { type dockerMetadata struct { // Old selectors [Deprecated] - Docker common.MapStr + Docker mapstr.M // New ECS-based selectors - Container common.MapStr + Container mapstr.M // Metadata used to enrich events, like ECS-based selectors but can // have modifications like dedotting - Metadata common.MapStr + Metadata mapstr.M } func (d *Provider) generateMetaDocker(event bus.Event) (*docker.Container, *dockerMetadata) { @@ -181,8 +182,8 @@ func (d *Provider) generateMetaDocker(event bus.Event) (*docker.Container, *dock } // Don't dedot selectors, dedot only metadata used for events enrichment - labelMap := common.MapStr{} - metaLabelMap := common.MapStr{} + labelMap := mapstr.M{} + metaLabelMap := mapstr.M{} for k, v := range container.Labels { err := safemapstr.Put(labelMap, k, v) if err != nil { @@ -203,32 +204,32 @@ func (d *Provider) generateMetaDocker(event bus.Event) (*docker.Container, *dock } meta := &dockerMetadata{ - Docker: common.MapStr{ - "container": common.MapStr{ + Docker: mapstr.M{ + "container": mapstr.M{ "id": container.ID, "name": container.Name, "image": container.Image, "labels": labelMap, }, }, - Container: common.MapStr{ + Container: mapstr.M{ "id": container.ID, "name": container.Name, - "image": common.MapStr{ + "image": mapstr.M{ "name": container.Image, }, "labels": labelMap, }, - Metadata: common.MapStr{ - "container": common.MapStr{ + Metadata: mapstr.M{ + "container": mapstr.M{ "id": container.ID, "name": container.Name, - "image": common.MapStr{ + "image": mapstr.M{ "name": container.Image, }, }, - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "labels": metaLabelMap, }, }, @@ -282,7 +283,7 @@ func (d *Provider) stopContainer(container *docker.Container, meta *dockerMetada func (d *Provider) emitContainer(container *docker.Container, meta *dockerMetadata, flag string) { var host string - var ports common.MapStr + var ports mapstr.M if len(container.IPAddresses) > 0 { host = container.IPAddresses[0] } @@ -302,7 +303,7 @@ func (d *Provider) emitContainer(container *docker.Container, meta *dockerMetada events = append(events, event) } else { - ports = common.MapStr{} + ports = mapstr.M{} for _, port := range container.Ports { ports[strconv.FormatUint(uint64(port.PrivatePort), 10)] = port.PublicPort } @@ -346,7 +347,7 @@ func (d *Provider) publish(events []bus.Event) { } // Since all the events belong to the same event ID pick on and add in all the configs - event := bus.Event(common.MapStr(events[0]).Clone()) + event := bus.Event(mapstr.M(events[0]).Clone()) // Remove the port to avoid ambiguity during debugging delete(event, "port") delete(event, "ports") @@ -361,11 +362,11 @@ func (d *Provider) generateHints(event bus.Event) bus.Event { // Try to build a config with enabled builders. Send a provider agnostic payload. // Builders are Beat specific. e := bus.Event{} - var dockerMeta common.MapStr + var dockerMeta mapstr.M var ok bool - if rawDocker, err := common.MapStr(event).GetValue("docker.container"); err == nil { - dockerMeta, ok = rawDocker.(common.MapStr) + if rawDocker, err := mapstr.M(event).GetValue("docker.container"); err == nil { + dockerMeta, ok = rawDocker.(mapstr.M) if ok { e["container"] = dockerMeta } @@ -381,7 +382,7 @@ func (d *Provider) generateHints(event bus.Event) bus.Event { e["ports"] = ports } if labels, err := dockerMeta.GetValue("labels"); err == nil { - hints := builder.GenerateHints(labels.(common.MapStr), "", d.config.Prefix) + hints := builder.GenerateHints(labels.(mapstr.M), "", d.config.Prefix) e["hints"] = hints } return e diff --git a/libbeat/autodiscover/providers/docker/docker_integration_test.go b/libbeat/autodiscover/providers/docker/docker_integration_test.go index 28e9d98c0f3..ed774213d3a 100644 --- a/libbeat/autodiscover/providers/docker/docker_integration_test.go +++ b/libbeat/autodiscover/providers/docker/docker_integration_test.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" dk "github.com/elastic/beats/v7/libbeat/tests/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Test docker start emits an autodiscover event @@ -83,7 +84,7 @@ func TestDockerStart(t *testing.T) { } func getValue(e bus.Event, key string) interface{} { - val, err := common.MapStr(e).GetValue(key) + val, err := mapstr.M(e).GetValue(key) if err != nil { return nil } @@ -109,8 +110,8 @@ func checkEvent(t *testing.T, listener bus.Listener, id string, start bool) { assert.Equal(t, getValue(e, "container.image.name"), "busybox:latest") // labels.dedot=true by default assert.Equal(t, - common.MapStr{ - "label": common.MapStr{ + mapstr.M{ + "label": mapstr.M{ "value": "foo", "child": "bar", }, diff --git a/libbeat/autodiscover/providers/docker/docker_test.go b/libbeat/autodiscover/providers/docker/docker_test.go index 40fef07f183..4f14e63490b 100644 --- a/libbeat/autodiscover/providers/docker/docker_test.go +++ b/libbeat/autodiscover/providers/docker/docker_test.go @@ -22,9 +22,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/common/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints(t *testing.T) { @@ -40,15 +40,15 @@ func TestGenerateHints(t *testing.T) { // Docker meta must be present in the hints { event: bus.Event{ - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "id": "abc", "name": "foobar", }, }, }, result: bus.Event{ - "container": common.MapStr{ + "container": mapstr.M{ "id": "abc", "name": "foobar", }, @@ -59,11 +59,11 @@ func TestGenerateHints(t *testing.T) { // logs/disable should be present in hints.logs.disable=true { event: bus.Event{ - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "id": "abc", "name": "foobar", - "labels": getNestedAnnotations(common.MapStr{ + "labels": getNestedAnnotations(mapstr.M{ "do.not.include": "true", "co.elastic.logs/disable": "true", }), @@ -71,16 +71,16 @@ func TestGenerateHints(t *testing.T) { }, }, result: bus.Event{ - "container": common.MapStr{ + "container": mapstr.M{ "id": "abc", "name": "foobar", - "labels": getNestedAnnotations(common.MapStr{ + "labels": getNestedAnnotations(mapstr.M{ "do.not.include": "true", "co.elastic.logs/disable": "true", }), }, - "hints": common.MapStr{ - "logs": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ "disable": "true", }, }, @@ -98,8 +98,8 @@ func TestGenerateHints(t *testing.T) { } } -func getNestedAnnotations(in common.MapStr) common.MapStr { - out := common.MapStr{} +func getNestedAnnotations(in mapstr.M) mapstr.M { + out := mapstr.M{} for k, v := range in { out.Put(k, v) @@ -126,41 +126,41 @@ func TestGenerateMetaDockerNoDedot(t *testing.T) { } _, meta := p.generateMetaDocker(event) expectedMeta := &dockerMetadata{ - Docker: common.MapStr{ - "container": common.MapStr{ + Docker: mapstr.M{ + "container": mapstr.M{ "id": "abc", "name": "foobar", "image": "", - "labels": common.MapStr{ - "do": common.MapStr{"not": common.MapStr{"include": "true"}}, - "co": common.MapStr{"elastic": common.MapStr{"logs/disable": "true"}}, + "labels": mapstr.M{ + "do": mapstr.M{"not": mapstr.M{"include": "true"}}, + "co": mapstr.M{"elastic": mapstr.M{"logs/disable": "true"}}, }, }, }, - Container: common.MapStr{ + Container: mapstr.M{ "id": "abc", "name": "foobar", - "image": common.MapStr{ + "image": mapstr.M{ "name": "", }, - "labels": common.MapStr{ - "do": common.MapStr{"not": common.MapStr{"include": "true"}}, - "co": common.MapStr{"elastic": common.MapStr{"logs/disable": "true"}}, + "labels": mapstr.M{ + "do": mapstr.M{"not": mapstr.M{"include": "true"}}, + "co": mapstr.M{"elastic": mapstr.M{"logs/disable": "true"}}, }, }, - Metadata: common.MapStr{ - "container": common.MapStr{ + Metadata: mapstr.M{ + "container": mapstr.M{ "id": "abc", "name": "foobar", - "image": common.MapStr{ + "image": mapstr.M{ "name": "", }, }, - "docker": common.MapStr{ - "container": common.MapStr{ - "labels": common.MapStr{ - "do": common.MapStr{"not": common.MapStr{"include": "true"}}, - "co": common.MapStr{"elastic": common.MapStr{"logs/disable": "true"}}, + "docker": mapstr.M{ + "container": mapstr.M{ + "labels": mapstr.M{ + "do": mapstr.M{"not": mapstr.M{"include": "true"}}, + "co": mapstr.M{"elastic": mapstr.M{"logs/disable": "true"}}, }, }, }, @@ -190,39 +190,39 @@ func TestGenerateMetaDockerWithDedot(t *testing.T) { } _, meta := p.generateMetaDocker(event) expectedMeta := &dockerMetadata{ - Docker: common.MapStr{ - "container": common.MapStr{ + Docker: mapstr.M{ + "container": mapstr.M{ "id": "abc", "name": "foobar", "image": "", - "labels": common.MapStr{ - "do": common.MapStr{"not": common.MapStr{"include": "true"}}, - "co": common.MapStr{"elastic": common.MapStr{"logs/disable": "true"}}, + "labels": mapstr.M{ + "do": mapstr.M{"not": mapstr.M{"include": "true"}}, + "co": mapstr.M{"elastic": mapstr.M{"logs/disable": "true"}}, }, }, }, - Container: common.MapStr{ + Container: mapstr.M{ "id": "abc", "name": "foobar", - "image": common.MapStr{ + "image": mapstr.M{ "name": "", }, - "labels": common.MapStr{ - "do": common.MapStr{"not": common.MapStr{"include": "true"}}, - "co": common.MapStr{"elastic": common.MapStr{"logs/disable": "true"}}, + "labels": mapstr.M{ + "do": mapstr.M{"not": mapstr.M{"include": "true"}}, + "co": mapstr.M{"elastic": mapstr.M{"logs/disable": "true"}}, }, }, - Metadata: common.MapStr{ - "container": common.MapStr{ + Metadata: mapstr.M{ + "container": mapstr.M{ "id": "abc", "name": "foobar", - "image": common.MapStr{ + "image": mapstr.M{ "name": "", }, }, - "docker": common.MapStr{ - "container": common.MapStr{ - "labels": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ + "labels": mapstr.M{ "do_not_include": "true", "co_elastic_logs/disable": "true", }, diff --git a/libbeat/autodiscover/providers/jolokia/discovery.go b/libbeat/autodiscover/providers/jolokia/discovery.go index 380369fa911..e1bf59f2194 100644 --- a/libbeat/autodiscover/providers/jolokia/discovery.go +++ b/libbeat/autodiscover/providers/jolokia/discovery.go @@ -27,11 +27,11 @@ import ( "github.com/gofrs/uuid" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Jolokia Discovery query @@ -76,7 +76,7 @@ type Event struct { ProviderUUID uuid.UUID Type string AgentID string - Message common.MapStr + Message mapstr.M } // BusEvent converts a Jolokia Discovery event to a autodiscover bus event @@ -86,7 +86,7 @@ func (e *Event) BusEvent() bus.Event { "provider": e.ProviderUUID, "id": e.AgentID, "jolokia": e.Message, - "meta": common.MapStr{ + "meta": mapstr.M{ "jolokia": e.Message, }, } @@ -99,7 +99,7 @@ type Instance struct { LastSeen time.Time LastInterface *InterfaceConfig AgentID string - Message common.MapStr + Message mapstr.M } // Discovery controls the Jolokia Discovery probes @@ -272,7 +272,7 @@ func (d *Discovery) sendProbe(config InterfaceConfig) { wg.Wait() } -func (d *Discovery) update(config InterfaceConfig, message common.MapStr) { +func (d *Discovery) update(config InterfaceConfig, message mapstr.M) { log := d.log v, err := message.GetValue("agent.id") diff --git a/libbeat/autodiscover/providers/kubernetes/config_test.go b/libbeat/autodiscover/providers/kubernetes/config_test.go index 5be6f334703..24bc679182d 100644 --- a/libbeat/autodiscover/providers/kubernetes/config_test.go +++ b/libbeat/autodiscover/providers/kubernetes/config_test.go @@ -22,6 +22,7 @@ import ( "github.com/stretchr/testify/assert" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" "github.com/elastic/beats/v7/libbeat/autodiscover" @@ -32,11 +33,11 @@ import ( func TestConfigWithCustomBuilders(t *testing.T) { autodiscover.Registry.AddBuilder("mock", newMockBuilder) - cfg := common.MapStr{ + cfg := mapstr.M{ "hints.enabled": false, - "builders": []common.MapStr{ + "builders": []mapstr.M{ { - "mock": common.MapStr{}, + "mock": mapstr.M{}, }, }, } @@ -46,7 +47,7 @@ func TestConfigWithCustomBuilders(t *testing.T) { err := config.Unpack(&c) assert.NoError(t, err) - cfg1 := common.MapStr{ + cfg1 := mapstr.M{ "hints.enabled": false, } config, err = common.NewConfigFrom(&cfg1) @@ -56,7 +57,7 @@ func TestConfigWithCustomBuilders(t *testing.T) { } func TestConfigWithIncorrectScope(t *testing.T) { - cfg := common.MapStr{ + cfg := mapstr.M{ "scope": "node", "resource": "service", "hints.enabled": true, diff --git a/libbeat/autodiscover/providers/kubernetes/kubernetes.go b/libbeat/autodiscover/providers/kubernetes/kubernetes.go index 67f14870bb1..0fe9b5f1a48 100644 --- a/libbeat/autodiscover/providers/kubernetes/kubernetes.go +++ b/libbeat/autodiscover/providers/kubernetes/kubernetes.go @@ -41,6 +41,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/kubernetes/k8skeystore" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -192,7 +193,7 @@ func (p *Provider) publish(events []bus.Event) { } // Since all the events belong to the same event ID pick on and add in all the configs - event := bus.Event(common.MapStr(events[0]).Clone()) + event := bus.Event(mapstr.M(events[0]).Clone()) // Remove the port to avoid ambiguity during debugging delete(event, "port") event["config"] = configs diff --git a/libbeat/autodiscover/providers/kubernetes/node.go b/libbeat/autodiscover/providers/kubernetes/node.go index 788ea213807..5f27ca5aae7 100644 --- a/libbeat/autodiscover/providers/kubernetes/node.go +++ b/libbeat/autodiscover/providers/kubernetes/node.go @@ -34,8 +34,9 @@ import ( "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) type node struct { @@ -134,15 +135,15 @@ func (n *node) GenerateHints(event bus.Event) bus.Event { // Try to build a config with enabled builders. Send a provider agnostic payload. // Builders are Beat specific. e := bus.Event{} - var annotations common.MapStr - var kubeMeta common.MapStr + var annotations mapstr.M + var kubeMeta mapstr.M rawMeta, ok := event["kubernetes"] if ok { - kubeMeta = rawMeta.(common.MapStr) + kubeMeta = rawMeta.(mapstr.M) // The builder base config can configure any of the field values of kubernetes if need be. e["kubernetes"] = kubeMeta if rawAnn, ok := kubeMeta["annotations"]; ok { - annotations = rawAnn.(common.MapStr) + annotations = rawAnn.(mapstr.M) } } if host, ok := event["host"]; ok { @@ -190,10 +191,10 @@ func (n *node) emit(node *kubernetes.Node, flag string) { meta := n.metagen.Generate(node) kubemetaMap, _ := meta.GetValue("kubernetes") - kubemeta, _ := kubemetaMap.(common.MapStr) + kubemeta, _ := kubemetaMap.(mapstr.M) kubemeta = kubemeta.Clone() // Pass annotations to all events so that it can be used in templating and by annotation builders. - annotations := common.MapStr{} + annotations := mapstr.M{} for k, v := range node.GetObjectMeta().GetAnnotations() { safemapstr.Put(annotations, k, v) } diff --git a/libbeat/autodiscover/providers/kubernetes/node_test.go b/libbeat/autodiscover/providers/kubernetes/node_test.go index 7da94be7d9c..347f7cc29d3 100644 --- a/libbeat/autodiscover/providers/kubernetes/node_test.go +++ b/libbeat/autodiscover/providers/kubernetes/node_test.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints_Node(t *testing.T) { @@ -49,15 +50,15 @@ func TestGenerateHints_Node(t *testing.T) { // Only kubernetes payload must return only kubernetes as part of the hint { event: bus.Event{ - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "foobar", }, }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "foobar", }, }, @@ -68,30 +69,30 @@ func TestGenerateHints_Node(t *testing.T) { // not.to.include must not be part of hints { event: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "not.to.include": "true", }), - "node": common.MapStr{ + "node": mapstr.M{ "name": "foobar", }, }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "not.to.include": "true", "co.elastic.metrics/period": "10s", }), - "node": common.MapStr{ + "node": mapstr.M{ "name": "foobar", }, }, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "10s", }, @@ -171,17 +172,17 @@ func TestEmitEvent_Node(t *testing.T) { "host": "192.168.0.1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "hostname": "node1", }, - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "hostname": "node1", @@ -222,17 +223,17 @@ func TestEmitEvent_Node(t *testing.T) { "host": "node1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "hostname": "node1", }, - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "hostname": "node1", @@ -283,17 +284,17 @@ func TestEmitEvent_Node(t *testing.T) { "host": "node1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "hostname": "node1", }, - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ - "node": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "hostname": "node1", diff --git a/libbeat/autodiscover/providers/kubernetes/pod.go b/libbeat/autodiscover/providers/kubernetes/pod.go index 8248114c875..999420c5a20 100644 --- a/libbeat/autodiscover/providers/kubernetes/pod.go +++ b/libbeat/autodiscover/providers/kubernetes/pod.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type pod struct { @@ -173,16 +174,16 @@ func (p *pod) GenerateHints(event bus.Event) bus.Event { // Try to build a config with enabled builders. Send a provider agnostic payload. // Builders are Beat specific. e := bus.Event{} - var kubeMeta, container common.MapStr + var kubeMeta, container mapstr.M - annotations := make(common.MapStr, 0) + annotations := make(mapstr.M, 0) rawMeta, ok := event["kubernetes"] if ok { - kubeMeta = rawMeta.(common.MapStr) + kubeMeta = rawMeta.(mapstr.M) // The builder base config can configure any of the field values of kubernetes if need be. e["kubernetes"] = kubeMeta if rawAnn, ok := kubeMeta["annotations"]; ok { - anns, _ := rawAnn.(common.MapStr) + anns, _ := rawAnn.(mapstr.M) if len(anns) != 0 { annotations = anns.Clone() } @@ -190,7 +191,7 @@ func (p *pod) GenerateHints(event bus.Event) bus.Event { // Look at all the namespace level default annotations and do a merge with priority going to the pod annotations. if rawNsAnn, ok := kubeMeta["namespace_annotations"]; ok { - namespaceAnnotations, _ := rawNsAnn.(common.MapStr) + namespaceAnnotations, _ := rawNsAnn.(mapstr.M) if len(namespaceAnnotations) != 0 { annotations.DeepUpdateNoOverwrite(namespaceAnnotations) } @@ -207,7 +208,7 @@ func (p *pod) GenerateHints(event bus.Event) bus.Event { } if rawCont, ok := kubeMeta["container"]; ok { - container = rawCont.(common.MapStr) + container = rawCont.(mapstr.M) // This would end up adding a runtime entry into the event. This would make sure // that there is not an attempt to spin up a docker input for a rkt container and when a // rkt input exists it would be natively supported. @@ -278,7 +279,7 @@ func (p *pod) emit(pod *kubernetes.Pod, flag string) { namespaceAnnotations := kubernetes.PodNamespaceAnnotations(pod, p.namespaceWatcher) eventList := make([][]bus.Event, 0) - portsMap := common.MapStr{} + portsMap := mapstr.M{} containers := kubernetes.GetContainersInPod(pod) anyContainerRunning := false for _, c := range containers { @@ -312,7 +313,7 @@ func (p *pod) emit(pod *kubernetes.Pod, flag string) { // running. // If the container ID is unknown, only "stop" events are generated. // It also returns a map with the named ports. -func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *kubernetes.ContainerInPod, annotations, namespaceAnnotations common.MapStr) ([]bus.Event, common.MapStr) { +func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *kubernetes.ContainerInPod, annotations, namespaceAnnotations mapstr.M) ([]bus.Event, mapstr.M) { if c.ID == "" && flag != "stop" { return nil, nil } @@ -323,20 +324,20 @@ func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *kubernetes meta := p.metagen.Generate(pod, metadata.WithFields("container.name", c.Spec.Name)) - cmeta := common.MapStr{ + cmeta := mapstr.M{ "id": c.ID, "runtime": c.Runtime, - "image": common.MapStr{ + "image": mapstr.M{ "name": c.Spec.Image, }, } // Information that can be used in discovering a workload kubemetaMap, _ := meta.GetValue("kubernetes") - kubemeta, _ := kubemetaMap.(common.MapStr) + kubemeta, _ := kubemetaMap.(mapstr.M) kubemeta = kubemeta.Clone() kubemeta["annotations"] = annotations - kubemeta["container"] = common.MapStr{ + kubemeta["container"] = mapstr.M{ "id": c.ID, "name": c.Spec.Name, "image": c.Spec.Image, @@ -355,7 +356,7 @@ func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *kubernetes } var events []bus.Event - portsMap := common.MapStr{} + portsMap := mapstr.M{} meta.Put("container", cmeta) @@ -386,12 +387,12 @@ func (p *pod) containerPodEvents(flag string, pod *kubernetes.Pod, c *kubernetes // podEvent creates an event for a pod. // It only includes network information if `includeNetwork` is true. -func (p *pod) podEvent(flag string, pod *kubernetes.Pod, ports common.MapStr, includeNetwork bool, annotations, namespaceAnnotations common.MapStr) bus.Event { +func (p *pod) podEvent(flag string, pod *kubernetes.Pod, ports mapstr.M, includeNetwork bool, annotations, namespaceAnnotations mapstr.M) bus.Event { meta := p.metagen.Generate(pod) // Information that can be used in discovering a workload kubemetaMap, _ := meta.GetValue("kubernetes") - kubemeta, _ := kubemetaMap.(common.MapStr) + kubemeta, _ := kubemetaMap.(mapstr.M) kubemeta = kubemeta.Clone() kubemeta["annotations"] = annotations if len(namespaceAnnotations) != 0 { diff --git a/libbeat/autodiscover/providers/kubernetes/pod_test.go b/libbeat/autodiscover/providers/kubernetes/pod_test.go index adf98a7cdad..a2ca2f22ee9 100644 --- a/libbeat/autodiscover/providers/kubernetes/pod_test.go +++ b/libbeat/autodiscover/providers/kubernetes/pod_test.go @@ -35,6 +35,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints(t *testing.T) { @@ -50,15 +51,15 @@ func TestGenerateHints(t *testing.T) { // Only kubernetes payload must return only kubernetes as part of the hint { event: bus.Event{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "foobar", }, }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "foobar", }, }, @@ -67,8 +68,8 @@ func TestGenerateHints(t *testing.T) { // Kubernetes payload with container info must be bubbled to top level { event: bus.Event{ - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "rkt", @@ -76,14 +77,14 @@ func TestGenerateHints(t *testing.T) { }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "rkt", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "rkt", @@ -91,15 +92,15 @@ func TestGenerateHints(t *testing.T) { }, }, // Scenarios being tested: - // logs/multiline.pattern must be a nested common.MapStr under hints.logs - // logs/json.keys_under_root must be a nested common.MapStr under hints.logs + // logs/multiline.pattern must be a nested mapstr.M under hints.logs + // logs/json.keys_under_root must be a nested mapstr.M under hints.logs // metrics/module must be found in hints.metrics // not.to.include must not be part of hints // period is annotated at both container and pod level. Container level value must be in hints { event: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.logs/json.keys_under_root": "true", "co.elastic.metrics/module": "prometheus", @@ -107,7 +108,7 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics.foobar/period": "15s", "not.to.include": "true", }), - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -115,8 +116,8 @@ func TestGenerateHints(t *testing.T) { }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.logs/json.keys_under_root": "true", "co.elastic.metrics/module": "prometheus", @@ -124,27 +125,27 @@ func TestGenerateHints(t *testing.T) { "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", }), - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", }, }, - "hints": common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, - "json": common.MapStr{ + "json": mapstr.M{ "keys_under_root": "true", }, }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -156,18 +157,18 @@ func TestGenerateHints(t *testing.T) { // The resultant hints should have a combination of both { event: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.logs/json.keys_under_root": "true", "not.to.include": "true", }), - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", }), - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -176,39 +177,39 @@ func TestGenerateHints(t *testing.T) { }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.logs/multiline.pattern": "^test", "co.elastic.logs/json.keys_under_root": "true", "not.to.include": "true", }), - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", "co.elastic.metrics/module": "prometheus", }), - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", }, "namespace": "ns", }, - "hints": common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, - "json": common.MapStr{ + "json": mapstr.M{ "keys_under_root": "true", }, }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -220,20 +221,20 @@ func TestGenerateHints(t *testing.T) { // The resultant hints should honor only pods and not namespace. { event: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", "not.to.include": "true", }), - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "dropwizard", "co.elastic.metrics/period": "60s", "co.elastic.metrics.foobar/period": "25s", }), "namespace": "ns", - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -241,32 +242,32 @@ func TestGenerateHints(t *testing.T) { }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", "not.to.include": "true", }), - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "dropwizard", "co.elastic.metrics/period": "60s", "co.elastic.metrics.foobar/period": "25s", }), - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", }, "namespace": "ns", }, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -278,13 +279,13 @@ func TestGenerateHints(t *testing.T) { // The resultant hints should honor only namespace defaults. { event: bus.Event{ - "kubernetes": common.MapStr{ - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", }), - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -293,26 +294,26 @@ func TestGenerateHints(t *testing.T) { }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "co.elastic.metrics.foobar/period": "15s", }), - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", }, "namespace": "ns", }, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "15s", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "name": "foobar", "id": "abc", "runtime": "docker", @@ -398,26 +399,26 @@ func TestPod_EmitEvent(t *testing.T) { "host": "127.0.0.1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -430,39 +431,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(0), "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -522,30 +523,30 @@ func TestPod_EmitEvent(t *testing.T) { "host": "127.0.0.1", "id": uid, "provider": UUID, - "ports": common.MapStr{ + "ports": mapstr.M{ "port1": int32(8080), "port2": int32(9090), }, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -558,39 +559,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(8080), "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "runtime": "docker", "id": "foobar", }, @@ -603,39 +604,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(9090), "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -695,26 +696,26 @@ func TestPod_EmitEvent(t *testing.T) { "start": true, "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -725,39 +726,39 @@ func TestPod_EmitEvent(t *testing.T) { "start": true, "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "runtime": "docker", "id": "foobar", }, @@ -768,39 +769,39 @@ func TestPod_EmitEvent(t *testing.T) { "start": true, "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -908,24 +909,24 @@ func TestPod_EmitEvent(t *testing.T) { "stop": true, "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -936,37 +937,37 @@ func TestPod_EmitEvent(t *testing.T) { "stop": true, "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "runtime": "", "id": "", }, @@ -1010,26 +1011,26 @@ func TestPod_EmitEvent(t *testing.T) { "stop": true, "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -1040,39 +1041,39 @@ func TestPod_EmitEvent(t *testing.T) { "stop": true, "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "", "runtime": "", }, @@ -1132,26 +1133,26 @@ func TestPod_EmitEvent(t *testing.T) { "stop": true, "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -1162,39 +1163,39 @@ func TestPod_EmitEvent(t *testing.T) { "stop": true, "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "runtime": "docker", "id": "foobar", }, @@ -1205,39 +1206,39 @@ func TestPod_EmitEvent(t *testing.T) { "stop": true, "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -1307,30 +1308,30 @@ func TestPod_EmitEvent(t *testing.T) { "start": true, "host": "127.0.0.1", "id": uid, - "ports": common.MapStr{ + "ports": mapstr.M{ "http": int32(8080), }, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": "127.0.0.1", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": "127.0.0.1", - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -1343,39 +1344,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(8080), "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -1386,39 +1387,39 @@ func TestPod_EmitEvent(t *testing.T) { "start": true, "id": cid + "-init", "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat-init", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat-init", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -1468,26 +1469,26 @@ func TestPod_EmitEvent(t *testing.T) { "host": "127.0.0.1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -1500,39 +1501,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(0), "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -1584,26 +1585,26 @@ func TestPod_EmitEvent(t *testing.T) { "host": "127.0.0.1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -1616,39 +1617,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(0), "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -1731,26 +1732,26 @@ func TestPod_EmitEvent(t *testing.T) { "host": "127.0.0.1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", }, }, @@ -1764,39 +1765,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(0), "id": cid, "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar", "name": "filebeat", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar", "runtime": "docker", }, @@ -1810,39 +1811,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(0), "id": cid + "-init", "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar-init", "name": "filebeat-init", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat-init", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar-init", "runtime": "docker", }, @@ -1856,39 +1857,39 @@ func TestPod_EmitEvent(t *testing.T) { "port": int32(0), "id": cid + "-ephemeral", "provider": UUID, - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "id": "foobar-ephemeral", "name": "filebeat-ephemeral", "image": "elastic/filebeat:6.3.0", "runtime": "docker", }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "node", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "pod": common.MapStr{ + "pod": mapstr.M{ "name": "filebeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": podIP, - }, "node": common.MapStr{ + }, "node": mapstr.M{ "name": "node", - }, "container": common.MapStr{ + }, "container": mapstr.M{ "name": "filebeat-ephemeral", }, }, - "container": common.MapStr{ - "image": common.MapStr{"name": "elastic/filebeat:6.3.0"}, + "container": mapstr.M{ + "image": mapstr.M{"name": "elastic/filebeat:6.3.0"}, "id": "foobar-ephemeral", "runtime": "docker", }, @@ -2089,8 +2090,8 @@ func (p *publisher) publish(events []bus.Event) { } } -func getNestedAnnotations(in common.MapStr) common.MapStr { - out := common.MapStr{} +func getNestedAnnotations(in mapstr.M) mapstr.M { + out := mapstr.M{} for k, v := range in { out.Put(k, v) diff --git a/libbeat/autodiscover/providers/kubernetes/service.go b/libbeat/autodiscover/providers/kubernetes/service.go index 9b3c6a0aa77..908dc935d5c 100644 --- a/libbeat/autodiscover/providers/kubernetes/service.go +++ b/libbeat/autodiscover/providers/kubernetes/service.go @@ -32,8 +32,9 @@ import ( "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) type service struct { @@ -124,16 +125,16 @@ func (s *service) GenerateHints(event bus.Event) bus.Event { // Try to build a config with enabled builders. Send a provider agnostic payload. // Builders are Beat specific. e := bus.Event{} - var kubeMeta common.MapStr + var kubeMeta mapstr.M - annotations := make(common.MapStr, 0) + annotations := make(mapstr.M, 0) rawMeta, ok := event["kubernetes"] if ok { - kubeMeta = rawMeta.(common.MapStr) + kubeMeta = rawMeta.(mapstr.M) // The builder base config can configure any of the field values of kubernetes if need be. e["kubernetes"] = kubeMeta if rawAnn, ok := kubeMeta["annotations"]; ok { - anns, _ := rawAnn.(common.MapStr) + anns, _ := rawAnn.(mapstr.M) if len(anns) != 0 { annotations = anns.Clone() } @@ -141,7 +142,7 @@ func (s *service) GenerateHints(event bus.Event) bus.Event { // Look at all the namespace level default annotations and do a merge with priority going to the pod annotations. if rawNsAnn, ok := kubeMeta["namespace_annotations"]; ok { - nsAnn, _ := rawNsAnn.(common.MapStr) + nsAnn, _ := rawNsAnn.(mapstr.M) if len(nsAnn) != 0 { annotations.DeepUpdateNoOverwrite(nsAnn) } @@ -197,10 +198,10 @@ func (s *service) emit(svc *kubernetes.Service, flag string) { meta := s.metagen.Generate(svc) kubemetaMap, _ := meta.GetValue("kubernetes") - kubemeta, _ := kubemetaMap.(common.MapStr) + kubemeta, _ := kubemetaMap.(mapstr.M) kubemeta = kubemeta.Clone() // Pass annotations to all events so that it can be used in templating and by annotation builders. - annotations := common.MapStr{} + annotations := mapstr.M{} for k, v := range svc.GetObjectMeta().GetAnnotations() { safemapstr.Put(annotations, k, v) } @@ -209,7 +210,7 @@ func (s *service) emit(svc *kubernetes.Service, flag string) { if s.namespaceWatcher != nil { if rawNs, ok, err := s.namespaceWatcher.Store().GetByKey(svc.Namespace); ok && err == nil { if namespace, ok := rawNs.(*kubernetes.Namespace); ok { - nsAnns := common.MapStr{} + nsAnns := mapstr.M{} for k, v := range namespace.GetAnnotations() { safemapstr.Put(nsAnns, k, v) diff --git a/libbeat/autodiscover/providers/kubernetes/service_test.go b/libbeat/autodiscover/providers/kubernetes/service_test.go index c9ee7764632..e0948067b45 100644 --- a/libbeat/autodiscover/providers/kubernetes/service_test.go +++ b/libbeat/autodiscover/providers/kubernetes/service_test.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints_Service(t *testing.T) { @@ -49,15 +50,15 @@ func TestGenerateHints_Service(t *testing.T) { // Only kubernetes payload must return only kubernetes as part of the hint { event: bus.Event{ - "kubernetes": common.MapStr{ - "service": common.MapStr{ + "kubernetes": mapstr.M{ + "service": mapstr.M{ "name": "foobar", }, }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "service": common.MapStr{ + "kubernetes": mapstr.M{ + "service": mapstr.M{ "name": "foobar", }, }, @@ -68,30 +69,30 @@ func TestGenerateHints_Service(t *testing.T) { // not.to.include must not be part of hints { event: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "not.to.include": "true", }), - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "not.to.include": "true", "co.elastic.metrics/period": "10s", }), - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, }, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "10s", }, @@ -103,36 +104,36 @@ func TestGenerateHints_Service(t *testing.T) { // The resultant should have both { event: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "not.to.include": "true", }), - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/period": "10s", }), - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, "namespace": "ns", }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "not.to.include": "true", }), - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/period": "10s", }), "namespace": "ns", }, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "10s", }, @@ -144,40 +145,40 @@ func TestGenerateHints_Service(t *testing.T) { // The resultant should have the ones from service alone { event: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "not.to.include": "true", }), - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "dropwizard", "co.elastic.metrics/period": "60s", }), "namespace": "ns", - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", "not.to.include": "true", }), - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "dropwizard", "co.elastic.metrics/period": "60s", }), "namespace": "ns", - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, }, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "10s", }, @@ -189,30 +190,30 @@ func TestGenerateHints_Service(t *testing.T) { // The resultant should have honored the namespace defaults { event: bus.Event{ - "kubernetes": common.MapStr{ - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", }), - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, "namespace": "ns", }, }, result: bus.Event{ - "kubernetes": common.MapStr{ - "namespace_annotations": getNestedAnnotations(common.MapStr{ + "kubernetes": mapstr.M{ + "namespace_annotations": getNestedAnnotations(mapstr.M{ "co.elastic.metrics/module": "prometheus", "co.elastic.metrics/period": "10s", }), - "service": common.MapStr{ + "service": mapstr.M{ "name": "foobar", }, "namespace": "ns", }, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "period": "10s", }, @@ -281,18 +282,18 @@ func TestEmitEvent_Service(t *testing.T) { "host": "192.168.0.1", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "service": common.MapStr{ + "kubernetes": mapstr.M{ + "service": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "service": common.MapStr{ + "service": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", }, @@ -368,18 +369,18 @@ func TestEmitEvent_Service(t *testing.T) { "host": "", "id": uid, "provider": UUID, - "kubernetes": common.MapStr{ - "service": common.MapStr{ + "kubernetes": mapstr.M{ + "service": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", }, "namespace": "default", - "annotations": common.MapStr{}, + "annotations": mapstr.M{}, }, - "meta": common.MapStr{ - "kubernetes": common.MapStr{ + "meta": mapstr.M{ + "kubernetes": mapstr.M{ "namespace": "default", - "service": common.MapStr{ + "service": mapstr.M{ "name": "metricbeat", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", }, diff --git a/libbeat/autodiscover/template/config.go b/libbeat/autodiscover/template/config.go index a1f87d2bcfc..5151200b7be 100644 --- a/libbeat/autodiscover/template/config.go +++ b/libbeat/autodiscover/template/config.go @@ -20,6 +20,7 @@ package template import ( "fmt" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" "github.com/elastic/go-ucfg/parse" @@ -75,11 +76,11 @@ func NewConfigMapper( } // Event adapts MapStr to processors.ValuesMap interface -type Event common.MapStr +type Event mapstr.M // GetValue extracts given key from an Event func (e Event) GetValue(key string) (interface{}, error) { - val, err := common.MapStr(e).GetValue(key) + val, err := mapstr.M(e).GetValue(key) if err != nil { return nil, err } diff --git a/libbeat/autodiscover/template/config_test.go b/libbeat/autodiscover/template/config_test.go index 7964ba24126..5773261dcce 100644 --- a/libbeat/autodiscover/template/config_test.go +++ b/libbeat/autodiscover/template/config_test.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConfigsMapping(t *testing.T) { @@ -110,7 +111,7 @@ func TestConfigsMapping(t *testing.T) { event: bus.Event{ "foo": 3, "host": "1.2.3.4", - "ports": common.MapStr{ + "ports": mapstr.M{ "web": 8080, }, }, diff --git a/libbeat/beat/event.go b/libbeat/beat/event.go index bc8ffe6c3e9..28549001c56 100644 --- a/libbeat/beat/event.go +++ b/libbeat/beat/event.go @@ -23,6 +23,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // FlagField fields used to keep information or errors when events are parsed. @@ -39,8 +40,8 @@ const ( // Output can optionally publish a subset of Meta, or ignore Meta. type Event struct { Timestamp time.Time - Meta common.MapStr - Fields common.MapStr + Meta mapstr.M + Fields mapstr.M Private interface{} // for beats private use TimeSeries bool // true if the event contains timeseries data } @@ -54,7 +55,7 @@ var ( // If Meta is nil, a new Meta dictionary is created. func (e *Event) SetID(id string) { if e.Meta == nil { - e.Meta = common.MapStr{} + e.Meta = mapstr.M{} } e.Meta["_id"] = id } @@ -90,7 +91,7 @@ func (e *Event) Clone() *Event { // via `DeepUpdate`. // `DeepUpdateNoOverwrite` is a version of this function that does not // overwrite existing values. -func (e *Event) DeepUpdate(d common.MapStr) { +func (e *Event) DeepUpdate(d mapstr.M) { e.deepUpdate(d, true) } @@ -101,17 +102,17 @@ func (e *Event) DeepUpdate(d common.MapStr) { // If the key is present and the value is a map as well, the sub-map will be updated recursively // via `DeepUpdateNoOverwrite`. // `DeepUpdate` is a version of this function that overwrites existing values. -func (e *Event) DeepUpdateNoOverwrite(d common.MapStr) { +func (e *Event) DeepUpdateNoOverwrite(d mapstr.M) { e.deepUpdate(d, false) } -func (e *Event) deepUpdate(d common.MapStr, overwrite bool) { +func (e *Event) deepUpdate(d mapstr.M, overwrite bool) { if len(d) == 0 { return } fieldsUpdate := d.Clone() // so we can delete redundant keys - var metaUpdate common.MapStr + var metaUpdate mapstr.M for fieldKey, value := range d { switch fieldKey { @@ -126,10 +127,10 @@ func (e *Event) deepUpdate(d common.MapStr, overwrite bool) { // some updates are addressed for the metadata not the fields case metadataFieldKey: switch meta := value.(type) { - case common.MapStr: + case mapstr.M: metaUpdate = meta case map[string]interface{}: - metaUpdate = common.MapStr(meta) + metaUpdate = mapstr.M(meta) } delete(fieldsUpdate, fieldKey) @@ -138,7 +139,7 @@ func (e *Event) deepUpdate(d common.MapStr, overwrite bool) { if metaUpdate != nil { if e.Meta == nil { - e.Meta = common.MapStr{} + e.Meta = mapstr.M{} } if overwrite { e.Meta.DeepUpdate(metaUpdate) @@ -152,7 +153,7 @@ func (e *Event) deepUpdate(d common.MapStr, overwrite bool) { } if e.Fields == nil { - e.Fields = common.MapStr{} + e.Fields = mapstr.M{} } if overwrite { @@ -182,7 +183,7 @@ func (e *Event) PutValue(key string, v interface{}) (interface{}, error) { } else if subKey, ok := metadataKey(key); ok { if subKey == "" { switch meta := v.(type) { - case common.MapStr: + case mapstr.M: e.Meta = meta case map[string]interface{}: e.Meta = meta @@ -190,7 +191,7 @@ func (e *Event) PutValue(key string, v interface{}) (interface{}, error) { return nil, errNoMapStr } } else if e.Meta == nil { - e.Meta = common.MapStr{} + e.Meta = mapstr.M{} } return e.Meta.Put(subKey, v) } @@ -228,7 +229,7 @@ func metadataKey(key string) (string, bool) { } // SetErrorWithOption sets jsonErr value in the event fields according to addErrKey value. -func (e *Event) SetErrorWithOption(jsonErr common.MapStr, addErrKey bool) { +func (e *Event) SetErrorWithOption(jsonErr mapstr.M, addErrKey bool) { if addErrKey { e.Fields["error"] = jsonErr } diff --git a/libbeat/beat/event_test.go b/libbeat/beat/event_test.go index 2f4f226ecf9..5575f495106 100644 --- a/libbeat/beat/event_test.go +++ b/libbeat/beat/event_test.go @@ -23,11 +23,11 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func newEmptyEvent() *Event { - return &Event{Fields: common.MapStr{}} + return &Event{Fields: mapstr.M{}} } func TestEventPutGetTimestamp(t *testing.T) { @@ -54,20 +54,20 @@ func TestDeepUpdate(t *testing.T) { cases := []struct { name string event *Event - update common.MapStr + update mapstr.M overwrite bool expected *Event }{ { name: "does nothing if no update", event: &Event{}, - update: common.MapStr{}, + update: mapstr.M{}, expected: &Event{}, }, { name: "updates timestamp", event: &Event{}, - update: common.MapStr{ + update: mapstr.M{ timestampFieldKey: ts, }, overwrite: true, @@ -80,7 +80,7 @@ func TestDeepUpdate(t *testing.T) { event: &Event{ Timestamp: ts, }, - update: common.MapStr{ + update: mapstr.M{ timestampFieldKey: time.Now().Add(time.Hour), }, overwrite: false, @@ -91,14 +91,14 @@ func TestDeepUpdate(t *testing.T) { { name: "initializes metadata if nil", event: &Event{}, - update: common.MapStr{ - metadataFieldKey: common.MapStr{ + update: mapstr.M{ + metadataFieldKey: mapstr.M{ "first": "new", "second": 42, }, }, expected: &Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "first": "new", "second": 42, }, @@ -107,19 +107,19 @@ func TestDeepUpdate(t *testing.T) { { name: "updates metadata but does not overwrite", event: &Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "first": "initial", }, }, - update: common.MapStr{ - metadataFieldKey: common.MapStr{ + update: mapstr.M{ + metadataFieldKey: mapstr.M{ "first": "new", "second": 42, }, }, overwrite: false, expected: &Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "first": "initial", "second": 42, }, @@ -128,19 +128,19 @@ func TestDeepUpdate(t *testing.T) { { name: "updates metadata and overwrites", event: &Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "first": "initial", }, }, - update: common.MapStr{ - metadataFieldKey: common.MapStr{ + update: mapstr.M{ + metadataFieldKey: mapstr.M{ "first": "new", "second": 42, }, }, overwrite: true, expected: &Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "first": "new", "second": 42, }, @@ -149,17 +149,17 @@ func TestDeepUpdate(t *testing.T) { { name: "updates fields but does not overwrite", event: &Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "first": "initial", }, }, - update: common.MapStr{ + update: mapstr.M{ "first": "new", "second": 42, }, overwrite: false, expected: &Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "first": "initial", "second": 42, }, @@ -168,17 +168,17 @@ func TestDeepUpdate(t *testing.T) { { name: "updates metadata and overwrites", event: &Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "first": "initial", }, }, - update: common.MapStr{ + update: mapstr.M{ "first": "new", "second": 42, }, overwrite: true, expected: &Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "first": "new", "second": 42, }, @@ -187,12 +187,12 @@ func TestDeepUpdate(t *testing.T) { { name: "initializes fields if nil", event: &Event{}, - update: common.MapStr{ + update: mapstr.M{ "first": "new", "second": 42, }, expected: &Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "first": "new", "second": 42, }, @@ -212,7 +212,7 @@ func TestDeepUpdate(t *testing.T) { func TestEventMetadata(t *testing.T) { const id = "123" - newMeta := func() common.MapStr { return common.MapStr{"_id": id} } + newMeta := func() mapstr.M { return mapstr.M{"_id": id} } t.Run("put", func(t *testing.T) { evt := newEmptyEvent() @@ -286,7 +286,7 @@ func TestEventMetadata(t *testing.T) { evt.PutValue("@metadataSpecial", id) - assert.Equal(t, common.MapStr{"@metadataSpecial": id}, evt.Fields) + assert.Equal(t, mapstr.M{"@metadataSpecial": id}, evt.Fields) }) t.Run("delete non-metadata", func(t *testing.T) { diff --git a/libbeat/beat/events/util_test.go b/libbeat/beat/events/util_test.go index d9c138130b7..a4fbaba9957 100644 --- a/libbeat/beat/events/util_test.go +++ b/libbeat/beat/events/util_test.go @@ -23,7 +23,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGetMetaStringValue(t *testing.T) { @@ -35,17 +35,17 @@ func TestGetMetaStringValue(t *testing.T) { }{ "nonexistent_field": { beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "foo": "bar", }, }, "nonexistent", "", - common.ErrKeyNotFound, + mapstr.ErrKeyNotFound, }, "root": { beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "foo": "bar", "baz": "hello", }, @@ -56,9 +56,9 @@ func TestGetMetaStringValue(t *testing.T) { }, "nested": { beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "foo": "bar", - "baz": common.MapStr{ + "baz": mapstr.M{ "qux": "hello", }, }, @@ -69,7 +69,7 @@ func TestGetMetaStringValue(t *testing.T) { }, "non_string": { beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "foo": "bar", "baz": 17, }, diff --git a/libbeat/beat/pipeline.go b/libbeat/beat/pipeline.go index f13b3c39ff2..24a276a5dac 100644 --- a/libbeat/beat/pipeline.go +++ b/libbeat/beat/pipeline.go @@ -20,7 +20,7 @@ package beat import ( "time" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Pipeline provides access to libbeat event publishing by creating a Client @@ -99,17 +99,17 @@ type CloseRef interface { // pass to the publisher pipeline on Connect. type ProcessingConfig struct { // EventMetadata configures additional fields/tags to be added to published events. - EventMetadata common.EventMetadata + EventMetadata mapstr.EventMetadata // Meta provides additional meta data to be added to the Meta field in the beat.Event // structure. - Meta common.MapStr + Meta mapstr.M // Fields provides additional 'global' fields to be added to every event - Fields common.MapStr + Fields mapstr.M // DynamicFields provides additional fields to be added to every event, supporting live updates - DynamicFields *common.MapStrPointer + DynamicFields *mapstr.Pointer // Processors passes additional processor to the client, to be executed before // the pipeline processors. diff --git a/libbeat/cfgfile/list_test.go b/libbeat/cfgfile/list_test.go index 01007b64cc1..1a3364705b8 100644 --- a/libbeat/cfgfile/list_test.go +++ b/libbeat/cfgfile/list_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/reload" pubtest "github.com/elastic/beats/v7/libbeat/publisher/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) type runner struct { @@ -232,17 +233,17 @@ func TestHas(t *testing.T) { } func TestCreateRunnerAddsDynamicMeta(t *testing.T) { - newMapStrPointer := func(m common.MapStr) *common.MapStrPointer { - p := common.NewMapStrPointer(m) + newMapStrPointer := func(m mapstr.M) *mapstr.Pointer { + p := mapstr.NewPointer(m) return &p } cases := map[string]struct { - meta *common.MapStrPointer + meta *mapstr.Pointer }{ "no dynamic metadata": {}, "with dynamic fields": { - meta: newMapStrPointer(common.MapStr{"test": 1}), + meta: newMapStrPointer(mapstr.M{"test": 1}), }, } diff --git a/libbeat/cfgfile/reload_test.go b/libbeat/cfgfile/reload_test.go index d98f5ddc1b9..c4ed45f6636 100644 --- a/libbeat/cfgfile/reload_test.go +++ b/libbeat/cfgfile/reload_test.go @@ -30,6 +30,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestReloader(t *testing.T) { @@ -41,9 +42,9 @@ func TestReloader(t *testing.T) { } glob := dir + "/*.yml" - config := common.MustNewConfigFrom(common.MapStr{ + config := common.MustNewConfigFrom(mapstr.M{ "path": glob, - "reload": common.MapStr{ + "reload": mapstr.M{ "period": "1s", "enabled": true, }, diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index 4925f047826..df72789f867 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -72,6 +72,7 @@ import ( "github.com/elastic/beats/v7/libbeat/publisher/processing" svc "github.com/elastic/beats/v7/libbeat/service" "github.com/elastic/beats/v7/libbeat/version" + "github.com/elastic/elastic-agent-libs/mapstr" sysinfo "github.com/elastic/go-sysinfo" "github.com/elastic/go-sysinfo/types" ucfg "github.com/elastic/go-ucfg" @@ -1050,10 +1051,10 @@ func logSystemInfo(info beat.Info) { log := logp.NewLogger("beat").With(logp.Namespace("system_info")) // Beat - beat := common.MapStr{ + beat := mapstr.M{ "type": info.Beat, "uuid": info.ID, - "path": common.MapStr{ + "path": mapstr.M{ "config": paths.Resolve(paths.Config, ""), "data": paths.Resolve(paths.Data, ""), "home": paths.Resolve(paths.Home, ""), @@ -1063,7 +1064,7 @@ func logSystemInfo(info beat.Info) { log.Infow("Beat info", "beat", beat) // Build - build := common.MapStr{ + build := mapstr.M{ "commit": version.Commit(), "time": version.BuildTime(), "version": info.Version, @@ -1081,7 +1082,7 @@ func logSystemInfo(info beat.Info) { // Process if self, err := sysinfo.Self(); err == nil { - process := common.MapStr{} + process := mapstr.M{} if info, err := self.Info(); err == nil { process["name"] = info.Name diff --git a/libbeat/cmd/instance/beat_integration_test.go b/libbeat/cmd/instance/beat_integration_test.go index 7e7db83daa4..5ac677acb87 100644 --- a/libbeat/cmd/instance/beat_integration_test.go +++ b/libbeat/cmd/instance/beat_integration_test.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/cmd/instance" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mock" + "github.com/elastic/elastic-agent-libs/mapstr" ) type mockbeat struct { @@ -51,7 +52,7 @@ func (mb mockbeat) Run(b *beat.Beat) error { case <-ticker.C: client.Publish(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "mock", "message": "Mockbeat is alive!", }, diff --git a/libbeat/common/bus/bus.go b/libbeat/common/bus/bus.go index 7b3cf7f9d36..12e19cb76aa 100644 --- a/libbeat/common/bus/bus.go +++ b/libbeat/common/bus/bus.go @@ -20,12 +20,12 @@ package bus import ( "sync" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Event sent to the bus -type Event common.MapStr +type Event mapstr.M // Bus provides a common channel to emit and listen for Events type Bus interface { diff --git a/libbeat/common/datetime_test.go b/libbeat/common/datetime_test.go index 7fb071b2e5c..4150e8cea8d 100644 --- a/libbeat/common/datetime_test.go +++ b/libbeat/common/datetime_test.go @@ -26,6 +26,8 @@ import ( "time" "github.com/stretchr/testify/assert" + + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestParseTime(t *testing.T) { @@ -78,19 +80,19 @@ func TestParseTimeNegative(t *testing.T) { func TestTimeMarshal(t *testing.T) { type inputOutput struct { - Input MapStr + Input mapstr.M Output string } tests := []inputOutput{ { - Input: MapStr{ + Input: mapstr.M{ "@timestamp": Time(time.Date(2015, time.March, 01, 11, 19, 05, 112*1e6, time.UTC)), }, Output: `{"@timestamp":"2015-03-01T11:19:05.112Z"}`, }, { - Input: MapStr{ + Input: mapstr.M{ "@timestamp": MustParseTime("2015-03-01T11:19:05.112Z"), "another": MustParseTime("2015-03-01T14:19:05.112Z"), }, diff --git a/libbeat/common/docker/helpers.go b/libbeat/common/docker/helpers.go index e53c8499710..74cfd2f923d 100644 --- a/libbeat/common/docker/helpers.go +++ b/libbeat/common/docker/helpers.go @@ -21,7 +21,8 @@ import ( "strings" "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) // ExtractContainerName strips the `/` characters that frequently appear in container names @@ -38,11 +39,11 @@ func ExtractContainerName(names []string) string { return strings.Trim(output, "/") } -// DeDotLabels returns a new common.MapStr containing a copy of the labels +// DeDotLabels returns a new mapstr.M containing a copy of the labels // where the dots have been converted into nested structure, avoiding // possible mapping errors -func DeDotLabels(labels map[string]string, dedot bool) common.MapStr { - outputLabels := common.MapStr{} +func DeDotLabels(labels map[string]string, dedot bool) mapstr.M { + outputLabels := mapstr.M{} for k, v := range labels { if dedot { // This is necessary so that ES does not interpret '.' fields as new diff --git a/libbeat/common/event.go b/libbeat/common/event.go index d96b7620a95..d4e1b8cc97c 100644 --- a/libbeat/common/event.go +++ b/libbeat/common/event.go @@ -29,14 +29,15 @@ import ( "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) -// EventConverter is used to convert MapStr objects for publishing +// EventConverter is used to convert mapstr.M objects for publishing type EventConverter interface { - Convert(m MapStr) MapStr + Convert(m mapstr.M) mapstr.M } -// GenericEventConverter is used to normalize MapStr objects for publishing +// GenericEventConverter is used to normalize mapstr.M objects for publishing type GenericEventConverter struct { log *logp.Logger keepNull bool @@ -50,11 +51,11 @@ func NewGenericEventConverter(keepNull bool) *GenericEventConverter { } } -// Convert normalizes the types contained in the given MapStr. +// Convert normalizes the types contained in the given mapstr.M. // // Nil values in maps are dropped during the conversion. Any unsupported types -// that are found in the MapStr are dropped and warnings are logged. -func (e *GenericEventConverter) Convert(m MapStr) MapStr { +// that are found in the mapstr.M are dropped and warnings are logged. +func (e *GenericEventConverter) Convert(m mapstr.M) mapstr.M { keys := make([]string, 0, 10) event, errs := e.normalizeMap(m, keys...) if len(errs) > 0 { @@ -67,10 +68,10 @@ func (e *GenericEventConverter) Convert(m MapStr) MapStr { // normalizeMap normalizes each element contained in the given map. If an error // occurs during normalization, processing of m will continue, and all errors // are returned at the end. -func (e *GenericEventConverter) normalizeMap(m MapStr, keys ...string) (MapStr, []error) { +func (e *GenericEventConverter) normalizeMap(m mapstr.M, keys ...string) (mapstr.M, []error) { var errs []error - out := make(MapStr, len(m)) + out := make(mapstr.M, len(m)) for key, value := range m { v, err := e.normalizeValue(value, append(keys, key)...) if len(err) > 0 { @@ -91,11 +92,11 @@ func (e *GenericEventConverter) normalizeMap(m MapStr, keys ...string) (MapStr, return out, errs } -// normalizeMapStrSlice normalizes each individual MapStr. -func (e *GenericEventConverter) normalizeMapStrSlice(maps []MapStr, keys ...string) ([]MapStr, []error) { +// normalizeMapStrSlice normalizes each individual mapstr.M. +func (e *GenericEventConverter) normalizeMapStrSlice(maps []mapstr.M, keys ...string) ([]mapstr.M, []error) { var errs []error - out := make([]MapStr, 0, len(maps)) + out := make([]mapstr.M, 0, len(maps)) for i, m := range maps { normalizedMap, err := e.normalizeMap(m, append(keys, strconv.Itoa(i))...) if len(err) > 0 { @@ -107,12 +108,12 @@ func (e *GenericEventConverter) normalizeMapStrSlice(maps []MapStr, keys ...stri return out, errs } -// normalizeMapStringSlice normalizes each individual map[string]interface{} and -// returns a []MapStr. -func (e *GenericEventConverter) normalizeMapStringSlice(maps []map[string]interface{}, keys ...string) ([]MapStr, []error) { +// normalizemMapStringSlice normalizes each individual map[string]interface{} and +// returns a []mapstr.M. +func (e *GenericEventConverter) normalizeMapStringSlice(maps []map[string]interface{}, keys ...string) ([]mapstr.M, []error) { var errs []error - out := make([]MapStr, 0, len(maps)) + out := make([]mapstr.M, 0, len(maps)) for i, m := range maps { normalizedMap, err := e.normalizeMap(m, append(keys, strconv.Itoa(i))...) if len(err) > 0 { @@ -209,10 +210,10 @@ func (e *GenericEventConverter) normalizeValue(value interface{}, keys ...string case complex64, complex128: case []complex64, []complex128: case Time, []Time: - case MapStr: - return e.normalizeMap(value.(MapStr), keys...) - case []MapStr: - return e.normalizeMapStrSlice(value.([]MapStr), keys...) + case mapstr.M: + return e.normalizeMap(value.(mapstr.M), keys...) + case []mapstr.M: + return e.normalizeMapStrSlice(value.([]mapstr.M), keys...) case map[string]interface{}: return e.normalizeMap(value.(map[string]interface{}), keys...) case []map[string]interface{}: @@ -239,10 +240,10 @@ func (e *GenericEventConverter) normalizeValue(value interface{}, keys ...string case reflect.Array, reflect.Slice: return e.normalizeSlice(v, keys...) case reflect.Map, reflect.Struct: - var m MapStr + var m mapstr.M err := marshalUnmarshal(value, &m) if err != nil { - return m, []error{errors.Wrapf(err, "key=%v: error converting %T to MapStr", joinKeys(keys...), value)} + return m, []error{errors.Wrapf(err, "key=%v: error converting %T to mapstr.M", joinKeys(keys...), value)} } return m, nil default: @@ -255,8 +256,8 @@ func (e *GenericEventConverter) normalizeValue(value interface{}, keys ...string return value, nil } -// marshalUnmarshal converts an interface to a MapStr by marshalling to JSON -// then unmarshalling the JSON object into a MapStr. +// marshalUnmarshal converts an interface to a mapstr.M by marshalling to JSON +// then unmarshalling the JSON object into a mapstr.M. func marshalUnmarshal(in interface{}, out interface{}) error { // Decode and encode as JSON to normalized the types. marshaled, err := json.Marshal(in) @@ -313,8 +314,8 @@ func DeDotJSON(json interface{}) interface{} { result[DeDot(key)] = DeDotJSON(value) } return result - case MapStr: - result := MapStr{} + case mapstr.M: + result := mapstr.M{} for key, value := range json { result[DeDot(key)] = DeDotJSON(value) } diff --git a/libbeat/common/event_test.go b/libbeat/common/event_test.go index ec116187b64..5ace5ff80f8 100644 --- a/libbeat/common/event_test.go +++ b/libbeat/common/event_test.go @@ -26,110 +26,111 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConvertNestedMapStr(t *testing.T) { logp.TestingSetup() type io struct { - Input MapStr - Output MapStr + Input mapstr.M + Output mapstr.M } type String string tests := []io{ { - Input: MapStr{ - "key": MapStr{ + Input: mapstr.M{ + "key": mapstr.M{ "key1": "value1", }, }, - Output: MapStr{ - "key": MapStr{ + Output: mapstr.M{ + "key": mapstr.M{ "key1": "value1", }, }, }, { - Input: MapStr{ - "key": MapStr{ + Input: mapstr.M{ + "key": mapstr.M{ "key1": String("value1"), }, }, - Output: MapStr{ - "key": MapStr{ + Output: mapstr.M{ + "key": mapstr.M{ "key1": "value1", }, }, }, { - Input: MapStr{ - "key": MapStr{ + Input: mapstr.M{ + "key": mapstr.M{ "key1": []string{"value1", "value2"}, }, }, - Output: MapStr{ - "key": MapStr{ + Output: mapstr.M{ + "key": mapstr.M{ "key1": []string{"value1", "value2"}, }, }, }, { - Input: MapStr{ - "key": MapStr{ + Input: mapstr.M{ + "key": mapstr.M{ "key1": []String{"value1", "value2"}, }, }, - Output: MapStr{ - "key": MapStr{ + Output: mapstr.M{ + "key": mapstr.M{ "key1": []interface{}{"value1", "value2"}, }, }, }, { - Input: MapStr{ + Input: mapstr.M{ "@timestamp": MustParseTime("2015-03-01T12:34:56.123Z"), }, - Output: MapStr{ + Output: mapstr.M{ "@timestamp": MustParseTime("2015-03-01T12:34:56.123Z"), }, }, { - Input: MapStr{ + Input: mapstr.M{ "env": nil, "key2": uintptr(88), "key3": func() { t.Log("hello") }, }, - Output: MapStr{}, + Output: mapstr.M{}, }, { - Input: MapStr{ - "key": []MapStr{ + Input: mapstr.M{ + "key": []mapstr.M{ {"keyX": []String{"value1", "value2"}}, }, }, - Output: MapStr{ - "key": []MapStr{ + Output: mapstr.M{ + "key": []mapstr.M{ {"keyX": []interface{}{"value1", "value2"}}, }, }, }, { - Input: MapStr{ + Input: mapstr.M{ "key": []interface{}{ - MapStr{"key1": []string{"value1", "value2"}}, + mapstr.M{"key1": []string{"value1", "value2"}}, }, }, - Output: MapStr{ + Output: mapstr.M{ "key": []interface{}{ - MapStr{"key1": []string{"value1", "value2"}}, + mapstr.M{"key1": []string{"value1", "value2"}}, }, }, }, { - MapStr{"k": map[string]int{"hits": 1}}, - MapStr{"k": MapStr{"hits": float64(1)}}, + mapstr.M{"k": map[string]int{"hits": 1}}, + mapstr.M{"k": mapstr.M{"hits": float64(1)}}, }, } @@ -143,8 +144,8 @@ func TestConvertNestedStruct(t *testing.T) { logp.TestingSetup() type io struct { - Input MapStr - Output MapStr + Input mapstr.M + Output mapstr.M } type TestStruct struct { @@ -154,17 +155,17 @@ func TestConvertNestedStruct(t *testing.T) { tests := []io{ { - Input: MapStr{ - "key": MapStr{ + Input: mapstr.M{ + "key": mapstr.M{ "key1": TestStruct{ A: "hello", B: 5, }, }, }, - Output: MapStr{ - "key": MapStr{ - "key1": MapStr{ + Output: mapstr.M{ + "key": mapstr.M{ + "key1": mapstr.M{ "A": "hello", "B": float64(5), }, @@ -172,7 +173,7 @@ func TestConvertNestedStruct(t *testing.T) { }, }, { - Input: MapStr{ + Input: mapstr.M{ "key": []interface{}{ TestStruct{ A: "hello", @@ -180,9 +181,9 @@ func TestConvertNestedStruct(t *testing.T) { }, }, }, - Output: MapStr{ + Output: mapstr.M{ "key": []interface{}{ - MapStr{ + mapstr.M{ "A": "hello", "B": float64(5), }, @@ -201,8 +202,8 @@ func TestConvertWithNullEmission(t *testing.T) { logp.TestingSetup() type io struct { - Input MapStr - Output MapStr + Input mapstr.M + Output mapstr.M } type String string @@ -212,25 +213,25 @@ func TestConvertWithNullEmission(t *testing.T) { tests := []io{ { - Input: MapStr{ - "key": MapStr{ + Input: mapstr.M{ + "key": mapstr.M{ "key1": nil, }, }, - Output: MapStr{ - "key": MapStr{ + Output: mapstr.M{ + "key": mapstr.M{ "key1": nil, }, }, }, { - Input: MapStr{ + Input: mapstr.M{ "key": TestStruct{ A: nil, }, }, - Output: MapStr{ - "key": MapStr{ + Output: mapstr.M{ + "key": mapstr.M{ "A": nil, }, }, @@ -295,21 +296,21 @@ func TestNormalizeValue(t *testing.T) { "uint8 value": {uint8(8), uint8(8)}, "uint64 masked": {uint64(1<<63 + 10), uint64(10)}, "string value": {"hello", "hello"}, - "map to MapStr": {map[string]interface{}{"foo": "bar"}, MapStr{"foo": "bar"}}, + "map to mapstr.M": {map[string]interface{}{"foo": "bar"}, mapstr.M{"foo": "bar"}}, // Other map types are converted using marshalUnmarshal which will lose // type information for arrays which become []interface{} and numbers // which all become float64. - "map[string]string to MapStr": {map[string]string{"foo": "bar"}, MapStr{"foo": "bar"}}, - "map[string][]string to MapStr": {map[string][]string{"list": {"foo", "bar"}}, MapStr{"list": []interface{}{"foo", "bar"}}}, + "map[string]string to mapstr.M": {map[string]string{"foo": "bar"}, mapstr.M{"foo": "bar"}}, + "map[string][]string to mapstr.M": {map[string][]string{"list": {"foo", "bar"}}, mapstr.M{"list": []interface{}{"foo", "bar"}}}, - "array of strings": {[]string{"foo", "bar"}, []string{"foo", "bar"}}, - "array of bools": {[]bool{true, false}, []bool{true, false}}, - "array of ints": {[]int{10, 11}, []int{10, 11}}, - "array of uint64 ok": {[]uint64{1, 2, 3}, []uint64{1, 2, 3}}, - "array of uint64 masked": {[]uint64{1<<63 + 1, 1<<63 + 2, 1<<63 + 3}, []uint64{1, 2, 3}}, - "array of MapStr": {[]MapStr{{"foo": "bar"}}, []MapStr{{"foo": "bar"}}}, - "array of map to MapStr": {[]map[string]interface{}{{"foo": "bar"}}, []MapStr{{"foo": "bar"}}}, + "array of strings": {[]string{"foo", "bar"}, []string{"foo", "bar"}}, + "array of bools": {[]bool{true, false}, []bool{true, false}}, + "array of ints": {[]int{10, 11}, []int{10, 11}}, + "array of uint64 ok": {[]uint64{1, 2, 3}, []uint64{1, 2, 3}}, + "array of uint64 masked": {[]uint64{1<<63 + 1, 1<<63 + 2, 1<<63 + 3}, []uint64{1, 2, 3}}, + "array of mapstr.M": {[]mapstr.M{{"foo": "bar"}}, []mapstr.M{{"foo": "bar"}}}, + "array of map to mapstr.M": {[]map[string]interface{}{{"foo": "bar"}}, []mapstr.M{{"foo": "bar"}}}, // Wrapper types are converted to primitives using reflection. "custom bool type": {mybool(true), true}, @@ -333,7 +334,7 @@ func TestNormalizeValue(t *testing.T) { } func TestNormalizeMapError(t *testing.T) { - badInputs := []MapStr{ + badInputs := []mapstr.M{ {"func": func() {}}, {"chan": make(chan struct{})}, {"uintptr": uintptr(123)}, @@ -358,14 +359,14 @@ func TestJoinKeys(t *testing.T) { func TestMarshalUnmarshalMap(t *testing.T) { tests := []struct { - in MapStr - out MapStr + in mapstr.M + out mapstr.M }{ - {MapStr{"names": []string{"a", "b"}}, MapStr{"names": []interface{}{"a", "b"}}}, + {mapstr.M{"names": []string{"a", "b"}}, mapstr.M{"names": []interface{}{"a", "b"}}}, } for i, test := range tests { - var out MapStr + var out mapstr.M err := marshalUnmarshal(test.in, &out) if err != nil { t.Error(err) @@ -422,7 +423,7 @@ func TestNormalizeTime(t *testing.T) { func BenchmarkConvertToGenericEventNetString(b *testing.B) { g := NewGenericEventConverter(false) for i := 0; i < b.N; i++ { - g.Convert(MapStr{"key": NetString("hola")}) + g.Convert(mapstr.M{"key": NetString("hola")}) } } @@ -430,15 +431,15 @@ func BenchmarkConvertToGenericEventNetString(b *testing.B) { func BenchmarkConvertToGenericEventMapStringString(b *testing.B) { g := NewGenericEventConverter(false) for i := 0; i < b.N; i++ { - g.Convert(MapStr{"key": map[string]string{"greeting": "hola"}}) + g.Convert(mapstr.M{"key": map[string]string{"greeting": "hola"}}) } } -// Uses recursion to step into the nested MapStr. +// Uses recursion to step into the nested mapstr.M. func BenchmarkConvertToGenericEventMapStr(b *testing.B) { g := NewGenericEventConverter(false) for i := 0; i < b.N; i++ { - g.Convert(MapStr{"key": map[string]interface{}{"greeting": "hola"}}) + g.Convert(mapstr.M{"key": map[string]interface{}{"greeting": "hola"}}) } } @@ -446,7 +447,7 @@ func BenchmarkConvertToGenericEventMapStr(b *testing.B) { func BenchmarkConvertToGenericEventStringSlice(b *testing.B) { g := NewGenericEventConverter(false) for i := 0; i < b.N; i++ { - g.Convert(MapStr{"key": []string{"foo", "bar"}}) + g.Convert(mapstr.M{"key": []string{"foo", "bar"}}) } } @@ -455,7 +456,7 @@ func BenchmarkConvertToGenericEventCustomStringSlice(b *testing.B) { g := NewGenericEventConverter(false) type myString string for i := 0; i < b.N; i++ { - g.Convert(MapStr{"key": []myString{"foo", "bar"}}) + g.Convert(mapstr.M{"key": []myString{"foo", "bar"}}) } } @@ -464,7 +465,7 @@ func BenchmarkConvertToGenericEventStringPointer(b *testing.B) { g := NewGenericEventConverter(false) val := "foo" for i := 0; i < b.N; i++ { - g.Convert(MapStr{"key": &val}) + g.Convert(mapstr.M{"key": &val}) } } func TestDeDotJSON(t *testing.T) { @@ -520,7 +521,7 @@ func TestDeDotJSON(t *testing.T) { assert.Nil(t, json.Unmarshal(test.output, &output)) assert.Equal(t, output, DeDotJSON(input)) if _, ok := test.valuer().(map[string]interface{}); ok { - assert.Equal(t, MapStr(output.(map[string]interface{})), DeDotJSON(MapStr(input.(map[string]interface{})))) + assert.Equal(t, mapstr.M(output.(map[string]interface{})), DeDotJSON(mapstr.M(input.(map[string]interface{})))) } } } diff --git a/libbeat/common/fmtstr/formatevents_test.go b/libbeat/common/fmtstr/formatevents_test.go index a644676db75..b59872989d8 100644 --- a/libbeat/common/fmtstr/formatevents_test.go +++ b/libbeat/common/fmtstr/formatevents_test.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEventFormatString(t *testing.T) { @@ -52,56 +53,56 @@ func TestEventFormatString(t *testing.T) { { "expand event field", "%{[key]}", - beat.Event{Fields: common.MapStr{"key": "value"}}, + beat.Event{Fields: mapstr.M{"key": "value"}}, "value", []string{"key"}, }, { "expand with default", "%{[key]:default}", - beat.Event{Fields: common.MapStr{}}, + beat.Event{Fields: mapstr.M{}}, "default", nil, }, { "expand nested event field", "%{[nested.key]}", - beat.Event{Fields: common.MapStr{"nested": common.MapStr{"key": "value"}}}, + beat.Event{Fields: mapstr.M{"nested": mapstr.M{"key": "value"}}}, "value", []string{"nested.key"}, }, { "expand nested event field (alt. syntax)", "%{[nested][key]}", - beat.Event{Fields: common.MapStr{"nested": common.MapStr{"key": "value"}}}, + beat.Event{Fields: mapstr.M{"nested": mapstr.M{"key": "value"}}}, "value", []string{"nested.key"}, }, { "multiple event fields", "%{[key1]} - %{[key2]}", - beat.Event{Fields: common.MapStr{"key1": "v1", "key2": "v2"}}, + beat.Event{Fields: mapstr.M{"key1": "v1", "key2": "v2"}}, "v1 - v2", []string{"key1", "key2"}, }, { "same fields", "%{[key]} - %{[key]}", - beat.Event{Fields: common.MapStr{"key": "value"}}, + beat.Event{Fields: mapstr.M{"key": "value"}}, "value - value", []string{"key"}, }, { "same fields with default (first)", "%{[key]:default} - %{[key]}", - beat.Event{Fields: common.MapStr{"key": "value"}}, + beat.Event{Fields: mapstr.M{"key": "value"}}, "value - value", []string{"key"}, }, { "same fields with default (second)", "%{[key]} - %{[key]:default}", - beat.Event{Fields: common.MapStr{"key": "value"}}, + beat.Event{Fields: mapstr.M{"key": "value"}}, "value - value", []string{"key"}, }, @@ -110,7 +111,7 @@ func TestEventFormatString(t *testing.T) { "%{[key]}: %{+YYYY.MM.dd}", beat.Event{ Timestamp: time.Date(2015, 5, 1, 20, 12, 34, 0, time.UTC), - Fields: common.MapStr{ + Fields: mapstr.M{ "key": "timestamp", }, }, @@ -122,7 +123,7 @@ func TestEventFormatString(t *testing.T) { "%{[@timestamp]}: %{+YYYY.MM.dd}", beat.Event{ Timestamp: time.Date(2015, 5, 1, 20, 12, 34, 0, time.UTC), - Fields: common.MapStr{ + Fields: mapstr.M{ "key": "timestamp", }, }, @@ -189,7 +190,7 @@ func TestEventFormatStringErrors(t *testing.T) { "missing required field", "%{[key]}", true, - beat.Event{Fields: common.MapStr{}}, + beat.Event{Fields: mapstr.M{}}, }, } @@ -219,22 +220,22 @@ func TestEventFormatStringFromConfig(t *testing.T) { }{ { "plain string", - beat.Event{Fields: common.MapStr{}}, + beat.Event{Fields: mapstr.M{}}, "plain string", }, { 100, - beat.Event{Fields: common.MapStr{}}, + beat.Event{Fields: mapstr.M{}}, "100", }, { true, - beat.Event{Fields: common.MapStr{}}, + beat.Event{Fields: mapstr.M{}}, "true", }, { "%{[key]}", - beat.Event{Fields: common.MapStr{"key": "value"}}, + beat.Event{Fields: mapstr.M{"key": "value"}}, "value", }, } @@ -242,7 +243,7 @@ func TestEventFormatStringFromConfig(t *testing.T) { for i, test := range tests { t.Logf("run (%v): %v -> %v", i, test.v, test.expected) - config, err := common.NewConfigFrom(common.MapStr{ + config, err := common.NewConfigFrom(mapstr.M{ "test": test.v, }) if err != nil { diff --git a/libbeat/common/fmtstr/formattimestamp.go b/libbeat/common/fmtstr/formattimestamp.go index e58ce8b2cd9..652fae018d7 100644 --- a/libbeat/common/fmtstr/formattimestamp.go +++ b/libbeat/common/fmtstr/formattimestamp.go @@ -21,7 +21,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TimestampFormatString is a wrapper around EventFormatString for the @@ -29,14 +29,14 @@ import ( // shared static fields (typically agent / version) and the event timestamp. type TimestampFormatString struct { eventFormatString *EventFormatString - fields common.MapStr + fields mapstr.M } // NewTimestampFormatString creates from the given event format string a // TimestampFormatString that includes only the given static fields and // a timestamp. func NewTimestampFormatString( - eventFormatString *EventFormatString, staticFields common.MapStr, + eventFormatString *EventFormatString, staticFields mapstr.M, ) (*TimestampFormatString, error) { return &TimestampFormatString{ eventFormatString: eventFormatString, @@ -44,21 +44,21 @@ func NewTimestampFormatString( }, nil } -// FieldsForBeat returns a common.MapStr with the given beat name and +// FieldsForBeat returns a mapstr.M with the given beat name and // version assigned to their standard field names. -func FieldsForBeat(beat string, version string) common.MapStr { - return common.MapStr{ +func FieldsForBeat(beat string, version string) mapstr.M { + return mapstr.M{ // beat object was left in for backward compatibility reason for older configs. - "beat": common.MapStr{ + "beat": mapstr.M{ "name": beat, "version": version, }, - "agent": common.MapStr{ + "agent": mapstr.M{ "name": beat, "version": version, }, // For the Beats that have an observer role - "observer": common.MapStr{ + "observer": mapstr.M{ "name": beat, "version": version, }, diff --git a/libbeat/common/fmtstr/formattimestamp_test.go b/libbeat/common/fmtstr/formattimestamp_test.go index 1a2c91decdd..714dd30840b 100644 --- a/libbeat/common/fmtstr/formattimestamp_test.go +++ b/libbeat/common/fmtstr/formattimestamp_test.go @@ -23,14 +23,14 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestTimestampFormatString(t *testing.T) { tests := []struct { title string format string - staticFields common.MapStr + staticFields mapstr.M timestamp time.Time expected string }{ @@ -51,7 +51,7 @@ func TestTimestampFormatString(t *testing.T) { { "expand field", "%{[key]}", - common.MapStr{"key": "value"}, + mapstr.M{"key": "value"}, time.Time{}, "value", }, @@ -65,21 +65,21 @@ func TestTimestampFormatString(t *testing.T) { { "expand nested field", "%{[nested.key]}", - common.MapStr{"nested": common.MapStr{"key": "value"}}, + mapstr.M{"nested": mapstr.M{"key": "value"}}, time.Time{}, "value", }, { "test timestamp formatter", "%{[key]}: %{+YYYY.MM.dd}", - common.MapStr{"key": "timestamp"}, + mapstr.M{"key": "timestamp"}, time.Date(2015, 5, 1, 20, 12, 34, 0, time.UTC), "timestamp: 2015.05.01", }, { "test timestamp formatter", "%{[@timestamp]}: %{+YYYY.MM.dd}", - common.MapStr{"key": "timestamp"}, + mapstr.M{"key": "timestamp"}, time.Date(2015, 5, 1, 20, 12, 34, 0, time.UTC), "2015-05-01T20:12:34.000Z: 2015.05.01", }, diff --git a/libbeat/common/jsonblob_test.go b/libbeat/common/jsonblob_test.go index c4cdc826920..62abe47c2c4 100644 --- a/libbeat/common/jsonblob_test.go +++ b/libbeat/common/jsonblob_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConfigJSONBlob(t *testing.T) { @@ -45,9 +47,9 @@ func TestConfigJSONBlob(t *testing.T) { expectedOut: []byte(`{"key":"value"}`), }, { - name: "successfully unpacks MapStr", + name: "successfully unpacks mapstr.M", config: map[string]interface{}{ - "jsonBlob": MapStr{"key": "value"}, + "jsonBlob": mapstr.M{"key": "value"}, }, expectedOut: []byte(`{"key":"value"}`), }, diff --git a/libbeat/common/jsontransform/expand.go b/libbeat/common/jsontransform/expand.go index 534429d96dc..5f5bc0cafcf 100644 --- a/libbeat/common/jsontransform/expand.go +++ b/libbeat/common/jsontransform/expand.go @@ -23,7 +23,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // expandFields de-dots the keys in m by expanding them in-place into a @@ -33,7 +33,7 @@ import ( // // Note that expandFields is destructive, and in the case of an error the // map may be left in a semi-expanded state. -func expandFields(m common.MapStr) error { +func expandFields(m mapstr.M) error { for k, v := range m { newMap, newIsMap := getMap(v) if newIsMap { @@ -81,7 +81,7 @@ func expandFields(m common.MapStr) error { // objects with the same key in each object. If there exist // two entries with the same key in each object which // are not both objects, then an error will result. -func mergeObjects(lhs, rhs common.MapStr) error { +func mergeObjects(lhs, rhs mapstr.M) error { for k, rhsValue := range rhs { lhsValue, ok := lhs[k] if !ok { @@ -107,7 +107,7 @@ func getMap(v interface{}) (map[string]interface{}, bool) { switch v := v.(type) { case map[string]interface{}: return v, true - case common.MapStr: + case mapstr.M: return v, true } return nil, false diff --git a/libbeat/common/jsontransform/expand_test.go b/libbeat/common/jsontransform/expand_test.go index 3cdcb94f37d..b27d5aca94c 100644 --- a/libbeat/common/jsontransform/expand_test.go +++ b/libbeat/common/jsontransform/expand_test.go @@ -23,73 +23,73 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestExpand(t *testing.T) { type data struct { - Event common.MapStr - Expected common.MapStr + Event mapstr.M + Expected mapstr.M Err string } tests := []data{ { - Event: common.MapStr{ + Event: mapstr.M{ "hello.world": 15, }, - Expected: common.MapStr{ - "hello": common.MapStr{ + Expected: mapstr.M{ + "hello": mapstr.M{ "world": 15, }, }, }, { - Event: common.MapStr{ + Event: mapstr.M{ "test": 15, }, - Expected: common.MapStr{ + Expected: mapstr.M{ "test": 15, }, }, { - Event: common.MapStr{ + Event: mapstr.M{ "test": 15, "hello.there": 1, "hello.world.ok": "test", "elastic.for": "search", }, - Expected: common.MapStr{ + Expected: mapstr.M{ "test": 15, - "hello": common.MapStr{ + "hello": mapstr.M{ "there": 1, - "world": common.MapStr{ + "world": mapstr.M{ "ok": "test", }, }, - "elastic": common.MapStr{ + "elastic": mapstr.M{ "for": "search", }, }, }, { - Event: common.MapStr{ - "root": common.MapStr{ + Event: mapstr.M{ + "root": mapstr.M{ "ok": 1, }, "root.shared": "yes", "root.one.two.three": 4, }, - Expected: common.MapStr{ - "root": common.MapStr{ + Expected: mapstr.M{ + "root": mapstr.M{ "ok": 1, "shared": "yes", - "one": common.MapStr{"two": common.MapStr{"three": 4}}, + "one": mapstr.M{"two": mapstr.M{"three": 4}}, }, }, }, { - Event: common.MapStr{ - "root": common.MapStr{ + Event: mapstr.M{ + "root": mapstr.M{ "seven": 1, }, "root.seven.eight": 2, @@ -97,22 +97,22 @@ func TestExpand(t *testing.T) { Err: `cannot expand .*`, }, { - Event: common.MapStr{ + Event: mapstr.M{ "a.b": 1, - "a": common.MapStr{ + "a": mapstr.M{ "b": 2, }, }, Err: `cannot expand .*`, }, { - Event: common.MapStr{ - "a.b": common.MapStr{ - "c": common.MapStr{ + Event: mapstr.M{ + "a.b": mapstr.M{ + "c": mapstr.M{ "d": 1, }, }, - "a.b.c": common.MapStr{ + "a.b.c": mapstr.M{ "d": 2, }, }, diff --git a/libbeat/common/jsontransform/jsonhelper.go b/libbeat/common/jsontransform/jsonhelper.go index 11d42eefcbb..073fc065209 100644 --- a/libbeat/common/jsontransform/jsonhelper.go +++ b/libbeat/common/jsontransform/jsonhelper.go @@ -23,8 +23,8 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -80,7 +80,7 @@ func WriteJSONKeys(event *beat.Event, keys map[string]interface{}, expandKeys, o switch m := v.(type) { case map[string]string: if event.Meta == nil && len(m) > 0 { - event.Meta = common.MapStr{} + event.Meta = mapstr.M{} } for meta, value := range m { event.Meta[meta] = value @@ -88,9 +88,9 @@ func WriteJSONKeys(event *beat.Event, keys map[string]interface{}, expandKeys, o case map[string]interface{}: if event.Meta == nil { - event.Meta = common.MapStr{} + event.Meta = mapstr.M{} } - event.Meta.DeepUpdate(common.MapStr(m)) + event.Meta.DeepUpdate(mapstr.M(m)) default: event.SetErrorWithOption(createJSONError("failed to update @metadata"), addErrKey) @@ -118,8 +118,8 @@ func WriteJSONKeys(event *beat.Event, keys map[string]interface{}, expandKeys, o event.Fields.DeepUpdate(keys) } -func createJSONError(message string) common.MapStr { - return common.MapStr{"message": message, "type": "json"} +func createJSONError(message string) mapstr.M { + return mapstr.M{"message": message, "type": "json"} } func removeKeys(keys map[string]interface{}, names ...string) { diff --git a/libbeat/common/jsontransform/jsonhelper_test.go b/libbeat/common/jsontransform/jsonhelper_test.go index faa00123beb..bed1de4c0fd 100644 --- a/libbeat/common/jsontransform/jsonhelper_test.go +++ b/libbeat/common/jsontransform/jsonhelper_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestWriteJSONKeys(t *testing.T) { @@ -32,15 +32,15 @@ func TestWriteJSONKeys(t *testing.T) { now = now.Round(time.Second) eventTimestamp := time.Date(2020, 01, 01, 01, 01, 00, 0, time.UTC) - eventMetadata := common.MapStr{ + eventMetadata := mapstr.M{ "foo": "bar", - "baz": common.MapStr{ + "baz": mapstr.M{ "qux": 17, }, } - eventFields := common.MapStr{ + eventFields := mapstr.M{ "top_a": 23, - "top_b": common.MapStr{ + "top_b": mapstr.M{ "inner_c": "see", "inner_d": "dee", }, @@ -50,9 +50,9 @@ func TestWriteJSONKeys(t *testing.T) { keys map[string]interface{} expandKeys bool overwriteKeys bool - expectedMetadata common.MapStr + expectedMetadata mapstr.M expectedTimestamp time.Time - expectedFields common.MapStr + expectedFields mapstr.M }{ "overwrite_true": { overwriteKeys: true, @@ -71,17 +71,17 @@ func TestWriteJSONKeys(t *testing.T) { }, "top_c": "COMPLETELY_NEW_c", }, - expectedMetadata: common.MapStr{ + expectedMetadata: mapstr.M{ "foo": "NEW_bar", - "baz": common.MapStr{ + "baz": mapstr.M{ "qux": "NEW_qux", "durrr": "COMPLETELY_NEW", }, }, expectedTimestamp: now, - expectedFields: common.MapStr{ + expectedFields: mapstr.M{ "top_a": 23, - "top_b": common.MapStr{ + "top_b": mapstr.M{ "inner_c": "see", "inner_d": "NEW_dee", "inner_e": "COMPLETELY_NEW_e", @@ -106,17 +106,17 @@ func TestWriteJSONKeys(t *testing.T) { }, "top_c": "COMPLETELY_NEW_c", }, - expectedMetadata: common.MapStr{ + expectedMetadata: mapstr.M{ "foo": "NEW_bar", - "baz": common.MapStr{ + "baz": mapstr.M{ "qux": "NEW_qux", "durrr": "COMPLETELY_NEW", }, }, expectedTimestamp: now, - expectedFields: common.MapStr{ + expectedFields: mapstr.M{ "top_a": 23, - "top_b": common.MapStr{ + "top_b": mapstr.M{ "inner_c": "see", "inner_d": "NEW_dee", "inner_e": "COMPLETELY_NEW_e", @@ -143,9 +143,9 @@ func TestWriteJSONKeys(t *testing.T) { }, expectedMetadata: eventMetadata.Clone(), expectedTimestamp: eventTimestamp, - expectedFields: common.MapStr{ + expectedFields: mapstr.M{ "top_a": 23, - "top_b": common.MapStr{ + "top_b": mapstr.M{ "inner_c": "see", "inner_d": "dee", "inner_e": "COMPLETELY_NEW_e", @@ -163,11 +163,11 @@ func TestWriteJSONKeys(t *testing.T) { }, expectedMetadata: eventMetadata.Clone(), expectedTimestamp: eventTimestamp, - expectedFields: common.MapStr{ + expectedFields: mapstr.M{ "top_a": 23, - "top_b": common.MapStr{ + "top_b": mapstr.M{ "inner_c": "see", - "inner_d": common.MapStr{ + "inner_d": mapstr.M{ "inner_e": "COMPLETELY_NEW_e", }, }, @@ -183,9 +183,9 @@ func TestWriteJSONKeys(t *testing.T) { }, expectedMetadata: eventMetadata.Clone(), expectedTimestamp: eventTimestamp, - expectedFields: common.MapStr{ + expectedFields: mapstr.M{ "top_a": 23, - "top_b": common.MapStr{ + "top_b": mapstr.M{ "inner_c": "see", "inner_d": "dee", "inner_d.inner_e": "COMPLETELY_NEW_e", diff --git a/libbeat/common/jsontransform/transform.go b/libbeat/common/jsontransform/transform.go index b6e19a06fe3..913c2f9fe36 100644 --- a/libbeat/common/jsontransform/transform.go +++ b/libbeat/common/jsontransform/transform.go @@ -20,13 +20,13 @@ package jsontransform import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TransformNumbers walks a json decoded tree an replaces json.Number // with int64, float64, or string, in this order of preference (i.e. if it // parses as an int, use int. if it parses as a float, use float. etc). -func TransformNumbers(dict common.MapStr) { +func TransformNumbers(dict mapstr.M) { for k, v := range dict { switch vv := v.(type) { case json.Number: diff --git a/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore.go b/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore.go index 0279e0a9a4d..7f09a54ce0a 100644 --- a/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore.go +++ b/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // KubernetesKeystoresRegistry implements a Provider for Keystore. @@ -61,7 +62,7 @@ func NewKubernetesKeystoresRegistry(logger *logp.Logger, client k8s.Interface) k func (kr *KubernetesKeystoresRegistry) GetKeystore(event bus.Event) keystore.Keystore { namespace := "" if val, ok := event["kubernetes"]; ok { - kubernetesMeta := val.(common.MapStr) + kubernetesMeta := val.(mapstr.M) ns, err := kubernetesMeta.GetValue("namespace") if err != nil { kr.logger.Debugf("Cannot retrieve kubernetes namespace from event: %s", event) diff --git a/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore_test.go b/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore_test.go index 359479b9eb8..45aca4f00ed 100644 --- a/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore_test.go +++ b/libbeat/common/kubernetes/k8skeystore/kubernetes_keystore_test.go @@ -27,17 +27,17 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" k8sfake "k8s.io/client-go/kubernetes/fake" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGetKeystore(t *testing.T) { kRegistry := NewKubernetesKeystoresRegistry(nil, nil) - k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": "my_namespace"}}) - k2 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": "my_namespace"}}) + k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": "my_namespace"}}) + k2 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": "my_namespace"}}) assert.Equal(t, k1, k2) - k3 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": "my_namespace_2"}}) + k3 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": "my_namespace_2"}}) assert.NotEqual(t, k2, k3) } @@ -64,7 +64,7 @@ func TestGetKeystoreAndRetrieve(t *testing.T) { } kRegistry := NewKubernetesKeystoresRegistry(nil, client) - k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": ns}}) + k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": ns}}) key := "kubernetes.test_namespace.testing_secret.secret_value" secure, err := k1.Retrieve(key) if err != nil { @@ -100,7 +100,7 @@ func TestGetKeystoreAndRetrieveWithNonAllowedNamespace(t *testing.T) { } kRegistry := NewKubernetesKeystoresRegistry(logger, client) - k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": ns}}) + k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": ns}}) key := "kubernetes.test_namespace_HACK.testing_secret.secret_value" _, err = k1.Retrieve(key) assert.Error(t, err) @@ -130,7 +130,7 @@ func TestGetKeystoreAndRetrieveWithWrongKeyFormat(t *testing.T) { } kRegistry := NewKubernetesKeystoresRegistry(logger, client) - k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": ns}}) + k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": ns}}) key := "HACK_test_namespace_HACK.testing_secret.secret_value" _, err = k1.Retrieve(key) assert.Error(t, err) @@ -142,7 +142,7 @@ func TestGetKeystoreAndRetrieveWithNoSecretsExistent(t *testing.T) { ns := "test_namespace" kRegistry := NewKubernetesKeystoresRegistry(logger, client) - k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": ns}}) + k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": ns}}) key := "kubernetes.test_namespace.testing_secret.secret_value" _, err := k1.Retrieve(key) assert.Error(t, err) @@ -172,7 +172,7 @@ func TestGetKeystoreAndRetrieveWithWrongSecretName(t *testing.T) { } kRegistry := NewKubernetesKeystoresRegistry(logger, client) - k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": ns}}) + k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": ns}}) key := "kubernetes.test_namespace.testing_secret_WRONG.secret_value" _, err = k1.Retrieve(key) assert.Error(t, err) @@ -202,7 +202,7 @@ func TestGetKeystoreAndRetrieveWithWrongSecretValue(t *testing.T) { } kRegistry := NewKubernetesKeystoresRegistry(logger, client) - k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": common.MapStr{"namespace": ns}}) + k1 := kRegistry.GetKeystore(bus.Event{"kubernetes": mapstr.M{"namespace": ns}}) key := "kubernetes.test_namespace.testing_secret.secret_value_WRONG" _, err = k1.Retrieve(key) assert.Error(t, err) diff --git a/libbeat/common/kubernetes/metadata/metadata.go b/libbeat/common/kubernetes/metadata/metadata.go index 618a914ab69..b160170b1c2 100644 --- a/libbeat/common/kubernetes/metadata/metadata.go +++ b/libbeat/common/kubernetes/metadata/metadata.go @@ -31,7 +31,8 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) // MetaGen allows creation of metadata from either Kubernetes resources or their Resource names. @@ -43,17 +44,17 @@ type MetaGen interface { // "some.ecs.field": "asdf, // populated by GenerateECS() // } // This method is called in top level and returns the complete map of metadata. - Generate(kubernetes.Resource, ...FieldOptions) common.MapStr + Generate(kubernetes.Resource, ...FieldOptions) mapstr.M // GenerateFromName generates metadata for a given resource based on it's name - GenerateFromName(string, ...FieldOptions) common.MapStr + GenerateFromName(string, ...FieldOptions) mapstr.M // GenerateK8s generates kubernetes metadata for a given resource - GenerateK8s(kubernetes.Resource, ...FieldOptions) common.MapStr + GenerateK8s(kubernetes.Resource, ...FieldOptions) mapstr.M // GenerateECS generates ECS metadata for a given resource - GenerateECS(kubernetes.Resource) common.MapStr + GenerateECS(kubernetes.Resource) mapstr.M } // FieldOptions allows additional enrichment to be done on top of existing metadata -type FieldOptions func(common.MapStr) +type FieldOptions func(mapstr.M) type ClusterInfo struct { Url string @@ -67,7 +68,7 @@ type ClusterConfiguration struct { // WithFields FieldOption allows adding specific fields into the generated metadata func WithFields(key string, value interface{}) FieldOptions { - return func(meta common.MapStr) { + return func(meta mapstr.M) { safemapstr.Put(meta, key, value) } } @@ -75,7 +76,7 @@ func WithFields(key string, value interface{}) FieldOptions { // WithMetadata FieldOption allows adding labels and annotations under sub-resource(kind) // example if kind=namespace namespace.labels key will be added func WithMetadata(kind string) FieldOptions { - return func(meta common.MapStr) { + return func(meta mapstr.M) { if meta["labels"] != nil { safemapstr.Put(meta, strings.ToLower(kind)+".labels", meta["labels"]) } diff --git a/libbeat/common/kubernetes/metadata/namespace.go b/libbeat/common/kubernetes/metadata/namespace.go index 920a0ecf2a1..8239fe4e439 100644 --- a/libbeat/common/kubernetes/metadata/namespace.go +++ b/libbeat/common/kubernetes/metadata/namespace.go @@ -23,6 +23,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/elastic-agent-libs/mapstr" ) const resource = "namespace" @@ -48,9 +49,9 @@ func NewNamespaceMetadataGenerator(cfg *common.Config, namespaces cache.Store, c // } // All Kubernetes fields that need to be stored under kuberentes. prefix are populetad by // GenerateK8s method while fields that are part of ECS are generated by GenerateECS method -func (n *namespace) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (n *namespace) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { ecsFields := n.GenerateECS(obj) - meta := common.MapStr{ + meta := mapstr.M{ "kubernetes": n.GenerateK8s(obj, opts...), } meta.DeepUpdate(ecsFields) @@ -58,12 +59,12 @@ func (n *namespace) Generate(obj kubernetes.Resource, opts ...FieldOptions) comm } // GenerateECS generates namespace ECS metadata from a resource object -func (n *namespace) GenerateECS(obj kubernetes.Resource) common.MapStr { +func (n *namespace) GenerateECS(obj kubernetes.Resource) mapstr.M { return n.resource.GenerateECS(obj) } // GenerateK8s generates namespace metadata from a resource object -func (n *namespace) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (n *namespace) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { _, ok := obj.(*kubernetes.Namespace) if !ok { return nil @@ -77,7 +78,7 @@ func (n *namespace) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) c } // GenerateFromName generates pod metadata from a namespace name -func (n *namespace) GenerateFromName(name string, opts ...FieldOptions) common.MapStr { +func (n *namespace) GenerateFromName(name string, opts ...FieldOptions) mapstr.M { if n.store == nil { return nil } @@ -94,14 +95,14 @@ func (n *namespace) GenerateFromName(name string, opts ...FieldOptions) common.M return nil } -func flattenMetadata(in common.MapStr) common.MapStr { - out := common.MapStr{} +func flattenMetadata(in mapstr.M) mapstr.M { + out := mapstr.M{} rawFields, err := in.GetValue(resource) if err != nil { return nil } - fields, ok := rawFields.(common.MapStr) + fields, ok := rawFields.(mapstr.M) if !ok { return nil } @@ -119,7 +120,7 @@ func flattenMetadata(in common.MapStr) common.MapStr { if err != nil { continue } - values, ok := rawValues.(common.MapStr) + values, ok := rawValues.(mapstr.M) if ok { out[resource+"_"+key] = values } diff --git a/libbeat/common/kubernetes/metadata/namespace_test.go b/libbeat/common/kubernetes/metadata/namespace_test.go index 88cc7859cdd..9b095cca98c 100644 --- a/libbeat/common/kubernetes/metadata/namespace_test.go +++ b/libbeat/common/kubernetes/metadata/namespace_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNamespace_Generate(t *testing.T) { @@ -40,7 +41,7 @@ func TestNamespace_Generate(t *testing.T) { name := "obj" tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -63,13 +64,13 @@ func TestNamespace_Generate(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{"kubernetes": common.MapStr{ + output: mapstr.M{"kubernetes": mapstr.M{ "namespace": name, "namespace_uid": uid, - "namespace_labels": common.MapStr{ + "namespace_labels": mapstr.M{ "foo": "bar", }, - "namespace_annotations": common.MapStr{ + "namespace_annotations": mapstr.M{ "spam": "baz", }, }}, @@ -98,7 +99,7 @@ func TestNamespace_GenerateFromName(t *testing.T) { name := "obj" tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -119,13 +120,13 @@ func TestNamespace_GenerateFromName(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{ + output: mapstr.M{ "namespace": name, "namespace_uid": uid, - "namespace_labels": common.MapStr{ + "namespace_labels": mapstr.M{ "foo": "bar", }, - "namespace_annotations": common.MapStr{ + "namespace_annotations": mapstr.M{ "spam": "baz", }, }, diff --git a/libbeat/common/kubernetes/metadata/node.go b/libbeat/common/kubernetes/metadata/node.go index 54eb352ec7e..21f55ef7788 100644 --- a/libbeat/common/kubernetes/metadata/node.go +++ b/libbeat/common/kubernetes/metadata/node.go @@ -24,6 +24,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/elastic-agent-libs/mapstr" ) type node struct { @@ -47,9 +48,9 @@ func NewNodeMetadataGenerator(cfg *common.Config, nodes cache.Store, client k8s. // } // All Kubernetes fields that need to be stored under kuberentes. prefix are populetad by // GenerateK8s method while fields that are part of ECS are generated by GenerateECS method -func (n *node) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (n *node) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { ecsFields := n.GenerateECS(obj) - meta := common.MapStr{ + meta := mapstr.M{ "kubernetes": n.GenerateK8s(obj, opts...), } meta.DeepUpdate(ecsFields) @@ -57,12 +58,12 @@ func (n *node) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.Ma } // GenerateECS generates node ECS metadata from a resource object -func (n *node) GenerateECS(obj kubernetes.Resource) common.MapStr { +func (n *node) GenerateECS(obj kubernetes.Resource) mapstr.M { return n.resource.GenerateECS(obj) } // GenerateK8s generates node metadata from a resource object -func (n *node) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (n *node) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { node, ok := obj.(*kubernetes.Node) if !ok { return nil @@ -78,7 +79,7 @@ func (n *node) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common } // GenerateFromName generates pod metadata from a service name -func (n *node) GenerateFromName(name string, opts ...FieldOptions) common.MapStr { +func (n *node) GenerateFromName(name string, opts ...FieldOptions) mapstr.M { if n.store == nil { return nil } diff --git a/libbeat/common/kubernetes/metadata/node_test.go b/libbeat/common/kubernetes/metadata/node_test.go index b0e9423c9e6..bf60d353238 100644 --- a/libbeat/common/kubernetes/metadata/node_test.go +++ b/libbeat/common/kubernetes/metadata/node_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNode_Generate(t *testing.T) { @@ -40,7 +41,7 @@ func TestNode_Generate(t *testing.T) { name := "obj" tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -65,16 +66,16 @@ func TestNode_Generate(t *testing.T) { Addresses: []v1.NodeAddress{{Type: v1.NodeHostName, Address: "node1"}}, }, }, - output: common.MapStr{"kubernetes": common.MapStr{ - "node": common.MapStr{ + output: mapstr.M{"kubernetes": mapstr.M{ + "node": mapstr.M{ "name": "obj", "uid": uid, "hostname": "node1", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "key2": "value2", }, }}, @@ -98,7 +99,7 @@ func TestNode_GenerateFromName(t *testing.T) { name := "obj" tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -122,16 +123,16 @@ func TestNode_GenerateFromName(t *testing.T) { Addresses: []v1.NodeAddress{{Type: v1.NodeHostName, Address: "node1"}}, }, }, - output: common.MapStr{ - "node": common.MapStr{ + output: mapstr.M{ + "node": mapstr.M{ "name": "obj", "uid": uid, "hostname": "node1", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "key": "value", }, }, diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index 23032bb7ff0..657331f43f6 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/elastic-agent-libs/mapstr" ) type pod struct { @@ -64,9 +65,9 @@ func NewPodMetadataGenerator( // } // All Kubernetes fields that need to be stored under kubernetes. prefix are populated by // GenerateK8s method while fields that are part of ECS are generated by GenerateECS method -func (p *pod) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (p *pod) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { ecsFields := p.GenerateECS(obj) - meta := common.MapStr{ + meta := mapstr.M{ "kubernetes": p.GenerateK8s(obj, opts...), } meta.DeepUpdate(ecsFields) @@ -74,12 +75,12 @@ func (p *pod) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.Map } // GenerateECS generates pod ECS metadata from a resource object -func (p *pod) GenerateECS(obj kubernetes.Resource) common.MapStr { +func (p *pod) GenerateECS(obj kubernetes.Resource) mapstr.M { return p.resource.GenerateECS(obj) } // GenerateK8s generates pod metadata from a resource object -func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { po, ok := obj.(*kubernetes.Pod) if !ok { return nil @@ -135,7 +136,7 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common. } // GenerateFromName generates pod metadata from a pod name -func (p *pod) GenerateFromName(name string, opts ...FieldOptions) common.MapStr { +func (p *pod) GenerateFromName(name string, opts ...FieldOptions) mapstr.M { if p.store == nil { return nil } diff --git a/libbeat/common/kubernetes/metadata/pod_test.go b/libbeat/common/kubernetes/metadata/pod_test.go index 97a02c55f0c..295006677c5 100644 --- a/libbeat/common/kubernetes/metadata/pod_test.go +++ b/libbeat/common/kubernetes/metadata/pod_test.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/elastic-agent-libs/mapstr" ) var addResourceMetadata = GetDefaultResourceMetadataConfig() @@ -96,7 +97,7 @@ func TestPod_Generate(t *testing.T) { tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -122,21 +123,21 @@ func TestPod_Generate(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, "namespace": "default", - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, }, @@ -174,24 +175,24 @@ func TestPod_Generate(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", - "deployment": common.MapStr{ + "deployment": mapstr.M{ "name": "owner", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, }, @@ -229,24 +230,24 @@ func TestPod_Generate(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", - "daemonset": common.MapStr{ + "daemonset": mapstr.M{ "name": "owner", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, }, @@ -284,24 +285,24 @@ func TestPod_Generate(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", - "job": common.MapStr{ + "job": mapstr.M{ "name": "owner", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, }, @@ -339,27 +340,27 @@ func TestPod_Generate(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", - "deployment": common.MapStr{ + "deployment": mapstr.M{ "name": "nginx-deployment", }, - "replicaset": common.MapStr{ + "replicaset": mapstr.M{ "name": "nginx-rs", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, }, @@ -397,28 +398,28 @@ func TestPod_Generate(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", - "deployment": common.MapStr{ + "deployment": mapstr.M{ "name": "nginx-deployment", }, - "replicaset": common.MapStr{ + "replicaset": mapstr.M{ "name": "nginx-rs", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ - "k8s": common.MapStr{"app": "production"}, + "annotations": mapstr.M{ + "k8s": mapstr.M{"app": "production"}, }, }, }, @@ -447,7 +448,7 @@ func TestPod_GenerateFromName(t *testing.T) { boolean := true tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -473,20 +474,20 @@ func TestPod_GenerateFromName(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "k8s_app": "production", }, }, @@ -523,23 +524,23 @@ func TestPod_GenerateFromName(t *testing.T) { }, Status: v1.PodStatus{PodIP: "127.0.0.5"}, }, - output: common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", - "deployment": common.MapStr{ + "deployment": mapstr.M{ "name": "owner", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, }, @@ -573,7 +574,7 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) { input kubernetes.Resource node kubernetes.Resource namespace kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -630,29 +631,29 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{"kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{"kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", "namespace_uid": uid, - "namespace_labels": common.MapStr{ + "namespace_labels": mapstr.M{ "nskey": "nsvalue", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", "uid": uid, - "labels": common.MapStr{ + "labels": mapstr.M{ "nodekey": "nodevalue", }, "hostname": "node1", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, }}, @@ -693,7 +694,7 @@ func TestPod_GenerateWithNodeNamespaceWithAddResourceConfig(t *testing.T) { input kubernetes.Resource node kubernetes.Resource namespace kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -766,38 +767,38 @@ func TestPod_GenerateWithNodeNamespaceWithAddResourceConfig(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{"kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{"kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, "ip": "127.0.0.5", }, "namespace": "default", "namespace_uid": uid, - "namespace_labels": common.MapStr{ + "namespace_labels": mapstr.M{ "app_kubernetes_io/name": "kube-state-metrics", }, - "namespace_annotations": common.MapStr{ + "namespace_annotations": mapstr.M{ "ns_annotation": "ns.value", }, - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", "uid": uid, - "labels": common.MapStr{ + "labels": mapstr.M{ "nodekey2": "nodevalue2", }, "hostname": "node1", - "annotations": common.MapStr{ + "annotations": mapstr.M{ "node_annotation": "node.value", }, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "app_kubernetes_io/component": "exporter", }, - "annotations": common.MapStr{ + "annotations": mapstr.M{ "app": "production", }, - "replicaset": common.MapStr{ + "replicaset": mapstr.M{ "name": "nginx-rs", }, }}, diff --git a/libbeat/common/kubernetes/metadata/resource.go b/libbeat/common/kubernetes/metadata/resource.go index 7bff84e9e40..299c7c750b6 100644 --- a/libbeat/common/kubernetes/metadata/resource.go +++ b/libbeat/common/kubernetes/metadata/resource.go @@ -25,7 +25,8 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) // Resource generates metadata for any kubernetes resource @@ -57,9 +58,9 @@ func NewResourceMetadataGenerator(cfg *common.Config, client k8s.Interface) *Res // } // This method should be called in top level and not as part of other metadata generators. // For retrieving metadata without kubernetes. prefix one should call GenerateK8s instead. -func (r *Resource) Generate(kind string, obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (r *Resource) Generate(kind string, obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { ecsFields := r.GenerateECS(obj) - meta := common.MapStr{ + meta := mapstr.M{ "kubernetes": r.GenerateK8s(kind, obj, opts...), } meta.DeepUpdate(ecsFields) @@ -67,8 +68,8 @@ func (r *Resource) Generate(kind string, obj kubernetes.Resource, opts ...FieldO } // GenerateECS generates ECS metadata from a resource object -func (r *Resource) GenerateECS(obj kubernetes.Resource) common.MapStr { - ecsMeta := common.MapStr{} +func (r *Resource) GenerateECS(obj kubernetes.Resource) mapstr.M { + ecsMeta := mapstr.M{} if r.clusterInfo.Url != "" { ecsMeta.Put("orchestrator.cluster.url", r.clusterInfo.Url) } @@ -79,13 +80,13 @@ func (r *Resource) GenerateECS(obj kubernetes.Resource) common.MapStr { } // GenerateK8s takes a kind and an object and creates metadata for the same -func (r *Resource) GenerateK8s(kind string, obj kubernetes.Resource, options ...FieldOptions) common.MapStr { +func (r *Resource) GenerateK8s(kind string, obj kubernetes.Resource, options ...FieldOptions) mapstr.M { accessor, err := meta.Accessor(obj) if err != nil { return nil } - var labelMap common.MapStr + var labelMap mapstr.M if len(r.config.IncludeLabels) == 0 { labelMap = GenerateMap(accessor.GetLabels(), r.config.LabelsDedot) } else { @@ -99,8 +100,8 @@ func (r *Resource) GenerateK8s(kind string, obj kubernetes.Resource, options ... annotationsMap := generateMapSubset(accessor.GetAnnotations(), r.config.IncludeAnnotations, r.config.AnnotationsDedot) - meta := common.MapStr{ - strings.ToLower(kind): common.MapStr{ + meta := mapstr.M{ + strings.ToLower(kind): mapstr.M{ "name": accessor.GetName(), "uid": string(accessor.GetUID()), }, @@ -141,8 +142,8 @@ func (r *Resource) GenerateK8s(kind string, obj kubernetes.Resource, options ... return meta } -func generateMapSubset(input map[string]string, keys []string, dedot bool) common.MapStr { - output := common.MapStr{} +func generateMapSubset(input map[string]string, keys []string, dedot bool) mapstr.M { + output := mapstr.M{} if input == nil { return output } @@ -162,8 +163,8 @@ func generateMapSubset(input map[string]string, keys []string, dedot bool) commo return output } -func GenerateMap(input map[string]string, dedot bool) common.MapStr { - output := common.MapStr{} +func GenerateMap(input map[string]string, dedot bool) mapstr.M { + output := mapstr.M{} if input == nil { return output } diff --git a/libbeat/common/kubernetes/metadata/resource_test.go b/libbeat/common/kubernetes/metadata/resource_test.go index 5ed6f20fce0..ee33fb35ce7 100644 --- a/libbeat/common/kubernetes/metadata/resource_test.go +++ b/libbeat/common/kubernetes/metadata/resource_test.go @@ -25,9 +25,9 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" ) @@ -38,7 +38,7 @@ func TestResource_Generate(t *testing.T) { boolean := true tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -58,13 +58,13 @@ func TestResource_Generate(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, "namespace": "default", @@ -97,17 +97,17 @@ func TestResource_Generate(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "obj", "uid": uid, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, "namespace": "default", - "deployment": common.MapStr{ + "deployment": mapstr.M{ "name": "owner", }, }, diff --git a/libbeat/common/kubernetes/metadata/service.go b/libbeat/common/kubernetes/metadata/service.go index d87776a5674..de926f586fc 100644 --- a/libbeat/common/kubernetes/metadata/service.go +++ b/libbeat/common/kubernetes/metadata/service.go @@ -23,7 +23,8 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) type service struct { @@ -49,9 +50,9 @@ func NewServiceMetadataGenerator(cfg *common.Config, services cache.Store, names // } // All Kubernetes fields that need to be stored under kuberentes. prefix are populetad by // GenerateK8s method while fields that are part of ECS are generated by GenerateECS method -func (s *service) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (s *service) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { ecsFields := s.GenerateECS(obj) - meta := common.MapStr{ + meta := mapstr.M{ "kubernetes": s.GenerateK8s(obj, opts...), } meta.DeepUpdate(ecsFields) @@ -59,12 +60,12 @@ func (s *service) Generate(obj kubernetes.Resource, opts ...FieldOptions) common } // GenerateECS generates service ECS metadata from a resource object -func (s *service) GenerateECS(obj kubernetes.Resource) common.MapStr { +func (s *service) GenerateECS(obj kubernetes.Resource) mapstr.M { return s.resource.GenerateECS(obj) } // GenerateK8s generates service metadata from a resource object -func (s *service) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.MapStr { +func (s *service) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { svc, ok := obj.(*kubernetes.Service) if !ok { return nil @@ -94,7 +95,7 @@ func (s *service) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) com } // GenerateFromName generates pod metadata from a service name -func (s *service) GenerateFromName(name string, opts ...FieldOptions) common.MapStr { +func (s *service) GenerateFromName(name string, opts ...FieldOptions) mapstr.M { if s.store == nil { return nil } diff --git a/libbeat/common/kubernetes/metadata/service_test.go b/libbeat/common/kubernetes/metadata/service_test.go index 29aaaa4d8d5..0da35c5d8ff 100644 --- a/libbeat/common/kubernetes/metadata/service_test.go +++ b/libbeat/common/kubernetes/metadata/service_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestService_Generate(t *testing.T) { @@ -42,7 +43,7 @@ func TestService_Generate(t *testing.T) { boolean := true tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -68,16 +69,16 @@ func TestService_Generate(t *testing.T) { }, }, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "service": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "service": mapstr.M{ "name": "obj", "uid": uid, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "selectors": common.MapStr{ + "selectors": mapstr.M{ "app": "istiod", "istio": "pilot", }, @@ -117,21 +118,21 @@ func TestService_Generate(t *testing.T) { }, }, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "service": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "service": mapstr.M{ "name": "obj", "uid": uid, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, - "selectors": common.MapStr{ + "selectors": mapstr.M{ "app": "istiod", "istio": "pilot", }, "namespace": "default", - "deployment": common.MapStr{ + "deployment": mapstr.M{ "name": "owner", }, }, @@ -156,7 +157,7 @@ func TestService_GenerateFromName(t *testing.T) { boolean := true tests := []struct { input kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -176,12 +177,12 @@ func TestService_GenerateFromName(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{ - "service": common.MapStr{ + output: mapstr.M{ + "service": mapstr.M{ "name": "obj", "uid": uid, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, "namespace": "default", @@ -213,16 +214,16 @@ func TestService_GenerateFromName(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{ - "service": common.MapStr{ + output: mapstr.M{ + "service": mapstr.M{ "name": "obj", "uid": uid, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, "namespace": "default", - "deployment": common.MapStr{ + "deployment": mapstr.M{ "name": "owner", }, }, @@ -252,7 +253,7 @@ func TestService_GenerateWithNamespace(t *testing.T) { tests := []struct { input kubernetes.Resource namespace kubernetes.Resource - output common.MapStr + output mapstr.M name string }{ { @@ -288,21 +289,21 @@ func TestService_GenerateWithNamespace(t *testing.T) { APIVersion: "v1", }, }, - output: common.MapStr{ - "kubernetes": common.MapStr{ - "service": common.MapStr{ + output: mapstr.M{ + "kubernetes": mapstr.M{ + "service": mapstr.M{ "name": "obj", "uid": uid, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "foo": "bar", }, "namespace": "default", "namespace_uid": uid, - "namespace_labels": common.MapStr{ + "namespace_labels": mapstr.M{ "nskey": "nsvalue", }, - "namespace_annotations": common.MapStr{ + "namespace_annotations": mapstr.M{ "ns_annotation": "value", }, }, diff --git a/libbeat/common/kubernetes/util.go b/libbeat/common/kubernetes/util.go index b27f32a041f..0ff8adea76e 100644 --- a/libbeat/common/kubernetes/util.go +++ b/libbeat/common/kubernetes/util.go @@ -24,8 +24,8 @@ import ( "os" "strings" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -38,8 +38,6 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" ) -const namespaceFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" - type HostDiscoveryUtils interface { GetNamespace() (string, error) GetPodName() (string, error) @@ -272,8 +270,8 @@ func GetContainersInPod(pod *Pod) []*ContainerInPod { } // PodAnnotations returns the annotations in a pod -func PodAnnotations(pod *Pod) common.MapStr { - annotations := common.MapStr{} +func PodAnnotations(pod *Pod) mapstr.M { + annotations := mapstr.M{} for k, v := range pod.GetObjectMeta().GetAnnotations() { safemapstr.Put(annotations, k, v) } @@ -281,7 +279,7 @@ func PodAnnotations(pod *Pod) common.MapStr { } // PodNamespaceAnnotations returns the annotations of the namespace of the pod -func PodNamespaceAnnotations(pod *Pod, watcher Watcher) common.MapStr { +func PodNamespaceAnnotations(pod *Pod, watcher Watcher) mapstr.M { if watcher == nil { return nil } @@ -296,7 +294,7 @@ func PodNamespaceAnnotations(pod *Pod, watcher Watcher) common.MapStr { return nil } - annotations := common.MapStr{} + annotations := mapstr.M{} for k, v := range namespace.GetAnnotations() { safemapstr.Put(annotations, k, v) } diff --git a/libbeat/common/mapstr.go b/libbeat/common/mapstr.go deleted file mode 100644 index d70f05dd349..00000000000 --- a/libbeat/common/mapstr.go +++ /dev/null @@ -1,482 +0,0 @@ -// 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 common - -import ( - "encoding/json" - "fmt" - "io" - "sort" - "strings" - - "github.com/pkg/errors" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -// Event metadata constants. These keys are used within libbeat to identify -// metadata stored in an event. -const ( - FieldsKey = "fields" - TagsKey = "tags" -) - -var ( - // ErrKeyNotFound indicates that the specified key was not found. - ErrKeyNotFound = errors.New("key not found") -) - -// EventMetadata contains fields and tags that can be added to an event via -// configuration. -type EventMetadata struct { - Fields MapStr - FieldsUnderRoot bool `config:"fields_under_root"` - Tags []string -} - -// MapStr is a map[string]interface{} wrapper with utility methods for common -// map operations like converting to JSON. -type MapStr map[string]interface{} - -// Update copies all the key-value pairs from d to this map. If the key -// already exists then it is overwritten. This method does not merge nested -// maps. -func (m MapStr) Update(d MapStr) { - for k, v := range d { - m[k] = v - } -} - -// DeepUpdate recursively copies the key-value pairs from d to this map. -// If the key is present and a map as well, the sub-map will be updated recursively -// via DeepUpdate. -// DeepUpdateNoOverwrite is a version of this function that does not -// overwrite existing values. -func (m MapStr) DeepUpdate(d MapStr) { - m.deepUpdateMap(d, true) -} - -// DeepUpdateNoOverwrite recursively copies the key-value pairs from d to this map. -// If a key is already present it will not be overwritten. -// DeepUpdate is a version of this function that overwrites existing values. -func (m MapStr) DeepUpdateNoOverwrite(d MapStr) { - m.deepUpdateMap(d, false) -} - -func (m MapStr) deepUpdateMap(d MapStr, overwrite bool) { - for k, v := range d { - switch val := v.(type) { - case map[string]interface{}: - m[k] = deepUpdateValue(m[k], MapStr(val), overwrite) - case MapStr: - m[k] = deepUpdateValue(m[k], val, overwrite) - default: - if overwrite { - m[k] = v - } else if _, exists := m[k]; !exists { - m[k] = v - } - } - } -} - -func deepUpdateValue(old interface{}, val MapStr, overwrite bool) interface{} { - switch sub := old.(type) { - case MapStr: - if sub == nil { - return val - } - - sub.deepUpdateMap(val, overwrite) - return sub - case map[string]interface{}: - if sub == nil { - return val - } - - tmp := MapStr(sub) - tmp.deepUpdateMap(val, overwrite) - return tmp - default: - // We reach the default branch if old is no map or if old == nil. - // In either case we return `val`, such that the old value is completely - // replaced when merging. - return val - } -} - -// Delete deletes the given key from the map. -func (m MapStr) Delete(key string) error { - k, d, _, found, err := mapFind(key, m, false) - if err != nil { - return err - } - if !found { - return ErrKeyNotFound - } - - delete(d, k) - return nil -} - -// CopyFieldsTo copies the field specified by key to the given map. It will -// overwrite the key if it exists. An error is returned if the key does not -// exist in the source map. -func (m MapStr) CopyFieldsTo(to MapStr, key string) error { - v, err := m.GetValue(key) - if err != nil { - return err - } - - _, err = to.Put(key, v) - return err -} - -// Clone returns a copy of the MapStr. It recursively makes copies of inner -// maps. -func (m MapStr) Clone() MapStr { - result := MapStr{} - - for k, v := range m { - if innerMap, ok := tryToMapStr(v); ok { - v = innerMap.Clone() - } - result[k] = v - } - - return result -} - -// HasKey returns true if the key exist. If an error occurs then false is -// returned with a non-nil error. -func (m MapStr) HasKey(key string) (bool, error) { - _, _, _, hasKey, err := mapFind(key, m, false) - return hasKey, err -} - -// GetValue gets a value from the map. If the key does not exist then an error -// is returned. -func (m MapStr) GetValue(key string) (interface{}, error) { - _, _, v, found, err := mapFind(key, m, false) - if err != nil { - return nil, err - } - if !found { - return nil, ErrKeyNotFound - } - return v, nil -} - -// Put associates the specified value with the specified key. If the map -// previously contained a mapping for the key, the old value is replaced and -// returned. The key can be expressed in dot-notation (e.g. x.y) to put a value -// into a nested map. -// -// If you need insert keys containing dots then you must use bracket notation -// to insert values (e.g. m[key] = value). -func (m MapStr) Put(key string, value interface{}) (interface{}, error) { - // XXX `safemapstr.Put` mimics this implementation, both should be updated to have similar behavior - k, d, old, _, err := mapFind(key, m, true) - if err != nil { - return nil, err - } - - d[k] = value - return old, nil -} - -// StringToPrint returns the MapStr as pretty JSON. -func (m MapStr) StringToPrint() string { - json, err := json.MarshalIndent(m, "", " ") - if err != nil { - return fmt.Sprintf("Not valid json: %v", err) - } - return string(json) -} - -// String returns the MapStr as JSON. -func (m MapStr) String() string { - bytes, err := json.Marshal(m) - if err != nil { - return fmt.Sprintf("Not valid json: %v", err) - } - return string(bytes) -} - -// MarshalLogObject implements the zapcore.ObjectMarshaler interface and allows -// for more efficient marshaling of MapStr in structured logging. -func (m MapStr) MarshalLogObject(enc zapcore.ObjectEncoder) error { - if len(m) == 0 { - return nil - } - - debugM := m.Clone() - applyLoggingMask(map[string]interface{}(debugM)) - - keys := make([]string, 0, len(debugM)) - for k := range debugM { - keys = append(keys, k) - } - sort.Strings(keys) - for _, k := range keys { - v := debugM[k] - if inner, ok := tryToMapStr(v); ok { - enc.AddObject(k, inner) - continue - } - zap.Any(k, v).AddTo(enc) - } - return nil -} - -// Format implements fmt.Formatter -func (m MapStr) Format(f fmt.State, c rune) { - if f.Flag('+') || f.Flag('#') { - io.WriteString(f, m.String()) - return - } - - debugM := m.Clone() - applyLoggingMask(map[string]interface{}(debugM)) - - io.WriteString(f, debugM.String()) -} - -// Flatten flattens the given MapStr and returns a flat MapStr. -// -// Example: -// "hello": MapStr{"world": "test" } -// -// This is converted to: -// "hello.world": "test" -// -// This can be useful for testing or logging. -func (m MapStr) Flatten() MapStr { - return flatten("", m, MapStr{}) -} - -// flatten is a helper for Flatten. See docs for Flatten. For convenience the -// out parameter is returned. -func flatten(prefix string, in, out MapStr) MapStr { - for k, v := range in { - var fullKey string - if prefix == "" { - fullKey = k - } else { - fullKey = prefix + "." + k - } - - if m, ok := tryToMapStr(v); ok { - flatten(fullKey, m, out) - } else { - out[fullKey] = v - } - } - return out -} - -// MapStrUnion creates a new MapStr containing the union of the -// key-value pairs of the two maps. If the same key is present in -// both, the key-value pairs from dict2 overwrite the ones from dict1. -func MapStrUnion(dict1 MapStr, dict2 MapStr) MapStr { - dict := MapStr{} - - for k, v := range dict1 { - dict[k] = v - } - - for k, v := range dict2 { - dict[k] = v - } - return dict -} - -// MergeFields merges the top-level keys and values in each source map (it does -// not perform a deep merge). If the same key exists in both, the value in -// fields takes precedence. If underRoot is true then the contents of the fields -// MapStr is merged with the value of the 'fields' key in target. -// -// An error is returned if underRoot is true and the value of ms.fields is not a -// MapStr. -func MergeFields(target, from MapStr, underRoot bool) error { - if target == nil || len(from) == 0 { - return nil - } - - destMap, err := mergeFieldsGetDestMap(target, from, underRoot) - if err != nil { - return err - } - - // Add fields and override. - for k, v := range from { - destMap[k] = v - } - - return nil -} - -// MergeFieldsDeep recursively merges the keys and values from `from` into `target`, either -// into ms itself (if underRoot == true) or into ms["fields"] (if underRoot == false). If -// the same key exists in `from` and the destination map, the value in fields takes precedence. -// -// An error is returned if underRoot is true and the value of ms["fields"] is not a -// MapStr. -func MergeFieldsDeep(target, from MapStr, underRoot bool) error { - if target == nil || len(from) == 0 { - return nil - } - - destMap, err := mergeFieldsGetDestMap(target, from, underRoot) - if err != nil { - return err - } - - destMap.DeepUpdate(from) - return nil -} - -func mergeFieldsGetDestMap(target, from MapStr, underRoot bool) (MapStr, error) { - destMap := target - if !underRoot { - f, ok := target[FieldsKey] - if !ok { - destMap = make(MapStr, len(from)) - target[FieldsKey] = destMap - } else { - // Use existing 'fields' value. - var err error - destMap, err = toMapStr(f) - if err != nil { - return nil, err - } - } - } - - return destMap, nil -} - -// AddTags appends a tag to the tags field of ms. If the tags field does not -// exist then it will be created. If the tags field exists and is not a []string -// then an error will be returned. It does not deduplicate the list of tags. -func AddTags(ms MapStr, tags []string) error { - return AddTagsWithKey(ms, TagsKey, tags) -} - -// AddTagsWithKey appends a tag to the key field of ms. If the field does not -// exist then it will be created. If the field exists and is not a []string -// then an error will be returned. It does not deduplicate the list. -func AddTagsWithKey(ms MapStr, key string, tags []string) error { - if ms == nil || len(tags) == 0 { - return nil - } - - k, subMap, oldTags, present, err := mapFind(key, ms, true) - if err != nil { - return err - } - - if !present { - subMap[k] = tags - return nil - } - - switch arr := oldTags.(type) { - case []string: - subMap[k] = append(arr, tags...) - case []interface{}: - for _, tag := range tags { - arr = append(arr, tag) - } - subMap[k] = arr - default: - return errors.Errorf("expected string array by type is %T", oldTags) - - } - return nil -} - -// toMapStr performs a type assertion on v and returns a MapStr. v can be either -// a MapStr or a map[string]interface{}. If it's any other type or nil then -// an error is returned. -func toMapStr(v interface{}) (MapStr, error) { - m, ok := tryToMapStr(v) - if !ok { - return nil, errors.Errorf("expected map but type is %T", v) - } - return m, nil -} - -func tryToMapStr(v interface{}) (MapStr, bool) { - switch m := v.(type) { - case MapStr: - return m, true - case map[string]interface{}: - return MapStr(m), true - default: - return nil, false - } -} - -// mapFind iterates a MapStr based on a the given dotted key, finding the final -// subMap and subKey to operate on. -// An error is returned if some intermediate is no map or the key doesn't exist. -// If createMissing is set to true, intermediate maps are created. -// The final map and un-dotted key to run further operations on are returned in -// subKey and subMap. The subMap already contains a value for subKey, the -// present flag is set to true and the oldValue return will hold -// the original value. -func mapFind( - key string, - data MapStr, - createMissing bool, -) (subKey string, subMap MapStr, oldValue interface{}, present bool, err error) { - // XXX `safemapstr.mapFind` mimics this implementation, both should be updated to have similar behavior - - for { - // Fast path, key is present as is. - if v, exists := data[key]; exists { - return key, data, v, true, nil - } - - idx := strings.IndexRune(key, '.') - if idx < 0 { - return key, data, nil, false, nil - } - - k := key[:idx] - d, exists := data[k] - if !exists { - if createMissing { - d = MapStr{} - data[k] = d - } else { - return "", nil, nil, false, ErrKeyNotFound - } - } - - v, err := toMapStr(d) - if err != nil { - return "", nil, nil, false, err - } - - // advance to sub-map - key = key[idx+1:] - data = v - } -} diff --git a/libbeat/common/mapstr_pointer.go b/libbeat/common/mapstr_pointer.go deleted file mode 100644 index 3129bfcd848..00000000000 --- a/libbeat/common/mapstr_pointer.go +++ /dev/null @@ -1,51 +0,0 @@ -// 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 common - -import ( - "sync/atomic" - "unsafe" -) - -// MapStrPointer stores a pointer to atomically get/set a MapStr object -// This should give faster access for use cases with lots of reads and a few -// changes. -// It's important to note that modifying the map is not thread safe, only fully -// replacing it. -type MapStrPointer struct { - p *unsafe.Pointer -} - -// NewMapStrPointer initializes and returns a pointer to the given MapStr -func NewMapStrPointer(m MapStr) MapStrPointer { - pointer := unsafe.Pointer(&m) - return MapStrPointer{p: &pointer} -} - -// Get returns the MapStr stored under this pointer -func (m MapStrPointer) Get() MapStr { - if m.p == nil { - return nil - } - return *(*MapStr)(atomic.LoadPointer(m.p)) -} - -// Set stores a pointer the given MapStr, replacing any previous one -func (m *MapStrPointer) Set(p MapStr) { - atomic.StorePointer(m.p, unsafe.Pointer(&p)) -} diff --git a/libbeat/common/mapstr_pointer_test.go b/libbeat/common/mapstr_pointer_test.go deleted file mode 100644 index 1b94f324b89..00000000000 --- a/libbeat/common/mapstr_pointer_test.go +++ /dev/null @@ -1,56 +0,0 @@ -// 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 common - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestMapStrPointer(t *testing.T) { - data := MapStr{ - "foo": "bar", - } - - p := NewMapStrPointer(data) - assert.Equal(t, p.Get(), data) - - newData := MapStr{ - "new": "data", - } - p.Set(newData) - assert.Equal(t, p.Get(), newData) -} - -func BenchmarkMapStrPointer(b *testing.B) { - p := NewMapStrPointer(MapStr{"counter": 0}) - go func() { - counter := 0 - for { - counter++ - p.Set(MapStr{"counter": counter}) - time.Sleep(10 * time.Millisecond) - } - }() - - for n := 0; n < b.N; n++ { - _ = p.Get() - } -} diff --git a/libbeat/common/mapstr_test.go b/libbeat/common/mapstr_test.go deleted file mode 100644 index 3847d2eda46..00000000000 --- a/libbeat/common/mapstr_test.go +++ /dev/null @@ -1,1033 +0,0 @@ -// 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. - -//go:build !integration -// +build !integration - -package common - -import ( - "encoding/json" - "fmt" - "strings" - "testing" - - "github.com/stretchr/testify/assert" - "go.uber.org/zap/zapcore" - - "github.com/elastic/beats/v7/libbeat/logp" -) - -func TestMapStrUpdate(t *testing.T) { - assert := assert.New(t) - - a := MapStr{ - "a": 1, - "b": 2, - } - b := MapStr{ - "b": 3, - "c": 4, - } - - a.Update(b) - - assert.Equal(a, MapStr{"a": 1, "b": 3, "c": 4}) -} - -func TestMapStrDeepUpdate(t *testing.T) { - tests := []struct { - a, b, expected MapStr - }{ - { - MapStr{"a": 1}, - MapStr{"b": 2}, - MapStr{"a": 1, "b": 2}, - }, - { - MapStr{"a": 1}, - MapStr{"a": 2}, - MapStr{"a": 2}, - }, - { - MapStr{"a": 1}, - MapStr{"a": MapStr{"b": 1}}, - MapStr{"a": MapStr{"b": 1}}, - }, - { - MapStr{"a": MapStr{"b": 1}}, - MapStr{"a": MapStr{"c": 2}}, - MapStr{"a": MapStr{"b": 1, "c": 2}}, - }, - { - MapStr{"a": MapStr{"b": 1}}, - MapStr{"a": 1}, - MapStr{"a": 1}, - }, - { - MapStr{"a.b": 1}, - MapStr{"a": 1}, - MapStr{"a": 1, "a.b": 1}, - }, - { - MapStr{"a": 1}, - MapStr{"a.b": 1}, - MapStr{"a": 1, "a.b": 1}, - }, - { - MapStr{"a": (MapStr)(nil)}, - MapStr{"a": MapStr{"b": 1}}, - MapStr{"a": MapStr{"b": 1}}, - }, - } - - for i, test := range tests { - a, b, expected := test.a, test.b, test.expected - name := fmt.Sprintf("%v: %v + %v = %v", i, a, b, expected) - - t.Run(name, func(t *testing.T) { - a.DeepUpdate(b) - assert.Equal(t, expected, a) - }) - } -} - -func TestMapStrUnion(t *testing.T) { - assert := assert.New(t) - - a := MapStr{ - "a": 1, - "b": 2, - } - b := MapStr{ - "b": 3, - "c": 4, - } - - c := MapStrUnion(a, b) - - assert.Equal(c, MapStr{"a": 1, "b": 3, "c": 4}) -} - -func TestMapStrCopyFieldsTo(t *testing.T) { - assert := assert.New(t) - - m := MapStr{ - "a": MapStr{ - "a1": 2, - "a2": 3, - }, - "b": 2, - "c": MapStr{ - "c1": 1, - "c2": 2, - "c3": MapStr{ - "c31": 1, - "c32": 2, - }, - }, - } - c := MapStr{} - - err := m.CopyFieldsTo(c, "dd") - assert.Error(err) - assert.Equal(MapStr{}, c) - - err = m.CopyFieldsTo(c, "a") - assert.Equal(nil, err) - assert.Equal(MapStr{"a": MapStr{"a1": 2, "a2": 3}}, c) - - err = m.CopyFieldsTo(c, "c.c1") - assert.Equal(nil, err) - assert.Equal(MapStr{"a": MapStr{"a1": 2, "a2": 3}, "c": MapStr{"c1": 1}}, c) - - err = m.CopyFieldsTo(c, "b") - assert.Equal(nil, err) - assert.Equal(MapStr{"a": MapStr{"a1": 2, "a2": 3}, "c": MapStr{"c1": 1}, "b": 2}, c) - - err = m.CopyFieldsTo(c, "c.c3.c32") - assert.Equal(nil, err) - assert.Equal(MapStr{"a": MapStr{"a1": 2, "a2": 3}, "c": MapStr{"c1": 1, "c3": MapStr{"c32": 2}}, "b": 2}, c) -} - -func TestMapStrDelete(t *testing.T) { - assert := assert.New(t) - - m := MapStr{ - "c": MapStr{ - "c1": 1, - "c2": 2, - "c3": MapStr{ - "c31": 1, - "c32": 2, - }, - }, - } - - err := m.Delete("c.c2") - assert.Equal(nil, err) - assert.Equal(MapStr{"c": MapStr{"c1": 1, "c3": MapStr{"c31": 1, "c32": 2}}}, m) - - err = m.Delete("c.c2.c21") - assert.NotEqual(nil, err) - assert.Equal(MapStr{"c": MapStr{"c1": 1, "c3": MapStr{"c31": 1, "c32": 2}}}, m) - - err = m.Delete("c.c3.c31") - assert.Equal(nil, err) - assert.Equal(MapStr{"c": MapStr{"c1": 1, "c3": MapStr{"c32": 2}}}, m) - - err = m.Delete("c") - assert.Equal(nil, err) - assert.Equal(MapStr{}, m) -} - -func TestHasKey(t *testing.T) { - assert := assert.New(t) - - m := MapStr{ - "c": MapStr{ - "c1": 1, - "c2": 2, - "c3": MapStr{ - "c31": 1, - "c32": 2, - }, - "c4.f": 19, - }, - "d.f": 1, - } - - hasKey, err := m.HasKey("c.c2") - assert.Equal(nil, err) - assert.Equal(true, hasKey) - - hasKey, err = m.HasKey("c.c4") - assert.Equal(nil, err) - assert.Equal(false, hasKey) - - hasKey, err = m.HasKey("c.c3.c32") - assert.Equal(nil, err) - assert.Equal(true, hasKey) - - hasKey, err = m.HasKey("dd") - assert.Equal(nil, err) - assert.Equal(false, hasKey) - - hasKey, err = m.HasKey("d.f") - assert.Equal(nil, err) - assert.Equal(true, hasKey) - - hasKey, err = m.HasKey("c.c4.f") - assert.Equal(nil, err) - assert.Equal(true, hasKey) -} - -func TestMapStrPut(t *testing.T) { - m := MapStr{ - "subMap": MapStr{ - "a": 1, - }, - } - - // Add new value to the top-level. - v, err := m.Put("a", "ok") - assert.NoError(t, err) - assert.Nil(t, v) - assert.Equal(t, MapStr{"a": "ok", "subMap": MapStr{"a": 1}}, m) - - // Add new value to subMap. - v, err = m.Put("subMap.b", 2) - assert.NoError(t, err) - assert.Nil(t, v) - assert.Equal(t, MapStr{"a": "ok", "subMap": MapStr{"a": 1, "b": 2}}, m) - - // Overwrite a value in subMap. - v, err = m.Put("subMap.a", 2) - assert.NoError(t, err) - assert.Equal(t, 1, v) - assert.Equal(t, MapStr{"a": "ok", "subMap": MapStr{"a": 2, "b": 2}}, m) - - // Add value to map that does not exist. - m = MapStr{} - v, err = m.Put("subMap.newMap.a", 1) - assert.NoError(t, err) - assert.Nil(t, v) - assert.Equal(t, MapStr{"subMap": MapStr{"newMap": MapStr{"a": 1}}}, m) -} - -func TestMapStrGetValue(t *testing.T) { - - tests := []struct { - input MapStr - key string - output interface{} - error bool - }{ - { - MapStr{"a": 1}, - "a", - 1, - false, - }, - { - MapStr{"a": MapStr{"b": 1}}, - "a", - MapStr{"b": 1}, - false, - }, - { - MapStr{"a": MapStr{"b": 1}}, - "a.b", - 1, - false, - }, - { - MapStr{"a": MapStr{"b.c": 1}}, - "a", - MapStr{"b.c": 1}, - false, - }, - { - MapStr{"a": MapStr{"b.c": 1}}, - "a.b", - nil, - true, - }, - { - MapStr{"a.b": MapStr{"c": 1}}, - "a.b", - MapStr{"c": 1}, - false, - }, - { - MapStr{"a.b": MapStr{"c": 1}}, - "a.b.c", - nil, - true, - }, - { - MapStr{"a": MapStr{"b.c": 1}}, - "a.b.c", - 1, - false, - }, - } - - for _, test := range tests { - v, err := test.input.GetValue(test.key) - if test.error { - assert.Error(t, err) - } else { - assert.NoError(t, err) - } - assert.Equal(t, test.output, v) - - } -} - -func TestClone(t *testing.T) { - assert := assert.New(t) - - m := MapStr{ - "c1": 1, - "c2": 2, - "c3": MapStr{ - "c31": 1, - "c32": 2, - }, - } - - c := m.Clone() - assert.Equal(MapStr{"c31": 1, "c32": 2}, c["c3"]) -} - -func TestString(t *testing.T) { - type io struct { - Input MapStr - Output string - } - tests := []io{ - { - Input: MapStr{ - "a": "b", - }, - Output: `{"a":"b"}`, - }, - { - Input: MapStr{ - "a": []int{1, 2, 3}, - }, - Output: `{"a":[1,2,3]}`, - }, - } - for _, test := range tests { - assert.Equal(t, test.Output, test.Input.String()) - } -} - -// Smoke test. The method has no observable outputs so this -// is only verifying there are no panics. -func TestStringToPrint(t *testing.T) { - m := MapStr{} - - assert.Equal(t, "{}", m.StringToPrint()) - assert.Equal(t, true, len(m.StringToPrint()) > 0) -} - -func TestMergeFields(t *testing.T) { - type io struct { - UnderRoot bool - Event MapStr - Fields MapStr - Output MapStr - Err string - } - tests := []io{ - // underRoot = true, merges - { - UnderRoot: true, - Event: MapStr{ - "a": "1", - }, - Fields: MapStr{ - "b": 2, - }, - Output: MapStr{ - "a": "1", - "b": 2, - }, - }, - - // underRoot = true, overwrites existing - { - UnderRoot: true, - Event: MapStr{ - "a": "1", - }, - Fields: MapStr{ - "a": 2, - }, - Output: MapStr{ - "a": 2, - }, - }, - - // underRoot = false, adds new 'fields' when it doesn't exist - { - UnderRoot: false, - Event: MapStr{ - "a": "1", - }, - Fields: MapStr{ - "a": 2, - }, - Output: MapStr{ - "a": "1", - "fields": MapStr{ - "a": 2, - }, - }, - }, - - // underRoot = false, merge with existing 'fields' and overwrites existing keys - { - UnderRoot: false, - Event: MapStr{ - "fields": MapStr{ - "a": "1", - "b": 2, - }, - }, - Fields: MapStr{ - "a": 3, - "c": 4, - }, - Output: MapStr{ - "fields": MapStr{ - "a": 3, - "b": 2, - "c": 4, - }, - }, - }, - - // underRoot = false, error when 'fields' is wrong type - { - UnderRoot: false, - Event: MapStr{ - "fields": "not a MapStr", - }, - Fields: MapStr{ - "a": 3, - }, - Output: MapStr{ - "fields": "not a MapStr", - }, - Err: "expected map", - }, - } - - for _, test := range tests { - err := MergeFields(test.Event, test.Fields, test.UnderRoot) - assert.Equal(t, test.Output, test.Event) - if test.Err != "" { - assert.Contains(t, err.Error(), test.Err) - } else { - assert.NoError(t, err) - } - } -} - -func TestMergeFieldsDeep(t *testing.T) { - type io struct { - UnderRoot bool - Event MapStr - Fields MapStr - Output MapStr - Err string - } - tests := []io{ - // underRoot = true, merges - { - UnderRoot: true, - Event: MapStr{ - "a": "1", - }, - Fields: MapStr{ - "b": 2, - }, - Output: MapStr{ - "a": "1", - "b": 2, - }, - }, - - // underRoot = true, overwrites existing - { - UnderRoot: true, - Event: MapStr{ - "a": "1", - }, - Fields: MapStr{ - "a": 2, - }, - Output: MapStr{ - "a": 2, - }, - }, - - // underRoot = false, adds new 'fields' when it doesn't exist - { - UnderRoot: false, - Event: MapStr{ - "a": "1", - }, - Fields: MapStr{ - "a": 2, - }, - Output: MapStr{ - "a": "1", - "fields": MapStr{ - "a": 2, - }, - }, - }, - - // underRoot = false, merge with existing 'fields' and overwrites existing keys - { - UnderRoot: false, - Event: MapStr{ - "fields": MapStr{ - "a": "1", - "b": 2, - }, - }, - Fields: MapStr{ - "a": 3, - "c": 4, - }, - Output: MapStr{ - "fields": MapStr{ - "a": 3, - "b": 2, - "c": 4, - }, - }, - }, - - // underRoot = false, error when 'fields' is wrong type - { - UnderRoot: false, - Event: MapStr{ - "fields": "not a MapStr", - }, - Fields: MapStr{ - "a": 3, - }, - Output: MapStr{ - "fields": "not a MapStr", - }, - Err: "expected map", - }, - - // underRoot = true, merges recursively - { - UnderRoot: true, - Event: MapStr{ - "my": MapStr{ - "field1": "field1", - }, - }, - Fields: MapStr{ - "my": MapStr{ - "field2": "field2", - "field3": "field3", - }, - }, - Output: MapStr{ - "my": MapStr{ - "field1": "field1", - "field2": "field2", - "field3": "field3", - }, - }, - }, - - // underRoot = true, merges recursively and overrides - { - UnderRoot: true, - Event: MapStr{ - "my": MapStr{ - "field1": "field1", - "field2": "field2", - }, - }, - Fields: MapStr{ - "my": MapStr{ - "field2": "fieldTWO", - "field3": "field3", - }, - }, - Output: MapStr{ - "my": MapStr{ - "field1": "field1", - "field2": "fieldTWO", - "field3": "field3", - }, - }, - }, - - // underRoot = false, merges recursively under existing 'fields' - { - UnderRoot: false, - Event: MapStr{ - "fields": MapStr{ - "my": MapStr{ - "field1": "field1", - }, - }, - }, - Fields: MapStr{ - "my": MapStr{ - "field2": "field2", - "field3": "field3", - }, - }, - Output: MapStr{ - "fields": MapStr{ - "my": MapStr{ - "field1": "field1", - "field2": "field2", - "field3": "field3", - }, - }, - }, - }, - } - - for _, test := range tests { - err := MergeFieldsDeep(test.Event, test.Fields, test.UnderRoot) - assert.Equal(t, test.Output, test.Event) - if test.Err != "" { - assert.Contains(t, err.Error(), test.Err) - } else { - assert.NoError(t, err) - } - } -} - -func TestAddTag(t *testing.T) { - type io struct { - Event MapStr - Tags []string - Output MapStr - Err string - } - tests := []io{ - // No existing tags, creates new tag array - { - Event: MapStr{}, - Tags: []string{"json"}, - Output: MapStr{ - "tags": []string{"json"}, - }, - }, - // Existing tags is a []string, appends - { - Event: MapStr{ - "tags": []string{"json"}, - }, - Tags: []string{"docker"}, - Output: MapStr{ - "tags": []string{"json", "docker"}, - }, - }, - // Existing tags is a []interface{}, appends - { - Event: MapStr{ - "tags": []interface{}{"json"}, - }, - Tags: []string{"docker"}, - Output: MapStr{ - "tags": []interface{}{"json", "docker"}, - }, - }, - // Existing tags is not a []string or []interface{} - { - Event: MapStr{ - "tags": "not a slice", - }, - Tags: []string{"docker"}, - Output: MapStr{ - "tags": "not a slice", - }, - Err: "expected string array", - }, - } - - for _, test := range tests { - err := AddTags(test.Event, test.Tags) - assert.Equal(t, test.Output, test.Event) - if test.Err != "" { - assert.Contains(t, err.Error(), test.Err) - } else { - assert.NoError(t, err) - } - } -} - -func TestAddTagsWithKey(t *testing.T) { - type io struct { - Event MapStr - Key string - Tags []string - Output MapStr - Err string - } - tests := []io{ - // No existing tags, creates new tag array - { - Event: MapStr{}, - Key: "tags", - Tags: []string{"json"}, - Output: MapStr{ - "tags": []string{"json"}, - }, - }, - // Existing tags is a []string, appends - { - Event: MapStr{ - "tags": []string{"json"}, - }, - Key: "tags", - Tags: []string{"docker"}, - Output: MapStr{ - "tags": []string{"json", "docker"}, - }, - }, - // Existing tags are in submap and is a []interface{}, appends - { - Event: MapStr{ - "log": MapStr{ - "flags": []interface{}{"json"}, - }, - }, - Key: "log.flags", - Tags: []string{"docker"}, - Output: MapStr{ - "log": MapStr{ - "flags": []interface{}{"json", "docker"}, - }, - }, - }, - // Existing tags are in a submap and is not a []string or []interface{} - { - Event: MapStr{ - "log": MapStr{ - "flags": "not a slice", - }, - }, - Key: "log.flags", - Tags: []string{"docker"}, - Output: MapStr{ - "log": MapStr{ - "flags": "not a slice", - }, - }, - Err: "expected string array", - }, - } - - for _, test := range tests { - err := AddTagsWithKey(test.Event, test.Key, test.Tags) - assert.Equal(t, test.Output, test.Event) - if test.Err != "" { - assert.Contains(t, err.Error(), test.Err) - } else { - assert.NoError(t, err) - } - } -} - -func TestFlatten(t *testing.T) { - type data struct { - Event MapStr - Expected MapStr - } - tests := []data{ - { - Event: MapStr{ - "hello": MapStr{ - "world": 15, - }, - }, - Expected: MapStr{ - "hello.world": 15, - }, - }, - { - Event: MapStr{ - "test": 15, - }, - Expected: MapStr{ - "test": 15, - }, - }, - { - Event: MapStr{ - "test": 15, - "hello": MapStr{ - "world": MapStr{ - "ok": "test", - }, - }, - "elastic": MapStr{ - "for": "search", - }, - }, - Expected: MapStr{ - "test": 15, - "hello.world.ok": "test", - "elastic.for": "search", - }, - }, - } - - for _, test := range tests { - assert.Equal(t, test.Expected, test.Event.Flatten()) - } -} - -func BenchmarkMapStrFlatten(b *testing.B) { - m := MapStr{ - "test": 15, - "hello": MapStr{ - "world": MapStr{ - "ok": "test", - }, - }, - "elastic": MapStr{ - "for": "search", - }, - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - _ = m.Flatten() - } -} - -// Ensure the MapStr is marshaled in logs the same way it is by json.Marshal. -func TestMapStrJSONLog(t *testing.T) { - logp.DevelopmentSetup(logp.ToObserverOutput()) - - m := MapStr{ - "test": 15, - "hello": MapStr{ - "world": MapStr{ - "ok": "test", - }, - }, - "elastic": MapStr{ - "for": "search", - }, - } - - data, err := json.Marshal(MapStr{"m": m}) - if err != nil { - t.Fatal(err) - } - expectedJSON := string(data) - - logp.NewLogger("test").Infow("msg", "m", m) - logs := logp.ObserverLogs().TakeAll() - if assert.Len(t, logs, 1) { - log := logs[0] - - // Encode like zap does. - e := zapcore.NewJSONEncoder(zapcore.EncoderConfig{}) - buf, err := e.EncodeEntry(log.Entry, log.Context) - if err != nil { - t.Fatal(err) - } - - // Zap adds a newline to end the JSON object. - actualJSON := strings.TrimSpace(buf.String()) - - assert.Equal(t, string(expectedJSON), actualJSON) - } -} - -func BenchmarkMapStrLogging(b *testing.B) { - logp.DevelopmentSetup(logp.ToDiscardOutput()) - logger := logp.NewLogger("benchtest") - - m := MapStr{ - "test": 15, - "hello": MapStr{ - "world": MapStr{ - "ok": "test", - }, - }, - "elastic": MapStr{ - "for": "search", - }, - } - - b.ResetTimer() - for i := 0; i < b.N; i++ { - logger.Infow("test", "mapstr", m) - } -} - -func BenchmarkWalkMap(b *testing.B) { - - globalM := MapStr{ - "hello": MapStr{ - "world": MapStr{ - "ok": "test", - }, - }, - } - - b.Run("Get", func(b *testing.B) { - b.ResetTimer() - - for i := 0; i < b.N; i++ { - globalM.GetValue("test.world.ok") - } - }) - - b.Run("Put", func(b *testing.B) { - b.ResetTimer() - for i := 0; i < b.N; i++ { - m := MapStr{ - "hello": MapStr{ - "world": MapStr{ - "ok": "test", - }, - }, - } - - m.Put("hello.world.new", 17) - } - }) - - b.Run("PutMissing", func(b *testing.B) { - b.ResetTimer() - for i := 0; i < b.N; i++ { - m := MapStr{} - - m.Put("a.b.c", 17) - } - }) - - b.Run("HasKey", func(b *testing.B) { - b.ResetTimer() - - for i := 0; i < b.N; i++ { - globalM.HasKey("hello.world.ok") - globalM.HasKey("hello.world.no_ok") - } - }) - - b.Run("HasKeyFirst", func(b *testing.B) { - b.ResetTimer() - - for i := 0; i < b.N; i++ { - globalM.HasKey("hello") - } - }) - - b.Run("Delete", func(b *testing.B) { - b.ResetTimer() - - for i := 0; i < b.N; i++ { - m := MapStr{ - "hello": MapStr{ - "world": MapStr{ - "ok": "test", - }, - }, - } - m.Put("hello.world.test", 17) - } - }) -} - -func TestFormat(t *testing.T) { - input := MapStr{ - "foo": "bar", - "password": "SUPER_SECURE", - } - - tests := map[string]string{ - "%v": `{"foo":"bar","password":"xxxxx"}`, - "%+v": `{"foo":"bar","password":"SUPER_SECURE"}`, - "%#v": `{"foo":"bar","password":"SUPER_SECURE"}`, - "%s": `{"foo":"bar","password":"xxxxx"}`, - "%+s": `{"foo":"bar","password":"SUPER_SECURE"}`, - "%#s": `{"foo":"bar","password":"SUPER_SECURE"}`, - } - - for verb, expected := range tests { - t.Run(verb, func(t *testing.T) { - actual := fmt.Sprintf(verb, input) - assert.Equal(t, expected, actual) - }) - } -} diff --git a/libbeat/common/reload/reload.go b/libbeat/common/reload/reload.go index ed2fe528363..c1a25f5c601 100644 --- a/libbeat/common/reload/reload.go +++ b/libbeat/common/reload/reload.go @@ -23,6 +23,7 @@ import ( "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Register holds a registry of reloadable objects @@ -34,7 +35,7 @@ type ConfigWithMeta struct { Config *common.Config // Meta data related to this config - Meta *common.MapStrPointer + Meta *mapstr.Pointer } // ReloadableList provides a method to reload the configuration of a list of entities diff --git a/libbeat/common/safemapstr/safemapstr.go b/libbeat/common/safemapstr/safemapstr.go deleted file mode 100644 index 07d7d95d2ec..00000000000 --- a/libbeat/common/safemapstr/safemapstr.go +++ /dev/null @@ -1,113 +0,0 @@ -// 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 safemapstr - -import ( - "strings" - - "github.com/elastic/beats/v7/libbeat/common" -) - -const alternativeKey = "value" - -// Put This method implements a way to put dotted keys into a MapStr while -// ensuring they don't override each other. For example: -// -// a := MapStr{} -// safemapstr.Put(a, "com.docker.swarm.task", "x") -// safemapstr.Put(a, "com.docker.swarm.task.id", 1) -// safemapstr.Put(a, "com.docker.swarm.task.name", "foobar") -// -// Will result in `{"com":{"docker":{"swarm":{"task":{"id":1,"name":"foobar","value":"x"}}}}}` -// -// Put detects this scenario and renames the common base key, by appending -// `.value` -func Put(data common.MapStr, key string, value interface{}) error { - // XXX This implementation mimics `common.MapStr.Put`, both should be updated to have similar behavior - - d, k := mapFind(data, key, alternativeKey) - d[k] = value - return nil -} - -// mapFind walk the map based on the given dotted key and returns the final map -// and key to operate on. This function adds intermediate maps, if the key is -// missing from the original map. - -// mapFind iterates a MapStr based on the given dotted key, finding the final -// subMap and subKey to operate on. -// If a key is already used, but the used value is no map, an intermediate map will be inserted and -// the old value will be stored using the 'alternativeKey' in a new map. -// If the old value found under key is already an dictionary, subMap will be -// the old value and subKey will be set to alternativeKey. -func mapFind(data common.MapStr, key, alternativeKey string) (subMap common.MapStr, subKey string) { - // XXX This implementation mimics `common.mapFind`, both should be updated to have similar behavior - - for { - if oldValue, exists := data[key]; exists { - if oldMap, ok := tryToMapStr(oldValue); ok { - return oldMap, alternativeKey - } - return data, key - } - - idx := strings.IndexRune(key, '.') - if idx < 0 { - // if old value exists and is a dictionary, return the old dictionary and - // make sure we store the new value using the 'alternativeKey' - if oldValue, exists := data[key]; exists { - if oldMap, ok := tryToMapStr(oldValue); ok { - return oldMap, alternativeKey - } - } - - return data, key - } - - // Check if first sub-key exists. Create an intermediate map if not. - k := key[:idx] - d, exists := data[k] - if !exists { - d = common.MapStr{} - data[k] = d - } - - // store old value under 'alternativeKey' if the old value is no map. - // Do not overwrite old value. - v, ok := tryToMapStr(d) - if !ok { - v = common.MapStr{alternativeKey: d} - data[k] = v - } - - // advance into sub-map - key = key[idx+1:] - data = v - } -} - -func tryToMapStr(v interface{}) (common.MapStr, bool) { - switch m := v.(type) { - case common.MapStr: - return m, true - case map[string]interface{}: - return common.MapStr(m), true - default: - return nil, false - } -} diff --git a/libbeat/common/safemapstr/safemapstr_test.go b/libbeat/common/safemapstr/safemapstr_test.go deleted file mode 100644 index ce46d0300ec..00000000000 --- a/libbeat/common/safemapstr/safemapstr_test.go +++ /dev/null @@ -1,82 +0,0 @@ -// 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 safemapstr - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/elastic/beats/v7/libbeat/common" -) - -func TestPut(t *testing.T) { - m := common.MapStr{ - "subMap": common.MapStr{ - "a": 1, - }, - } - - // Add new value to the top-level. - err := Put(m, "a", "ok") - assert.NoError(t, err) - assert.Equal(t, common.MapStr{"a": "ok", "subMap": common.MapStr{"a": 1}}, m) - - // Add new value to subMap. - err = Put(m, "subMap.b", 2) - assert.NoError(t, err) - assert.Equal(t, common.MapStr{"a": "ok", "subMap": common.MapStr{"a": 1, "b": 2}}, m) - - // Overwrite a value in subMap. - err = Put(m, "subMap.a", 2) - assert.NoError(t, err) - assert.Equal(t, common.MapStr{"a": "ok", "subMap": common.MapStr{"a": 2, "b": 2}}, m) - - // Add value to map that does not exist. - m = common.MapStr{} - err = Put(m, "subMap.newMap.a", 1) - assert.NoError(t, err) - assert.Equal(t, common.MapStr{"subMap": common.MapStr{"newMap": common.MapStr{"a": 1}}}, m) -} - -func TestPutRenames(t *testing.T) { - assert := assert.New(t) - - a := common.MapStr{} - Put(a, "com.docker.swarm.task", "x") - Put(a, "com.docker.swarm.task.id", 1) - Put(a, "com.docker.swarm.task.name", "foobar") - assert.Equal(common.MapStr{"com": common.MapStr{"docker": common.MapStr{"swarm": common.MapStr{ - "task": common.MapStr{ - "id": 1, - "name": "foobar", - "value": "x", - }}}}}, a) - - // order is not important: - b := common.MapStr{} - Put(b, "com.docker.swarm.task.id", 1) - Put(b, "com.docker.swarm.task.name", "foobar") - Put(b, "com.docker.swarm.task", "x") - assert.Equal(common.MapStr{"com": common.MapStr{"docker": common.MapStr{"swarm": common.MapStr{ - "task": common.MapStr{ - "id": 1, - "name": "foobar", - "value": "x", - }}}}}, b) -} diff --git a/libbeat/common/schema/mapstriface/mapstriface.go b/libbeat/common/schema/mapstriface/mapstriface.go index ea01d266649..a626512f7fe 100644 --- a/libbeat/common/schema/mapstriface/mapstriface.go +++ b/libbeat/common/schema/mapstriface/mapstriface.go @@ -33,17 +33,17 @@ into metricbeat events. For example, given this input object: And the requirement to transform it into this one: - common.MapStr{ + mapstr.M{ "test_string": "hello", "test_int": int64(42), "test_int_from_float": int64(42), "test_int_from_int64": int64(42), "test_bool": true, "test_time": common.Time(ts), - "test_obj_1": common.MapStr{ + "test_obj_1": mapstr.M{ "test": "hello from top level", }, - "test_obj_2": common.MapStr{ + "test_obj_2": mapstr.M{ "test": "hello, object", }, } @@ -80,6 +80,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/schema" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type ConvMap struct { @@ -90,8 +91,8 @@ type ConvMap struct { } // Map drills down in the data dictionary by using the key -func (convMap ConvMap) Map(key string, event common.MapStr, data map[string]interface{}) multierror.Errors { - d, err := common.MapStr(data).GetValue(convMap.Key) +func (convMap ConvMap) Map(key string, event mapstr.M, data map[string]interface{}) multierror.Errors { + d, err := mapstr.M(data).GetValue(convMap.Key) if err != nil { err := schema.NewKeyNotFoundError(convMap.Key) err.Optional = convMap.Optional @@ -99,8 +100,8 @@ func (convMap ConvMap) Map(key string, event common.MapStr, data map[string]inte return multierror.Errors{err} } switch subData := d.(type) { - case map[string]interface{}, common.MapStr: - subEvent := common.MapStr{} + case map[string]interface{}, mapstr.M: + subEvent := mapstr.M{} _, errors := convMap.Schema.ApplyTo(subEvent, subData.(map[string]interface{})) for _, err := range errors { if err, ok := err.(schema.KeyError); ok { @@ -130,7 +131,7 @@ func Dict(key string, s schema.Schema, opts ...DictSchemaOption) ConvMap { } func toStrFromNum(key string, data map[string]interface{}) (interface{}, error) { - emptyIface, err := common.MapStr(data).GetValue(key) + emptyIface, err := mapstr.M(data).GetValue(key) if err != nil { return "", schema.NewKeyNotFoundError(key) } @@ -151,7 +152,7 @@ func StrFromNum(key string, opts ...schema.SchemaOption) schema.Conv { } func toStr(key string, data map[string]interface{}) (interface{}, error) { - emptyIface, err := common.MapStr(data).GetValue(key) + emptyIface, err := mapstr.M(data).GetValue(key) if err != nil { return "", schema.NewKeyNotFoundError(key) } @@ -169,7 +170,7 @@ func Str(key string, opts ...schema.SchemaOption) schema.Conv { } func toIfc(key string, data map[string]interface{}) (interface{}, error) { - intf, err := common.MapStr(data).GetValue(key) + intf, err := mapstr.M(data).GetValue(key) if err != nil { e := schema.NewKeyNotFoundError(key) e.Err = err @@ -184,7 +185,7 @@ func Ifc(key string, opts ...schema.SchemaOption) schema.Conv { } func toBool(key string, data map[string]interface{}) (interface{}, error) { - emptyIface, err := common.MapStr(data).GetValue(key) + emptyIface, err := mapstr.M(data).GetValue(key) if err != nil { return false, schema.NewKeyNotFoundError(key) } @@ -202,7 +203,7 @@ func Bool(key string, opts ...schema.SchemaOption) schema.Conv { } func toInteger(key string, data map[string]interface{}) (interface{}, error) { - emptyIface, err := common.MapStr(data).GetValue(key) + emptyIface, err := mapstr.M(data).GetValue(key) if err != nil { return 0, schema.NewKeyNotFoundError(key) } @@ -238,7 +239,7 @@ func Float(key string, opts ...schema.SchemaOption) schema.Conv { } func toFloat(key string, data map[string]interface{}) (interface{}, error) { - emptyIface, err := common.MapStr(data).GetValue(key) + emptyIface, err := mapstr.M(data).GetValue(key) if err != nil { return 0.0, schema.NewKeyNotFoundError(key) } @@ -274,7 +275,7 @@ func Int(key string, opts ...schema.SchemaOption) schema.Conv { } func toTime(key string, data map[string]interface{}) (interface{}, error) { - emptyIface, err := common.MapStr(data).GetValue(key) + emptyIface, err := mapstr.M(data).GetValue(key) if err != nil { return common.Time(time.Unix(0, 0)), schema.NewKeyNotFoundError(key) } diff --git a/libbeat/common/schema/mapstriface/mapstriface_test.go b/libbeat/common/schema/mapstriface/mapstriface_test.go index dcc56b24add..a96a15ba0c8 100644 --- a/libbeat/common/schema/mapstriface/mapstriface_test.go +++ b/libbeat/common/schema/mapstriface/mapstriface_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConversions(t *testing.T) { @@ -92,7 +93,7 @@ func TestConversions(t *testing.T) { "test_error_string": Str("testErrorString", s.Optional), } - expected := common.MapStr{ + expected := mapstr.M{ "test_string": "hello", "test_int": int64(42), "test_int_from_float": int64(42), @@ -106,10 +107,10 @@ func TestConversions(t *testing.T) { "test_bool": true, "test_time": common.Time(ts), "common_time": cTs, - "test_obj_1": common.MapStr{ + "test_obj_1": mapstr.M{ "test": "hello from top level", }, - "test_obj_2": common.MapStr{ + "test_obj_2": mapstr.M{ "test": "hello, object", }, "test_nested": map[string]interface{}{ @@ -129,7 +130,7 @@ func TestOptionalField(t *testing.T) { Description string Input map[string]interface{} Schema s.Schema - Expected common.MapStr + Expected mapstr.M ExpectError bool }{ { @@ -143,7 +144,7 @@ func TestOptionalField(t *testing.T) { "test_int": Int("testInt"), "test_opt": Bool("testOptionalInt", s.Optional), }, - common.MapStr{ + mapstr.M{ "test_string": "hello", "test_int": int64(42), }, @@ -157,7 +158,7 @@ func TestOptionalField(t *testing.T) { s.Schema{ "test_int": Int("testInt", s.Optional), }, - common.MapStr{}, + mapstr.M{}, true, }, } @@ -242,7 +243,7 @@ func TestFullFieldPathInErrors(t *testing.T) { assert.Contains(t, err.Error(), c.Expected, c.Description) } - _, errs := c.Schema.ApplyTo(common.MapStr{}, c.Input) + _, errs := c.Schema.ApplyTo(mapstr.M{}, c.Input) assert.Error(t, errs.Err(), c.Description) if assert.Equal(t, 1, len(errs), c.Description) { keyErr, ok := errs[0].(s.KeyError) @@ -258,7 +259,7 @@ func TestNestedFieldPaths(t *testing.T) { Description string Input map[string]interface{} Schema s.Schema - Expected common.MapStr + Expected mapstr.M ExpectError bool }{ { @@ -277,7 +278,7 @@ func TestNestedFieldPaths(t *testing.T) { "int": Int("root.int"), "bool": Bool("root.bool"), }, - common.MapStr{ + mapstr.M{ "foo": "bar", "float": float64(4.5), "int": int64(4), @@ -293,7 +294,7 @@ func TestNestedFieldPaths(t *testing.T) { s.Schema{ "foo": Str("root.foo"), }, - common.MapStr{ + mapstr.M{ "foo": "bar", }, false, @@ -312,8 +313,8 @@ func TestNestedFieldPaths(t *testing.T) { "foo": Str("foo"), }), }, - common.MapStr{ - "dict": common.MapStr{ + mapstr.M{ + "dict": mapstr.M{ "foo": "bar", }, }, diff --git a/libbeat/common/schema/mapstrstr/mapstrstr.go b/libbeat/common/schema/mapstrstr/mapstrstr.go index 0e114a72096..8bde7d6f221 100644 --- a/libbeat/common/schema/mapstrstr/mapstrstr.go +++ b/libbeat/common/schema/mapstrstr/mapstrstr.go @@ -29,12 +29,12 @@ into metricbeat events. For example, given this input object: And the requirement to transform it into this one: - common.MapStr{ + mapstr.M{ "test_string": "hello", "test_int": int64(42), "test_bool": true, "test_float": 42.1, - "test_obj": common.MapStr{ + "test_obj": mapstr.M{ "test_obj_string": "hello, object", }, } diff --git a/libbeat/common/schema/mapstrstr/mapstrstr_test.go b/libbeat/common/schema/mapstrstr/mapstrstr_test.go index 486f8e968f6..acd77a62c6d 100644 --- a/libbeat/common/schema/mapstrstr/mapstrstr_test.go +++ b/libbeat/common/schema/mapstrstr/mapstrstr_test.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConversions(t *testing.T) { @@ -60,13 +61,13 @@ func TestConversions(t *testing.T) { ts, err := time.Parse(time.RFC3339Nano, "2016-08-12T08:00:59.601478Z") assert.NoError(t, err) - expected := common.MapStr{ + expected := mapstr.M{ "test_string": "hello", "test_int": int64(42), "test_bool": true, "test_float": 42.1, "test_time": common.Time(ts), - "test_obj": common.MapStr{ + "test_obj": mapstr.M{ "test_obj_string": "hello, object", }, } @@ -122,7 +123,7 @@ func TestKeyInErrors(t *testing.T) { assert.Contains(t, err.Error(), c.Expected, c.Description) } - _, errs := c.Schema.ApplyTo(common.MapStr{}, c.Input) + _, errs := c.Schema.ApplyTo(mapstr.M{}, c.Input) assert.Error(t, errs.Err(), c.Description) if assert.Equal(t, 1, len(errs), c.Description) { keyErr, ok := errs[0].(s.KeyError) diff --git a/libbeat/common/schema/options.go b/libbeat/common/schema/options.go index 0f8b7e28840..de69b851cfc 100644 --- a/libbeat/common/schema/options.go +++ b/libbeat/common/schema/options.go @@ -20,18 +20,18 @@ package schema import ( "github.com/joeshaw/multierror" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // DefaultApplyOptions are the default options for Apply() var DefaultApplyOptions = []ApplyOption{AllRequired} // ApplyOption modifies the result of Apply -type ApplyOption func(common.MapStr, multierror.Errors) (common.MapStr, multierror.Errors) +type ApplyOption func(mapstr.M, multierror.Errors) (mapstr.M, multierror.Errors) // AllRequired considers any missing field as an error, except if explicitly // set as optional -func AllRequired(event common.MapStr, errors multierror.Errors) (common.MapStr, multierror.Errors) { +func AllRequired(event mapstr.M, errors multierror.Errors) (mapstr.M, multierror.Errors) { k := 0 for i, err := range errors { if err, ok := err.(*KeyNotFoundError); ok { @@ -47,7 +47,7 @@ func AllRequired(event common.MapStr, errors multierror.Errors) (common.MapStr, // FailOnRequired considers missing fields as an error only if they are set // as required -func FailOnRequired(event common.MapStr, errors multierror.Errors) (common.MapStr, multierror.Errors) { +func FailOnRequired(event mapstr.M, errors multierror.Errors) (mapstr.M, multierror.Errors) { k := 0 for i, err := range errors { if err, ok := err.(*KeyNotFoundError); ok { @@ -63,7 +63,7 @@ func FailOnRequired(event common.MapStr, errors multierror.Errors) (common.MapSt // NotFoundKeys calls a function with the list of missing keys as parameter func NotFoundKeys(cb func(keys []string)) ApplyOption { - return func(event common.MapStr, errors multierror.Errors) (common.MapStr, multierror.Errors) { + return func(event mapstr.M, errors multierror.Errors) (mapstr.M, multierror.Errors) { var keys []string for _, err := range errors { if err, ok := err.(*KeyNotFoundError); ok { diff --git a/libbeat/common/schema/options_test.go b/libbeat/common/schema/options_test.go index ecc65ead714..551f3aee587 100644 --- a/libbeat/common/schema/options_test.go +++ b/libbeat/common/schema/options_test.go @@ -24,7 +24,7 @@ import ( "github.com/pkg/errors" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestApplyOptions(t *testing.T) { @@ -91,7 +91,7 @@ func TestApplyOptions(t *testing.T) { } for _, c := range cases { - event := common.MapStr{} + event := mapstr.M{} errors := c.Errors for _, opt := range c.Options { event, errors = opt(event, errors) @@ -143,6 +143,6 @@ func TestNotFoundKeys(t *testing.T) { opt := NotFoundKeys(func(keys []string) { assert.ElementsMatch(t, c.Expected, keys, c.Description) }) - opt(common.MapStr{}, c.Errors) + opt(mapstr.M{}, c.Errors) } } diff --git a/libbeat/common/schema/schema.go b/libbeat/common/schema/schema.go index 1df3388d39b..d747d044c25 100644 --- a/libbeat/common/schema/schema.go +++ b/libbeat/common/schema/schema.go @@ -21,12 +21,11 @@ import ( "github.com/joeshaw/multierror" "github.com/elastic/beats/v7/libbeat/logp" - - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Schema describes how a map[string]interface{} object can be parsed and converted into -// an event. The conversions can be described using an (optionally nested) common.MapStr +// an event. The conversions can be described using an (optionally nested) mapstr.M // that contains Conv objects. type Schema map[string]Mapper @@ -34,7 +33,7 @@ type Schema map[string]Mapper type Mapper interface { // Map applies the Mapper conversion on the data and adds the result // to the event on the key. - Map(key string, event common.MapStr, data map[string]interface{}) multierror.Errors + Map(key string, event mapstr.M, data map[string]interface{}) multierror.Errors HasKey(key string) bool } @@ -53,7 +52,7 @@ type Converter func(key string, data map[string]interface{}) (interface{}, error // Map applies the conversion on the data and adds the result // to the event on the key. -func (conv Conv) Map(key string, event common.MapStr, data map[string]interface{}) multierror.Errors { +func (conv Conv) Map(key string, event mapstr.M, data map[string]interface{}) multierror.Errors { value, err := conv.Func(conv.Key, data) if err != nil { if err, keyNotFound := err.(*KeyNotFoundError); keyNotFound { @@ -78,8 +77,8 @@ func (conv Conv) HasKey(key string) bool { type Object map[string]Mapper // Map applies the schema for an object -func (o Object) Map(key string, event common.MapStr, data map[string]interface{}) multierror.Errors { - subEvent := common.MapStr{} +func (o Object) Map(key string, event mapstr.M, data map[string]interface{}) multierror.Errors { + subEvent := mapstr.M{} errs := applySchemaToEvent(subEvent, data, o) event[key] = subEvent return errs @@ -91,7 +90,7 @@ func (o Object) HasKey(key string) bool { // ApplyTo adds the fields extracted from data, converted using the schema, to the // event map. -func (s Schema) ApplyTo(event common.MapStr, data map[string]interface{}, opts ...ApplyOption) (common.MapStr, multierror.Errors) { +func (s Schema) ApplyTo(event mapstr.M, data map[string]interface{}, opts ...ApplyOption) (mapstr.M, multierror.Errors) { if len(opts) == 0 { opts = DefaultApplyOptions } @@ -103,8 +102,8 @@ func (s Schema) ApplyTo(event common.MapStr, data map[string]interface{}, opts . } // Apply converts the fields extracted from data, using the schema, into a new map and reports back the errors. -func (s Schema) Apply(data map[string]interface{}, opts ...ApplyOption) (common.MapStr, error) { - event, errors := s.ApplyTo(common.MapStr{}, data, opts...) +func (s Schema) Apply(data map[string]interface{}, opts ...ApplyOption) (mapstr.M, error) { + event, errors := s.ApplyTo(mapstr.M{}, data, opts...) return event, errors.Err() } @@ -122,7 +121,7 @@ func hasKey(key string, mappers map[string]Mapper) bool { return false } -func applySchemaToEvent(event common.MapStr, data map[string]interface{}, conversions map[string]Mapper) multierror.Errors { +func applySchemaToEvent(event mapstr.M, data map[string]interface{}, conversions map[string]Mapper) multierror.Errors { var errs multierror.Errors for key, mapper := range conversions { errors := mapper.Map(key, event, data) diff --git a/libbeat/common/schema/schema_test.go b/libbeat/common/schema/schema_test.go index 3b6b453e807..21cd98c7897 100644 --- a/libbeat/common/schema/schema_test.go +++ b/libbeat/common/schema/schema_test.go @@ -23,7 +23,7 @@ import ( "github.com/pkg/errors" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func nop(key string, data map[string]interface{}) (interface{}, error) { @@ -47,9 +47,9 @@ func TestSchema(t *testing.T) { } event, _ := schema.Apply(source) - assert.Equal(t, event, common.MapStr{ + assert.Equal(t, event, mapstr.M{ "test": "hello", - "test_obj": common.MapStr{ + "test_obj": mapstr.M{ "test_a": "helloA", "test_b": "helloB", }, @@ -98,7 +98,7 @@ func TestSchemaCases(t *testing.T) { source map[string]interface{} expectedErrorMessage string - expectedOutput common.MapStr + expectedOutput mapstr.M }{ { name: "standard schema conversion case", @@ -113,7 +113,7 @@ func TestSchemaCases(t *testing.T) { "inField": "10", }, - expectedOutput: common.MapStr{ + expectedOutput: mapstr.M{ "outField": "10", }, }, @@ -131,7 +131,7 @@ func TestSchemaCases(t *testing.T) { }, expectedErrorMessage: "test error", - expectedOutput: common.MapStr{}, + expectedOutput: mapstr.M{}, }, { name: "ignore error at conversion case", @@ -147,7 +147,7 @@ func TestSchemaCases(t *testing.T) { "doesntMatter": "", }, - expectedOutput: common.MapStr{}, + expectedOutput: mapstr.M{}, }, } diff --git a/libbeat/common/transform/typeconv/typeconv_test.go b/libbeat/common/transform/typeconv/typeconv_test.go index a7f4e9ec4c1..4e4ad147381 100644 --- a/libbeat/common/transform/typeconv/typeconv_test.go +++ b/libbeat/common/transform/typeconv/typeconv_test.go @@ -24,26 +24,26 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConversionWithMapStr(t *testing.T) { - t.Run("from MapStr", func(t *testing.T) { + t.Run("from mapstr.M", func(t *testing.T) { type testStruct struct { A int B int } var v testStruct - Convert(&v, &common.MapStr{"a": 1}) + Convert(&v, &mapstr.M{"a": 1}) assert.Equal(t, testStruct{1, 0}, v) }) - t.Run("to MapStr", func(t *testing.T) { - var m common.MapStr + t.Run("to mapstr.M", func(t *testing.T) { + var m mapstr.M err := Convert(&m, struct{ A string }{"test"}) require.NoError(t, err) - assert.Equal(t, common.MapStr{"a": "test"}, m) + assert.Equal(t, mapstr.M{"a": "test"}, m) }) } @@ -86,8 +86,8 @@ func TestConversionBetweenGoTypes(t *testing.T) { } func TestTimestamps(t *testing.T) { - t.Run("timestamp to MapStr", func(t *testing.T) { - var m common.MapStr + t.Run("timestamp to mapstr.M", func(t *testing.T) { + var m mapstr.M ts := time.Unix(1234, 5678).UTC() off := int16(-1) @@ -95,17 +95,17 @@ func TestTimestamps(t *testing.T) { err := Convert(&m, struct{ Timestamp time.Time }{ts}) require.NoError(t, err) - assert.Equal(t, common.MapStr{"timestamp": expected}, m) + assert.Equal(t, mapstr.M{"timestamp": expected}, m) }) - t.Run("timestamp from encoded MapStr", func(t *testing.T) { + t.Run("timestamp from encoded mapstr.M", func(t *testing.T) { type testStruct struct { Timestamp time.Time } var v testStruct off := int16(-1) - err := Convert(&v, common.MapStr{ + err := Convert(&v, mapstr.M{ "timestamp": []uint64{5678 | (uint64(uint16(off)))<<32, 1234}, }) require.NoError(t, err) @@ -120,7 +120,7 @@ func TestTimestamps(t *testing.T) { var v testStruct ts := time.Now() - err := Convert(&v, common.MapStr{ + err := Convert(&v, mapstr.M{ "timestamp": ts.Format(time.RFC3339Nano), }) require.NoError(t, err) @@ -148,13 +148,13 @@ func TestComplexExampleWithIntermediateConversion(t *testing.T) { } ) - input := common.MapStr{ + input := mapstr.M{ "_key": "test", - "internal": common.MapStr{ + "internal": mapstr.M{ "ttl": float64(1800000000000), "updated": []interface{}{float64(515579904576), float64(1588432943)}, }, - "cursor": common.MapStr{ + "cursor": mapstr.M{ "monotonictimestamp": float64(24881645756), "position": "s=86a99d3589f54f01804e844bebd787d5;i=4d19f;b=9c5d2b320b7946b4be53c0940a5b1289;m=5cb0fc8bc;t=5a488aeaa1130;x=ccbe23f507e8d0a4", "realtimetimestamp": float64(1588281836441904), diff --git a/libbeat/conditions/conditions_benchmarks_test.go b/libbeat/conditions/conditions_benchmarks_test.go index 1ccfe400334..a8cf54ed0d5 100644 --- a/libbeat/conditions/conditions_benchmarks_test.go +++ b/libbeat/conditions/conditions_benchmarks_test.go @@ -22,7 +22,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func BenchmarkSimpleCondition(b *testing.B) { @@ -37,7 +37,7 @@ func BenchmarkSimpleCondition(b *testing.B) { event := &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "@timestamp": "2015-06-11T09:51:23.642Z", "afield": "avalue", }, @@ -81,7 +81,7 @@ func BenchmarkCombinedCondition(b *testing.B) { event := &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "@timestamp": "2015-06-11T09:51:23.642Z", "bytes_in": 126, "bytes_out": 28033, @@ -89,7 +89,7 @@ func BenchmarkCombinedCondition(b *testing.B) { "client_port": 42840, "client_proc": "", "client_server": "mar.local", - "http": common.MapStr{ + "http": mapstr.M{ "code": 200, "content_length": 76985, "phrase": "OK", diff --git a/libbeat/conditions/conditions_test.go b/libbeat/conditions/conditions_test.go index 1161f817f4f..06d18bde79a 100644 --- a/libbeat/conditions/conditions_test.go +++ b/libbeat/conditions/conditions_test.go @@ -24,8 +24,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestCreateNilCondition(t *testing.T) { @@ -53,10 +53,10 @@ func GetConditions(t *testing.T, configs []Config) []Condition { var secdTestEvent = &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ - "proc": common.MapStr{ + Fields: mapstr.M{ + "proc": mapstr.M{ "cmdline": "/usr/libexec/secd", - "cpu": common.MapStr{ + "cpu": mapstr.M{ "start_time": "Apr10", "system": 1988, "total": 6029, @@ -78,7 +78,7 @@ var secdTestEvent = &beat.Event{ var httpResponseTestEvent = &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "@timestamp": "2015-06-11T09:51:23.642Z", "bytes_in": 126, "bytes_out": 28033, @@ -86,7 +86,7 @@ var httpResponseTestEvent = &beat.Event{ "client_port": 42840, "client_proc": "", "client_server": "mar.local", - "http": common.MapStr{ + "http": mapstr.M{ "code": 200, "content_length": 76985, "phrase": "OK", diff --git a/libbeat/conditions/matcher_test.go b/libbeat/conditions/matcher_test.go index 0bdf3ea6a68..637721cac2a 100644 --- a/libbeat/conditions/matcher_test.go +++ b/libbeat/conditions/matcher_test.go @@ -24,8 +24,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestRegxpCreate(t *testing.T) { @@ -100,7 +100,7 @@ func TestRegexpCondition(t *testing.T) { event := &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": `[Fri Dec 16 01:46:23 2005] [error] [client 1.2.3.4] Directory index forbidden by rule: /home/test/`, "source": "/var/log/apache2/error.log", "type": "log", @@ -111,7 +111,7 @@ func TestRegexpCondition(t *testing.T) { event1 := &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": `127.0.0.1 - - [28/Jul/2006:10:27:32 -0300] "GET /hidden/ HTTP/1.0" 404 7218`, "source": "/var/log/apache2/access.log", "type": "log", diff --git a/libbeat/conditions/network.go b/libbeat/conditions/network.go index e5c732d469e..b757edef803 100644 --- a/libbeat/conditions/network.go +++ b/libbeat/conditions/network.go @@ -24,8 +24,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -121,7 +121,7 @@ func NewNetworkCondition(fields map[string]interface{}) (*Network, error) { "strings or []strings are allowed", field, value, value) } - for field, value := range common.MapStr(fields).Flatten() { + for field, value := range mapstr.M(fields).Flatten() { switch v := value.(type) { case string: m, err := makeMatcher(v) diff --git a/libbeat/conditions/network_test.go b/libbeat/conditions/network_test.go index 9effaf62266..141f605e388 100644 --- a/libbeat/conditions/network_test.go +++ b/libbeat/conditions/network_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNetworkConfigUnpack(t *testing.T) { @@ -69,8 +70,8 @@ network: server: [loopback] ` - evt := &beat.Event{Fields: common.MapStr{ - "ip": common.MapStr{ + evt := &beat.Event{Fields: mapstr.M{ + "ip": mapstr.M{ "client": "127.0.0.1", "server": "127.0.0.1", }, @@ -255,7 +256,7 @@ func BenchmarkNetworkCondition(b *testing.B) { event := &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "@timestamp": "2015-06-11T09:51:23.642Z", "ip": "192.168.0.92", }, diff --git a/libbeat/conditions/range_test.go b/libbeat/conditions/range_test.go index aca5ce007ad..cbf4db37909 100644 --- a/libbeat/conditions/range_test.go +++ b/libbeat/conditions/range_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestRangeCreateNumeric(t *testing.T) { @@ -90,10 +90,10 @@ var procCPURangeConfig = &Config{ func TestOpenGteRangeConditionPositiveMatch(t *testing.T) { mdWorkerTestEvent := &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ - "proc": common.MapStr{ + Fields: mapstr.M{ + "proc": mapstr.M{ "cmdline": "/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker -c MDSImporterWorker -m com.apple.mdworker.single", - "cpu": common.MapStr{ + "cpu": mapstr.M{ "start_time": "09:19", "system": 22, "total": 66, diff --git a/libbeat/dashboards/dashboards.go b/libbeat/dashboards/dashboards.go index d1209235a54..f8b4b083b7d 100644 --- a/libbeat/dashboards/dashboards.go +++ b/libbeat/dashboards/dashboards.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ImportDashboards tries to import the kibana dashboards. @@ -35,7 +36,7 @@ func ImportDashboards( beatInfo beat.Info, homePath string, kibanaConfig, dashboardsConfig *common.Config, msgOutputter MessageOutputter, - pattern common.MapStr, + pattern mapstr.M, ) error { if dashboardsConfig == nil || !dashboardsConfig.Enabled() { return nil @@ -58,7 +59,7 @@ func ImportDashboards( } func setupAndImportDashboardsViaKibana(ctx context.Context, hostname, beatname string, kibanaConfig *common.Config, - dashboardsConfig *Config, msgOutputter MessageOutputter, fields common.MapStr) error { + dashboardsConfig *Config, msgOutputter MessageOutputter, fields mapstr.M) error { kibanaLoader, err := NewKibanaLoader(ctx, kibanaConfig, dashboardsConfig, hostname, msgOutputter, beatname) if err != nil { @@ -73,7 +74,7 @@ func setupAndImportDashboardsViaKibana(ctx context.Context, hostname, beatname s } // ImportDashboardsViaKibana imports Dashboards to Kibana -func ImportDashboardsViaKibana(kibanaLoader *KibanaLoader, fields common.MapStr) error { +func ImportDashboardsViaKibana(kibanaLoader *KibanaLoader, fields mapstr.M) error { version := kibanaLoader.version if !version.IsValid() { return errors.New("No valid kibana version available") diff --git a/libbeat/dashboards/decode.go b/libbeat/dashboards/decode.go index 10c0a694898..580779bf6c3 100644 --- a/libbeat/dashboards/decode.go +++ b/libbeat/dashboards/decode.go @@ -24,8 +24,8 @@ import ( "fmt" "io" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -73,7 +73,7 @@ func decodeLine(line []byte) []byte { return line } - o := common.MapStr{} + o := mapstr.M{} err := json.Unmarshal(line, &o) if err != nil { return line @@ -84,7 +84,7 @@ func decodeLine(line []byte) []byte { return []byte(o.String()) } -func decodeObject(o common.MapStr) common.MapStr { +func decodeObject(o mapstr.M) mapstr.M { for _, key := range responseToDecode { // All fields are optional, so errors are not caught err := decodeValue(o, key) @@ -98,7 +98,7 @@ func decodeObject(o common.MapStr) common.MapStr { return o } -func decodeEmbeddableConfig(o common.MapStr) common.MapStr { +func decodeEmbeddableConfig(o mapstr.M) mapstr.M { p, err := o.GetValue("attributes.panelsJSON") if err != nil { return o @@ -107,13 +107,13 @@ func decodeEmbeddableConfig(o common.MapStr) common.MapStr { if panels, ok := p.([]interface{}); ok { for i, pan := range panels { if panel, ok := pan.(map[string]interface{}); ok { - panelObj := common.MapStr(panel) + panelObj := mapstr.M(panel) embedded, err := panelObj.GetValue("embeddableConfig") if err != nil { continue } if embeddedConfig, ok := embedded.(map[string]interface{}); ok { - embeddedConfigObj := common.MapStr(embeddedConfig) + embeddedConfigObj := mapstr.M(embeddedConfig) panelObj.Put("embeddableConfig", decodeObject(embeddedConfigObj)) panels[i] = panelObj } @@ -125,7 +125,7 @@ func decodeEmbeddableConfig(o common.MapStr) common.MapStr { return o } -func decodeValue(data common.MapStr, key string) error { +func decodeValue(data mapstr.M, key string) error { v, err := data.GetValue(key) if err != nil { return err diff --git a/libbeat/dashboards/export.go b/libbeat/dashboards/export.go index b75adfc4b99..c0e9a19f524 100644 --- a/libbeat/dashboards/export.go +++ b/libbeat/dashboards/export.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/kibana" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -130,7 +131,7 @@ func SaveToFolder(dashboard []byte, root string, version common.Version) error { } func saveAsset(line []byte, assetRoot string) error { - var a common.MapStr + var a mapstr.M err := json.Unmarshal(line, &a) if err != nil { return fmt.Errorf("failed to decode dashboard asset: %+v", err) diff --git a/libbeat/dashboards/importer.go b/libbeat/dashboards/importer.go index b27ec695cad..8b0d8f84aad 100644 --- a/libbeat/dashboards/importer.go +++ b/libbeat/dashboards/importer.go @@ -32,6 +32,7 @@ import ( errw "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ErrNotFound returned when we cannot find any dashboard to import. @@ -56,11 +57,11 @@ type Importer struct { version common.Version loader KibanaLoader - fields common.MapStr + fields mapstr.M } // NewImporter creates a new dashboard importer -func NewImporter(version common.Version, cfg *Config, loader KibanaLoader, fields common.MapStr) (*Importer, error) { +func NewImporter(version common.Version, cfg *Config, loader KibanaLoader, fields mapstr.M) (*Importer, error) { // Current max version is 7 if version.Major > 6 { diff --git a/libbeat/dashboards/kibana_loader.go b/libbeat/dashboards/kibana_loader.go index 37f27811ff0..0d3e5f0f5a8 100644 --- a/libbeat/dashboards/kibana_loader.go +++ b/libbeat/dashboards/kibana_loader.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/kibana" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var importAPI = "/api/saved_objects/_import" @@ -106,7 +107,7 @@ func (loader KibanaLoader) ImportIndexFile(file string) error { return fmt.Errorf("fail to read index-pattern from file %s: %v", file, err) } - var indexContent common.MapStr + var indexContent mapstr.M err = json.Unmarshal(reader, &indexContent) if err != nil { return fmt.Errorf("fail to unmarshal the index content from file %s: %v", file, err) @@ -116,7 +117,7 @@ func (loader KibanaLoader) ImportIndexFile(file string) error { } // ImportIndex imports the passed index pattern to Kibana -func (loader KibanaLoader) ImportIndex(pattern common.MapStr) error { +func (loader KibanaLoader) ImportIndex(pattern mapstr.M) error { if loader.version.LessThan(kibana.MinimumRequiredVersionSavedObjects) { return fmt.Errorf("Kibana version must be at least " + kibana.MinimumRequiredVersionSavedObjects.String()) } @@ -207,7 +208,7 @@ func (loader KibanaLoader) addReferences(path string, dashboard []byte) (string, loader.loadedAssets[referencePath] = true } - var res common.MapStr + var res mapstr.M err = json.Unmarshal(dashboard, &res) if err != nil { return "", fmt.Errorf("failed to convert asset: %+v", err) diff --git a/libbeat/dashboards/modify_json.go b/libbeat/dashboards/modify_json.go index 2545e1fe02e..822677d9150 100644 --- a/libbeat/dashboards/modify_json.go +++ b/libbeat/dashboards/modify_json.go @@ -23,8 +23,8 @@ import ( "fmt" "regexp" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -46,7 +46,7 @@ type JSONObject struct { } // ReplaceIndexInIndexPattern replaces an index in a dashboard content body -func ReplaceIndexInIndexPattern(index string, content common.MapStr) (err error) { +func ReplaceIndexInIndexPattern(index string, content mapstr.M) (err error) { if index == "" { return nil } @@ -64,7 +64,7 @@ func ReplaceIndexInIndexPattern(index string, content common.MapStr) (err error) func replaceIndexInSearchObject(index string, savedObject string) (string, error) { - var record common.MapStr + var record mapstr.M err := json.Unmarshal([]byte(savedObject), &record) if err != nil { return "", fmt.Errorf("fail to unmarshal searchSourceJSON from search : %v", err) diff --git a/libbeat/dashboards/modify_json_test.go b/libbeat/dashboards/modify_json_test.go index 7981dac8c57..665bc97ecc6 100644 --- a/libbeat/dashboards/modify_json_test.go +++ b/libbeat/dashboards/modify_json_test.go @@ -22,7 +22,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestReplaceStringInDashboard(t *testing.T) { @@ -120,69 +120,69 @@ func TestReplaceIndexInIndexPattern(t *testing.T) { // Also ensures that the inner types are not modified after replacement. tests := []struct { title string - input common.MapStr + input mapstr.M index string - expected common.MapStr + expected mapstr.M }{ { title: "Replace in []mapstr.mapstr", - input: common.MapStr{ + input: mapstr.M{ "id": "phonybeat-*", "type": "index-pattern", - "attributes": common.MapStr{ + "attributes": mapstr.M{ "title": "phonybeat-*", "timeFieldName": "@timestamp", }}, index: "otherindex-*", - expected: common.MapStr{ + expected: mapstr.M{ "id": "otherindex-*", "type": "index-pattern", - "attributes": common.MapStr{ + "attributes": mapstr.M{ "title": "otherindex-*", "timeFieldName": "@timestamp", }}, }, { title: "Replace in []mapstr.interface(mapstr)", - input: common.MapStr{ + input: mapstr.M{ "id": "phonybeat-*", "type": "index-pattern", - "attributes": interface{}(common.MapStr{ + "attributes": interface{}(mapstr.M{ "title": "phonybeat-*", "timeFieldName": "@timestamp", })}, index: "otherindex-*", - expected: common.MapStr{ + expected: mapstr.M{ "id": "otherindex-*", "type": "index-pattern", - "attributes": interface{}(common.MapStr{ + "attributes": interface{}(mapstr.M{ "title": "otherindex-*", "timeFieldName": "@timestamp", })}, }, { title: "Do not create missing attributes", - input: common.MapStr{ - "attributes": common.MapStr{}, + input: mapstr.M{ + "attributes": mapstr.M{}, "id": "phonybeat-*", "type": "index-pattern", }, index: "otherindex-*", - expected: common.MapStr{ - "attributes": common.MapStr{}, + expected: mapstr.M{ + "attributes": mapstr.M{}, "id": "otherindex-*", "type": "index-pattern", }, }, { title: "Create missing id", - input: common.MapStr{ - "attributes": common.MapStr{}, + input: mapstr.M{ + "attributes": mapstr.M{}, "type": "index-pattern", }, index: "otherindex-*", - expected: common.MapStr{ - "attributes": common.MapStr{}, + expected: mapstr.M{ + "attributes": mapstr.M{}, "id": "otherindex-*", "type": "index-pattern", }, diff --git a/libbeat/esleg/eslegclient/enc.go b/libbeat/esleg/eslegclient/enc.go index 3116dc2f537..ae5520a1281 100644 --- a/libbeat/esleg/eslegclient/enc.go +++ b/libbeat/esleg/eslegclient/enc.go @@ -25,8 +25,8 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/outputs/codec" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-structform/gotype" "github.com/elastic/go-structform/json" ) @@ -65,8 +65,8 @@ type gzipEncoder struct { } type event struct { - Timestamp time.Time `struct:"@timestamp"` - Fields common.MapStr `struct:",inline"` + Timestamp time.Time `struct:"@timestamp"` + Fields mapstr.M `struct:",inline"` } func NewJSONEncoder(buf *bytes.Buffer, escapeHTML bool) *jsonEncoder { diff --git a/libbeat/esleg/eslegclient/enc_test.go b/libbeat/esleg/eslegclient/enc_test.go index 32b2f35e1f3..625ff26b342 100644 --- a/libbeat/esleg/eslegclient/enc_test.go +++ b/libbeat/esleg/eslegclient/enc_test.go @@ -24,15 +24,15 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/monitoring/report" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestJSONEncoderMarshalBeatEvent(t *testing.T) { encoder := NewJSONEncoder(nil, true) event := beat.Event{ Timestamp: time.Date(2017, time.November, 7, 12, 0, 0, 0, time.UTC), - Fields: common.MapStr{ + Fields: mapstr.M{ "field1": "value1", }, } @@ -49,7 +49,7 @@ func TestJSONEncoderMarshalMonitoringEvent(t *testing.T) { encoder := NewJSONEncoder(nil, true) event := report.Event{ Timestamp: time.Date(2017, time.November, 7, 12, 0, 0, 0, time.UTC), - Fields: common.MapStr{ + Fields: mapstr.M{ "field1": "value1", }, } diff --git a/libbeat/idxmgmt/ilm/client_handler_integration_test.go b/libbeat/idxmgmt/ilm/client_handler_integration_test.go index 42b19cf44ae..1ed34cde284 100644 --- a/libbeat/idxmgmt/ilm/client_handler_integration_test.go +++ b/libbeat/idxmgmt/ilm/client_handler_integration_test.go @@ -36,6 +36,7 @@ import ( "github.com/elastic/beats/v7/libbeat/esleg/eslegclient" "github.com/elastic/beats/v7/libbeat/idxmgmt/ilm" "github.com/elastic/beats/v7/libbeat/version" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -203,12 +204,12 @@ func TestFileClientHandler_CreateILMPolicy(t *testing.T) { c := newMockClient("") h := ilm.NewFileClientHandler(c) name := "test-policy" - body := common.MapStr{"foo": "bar"} + body := mapstr.M{"foo": "bar"} h.CreateILMPolicy(ilm.Policy{Name: name, Body: body}) assert.Equal(t, name, c.name) assert.Equal(t, "policy", c.component) - var out common.MapStr + var out mapstr.M json.Unmarshal([]byte(c.body), &out) assert.Equal(t, body, out) } diff --git a/libbeat/idxmgmt/ilm/config.go b/libbeat/idxmgmt/ilm/config.go index ea5cd9ac1e4..b240feebb71 100644 --- a/libbeat/idxmgmt/ilm/config.go +++ b/libbeat/idxmgmt/ilm/config.go @@ -19,8 +19,8 @@ package ilm import ( "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fmtstr" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Config is used for unpacking a common.Config. @@ -42,12 +42,12 @@ type Config struct { // configured. // By default the policy contains not warm, cold, or delete phase. // The index is configured to rollover every 50GB or after 30d. -var DefaultPolicy = common.MapStr{ - "policy": common.MapStr{ - "phases": common.MapStr{ - "hot": common.MapStr{ - "actions": common.MapStr{ - "rollover": common.MapStr{ +var DefaultPolicy = mapstr.M{ + "policy": mapstr.M{ + "phases": mapstr.M{ + "hot": mapstr.M{ + "actions": mapstr.M{ + "rollover": mapstr.M{ "max_size": "50gb", "max_age": "30d", }, diff --git a/libbeat/idxmgmt/ilm/ilm.go b/libbeat/idxmgmt/ilm/ilm.go index 30d4e4f888a..d4c6dc6e02c 100644 --- a/libbeat/idxmgmt/ilm/ilm.go +++ b/libbeat/idxmgmt/ilm/ilm.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fmtstr" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // SupportFactory is used to define a policy type to be used. @@ -61,7 +62,7 @@ type Manager interface { // See: [Policy phases and actions documentation](https://www.elastic.co/guide/en/elasticsearch/reference/master/ilm-policy-definition.html). type Policy struct { Name string - Body common.MapStr + Body mapstr.M } // DefaultSupport configures a new default ILM support implementation. diff --git a/libbeat/idxmgmt/ilm/ilm_test.go b/libbeat/idxmgmt/ilm/ilm_test.go index d6e33c2a892..6136547a491 100644 --- a/libbeat/idxmgmt/ilm/ilm_test.go +++ b/libbeat/idxmgmt/ilm/ilm_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDefaultSupport_Init(t *testing.T) { @@ -47,7 +48,7 @@ func TestDefaultSupport_Init(t *testing.T) { assert.Equal(true, s.overwrite) assert.Equal(false, s.checkExists) assert.Equal(true, s.Enabled()) - assert.Equal(DefaultPolicy, common.MapStr(s.Policy().Body)) + assert.Equal(DefaultPolicy, mapstr.M(s.Policy().Body)) }) t.Run("with custom alias config with fieldref", func(t *testing.T) { @@ -65,7 +66,7 @@ func TestDefaultSupport_Init(t *testing.T) { assert.Equal(true, s.overwrite) assert.Equal(false, s.checkExists) assert.Equal(true, s.Enabled()) - assert.Equal(DefaultPolicy, common.MapStr(s.Policy().Body)) + assert.Equal(DefaultPolicy, mapstr.M(s.Policy().Body)) }) t.Run("with default alias", func(t *testing.T) { @@ -84,15 +85,15 @@ func TestDefaultSupport_Init(t *testing.T) { assert.Equal(true, s.overwrite) assert.Equal(false, s.checkExists) assert.Equal(true, s.Enabled()) - assert.Equal(DefaultPolicy, common.MapStr(s.Policy().Body)) + assert.Equal(DefaultPolicy, mapstr.M(s.Policy().Body)) }) t.Run("load external policy", func(t *testing.T) { s, err := DefaultSupport(nil, info, common.MustNewConfigFrom( - common.MapStr{"policy_file": "testfiles/custom.json"}, + mapstr.M{"policy_file": "testfiles/custom.json"}, )) require.NoError(t, err) - assert.Equal(t, common.MapStr{"hello": "world"}, s.Policy().Body) + assert.Equal(t, mapstr.M{"hello": "world"}, s.Policy().Body) }) } diff --git a/libbeat/idxmgmt/std_test.go b/libbeat/idxmgmt/std_test.go index 8859204be91..a2de484982d 100644 --- a/libbeat/idxmgmt/std_test.go +++ b/libbeat/idxmgmt/std_test.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/idxmgmt/ilm" "github.com/elastic/beats/v7/libbeat/mapping" "github.com/elastic/beats/v7/libbeat/template" + "github.com/elastic/elastic-agent-libs/mapstr" ) type mockClientHandler struct { @@ -113,7 +114,7 @@ func TestDefaultSupport_BuildSelector(t *testing.T) { imCfg map[string]interface{} cfg map[string]interface{} want nameFunc - meta common.MapStr + meta mapstr.M }{ "without ilm": { ilmCalls: noILM, @@ -129,7 +130,7 @@ func TestDefaultSupport_BuildSelector(t *testing.T) { ilmCalls: noILM, cfg: map[string]interface{}{"index": "test-%{[agent.version]}"}, want: stable("test"), - meta: common.MapStr{ + meta: mapstr.M{ "index": "test", }, }, @@ -137,7 +138,7 @@ func TestDefaultSupport_BuildSelector(t *testing.T) { ilmCalls: noILM, cfg: map[string]interface{}{"index": "test-%{[agent.version]}"}, want: stable("test"), - meta: common.MapStr{ + meta: mapstr.M{ "index": "Test", }, }, @@ -155,7 +156,7 @@ func TestDefaultSupport_BuildSelector(t *testing.T) { ilmCalls: ilmTemplateSettings("test-9.9.9"), cfg: map[string]interface{}{"index": "test-%{[agent.version]}"}, want: stable("event-index"), - meta: common.MapStr{ + meta: mapstr.M{ "index": "event-index", }, }, @@ -195,9 +196,9 @@ func TestDefaultSupport_BuildSelector(t *testing.T) { meta := test.meta idx, err := sel.Select(&beat.Event{ Timestamp: ts, - Fields: common.MapStr{ + Fields: mapstr.M{ "test": "value", - "agent": common.MapStr{ + "agent": mapstr.M{ "version": "9.9.9", }, }, @@ -252,7 +253,7 @@ func TestIndexManager_VerifySetup(t *testing.T) { }, } { t.Run(name, func(t *testing.T) { - cfg, err := common.NewConfigFrom(common.MapStr{ + cfg, err := common.NewConfigFrom(mapstr.M{ "setup.ilm.enabled": setup.ilmEnabled, "setup.ilm.overwrite": setup.ilmOverwrite, "setup.template.enabled": setup.tmplEnabled, @@ -279,10 +280,10 @@ func TestIndexManager_Setup(t *testing.T) { } if c.Settings.Index != nil { - c.Settings.Index = (map[string]interface{})(common.MapStr(c.Settings.Index).Clone()) + c.Settings.Index = (map[string]interface{})(mapstr.M(c.Settings.Index).Clone()) } if c.Settings.Source != nil { - c.Settings.Source = (map[string]interface{})(common.MapStr(c.Settings.Source).Clone()) + c.Settings.Source = (map[string]interface{})(mapstr.M(c.Settings.Source).Clone()) } return c } @@ -308,7 +309,7 @@ func TestIndexManager_Setup(t *testing.T) { defaultCfg := template.DefaultConfig(info) cases := map[string]struct { - cfg common.MapStr + cfg mapstr.M loadTemplate, loadILM LoadMode err bool @@ -325,7 +326,7 @@ func TestIndexManager_Setup(t *testing.T) { policy: "test", }, "template default ilm default with policy changed": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.ilm.policy_name": "policy-keep", }, tmplCfg: cfgWith(template.DefaultConfig(info), map[string]interface{}{ @@ -337,14 +338,14 @@ func TestIndexManager_Setup(t *testing.T) { policy: "policy-keep", }, "template default ilm disabled": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.ilm.enabled": false, }, loadTemplate: LoadModeEnabled, tmplCfg: &defaultCfg, }, "template default loadMode Overwrite ilm disabled": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.ilm.enabled": false, }, loadTemplate: LoadModeOverwrite, @@ -355,7 +356,7 @@ func TestIndexManager_Setup(t *testing.T) { }), }, "template default loadMode Force ilm disabled": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.ilm.enabled": false, "name": "test-9.9.9", "pattern": "test-9.9.9", @@ -366,26 +367,26 @@ func TestIndexManager_Setup(t *testing.T) { }), }, "template loadMode disabled ilm disabled": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.ilm.enabled": false, }, loadTemplate: LoadModeDisabled, }, "template disabled ilm default": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.template.enabled": false, }, policy: "test", }, "template disabled ilm disabled, loadMode Overwrite": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.template.enabled": false, "setup.ilm.enabled": false, }, loadILM: LoadModeOverwrite, }, "template disabled ilm disabled loadMode Force": { - cfg: common.MapStr{ + cfg: mapstr.M{ "setup.template.enabled": false, "setup.ilm.enabled": false, }, diff --git a/libbeat/kibana/dashboard.go b/libbeat/kibana/dashboard.go index 1774fb1715f..62d3983b214 100644 --- a/libbeat/kibana/dashboard.go +++ b/libbeat/kibana/dashboard.go @@ -24,7 +24,7 @@ import ( "fmt" "io" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // RemoveIndexPattern removes the index pattern entry from a given dashboard export @@ -57,7 +57,7 @@ func removeLineIfIndexPattern(line []byte) ([]byte, error) { return line, nil } - var r common.MapStr + var r mapstr.M // Full struct need to not loose any data err := json.Unmarshal(line, &r) if err != nil { diff --git a/libbeat/kibana/fields_transformer.go b/libbeat/kibana/fields_transformer.go index 7b3db3103d9..2eb01de87dc 100644 --- a/libbeat/kibana/fields_transformer.go +++ b/libbeat/kibana/fields_transformer.go @@ -23,14 +23,15 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mapping" + "github.com/elastic/elastic-agent-libs/mapstr" ) var v640 = common.MustNewVersion("6.4.0") type fieldsTransformer struct { fields mapping.Fields - transformedFields []common.MapStr - transformedFieldFormatMap common.MapStr + transformedFields []mapstr.M + transformedFieldFormatMap mapstr.M version *common.Version keys map[string]int migration bool @@ -43,14 +44,14 @@ func newFieldsTransformer(version *common.Version, fields mapping.Fields, migrat return &fieldsTransformer{ fields: fields, version: version, - transformedFields: []common.MapStr{}, - transformedFieldFormatMap: common.MapStr{}, + transformedFields: []mapstr.M{}, + transformedFieldFormatMap: mapstr.M{}, keys: map[string]int{}, migration: migration, }, nil } -func (t *fieldsTransformer) transform() (transformed common.MapStr, err error) { +func (t *fieldsTransformer) transform() (transformed mapstr.M, err error) { defer func() { if r := recover(); r != nil { var ok bool @@ -70,7 +71,7 @@ func (t *fieldsTransformer) transform() (transformed common.MapStr, err error) { t.add(mapping.Field{Path: "_index", Type: "keyword", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy}) t.add(mapping.Field{Path: "_score", Type: "integer", Index: &falsy, Analyzed: &falsy, DocValues: &falsy, Searchable: &falsy, Aggregatable: &falsy}) - transformed = common.MapStr{ + transformed = mapstr.M{ "fields": t.transformedFields, "fieldFormatMap": t.transformedFieldFormatMap, } @@ -120,7 +121,7 @@ func (t *fieldsTransformer) transformFields(commonFields mapping.Fields, path st } } -func (t *fieldsTransformer) update(target *common.MapStr, override mapping.Field) error { +func (t *fieldsTransformer) update(target *mapstr.M, override mapping.Field) error { field, _ := transformField(t.version, override) if override.Type == "" || (*target)["type"] == field["type"] { target.Update(field) @@ -151,8 +152,8 @@ func (t *fieldsTransformer) add(f mapping.Field) { } } -func transformField(version *common.Version, f mapping.Field) (common.MapStr, common.MapStr) { - field := common.MapStr{ +func transformField(version *common.Version, f mapping.Field) (mapstr.M, mapstr.M) { + field := mapstr.M{ "name": f.Path, "count": f.Count, "scripted": false, @@ -198,9 +199,9 @@ func transformField(version *common.Version, f mapping.Field) (common.MapStr, co field["doc_values"] = false } - var format common.MapStr + var format mapstr.M if f.Format != "" || f.Pattern != "" { - format = common.MapStr{} + format = mapstr.M{} if f.Format != "" { format["id"] = f.Format @@ -218,7 +219,7 @@ func getVal(valP *bool, def bool) bool { return def } -func addParams(format *common.MapStr, version *common.Version, f mapping.Field) { +func addParams(format *mapstr.M, version *common.Version, f mapping.Field) { addFormatParam(format, "pattern", f.Pattern) addFormatParam(format, "inputFormat", f.InputFormat) addFormatParam(format, "outputFormat", f.OutputFormat) @@ -228,28 +229,28 @@ func addParams(format *common.MapStr, version *common.Version, f mapping.Field) addVersionedFormatParam(format, version, "urlTemplate", f.UrlTemplate) } -func addFormatParam(f *common.MapStr, key string, val interface{}) { +func addFormatParam(f *mapstr.M, key string, val interface{}) { switch val.(type) { case string: if v := val.(string); v != "" { createParam(f) - (*f)["params"].(common.MapStr)[key] = v + (*f)["params"].(mapstr.M)[key] = v } case *int: if v := val.(*int); v != nil { createParam(f) - (*f)["params"].(common.MapStr)[key] = *v + (*f)["params"].(mapstr.M)[key] = *v } case *bool: if v := val.(*bool); v != nil { createParam(f) - (*f)["params"].(common.MapStr)[key] = *v + (*f)["params"].(mapstr.M)[key] = *v } } } // takes the highest version where major version <= given version -func addVersionedFormatParam(f *common.MapStr, version *common.Version, key string, val []mapping.VersionizedString) { +func addVersionedFormatParam(f *mapstr.M, version *common.Version, key string, val []mapping.VersionizedString) { if len(val) == 0 { return } @@ -271,9 +272,9 @@ func addVersionedFormatParam(f *common.MapStr, version *common.Version, key stri } } -func createParam(f *common.MapStr) { +func createParam(f *mapstr.M) { if (*f)["params"] == nil { - (*f)["params"] = common.MapStr{} + (*f)["params"] = mapstr.M{} } } diff --git a/libbeat/kibana/fields_transformer_test.go b/libbeat/kibana/fields_transformer_test.go index a7c34385445..33f378ad941 100644 --- a/libbeat/kibana/fields_transformer_test.go +++ b/libbeat/kibana/fields_transformer_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mapping" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -40,10 +41,10 @@ func TestEmpty(t *testing.T) { assert.NoError(t, err) out, err := trans.transform() assert.NoError(t, err) - expected := common.MapStr{ - "fieldFormatMap": common.MapStr{}, - "fields": []common.MapStr{ - common.MapStr{ + expected := mapstr.M{ + "fieldFormatMap": mapstr.M{}, + "fields": []mapstr.M{ + mapstr.M{ "name": "_id", "type": "string", "scripted": false, @@ -54,7 +55,7 @@ func TestEmpty(t *testing.T) { "doc_values": false, "searchable": false, }, - common.MapStr{ + mapstr.M{ "name": "_type", "type": "string", "scripted": false, @@ -65,7 +66,7 @@ func TestEmpty(t *testing.T) { "doc_values": false, "searchable": true, }, - common.MapStr{ + mapstr.M{ "name": "_index", "type": "string", "scripted": false, @@ -76,7 +77,7 @@ func TestEmpty(t *testing.T) { "doc_values": false, "searchable": false, }, - common.MapStr{ + mapstr.M{ "name": "_score", "type": "number", "scripted": false, @@ -157,8 +158,8 @@ func TestValidDuplicateField(t *testing.T) { require.NoError(t, err) transformed, err := trans.transform() require.NoError(t, err) - out := transformed["fields"].([]common.MapStr)[0] - assert.Equal(t, out, common.MapStr{ + out := transformed["fields"].([]mapstr.M)[0] + assert.Equal(t, out, mapstr.M{ "aggregatable": false, "analyzed": true, "count": 2, @@ -213,7 +214,7 @@ func TestTransformTypes(t *testing.T) { trans, _ := newFieldsTransformer(version, mapping.Fields{test.commonField}, true) transformed, err := trans.transform() assert.NoError(t, err) - out := transformed["fields"].([]common.MapStr)[0] + out := transformed["fields"].([]mapstr.M)[0] assert.Equal(t, test.expected, out["type"], fmt.Sprintf("Failed for idx %v", idx)) } } @@ -258,7 +259,7 @@ func TestTransformGroup(t *testing.T) { trans, _ := newFieldsTransformer(version, test.commonFields, false) transformed, err := trans.transform() assert.NoError(t, err) - out := transformed["fields"].([]common.MapStr) + out := transformed["fields"].([]mapstr.M) assert.Equal(t, len(test.expected)+ctMetaData, len(out)) for i, e := range test.expected { assert.Equal(t, e, out[i]["name"], fmt.Sprintf("Failed for idx %v", idx)) @@ -334,7 +335,7 @@ func TestTransformMisc(t *testing.T) { trans, _ := newFieldsTransformer(version, mapping.Fields{test.commonField}, true) transformed, err := trans.transform() assert.NoError(t, err) - out := transformed["fields"].([]common.MapStr)[0] + out := transformed["fields"].([]mapstr.M)[0] msg := fmt.Sprintf("(%v): expected '%s' to be <%v> but was <%v>", idx, test.attr, test.expected, out[test.attr]) assert.Equal(t, test.expected, out[test.attr], msg) } @@ -349,21 +350,21 @@ func TestTransformFieldFormatMap(t *testing.T) { tests := []struct { commonField mapping.Field version *common.Version - expected common.MapStr + expected mapstr.M }{ { commonField: mapping.Field{Name: "c"}, - expected: common.MapStr{}, + expected: mapstr.M{}, version: version, }, { commonField: mapping.Field{Name: "c", Format: "url"}, - expected: common.MapStr{"c": common.MapStr{"id": "url"}}, + expected: mapstr.M{"c": mapstr.M{"id": "url"}}, version: version, }, { commonField: mapping.Field{Name: "c", Pattern: "p"}, - expected: common.MapStr{"c": common.MapStr{"params": common.MapStr{"pattern": "p"}}}, + expected: mapstr.M{"c": mapstr.M{"params": mapstr.M{"pattern": "p"}}}, version: version, }, { @@ -372,10 +373,10 @@ func TestTransformFieldFormatMap(t *testing.T) { Format: "url", Pattern: "p", }, - expected: common.MapStr{ - "c": common.MapStr{ + expected: mapstr.M{ + "c": mapstr.M{ "id": "url", - "params": common.MapStr{"pattern": "p"}, + "params": mapstr.M{"pattern": "p"}, }, }, version: version, @@ -386,10 +387,10 @@ func TestTransformFieldFormatMap(t *testing.T) { Format: "url", InputFormat: "string", }, - expected: common.MapStr{ - "c": common.MapStr{ + expected: mapstr.M{ + "c": mapstr.M{ "id": "url", - "params": common.MapStr{ + "params": mapstr.M{ "inputFormat": "string", }, }, @@ -404,10 +405,10 @@ func TestTransformFieldFormatMap(t *testing.T) { InputFormat: "string", OpenLinkInCurrentTab: &falsy, }, - expected: common.MapStr{ - "c": common.MapStr{ + expected: mapstr.M{ + "c": mapstr.M{ "id": "url", - "params": common.MapStr{ + "params": mapstr.M{ "pattern": "[^-]", "inputFormat": "string", "openLinkInCurrentTab": false, @@ -421,7 +422,7 @@ func TestTransformFieldFormatMap(t *testing.T) { Name: "c", InputFormat: "string", }, - expected: common.MapStr{}, + expected: mapstr.M{}, version: version, }, { @@ -440,10 +441,10 @@ func TestTransformFieldFormatMap(t *testing.T) { {MinVersion: "6.0.0", Value: "6x.urlTemplate"}, }, }, - expected: common.MapStr{ - "c": common.MapStr{ + expected: mapstr.M{ + "c": mapstr.M{ "id": "url", - "params": common.MapStr{ + "params": mapstr.M{ "pattern": "[^-]", "inputFormat": "string", "outputFormat": "float", @@ -464,8 +465,8 @@ func TestTransformFieldFormatMap(t *testing.T) { {MinVersion: "6.4.0", Value: "6x.urlTemplate"}, }, }, - expected: common.MapStr{ - "c": common.MapStr{"id": "url"}, + expected: mapstr.M{ + "c": mapstr.M{"id": "url"}, }, }, { @@ -478,10 +479,10 @@ func TestTransformFieldFormatMap(t *testing.T) { {MinVersion: "6.5.1", Value: "6x.urlTemplate"}, }, }, - expected: common.MapStr{ - "c": common.MapStr{ + expected: mapstr.M{ + "c": mapstr.M{ "id": "url", - "params": common.MapStr{ + "params": mapstr.M{ "urlTemplate": "4x.urlTemplate", }, }, @@ -498,10 +499,10 @@ func TestTransformFieldFormatMap(t *testing.T) { {MinVersion: "6.2.7", Value: "6.2.7.urlTemplate"}, }, }, - expected: common.MapStr{ - "c": common.MapStr{ + expected: mapstr.M{ + "c": mapstr.M{ "id": "url", - "params": common.MapStr{ + "params": mapstr.M{ "urlTemplate": "6.2.0.urlTemplate", }, }, @@ -519,10 +520,10 @@ func TestTransformFieldFormatMap(t *testing.T) { {MinVersion: "5.2.0-rc1", Value: "5.2.0-rc1.urlTemplate"}, }, }, - expected: common.MapStr{ - "c": common.MapStr{ + expected: mapstr.M{ + "c": mapstr.M{ "id": "url", - "params": common.MapStr{ + "params": mapstr.M{ "urlTemplate": "5.2.0-rc3.urlTemplate", }, }, @@ -601,7 +602,7 @@ func TestTransformGroupAndEnabled(t *testing.T) { trans, _ := newFieldsTransformer(version, test.commonFields, true) transformed, err := trans.transform() assert.NoError(t, err) - out := transformed["fields"].([]common.MapStr) + out := transformed["fields"].([]mapstr.M) assert.Equal(t, len(test.expected)+ctMetaData, len(out)) for i, e := range test.expected { assert.Equal(t, e, out[i]["name"], fmt.Sprintf("Failed for idx %v", idx)) @@ -621,7 +622,7 @@ func TestTransformMultiField(t *testing.T) { trans, _ := newFieldsTransformer(version, mapping.Fields{f}, true) transformed, err := trans.transform() assert.NoError(t, err) - out := transformed["fields"].([]common.MapStr) + out := transformed["fields"].([]mapstr.M) assert.Equal(t, "context", out[0]["name"]) assert.Equal(t, "context.keyword", out[1]["name"]) assert.Equal(t, "context.text", out[2]["name"]) diff --git a/libbeat/kibana/index_pattern_generator.go b/libbeat/kibana/index_pattern_generator.go index 6ec95d8b886..1d509df9030 100644 --- a/libbeat/kibana/index_pattern_generator.go +++ b/libbeat/kibana/index_pattern_generator.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mapping" + "github.com/elastic/elastic-agent-libs/mapstr" ) type IndexPatternGenerator struct { @@ -50,7 +51,7 @@ func NewGenerator(indexName, beatName string, fields []byte, beatVersion string, } // Generate creates the Index-Pattern for Kibana. -func (i *IndexPatternGenerator) Generate() (common.MapStr, error) { +func (i *IndexPatternGenerator) Generate() (mapstr.M, error) { idxPattern, err := i.generate() if err != nil { return nil, err @@ -59,8 +60,8 @@ func (i *IndexPatternGenerator) Generate() (common.MapStr, error) { return i.generatePattern(idxPattern), nil } -func (i *IndexPatternGenerator) generate() (common.MapStr, error) { - indexPattern := common.MapStr{ +func (i *IndexPatternGenerator) generate() (mapstr.M, error) { + indexPattern := mapstr.M{ "timeFieldName": "@timestamp", "title": i.indexName, } @@ -78,8 +79,8 @@ func (i *IndexPatternGenerator) generate() (common.MapStr, error) { return indexPattern, nil } -func (i *IndexPatternGenerator) generatePattern(attrs common.MapStr) common.MapStr { - out := common.MapStr{ +func (i *IndexPatternGenerator) generatePattern(attrs mapstr.M) mapstr.M { + out := mapstr.M{ "type": "index-pattern", "id": i.indexName, "version": i.beatVersion, @@ -89,13 +90,13 @@ func (i *IndexPatternGenerator) generatePattern(attrs common.MapStr) common.MapS return out } -func (i *IndexPatternGenerator) addGeneral(indexPattern *common.MapStr) error { +func (i *IndexPatternGenerator) addGeneral(indexPattern *mapstr.M) error { kibanaEntries, err := loadKibanaEntriesFromYaml(i.fields) if err != nil { return err } transformed := newTransformer(kibanaEntries).transform() - if srcFilters, ok := transformed["sourceFilters"].([]common.MapStr); ok { + if srcFilters, ok := transformed["sourceFilters"].([]mapstr.M); ok { sourceFiltersBytes, err := json.Marshal(srcFilters) if err != nil { return err @@ -105,7 +106,7 @@ func (i *IndexPatternGenerator) addGeneral(indexPattern *common.MapStr) error { return nil } -func (i *IndexPatternGenerator) addFieldsSpecific(indexPattern *common.MapStr) error { +func (i *IndexPatternGenerator) addFieldsSpecific(indexPattern *mapstr.M) error { fields, err := mapping.LoadFields(i.fields) if err != nil { return err @@ -138,7 +139,7 @@ func clean(name string) string { return reg.ReplaceAllString(name, "") } -func dumpToFile(f string, pattern common.MapStr) error { +func dumpToFile(f string, pattern mapstr.M) error { patternIndent, err := json.MarshalIndent(pattern, "", " ") if err != nil { return err diff --git a/libbeat/kibana/index_pattern_generator_test.go b/libbeat/kibana/index_pattern_generator_test.go index 130580b9b74..e4863a0ec4d 100644 --- a/libbeat/kibana/index_pattern_generator_test.go +++ b/libbeat/kibana/index_pattern_generator_test.go @@ -29,6 +29,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -77,7 +78,7 @@ func TestGenerate(t *testing.T) { v7, _ := common.NewVersion("7.0.0-alpha1") versions := []*common.Version{v7} - var d common.MapStr + var d mapstr.M for _, version := range versions { data, err := ioutil.ReadFile("./testdata/fields.yml") if err != nil { @@ -106,7 +107,7 @@ func TestGenerate(t *testing.T) { type compare struct { existing string - created common.MapStr + created mapstr.M } func TestGenerateExtensive(t *testing.T) { @@ -116,7 +117,7 @@ func TestGenerateExtensive(t *testing.T) { version7, _ := common.NewVersion("7.0.0-alpha1") versions := []*common.Version{version7} - var d common.MapStr + var d mapstr.M for _, version := range versions { data, err := ioutil.ReadFile("testdata/extensive/fields.yml") if err != nil { @@ -151,7 +152,7 @@ func testGenerate(t *testing.T, tests []compare, sourceFilters bool) { } for _, ex := range existing { - var attrExisting, attrCreated common.MapStr + var attrExisting, attrCreated mapstr.M if strings.Contains(test.existing, "6") { assert.Equal(t, ex["version"], test.created["version"]) @@ -159,7 +160,7 @@ func testGenerate(t *testing.T, tests []compare, sourceFilters bool) { assert.Equal(t, ex["type"], test.created["type"]) attrExisting = ex["attributes"].(map[string]interface{}) - attrCreated = test.created["attributes"].(common.MapStr) + attrCreated = test.created["attributes"].(mapstr.M) } else { attrExisting = ex attrCreated = test.created diff --git a/libbeat/kibana/transformer.go b/libbeat/kibana/transformer.go index 6e2b086e28b..4c500b88eb4 100644 --- a/libbeat/kibana/transformer.go +++ b/libbeat/kibana/transformer.go @@ -18,7 +18,7 @@ package kibana import ( - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg/yaml" ) @@ -35,13 +35,13 @@ func newTransformer(entries []kibanaEntry) *transformer { return &transformer{entries: entries} } -func (t *transformer) transform() common.MapStr { - transformed := common.MapStr{} +func (t *transformer) transform() mapstr.M { + transformed := mapstr.M{} - var srcFilters []common.MapStr + var srcFilters []mapstr.M for _, entry := range t.entries { for _, sourceFilter := range entry.Kibana.SourceFilters { - srcFilters = append(srcFilters, common.MapStr{"value": sourceFilter}) + srcFilters = append(srcFilters, mapstr.M{"value": sourceFilter}) } } if len(srcFilters) > 0 { diff --git a/libbeat/mapping/field.go b/libbeat/mapping/field.go index 9ac27d86ace..7615c5c39e6 100644 --- a/libbeat/mapping/field.go +++ b/libbeat/mapping/field.go @@ -24,7 +24,7 @@ import ( "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg/yaml" ) @@ -132,15 +132,15 @@ type Analyzer struct { } func (a *Analyzer) Unpack(v interface{}) error { - var m common.MapStr + var m mapstr.M switch v := v.(type) { case string: a.Name = v return nil - case common.MapStr: + case mapstr.M: m = v case map[string]interface{}: - m = common.MapStr(v) + m = mapstr.M(v) default: return fmt.Errorf("'%v' is invalid analyzer setting", v) } diff --git a/libbeat/mapping/field_test.go b/libbeat/mapping/field_test.go index 6d69787a445..6ca66eb0bed 100644 --- a/libbeat/mapping/field_test.go +++ b/libbeat/mapping/field_test.go @@ -27,6 +27,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg/yaml" ) @@ -342,65 +343,65 @@ func TestGetField(t *testing.T) { func TestFieldValidate(t *testing.T) { tests := map[string]struct { - cfg common.MapStr + cfg mapstr.M field Field err bool }{ "top level object type config": { - cfg: common.MapStr{"object_type": "scaled_float", "object_type_mapping_type": "float", "scaling_factor": 10}, + cfg: mapstr.M{"object_type": "scaled_float", "object_type_mapping_type": "float", "scaling_factor": 10}, field: Field{ObjectType: "scaled_float", ObjectTypeMappingType: "float", ScalingFactor: 10}, err: false, }, "multiple object type configs": { - cfg: common.MapStr{"object_type_params": []common.MapStr{ + cfg: mapstr.M{"object_type_params": []mapstr.M{ {"object_type": "scaled_float", "object_type_mapping_type": "float", "scaling_factor": 100}}}, field: Field{ObjectTypeParams: []ObjectTypeCfg{{ObjectType: "scaled_float", ObjectTypeMappingType: "float", ScalingFactor: 100}}}, err: false, }, "invalid config mixing object_type and object_type_params": { - cfg: common.MapStr{ + cfg: mapstr.M{ "object_type": "scaled_float", - "object_type_params": []common.MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}}, + "object_type_params": []mapstr.M{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}}, err: true, }, "invalid config mixing object_type_mapping_type and object_type_params": { - cfg: common.MapStr{ + cfg: mapstr.M{ "object_type_mapping_type": "float", - "object_type_params": []common.MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}}, + "object_type_params": []mapstr.M{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}}, err: true, }, "invalid config mixing scaling_factor and object_type_params": { - cfg: common.MapStr{ + cfg: mapstr.M{ "scaling_factor": 100, - "object_type_params": []common.MapStr{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}}, + "object_type_params": []mapstr.M{{"object_type": "scaled_float", "object_type_mapping_type": "float"}}}, err: true, }, "valid unit": { - cfg: common.MapStr{"type": "long", "unit": "nanos"}, + cfg: mapstr.M{"type": "long", "unit": "nanos"}, err: false, field: Field{Type: "long", Unit: "nanos"}, }, "invalid unit": { - cfg: common.MapStr{"type": "long", "unit": "celsius"}, + cfg: mapstr.M{"type": "long", "unit": "celsius"}, err: true, }, "valid metric type": { - cfg: common.MapStr{"type": "long", "metric_type": "gauge"}, + cfg: mapstr.M{"type": "long", "metric_type": "gauge"}, err: false, field: Field{Type: "long", MetricType: "gauge"}, }, "invalid metric type": { - cfg: common.MapStr{"type": "long", "metric_type": "timer"}, + cfg: mapstr.M{"type": "long", "metric_type": "timer"}, err: true, }, "invalid config mixing dynamic_template with object_type": { - cfg: common.MapStr{"dynamic_template": true, "type": "object", "object_type": "text"}, + cfg: mapstr.M{"dynamic_template": true, "type": "object", "object_type": "text"}, err: true, }, "invalid config mixing dynamic_template with object_type_params": { - cfg: common.MapStr{ + cfg: mapstr.M{ "type": "object", - "object_type_params": []common.MapStr{{ + "object_type_params": []mapstr.M{{ "object_type": "scaled_float", "object_type_mapping_type": "float", "scaling_factor": 100, }}, "dynamic_template": true, @@ -408,7 +409,7 @@ func TestFieldValidate(t *testing.T) { err: true, }, "allow ip_range": { - cfg: common.MapStr{"type": "ip_range"}, + cfg: mapstr.M{"type": "ip_range"}, err: false, field: Field{Type: "ip_range"}, }, diff --git a/libbeat/metric/system/cgroup/cgstats.go b/libbeat/metric/system/cgroup/cgstats.go index 0fecf06232c..cbff8e3da46 100644 --- a/libbeat/metric/system/cgroup/cgstats.go +++ b/libbeat/metric/system/cgroup/cgstats.go @@ -26,11 +26,12 @@ import ( "github.com/elastic/beats/v7/libbeat/common/transform/typeconv" "github.com/elastic/beats/v7/libbeat/metric/system/numcpu" "github.com/elastic/beats/v7/libbeat/opt" + "github.com/elastic/elastic-agent-libs/mapstr" ) // CGStats in an interface wrapper around the V2 and V1 cgroup stat objects type CGStats interface { - Format() (common.MapStr, error) + Format() (mapstr.M, error) CGVersion() CgroupsVersion FillPercentages(prev CGStats, curTime, prevTime time.Time) } @@ -41,8 +42,8 @@ func (stat StatsV1) CGVersion() CgroupsVersion { } //Format converts the stats object to a MapStr that can be sent to Report() -func (stat StatsV1) Format() (common.MapStr, error) { - to := common.MapStr{} +func (stat StatsV1) Format() (mapstr.M, error) { + to := mapstr.M{} err := typeconv.Convert(&to, stat) if err != nil { return to, errors.Wrap(err, "error formatting statsV1 object") @@ -97,8 +98,8 @@ func (curStat *StatsV1) FillPercentages(prev CGStats, curTime, prevTime time.Tim } //Format converts the stats object to a MapStr that can be sent to Report() -func (stat StatsV2) Format() (common.MapStr, error) { - to := common.MapStr{} +func (stat StatsV2) Format() (mapstr.M, error) { + to := mapstr.M{} err := typeconv.Convert(&to, stat) if err != nil { return to, errors.Wrap(err, "error formatting statsV2 object") diff --git a/libbeat/metric/system/diskio/diskstat_windows_test.go b/libbeat/metric/system/diskio/diskstat_windows_test.go index 6d363ea5883..15b347aff39 100644 --- a/libbeat/metric/system/diskio/diskstat_windows_test.go +++ b/libbeat/metric/system/diskio/diskstat_windows_test.go @@ -23,11 +23,10 @@ package diskio import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/stretchr/testify/assert" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestCDriveFilterOnWindowsTestEnv(t *testing.T) { @@ -45,8 +44,8 @@ func TestCDriveFilterOnWindowsTestEnv(t *testing.T) { assert.Empty(t, errs) assert.Equal(t, 1, len(data)) assert.Equal(t, data[0].MetricSetFields["name"], "C:") - reads := data[0].MetricSetFields["read"].(common.MapStr) - writes := data[0].MetricSetFields["write"].(common.MapStr) + reads := data[0].MetricSetFields["read"].(mapstr.M) + writes := data[0].MetricSetFields["write"].(mapstr.M) // Check values readCount := reads["count"].(uint64) readBytes := reads["bytes"].(uint64) diff --git a/libbeat/metric/system/host/host.go b/libbeat/metric/system/host/host.go index 6f5c9c15849..067c74be2b8 100644 --- a/libbeat/metric/system/host/host.go +++ b/libbeat/metric/system/host/host.go @@ -18,19 +18,19 @@ package host import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo" "github.com/elastic/go-sysinfo/types" ) // MapHostInfo converts the HostInfo to a MapStr based on ECS. -func MapHostInfo(info types.HostInfo) common.MapStr { - data := common.MapStr{ - "host": common.MapStr{ +func MapHostInfo(info types.HostInfo) mapstr.M { + data := mapstr.M{ + "host": mapstr.M{ "hostname": info.Hostname, "architecture": info.Architecture, - "os": common.MapStr{ + "os": mapstr.M{ "platform": info.OS.Platform, "version": info.OS.Version, "family": info.OS.Family, diff --git a/libbeat/metric/system/network/helpers.go b/libbeat/metric/system/network/helpers.go index e13ad0e2ad7..652ce05855a 100644 --- a/libbeat/metric/system/network/helpers.go +++ b/libbeat/metric/system/network/helpers.go @@ -18,14 +18,14 @@ package network import ( - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" sysinfotypes "github.com/elastic/go-sysinfo/types" ) // MapProcNetCounters converts the NetworkCountersInfo struct into a MapStr acceptable for sending upstream -func MapProcNetCounters(raw *sysinfotypes.NetworkCountersInfo) common.MapStr { +func MapProcNetCounters(raw *sysinfotypes.NetworkCountersInfo) mapstr.M { - eventByProto := common.MapStr{ + eventByProto := mapstr.M{ "ip": combineMap(raw.Netstat.IPExt, raw.SNMP.IP), "tcp": combineMap(raw.Netstat.TCPExt, raw.SNMP.TCP), "udp": raw.SNMP.UDP, diff --git a/libbeat/metric/system/process/process.go b/libbeat/metric/system/process/process.go index bb268497abd..3adc2735c99 100644 --- a/libbeat/metric/system/process/process.go +++ b/libbeat/metric/system/process/process.go @@ -29,9 +29,9 @@ import ( "github.com/pkg/errors" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo/types" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/match" "github.com/elastic/beats/v7/libbeat/common/transform/typeconv" "github.com/elastic/beats/v7/libbeat/logp" @@ -193,7 +193,7 @@ func (procStats *Stats) Init() error { } // Get fetches the configured processes and returns a list of formatted events and root ECS fields -func (procStats *Stats) Get() ([]common.MapStr, []common.MapStr, error) { +func (procStats *Stats) Get() ([]mapstr.M, []mapstr.M, error) { //If the user hasn't configured any kind of process glob, return if len(procStats.Procs) == 0 { return nil, nil, nil @@ -225,15 +225,15 @@ func (procStats *Stats) Get() ([]common.MapStr, []common.MapStr, error) { } //Format the list to the MapStr type used by the outputs - procs := []common.MapStr{} - rootEvents := []common.MapStr{} + procs := []mapstr.M{} + rootEvents := []mapstr.M{} for _, process := range plist { // Add the RSS pct memory first process.Memory.Rss.Pct = GetProcMemPercentage(process, totalPhyMem) //Create the root event root := process.FormatForRoot() - rootMap := common.MapStr{} + rootMap := mapstr.M{} err := typeconv.Convert(&rootMap, root) proc, err := procStats.getProcessEvent(&process) @@ -249,7 +249,7 @@ func (procStats *Stats) Get() ([]common.MapStr, []common.MapStr, error) { } // GetOne fetches process data for a given PID if its name matches the regexes provided from the host. -func (procStats *Stats) GetOne(pid int) (common.MapStr, error) { +func (procStats *Stats) GetOne(pid int) (mapstr.M, error) { pidStat, _, err := procStats.pidFill(pid, false) if err != nil { return nil, errors.Wrapf(err, "error fetching PID %d", pid) @@ -359,7 +359,7 @@ func (procStats *Stats) cacheCmdLine(in ProcState) ProcState { } // return a formatted MapStr of the process metrics -func (procStats *Stats) getProcessEvent(process *ProcState) (common.MapStr, error) { +func (procStats *Stats) getProcessEvent(process *ProcState) (mapstr.M, error) { // Remove CPUTicks if needed if !procStats.CPUTicks { @@ -368,7 +368,7 @@ func (procStats *Stats) getProcessEvent(process *ProcState) (common.MapStr, erro process.CPU.Total.Ticks = opt.NewUintNone() } - proc := common.MapStr{} + proc := mapstr.M{} err := typeconv.Convert(&proc, process) return proc, err diff --git a/libbeat/metric/system/process/process_darwin.go b/libbeat/metric/system/process/process_darwin.go index e54d6549c90..6f05e22b148 100644 --- a/libbeat/metric/system/process/process_darwin.go +++ b/libbeat/metric/system/process/process_darwin.go @@ -42,9 +42,9 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/libbeat/opt" + "github.com/elastic/elastic-agent-libs/mapstr" ) // FetchPids returns a map and array of pids @@ -151,7 +151,7 @@ func FillPidMetrics(_ resolve.Resolver, pid int, state ProcState, filter func(st return state, nil } -func getProcArgs(pid int, filter func(string) bool) ([]string, string, common.MapStr, error) { +func getProcArgs(pid int, filter func(string) bool) ([]string, string, mapstr.M, error) { exeName := "" @@ -203,7 +203,7 @@ func getProcArgs(pid int, filter func(string) bool) ([]string, string, common.Ma delim := []byte{61} // "=" for key value pairs - envVars := common.MapStr{} + envVars := mapstr.M{} for { line, err := bbuf.ReadBytes(0) if err == io.EOF || line[0] == 0 { diff --git a/libbeat/metric/system/process/process_linux_common.go b/libbeat/metric/system/process/process_linux_common.go index 4f11dd3534b..83ce740fc6c 100644 --- a/libbeat/metric/system/process/process_linux_common.go +++ b/libbeat/metric/system/process/process_linux_common.go @@ -33,10 +33,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/libbeat/opt" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Indulging in one non-const global variable for the sake of storing boot time @@ -226,13 +226,13 @@ func getUser(hostfs resolve.Resolver, pid int) (string, error) { return userFinal, nil } -func getEnvData(hostfs resolve.Resolver, pid int, filter func(string) bool) (common.MapStr, error) { +func getEnvData(hostfs resolve.Resolver, pid int, filter func(string) bool) (mapstr.M, error) { path := hostfs.Join("proc", strconv.Itoa(pid), "environ") data, err := ioutil.ReadFile(path) if err != nil { return nil, errors.Wrapf(err, "error opening file %s", path) } - env := common.MapStr{} + env := mapstr.M{} pairs := bytes.Split(data, []byte{0}) for _, kv := range pairs { diff --git a/libbeat/metric/system/process/process_test.go b/libbeat/metric/system/process/process_test.go index f4736ff723a..966413835cf 100644 --- a/libbeat/metric/system/process/process_test.go +++ b/libbeat/metric/system/process/process_test.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/metric/system/cgroup" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/libbeat/opt" + "github.com/elastic/elastic-agent-libs/mapstr" ) // numCPU is the number of CPUs of the host @@ -192,7 +193,7 @@ func BenchmarkGetProcess(b *testing.B) { if err != nil { b.Fatalf("Failed init: %s", err) } - procs := make(map[int]common.MapStr, 1) + procs := make(map[int]mapstr.M, 1) pid := os.Getpid() b.ResetTimer() for i := 0; i < b.N; i++ { @@ -211,7 +212,7 @@ func BenchmarkGetTop(b *testing.B) { if err != nil { b.Fatalf("Failed init: %s", err) } - procs := make(map[int][]common.MapStr) + procs := make(map[int][]mapstr.M) for i := 0; i < b.N; i++ { list, _, err := stat.Get() diff --git a/libbeat/metric/system/process/process_types.go b/libbeat/metric/system/process/process_types.go index c3974f19f43..a9cf9f488b9 100644 --- a/libbeat/metric/system/process/process_types.go +++ b/libbeat/metric/system/process/process_types.go @@ -20,9 +20,9 @@ package process import ( "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/cgroup" "github.com/elastic/beats/v7/libbeat/opt" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ProcState is the main struct for process information and metrics. @@ -36,11 +36,11 @@ type ProcState struct { Pgid opt.Int `struct:"pgid,omitempty"` // Extended Process Data - Args []string `struct:"args,omitempty"` - Cmdline string `struct:"cmdline,omitempty"` - Cwd string `struct:"cwd,omitempty"` - Exe string `struct:"exe,omitempty"` - Env common.MapStr `struct:"env,omitempty"` + Args []string `struct:"args,omitempty"` + Cmdline string `struct:"cmdline,omitempty"` + Cwd string `struct:"cwd,omitempty"` + Exe string `struct:"exe,omitempty"` + Env mapstr.M `struct:"env,omitempty"` // Resource Metrics Memory ProcMemInfo `struct:"memory,omitempty"` diff --git a/libbeat/mock/mockbeat.go b/libbeat/mock/mockbeat.go index 319b35083d3..cbfa5b560b8 100644 --- a/libbeat/mock/mockbeat.go +++ b/libbeat/mock/mockbeat.go @@ -24,6 +24,7 @@ import ( "github.com/elastic/beats/v7/libbeat/cmd/instance" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) ///*** Mock Beat Setup ***/// @@ -61,7 +62,7 @@ func (mb *Mockbeat) Run(b *beat.Beat) error { case <-ticker.C: client.Publish(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "mock", "message": "Mockbeat is alive!", }, diff --git a/libbeat/monitoring/cloudid_test.go b/libbeat/monitoring/cloudid_test.go index d9935fbb67b..2fe9e6bee06 100644 --- a/libbeat/monitoring/cloudid_test.go +++ b/libbeat/monitoring/cloudid_test.go @@ -25,39 +25,40 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestOverrideWithCloudSettings(t *testing.T) { tests := map[string]struct { - in common.MapStr - out common.MapStr + in mapstr.M + out mapstr.M errAssertionFunc assert.ErrorAssertionFunc }{ "cloud_id_no_es_hosts": { - common.MapStr{ + mapstr.M{ "cloud.id": "test:bG9jYWxob3N0JGVzY2x1c3RlciRiMGE1N2RhMTkwNzg0MzZmODcwZmQzNTgwZTRhNjE4ZQ==", }, - common.MapStr{ + mapstr.M{ "elasticsearch.hosts": []string{"https://escluster.localhost:443"}, }, assert.NoError, }, "cloud_id_with_es_hosts": { - common.MapStr{ + mapstr.M{ "cloud.id": "test:bG9jYWxob3N0JGVzY2x1c3RlciRiMGE1N2RhMTkwNzg0MzZmODcwZmQzNTgwZTRhNjE4ZQ==", "elasticsearch.hosts": []string{"foo", "bar"}, }, - common.MapStr{ + mapstr.M{ "elasticsearch.hosts": []string{"https://escluster.localhost:443"}, }, assert.NoError, }, "cloud_auth_no_es_auth": { - common.MapStr{ + mapstr.M{ "cloud.id": "test:bG9jYWxob3N0JGVzY2x1c3RlciRiMGE1N2RhMTkwNzg0MzZmODcwZmQzNTgwZTRhNjE4ZQ==", "cloud.auth": "elastic:changeme", }, - common.MapStr{ + mapstr.M{ "elasticsearch.hosts": []string{"https://escluster.localhost:443"}, "elasticsearch.username": "elastic", "elasticsearch.password": "changeme", @@ -65,13 +66,13 @@ func TestOverrideWithCloudSettings(t *testing.T) { assert.NoError, }, "cloud_auth_with_es_auth": { - common.MapStr{ + mapstr.M{ "cloud.id": "test:bG9jYWxob3N0JGVzY2x1c3RlciRiMGE1N2RhMTkwNzg0MzZmODcwZmQzNTgwZTRhNjE4ZQ==", "cloud.auth": "elastic:changeme", "elasticsearch.username": "foo", "elasticsearch.password": "bar", }, - common.MapStr{ + mapstr.M{ "elasticsearch.hosts": []string{"https://escluster.localhost:443"}, "elasticsearch.username": "elastic", "elasticsearch.password": "changeme", @@ -79,10 +80,10 @@ func TestOverrideWithCloudSettings(t *testing.T) { assert.NoError, }, "cloud_auth_no_id": { - common.MapStr{ + mapstr.M{ "cloud.auth": "elastic:changeme", }, - common.MapStr{ + mapstr.M{ "cloud.auth": "elastic:changeme", }, func(t assert.TestingT, err error, _ ...interface{}) bool { diff --git a/libbeat/monitoring/report/elasticsearch/client.go b/libbeat/monitoring/report/elasticsearch/client.go index 06f610a0c1a..86d17ba06a5 100644 --- a/libbeat/monitoring/report/elasticsearch/client.go +++ b/libbeat/monitoring/report/elasticsearch/client.go @@ -35,6 +35,7 @@ import ( "github.com/elastic/beats/v7/libbeat/monitoring/report" "github.com/elastic/beats/v7/libbeat/publisher" "github.com/elastic/beats/v7/libbeat/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) var createDocPrivAvailableESVersion = common.MustNewVersion("7.5.0") @@ -161,7 +162,7 @@ func (c *publishClient) String() string { } func (c *publishClient) publishBulk(ctx context.Context, event publisher.Event, typ string) error { - meta := common.MapStr{ + meta := mapstr.M{ "_index": getMonitoringIndexName(), "_routing": nil, } @@ -176,13 +177,13 @@ func (c *publishClient) publishBulk(ctx context.Context, event publisher.Event, opType = events.OpTypeIndex } - action := common.MapStr{ + action := mapstr.M{ opType.String(): meta, } event.Content.Fields.Put("timestamp", event.Content.Timestamp) - fields := common.MapStr{ + fields := mapstr.M{ "type": typ, typ: event.Content.Fields, } @@ -194,7 +195,7 @@ func (c *publishClient) publishBulk(ctx context.Context, event publisher.Event, fields.Put("interval_ms", interval) clusterUUID, err := event.Content.Meta.GetValue("cluster_uuid") - if err != nil && err != common.ErrKeyNotFound { + if err != nil && err != mapstr.ErrKeyNotFound { return errors.Wrap(err, "could not determine cluster_uuid field") } fields.Put("cluster_uuid", clusterUUID) diff --git a/libbeat/monitoring/report/elasticsearch/elasticsearch.go b/libbeat/monitoring/report/elasticsearch/elasticsearch.go index 96210c56c54..571cde03e57 100644 --- a/libbeat/monitoring/report/elasticsearch/elasticsearch.go +++ b/libbeat/monitoring/report/elasticsearch/elasticsearch.go @@ -36,6 +36,7 @@ import ( "github.com/elastic/beats/v7/libbeat/publisher/processing" "github.com/elastic/beats/v7/libbeat/publisher/queue" "github.com/elastic/beats/v7/libbeat/publisher/queue/memqueue" + "github.com/elastic/elastic-agent-libs/mapstr" ) type reporter struct { @@ -45,7 +46,7 @@ type reporter struct { checkRetry time.Duration // event metadata - beatMeta common.MapStr + beatMeta mapstr.M tags []string // pipeline @@ -261,7 +262,7 @@ func (r *reporter) snapshotLoop(namespace, prefix string, period time.Duration, continue } - fields := common.MapStr{ + fields := mapstr.M{ "beat": r.beatMeta, prefix: snapshot, } @@ -269,7 +270,7 @@ func (r *reporter) snapshotLoop(namespace, prefix string, period time.Duration, fields["tags"] = r.tags } - meta := common.MapStr{ + meta := mapstr.M{ "type": "beats_" + namespace, "interval_ms": int64(period / time.Millisecond), // Converting to seconds as interval only accepts `s` as unit @@ -321,8 +322,8 @@ func closing(log *logp.Logger, c io.Closer) { } } -func makeMeta(beat beat.Info) common.MapStr { - return common.MapStr{ +func makeMeta(beat beat.Info) mapstr.M { + return mapstr.M{ "type": beat.Beat, "version": beat.Version, "name": beat.Name, diff --git a/libbeat/monitoring/report/elasticsearch/snapshot.go b/libbeat/monitoring/report/elasticsearch/snapshot.go index 72c4c69fb79..c16a5249c63 100644 --- a/libbeat/monitoring/report/elasticsearch/snapshot.go +++ b/libbeat/monitoring/report/elasticsearch/snapshot.go @@ -18,11 +18,11 @@ package elasticsearch import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func makeSnapshot(R *monitoring.Registry) common.MapStr { +func makeSnapshot(R *monitoring.Registry) mapstr.M { mode := monitoring.Full - return common.MapStr(monitoring.CollectStructSnapshot(R, mode, false)) + return mapstr.M(monitoring.CollectStructSnapshot(R, mode, false)) } diff --git a/libbeat/monitoring/report/event.go b/libbeat/monitoring/report/event.go index f7d1e923c1b..885f254cc4a 100644 --- a/libbeat/monitoring/report/event.go +++ b/libbeat/monitoring/report/event.go @@ -20,7 +20,7 @@ package report import ( "time" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Event is the format of monitoring events. @@ -28,6 +28,6 @@ import ( // The only difference between report.Event and beat.Event // is Timestamp is serialized as "timestamp" in monitoring. type Event struct { - Timestamp time.Time `struct:"timestamp"` - Fields common.MapStr `struct:",inline"` + Timestamp time.Time `struct:"timestamp"` + Fields mapstr.M `struct:",inline"` } diff --git a/libbeat/monitoring/report/log/log.go b/libbeat/monitoring/report/log/log.go index edce95b8ff0..363b8604a2e 100644 --- a/libbeat/monitoring/report/log/log.go +++ b/libbeat/monitoring/report/log/log.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" "github.com/elastic/beats/v7/libbeat/monitoring/report" + "github.com/elastic/elastic-agent-libs/mapstr" ) // List of metrics that are gauges. This is used to identify metrics that should @@ -244,7 +245,7 @@ func toKeyValuePairs(snaps map[string]monitoring.FlatSnapshot) []interface{} { args := []interface{}{logp.Namespace("monitoring")} for name, snap := range snaps { - data := make(common.MapStr, snapshotLen(snap)) + data := make(mapstr.M, snapshotLen(snap)) for k, v := range snap.Bools { data.Put(k, v) } diff --git a/libbeat/monitoring/report/log/log_test.go b/libbeat/monitoring/report/log/log_test.go index 70a15cd5e1b..5606450dd9e 100644 --- a/libbeat/monitoring/report/log/log_test.go +++ b/libbeat/monitoring/report/log/log_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -112,7 +113,7 @@ func TestReporterLog(t *testing.T) { func assertMapHas(t *testing.T, m map[string]interface{}, key string, expectedValue interface{}) { t.Helper() - v, err := common.MapStr(m).GetValue(key) + v, err := mapstr.M(m).GetValue(key) if err != nil { t.Fatal(err) } diff --git a/libbeat/outputs/codec/format/format_test.go b/libbeat/outputs/codec/format/format_test.go index 96ded4f7841..96d4e1836c8 100644 --- a/libbeat/outputs/codec/format/format_test.go +++ b/libbeat/outputs/codec/format/format_test.go @@ -21,8 +21,8 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fmtstr" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFormatStringWriter(t *testing.T) { @@ -32,7 +32,7 @@ func TestFormatStringWriter(t *testing.T) { expectedValue := "test message" codec := New(format) - output, err := codec.Encode("test", &beat.Event{Fields: common.MapStr{"msg": "message"}}) + output, err := codec.Encode("test", &beat.Event{Fields: mapstr.M{"msg": "message"}}) if err != nil { t.Errorf("Error during event write %v", err) diff --git a/libbeat/outputs/codec/json/event.go b/libbeat/outputs/codec/json/event.go index c966df28f5f..c17a482e065 100644 --- a/libbeat/outputs/codec/json/event.go +++ b/libbeat/outputs/codec/json/event.go @@ -21,15 +21,15 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Event describes the event structure for events // (in-)directly send to logstash type event struct { - Timestamp time.Time `struct:"@timestamp"` - Meta meta `struct:"@metadata"` - Fields common.MapStr `struct:",inline"` + Timestamp time.Time `struct:"@timestamp"` + Meta meta `struct:"@metadata"` + Fields mapstr.M `struct:",inline"` } // Meta defines common event metadata to be stored in '@metadata' diff --git a/libbeat/outputs/codec/json/json_bench_test.go b/libbeat/outputs/codec/json/json_bench_test.go index dbdde0fa33e..9499897d8f9 100644 --- a/libbeat/outputs/codec/json/json_bench_test.go +++ b/libbeat/outputs/codec/json/json_bench_test.go @@ -22,7 +22,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var result []byte @@ -30,7 +30,7 @@ var result []byte func BenchmarkUTCTime(b *testing.B) { var r []byte codec := New("1.2.3", Config{}) - fields := common.MapStr{"msg": "message"} + fields := mapstr.M{"msg": "message"} var t time.Time var d time.Duration = 1000000000 @@ -46,7 +46,7 @@ func BenchmarkUTCTime(b *testing.B) { func BenchmarkLocalTime(b *testing.B) { var r []byte codec := New("1.2.3", Config{LocalTime: true}) - fields := common.MapStr{"msg": "message"} + fields := mapstr.M{"msg": "message"} var t time.Time var d time.Duration = 1000000000 diff --git a/libbeat/outputs/codec/json/json_test.go b/libbeat/outputs/codec/json/json_test.go index 18a1903abe8..87da4ce65a4 100644 --- a/libbeat/outputs/codec/json/json_test.go +++ b/libbeat/outputs/codec/json/json_test.go @@ -23,26 +23,26 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestJsonCodec(t *testing.T) { type testCase struct { config Config ts time.Time - in common.MapStr + in mapstr.M expected string } cases := map[string]testCase{ "default json": testCase{ config: defaultConfig, - in: common.MapStr{"msg": "message"}, + in: mapstr.M{"msg": "message"}, expected: `{"@timestamp":"0001-01-01T00:00:00.000Z","@metadata":{"beat":"test","type":"_doc","version":"1.2.3"},"msg":"message"}`, }, "pretty enabled": testCase{ config: Config{Pretty: true}, - in: common.MapStr{"msg": "message"}, + in: mapstr.M{"msg": "message"}, expected: `{ "@timestamp": "0001-01-01T00:00:00.000Z", "@metadata": { @@ -55,27 +55,27 @@ func TestJsonCodec(t *testing.T) { }, "html escaping enabled": { config: Config{EscapeHTML: true}, - in: common.MapStr{"msg": "world"}, + in: mapstr.M{"msg": "world"}, expected: `{"@timestamp":"0001-01-01T00:00:00.000Z","@metadata":{"beat":"test","type":"_doc","version":"1.2.3"},"msg":"\u003chello\u003eworld\u003c/hello\u003e"}`, }, "html escaping disabled": { config: Config{EscapeHTML: false}, - in: common.MapStr{"msg": "world"}, + in: mapstr.M{"msg": "world"}, expected: `{"@timestamp":"0001-01-01T00:00:00.000Z","@metadata":{"beat":"test","type":"_doc","version":"1.2.3"},"msg":"world"}`, }, "UTC timezone offset": { config: Config{LocalTime: true}, - in: common.MapStr{"msg": "message"}, + in: mapstr.M{"msg": "message"}, expected: `{"@timestamp":"0001-01-01T00:00:00.000+00:00","@metadata":{"beat":"test","type":"_doc","version":"1.2.3"},"msg":"message"}`, }, "PST timezone offset": { config: Config{LocalTime: true}, ts: time.Time{}.In(time.FixedZone("PST", -8*60*60)), - in: common.MapStr{"msg": "message"}, + in: mapstr.M{"msg": "message"}, expected: `{"@timestamp":"0000-12-31T16:00:00.000-08:00","@metadata":{"beat":"test","type":"_doc","version":"1.2.3"},"msg":"message"}`, }, "float undefined values": { - in: common.MapStr{"nan": math.NaN()}, + in: mapstr.M{"nan": math.NaN()}, expected: `{"@timestamp":"0001-01-01T00:00:00.000Z","@metadata":{"beat":"test","type":"_doc","version":"1.2.3"},"nan":null}`, }, } diff --git a/libbeat/outputs/console/console_test.go b/libbeat/outputs/console/console_test.go index 9a4b9a9439c..2952b88fe0f 100644 --- a/libbeat/outputs/console/console_test.go +++ b/libbeat/outputs/console/console_test.go @@ -30,7 +30,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fmtstr" "github.com/elastic/beats/v7/libbeat/outputs" "github.com/elastic/beats/v7/libbeat/outputs/codec" @@ -38,6 +37,7 @@ import ( "github.com/elastic/beats/v7/libbeat/outputs/codec/json" "github.com/elastic/beats/v7/libbeat/outputs/outest" "github.com/elastic/beats/v7/libbeat/publisher" + "github.com/elastic/elastic-agent-libs/mapstr" ) // capture stdout and return captured string @@ -137,6 +137,6 @@ func run(codec codec.Codec, batches ...publisher.Batch) (string, error) { }) } -func event(k, v string) common.MapStr { - return common.MapStr{k: v} +func event(k, v string) mapstr.M { + return mapstr.M{k: v} } diff --git a/libbeat/outputs/elasticsearch/client.go b/libbeat/outputs/elasticsearch/client.go index bdec35c73f1..abbfc5d654b 100644 --- a/libbeat/outputs/elasticsearch/client.go +++ b/libbeat/outputs/elasticsearch/client.go @@ -36,6 +36,7 @@ import ( "github.com/elastic/beats/v7/libbeat/outputs/outil" "github.com/elastic/beats/v7/libbeat/publisher" "github.com/elastic/beats/v7/libbeat/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -350,7 +351,7 @@ func (client *Client) createEventBulkMeta(version common.Version, event *beat.Ev func (client *Client) getPipeline(event *beat.Event) (string, error) { if event.Meta != nil { pipeline, err := events.GetMetaStringValue(*event, events.FieldMetaPipeline) - if err == common.ErrKeyNotFound { + if err == mapstr.ErrKeyNotFound { return "", nil } if err != nil { @@ -412,13 +413,13 @@ func (client *Client) bulkCollectPublishFails(result eslegclient.BulkResult, dat } else if client.NonIndexableAction == dead_letter_index { client.log.Warnf("Cannot index event %#v (status=%v): %s, trying dead letter index", data[i], status, msg) if data[i].Content.Meta == nil { - data[i].Content.Meta = common.MapStr{ + data[i].Content.Meta = mapstr.M{ dead_letter_marker_field: true, } } else { data[i].Content.Meta.Put(dead_letter_marker_field, true) } - data[i].Content.Fields = common.MapStr{ + data[i].Content.Fields = mapstr.M{ "message": data[i].Content.Fields.String(), "error.type": status, "error.message": string(msg), diff --git a/libbeat/outputs/elasticsearch/client_integration_test.go b/libbeat/outputs/elasticsearch/client_integration_test.go index efee7e8b69b..667a6d0e096 100644 --- a/libbeat/outputs/elasticsearch/client_integration_test.go +++ b/libbeat/outputs/elasticsearch/client_integration_test.go @@ -44,6 +44,7 @@ import ( "github.com/elastic/beats/v7/libbeat/monitoring" "github.com/elastic/beats/v7/libbeat/outputs" "github.com/elastic/beats/v7/libbeat/outputs/outest" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestClientPublishEvent(t *testing.T) { @@ -89,7 +90,7 @@ func testPublishEvent(t *testing.T, index string, cfg map[string]interface{}) { batch := outest.NewBatch(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "libbeat", "message": "Test message from libbeat", }, @@ -178,7 +179,7 @@ func TestClientPublishEventWithPipeline(t *testing.T) { publish(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "libbeat", "message": "Test message 1", "pipeline": pipeline, @@ -186,7 +187,7 @@ func TestClientPublishEventWithPipeline(t *testing.T) { }}) publish(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "libbeat", "message": "Test message 2", "testfield": 0, @@ -222,7 +223,7 @@ func TestClientBulkPublishEventsWithDeadletterIndex(t *testing.T) { err := output.Publish(context.Background(), outest.NewBatch(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "libbeat", "message": "Test message 1", "testfield": 0, @@ -234,7 +235,7 @@ func TestClientBulkPublishEventsWithDeadletterIndex(t *testing.T) { batch := outest.NewBatch(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "libbeat", "message": "Test message 2", "testfield": "foo0", @@ -324,7 +325,7 @@ func TestClientBulkPublishEventsWithPipeline(t *testing.T) { publish( beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "libbeat", "message": "Test message 1", "pipeline": pipeline, @@ -332,7 +333,7 @@ func TestClientBulkPublishEventsWithPipeline(t *testing.T) { }}, beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "libbeat", "message": "Test message 2", "testfield": 0, @@ -358,7 +359,7 @@ func TestClientPublishTracer(t *testing.T) { batch := outest.NewBatch(beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "Hello world", }, }) diff --git a/libbeat/outputs/elasticsearch/client_test.go b/libbeat/outputs/elasticsearch/client_test.go index 0a2ca672a7c..f3f6b0b9cae 100644 --- a/libbeat/outputs/elasticsearch/client_test.go +++ b/libbeat/outputs/elasticsearch/client_test.go @@ -42,6 +42,7 @@ import ( "github.com/elastic/beats/v7/libbeat/outputs/outil" "github.com/elastic/beats/v7/libbeat/publisher" "github.com/elastic/beats/v7/libbeat/version" + "github.com/elastic/elastic-agent-libs/mapstr" ) type testIndexSelector struct{} @@ -78,7 +79,7 @@ func TestPublishStatusCode(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 1}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 1}}} events := []publisher.Event{event} t.Run("returns pre-defined error and drops batch when 413", func(t *testing.T) { @@ -99,7 +100,7 @@ func TestPublishStatusCode(t *testing.T) { ) assert.NoError(t, err) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 1}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 1}}} events := []publisher.Event{event} batch := &batchMock{ events: events, @@ -154,7 +155,7 @@ func TestCollectPublishFailsNone(t *testing.T) { item := `{"create": {"status": 200}},` response := []byte(`{"items": [` + strings.Repeat(item, N) + `]}`) - event := common.MapStr{"field": 1} + event := mapstr.M{"field": 1} events := make([]publisher.Event, N) for i := 0; i < N; i++ { events[i] = publisher.Event{Content: beat.Event{Fields: event}} @@ -181,8 +182,8 @@ func TestCollectPublishFailMiddle(t *testing.T) { ]} `) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 1}}} - eventFail := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 2}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 1}}} + eventFail := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 2}}} events := []publisher.Event{event, eventFail, event} res, stats := client.bulkCollectPublishFails(response, events) @@ -227,8 +228,8 @@ func TestCollectPublishFailDeadLetterQueue(t *testing.T) { ]} `) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"bar": 1}}} - eventFail := publisher.Event{Content: beat.Event{Fields: common.MapStr{"bar": "bar1"}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"bar": 1}}} + eventFail := publisher.Event{Content: beat.Event{Fields: mapstr.M{"bar": "bar1"}}} events := []publisher.Event{event, eventFail, event} res, stats := client.bulkCollectPublishFails(response, events) @@ -236,12 +237,12 @@ func TestCollectPublishFailDeadLetterQueue(t *testing.T) { if len(res) == 1 { expected := publisher.Event{ Content: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "{\"bar\":\"bar1\"}", "error.type": 400, "error.message": "{\n\t\t\t\"root_cause\" : [\n\t\t\t {\n\t\t\t\t\"type\" : \"mapper_parsing_exception\",\n\t\t\t\t\"reason\" : \"failed to parse field [bar] of type [long] in document with id '1'. Preview of field's value: 'bar1'\"\n\t\t\t }\n\t\t\t],\n\t\t\t\"type\" : \"mapper_parsing_exception\",\n\t\t\t\"reason\" : \"failed to parse field [bar] of type [long] in document with id '1'. Preview of field's value: 'bar1'\",\n\t\t\t\"caused_by\" : {\n\t\t\t \"type\" : \"illegal_argument_exception\",\n\t\t\t \"reason\" : \"For input string: \\\"bar1\\\"\"\n\t\t\t}\n\t\t }", }, - Meta: common.MapStr{ + Meta: mapstr.M{ dead_letter_marker_field: true, }, }, @@ -285,8 +286,8 @@ func TestCollectPublishFailDrop(t *testing.T) { ]} `) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"bar": 1}}} - eventFail := publisher.Event{Content: beat.Event{Fields: common.MapStr{"bar": "bar1"}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"bar": 1}}} + eventFail := publisher.Event{Content: beat.Event{Fields: mapstr.M{"bar": "bar1"}}} events := []publisher.Event{event, eventFail, event} res, stats := client.bulkCollectPublishFails(response, events) @@ -311,7 +312,7 @@ func TestCollectPublishFailAll(t *testing.T) { ]} `) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 2}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 2}}} events := []publisher.Event{event, event, event} res, stats := client.bulkCollectPublishFails(response, events) @@ -360,7 +361,7 @@ func TestCollectPipelinePublishFail(t *testing.T) { ] }`) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 2}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 2}}} events := []publisher.Event{event} res, _ := client.bulkCollectPublishFails(response, events) @@ -385,7 +386,7 @@ func BenchmarkCollectPublishFailsNone(b *testing.B) { ]} `) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 1}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 1}}} events := []publisher.Event{event, event, event} for i := 0; i < b.N; i++ { @@ -413,8 +414,8 @@ func BenchmarkCollectPublishFailMiddle(b *testing.B) { ]} `) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 1}}} - eventFail := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 2}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 1}}} + eventFail := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 2}}} events := []publisher.Event{event, eventFail, event} for i := 0; i < b.N; i++ { @@ -442,7 +443,7 @@ func BenchmarkCollectPublishFailAll(b *testing.B) { ]} `) - event := publisher.Event{Content: beat.Event{Fields: common.MapStr{"field": 2}}} + event := publisher.Event{Content: beat.Event{Fields: mapstr.M{"field": 2}}} events := []publisher.Event{event, event, event} for i := 0; i < b.N; i++ { @@ -492,7 +493,7 @@ func TestClientWithHeaders(t *testing.T) { assert.Equal(t, 1, requestCount) // bulk request - event := beat.Event{Fields: common.MapStr{ + event := beat.Event{Fields: mapstr.M{ "@timestamp": common.Time(time.Now()), "type": "libbeat", "message": "Test message from libbeat", @@ -508,20 +509,20 @@ func TestBulkEncodeEvents(t *testing.T) { cases := map[string]struct { version string docType string - config common.MapStr - events []common.MapStr + config mapstr.M + events []mapstr.M }{ "6.x": { version: "6.8.0", docType: "doc", - config: common.MapStr{}, - events: []common.MapStr{{"message": "test"}}, + config: mapstr.M{}, + events: []mapstr.M{{"message": "test"}}, }, "latest": { version: version.GetDefaultVersion(), docType: "", - config: common.MapStr{}, - events: []common.MapStr{{"message": "test"}}, + config: mapstr.M{}, + events: []mapstr.M{{"message": "test"}}, }, } @@ -585,7 +586,7 @@ func TestBulkEncodeEvents(t *testing.T) { } func TestBulkEncodeEventsWithOpType(t *testing.T) { - cases := []common.MapStr{ + cases := []mapstr.M{ {"_id": "111", "op_type": e.OpTypeIndex, "message": "test 1", "bulkIndex": 0}, {"_id": "112", "message": "test 2", "bulkIndex": 2}, {"_id": "", "op_type": e.OpTypeDelete, "message": "test 6", "bulkIndex": -1}, // this won't get encoded due to missing _id @@ -594,7 +595,7 @@ func TestBulkEncodeEventsWithOpType(t *testing.T) { {"_id": "115", "op_type": e.OpTypeIndex, "message": "test 5", "bulkIndex": 7}, } - cfg := common.MustNewConfigFrom(common.MapStr{}) + cfg := common.MustNewConfigFrom(mapstr.M{}) info := beat.Info{ IndexPrefix: "test", Version: version.GetDefaultVersion(), @@ -608,7 +609,7 @@ func TestBulkEncodeEventsWithOpType(t *testing.T) { events := make([]publisher.Event, len(cases)) for i, fields := range cases { - meta := common.MapStr{ + meta := mapstr.M{ "_id": fields["_id"], } if opType, exists := fields["op_type"]; exists { @@ -618,7 +619,7 @@ func TestBulkEncodeEventsWithOpType(t *testing.T) { events[i] = publisher.Event{ Content: beat.Event{ Meta: meta, - Fields: common.MapStr{ + Fields: mapstr.M{ "message": fields["message"], }, }, diff --git a/libbeat/outputs/elasticsearch/elasticsearch_test.go b/libbeat/outputs/elasticsearch/elasticsearch_test.go index f84d32c2ccd..fd9bd84eae8 100644 --- a/libbeat/outputs/elasticsearch/elasticsearch_test.go +++ b/libbeat/outputs/elasticsearch/elasticsearch_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/esleg/eslegclient" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConnectCallbacksManagement(t *testing.T) { @@ -94,11 +95,11 @@ func TestPipelineSelection(t *testing.T) { want: "test", }, "pipeline via event meta": { - event: beat.Event{Meta: common.MapStr{"pipeline": "test"}}, + event: beat.Event{Meta: mapstr.M{"pipeline": "test"}}, want: "test", }, "pipeline via event meta must be lowercase": { - event: beat.Event{Meta: common.MapStr{"pipeline": "Test"}}, + event: beat.Event{Meta: mapstr.M{"pipeline": "Test"}}, want: "test", }, "pipelines setting": { diff --git a/libbeat/outputs/kafka/config_test.go b/libbeat/outputs/kafka/config_test.go index 816cb8f03e8..d475f995028 100644 --- a/libbeat/outputs/kafka/config_test.go +++ b/libbeat/outputs/kafka/config_test.go @@ -27,21 +27,22 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/internal/testutil" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConfigAcceptValid(t *testing.T) { - tests := map[string]common.MapStr{ - "default config is valid": common.MapStr{}, - "lz4 with 0.11": common.MapStr{ + tests := map[string]mapstr.M{ + "default config is valid": mapstr.M{}, + "lz4 with 0.11": mapstr.M{ "compression": "lz4", "version": "0.11", }, - "lz4 with 1.0": common.MapStr{ + "lz4 with 1.0": mapstr.M{ "compression": "lz4", "version": "1.0.0", }, - "Kerberos with keytab": common.MapStr{ - "kerberos": common.MapStr{ + "Kerberos with keytab": mapstr.M{ + "kerberos": mapstr.M{ "auth_type": "keytab", "username": "elastic", "keytab": "/etc/krb5kcd/kafka.keytab", @@ -50,8 +51,8 @@ func TestConfigAcceptValid(t *testing.T) { "realm": "ELASTIC", }, }, - "Kerberos with user and password pair": common.MapStr{ - "kerberos": common.MapStr{ + "Kerberos with user and password pair": mapstr.M{ + "kerberos": mapstr.M{ "auth_type": "password", "username": "elastic", "password": "changeme", @@ -79,9 +80,9 @@ func TestConfigAcceptValid(t *testing.T) { } func TestConfigInvalid(t *testing.T) { - tests := map[string]common.MapStr{ - "Kerberos with invalid auth_type": common.MapStr{ - "kerberos": common.MapStr{ + tests := map[string]mapstr.M{ + "Kerberos with invalid auth_type": mapstr.M{ + "kerberos": mapstr.M{ "auth_type": "invalid_auth_type", "config_path": "/etc/path/config", "service_name": "HTTP/elastic@ELASTIC", @@ -162,14 +163,14 @@ func TestTopicSelection(t *testing.T) { "use event field": { cfg: map[string]interface{}{"topic": "test-%{[field]}"}, event: beat.Event{ - Fields: common.MapStr{"field": "from-event"}, + Fields: mapstr.M{"field": "from-event"}, }, want: "test-from-event", }, "use event field must keep case": { cfg: map[string]interface{}{"topic": "Test-%{[field]}"}, event: beat.Event{ - Fields: common.MapStr{"field": "From-Event"}, + Fields: mapstr.M{"field": "From-Event"}, }, want: "Test-From-Event", }, diff --git a/libbeat/outputs/kafka/kafka_integration_test.go b/libbeat/outputs/kafka/kafka_integration_test.go index 1f75c271086..7c1fc190fd8 100644 --- a/libbeat/outputs/kafka/kafka_integration_test.go +++ b/libbeat/outputs/kafka/kafka_integration_test.go @@ -42,6 +42,7 @@ import ( _ "github.com/elastic/beats/v7/libbeat/outputs/codec/format" _ "github.com/elastic/beats/v7/libbeat/outputs/codec/json" "github.com/elastic/beats/v7/libbeat/outputs/outest" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -71,7 +72,7 @@ func TestKafkaPublish(t *testing.T) { "publish single event to test topic", nil, testTopic, - single(common.MapStr{ + single(mapstr.M{ "host": "test-host", "message": id, }), @@ -82,7 +83,7 @@ func TestKafkaPublish(t *testing.T) { "topic": "%{[type]}", }, logType, - single(common.MapStr{ + single(mapstr.M{ "host": "test-host", "type": logType, "message": id, @@ -94,7 +95,7 @@ func TestKafkaPublish(t *testing.T) { "codec.format.string": "%{[message]}", }, testTopic, - single(common.MapStr{ + single(mapstr.M{ "host": "test-host", "message": id, }), @@ -103,7 +104,7 @@ func TestKafkaPublish(t *testing.T) { "batch publish to test topic", nil, testTopic, - randMulti(5, 100, common.MapStr{ + randMulti(5, 100, mapstr.M{ "host": "test-host", }), }, @@ -113,7 +114,7 @@ func TestKafkaPublish(t *testing.T) { "topic": "%{[type]}", }, logType, - randMulti(5, 100, common.MapStr{ + randMulti(5, 100, mapstr.M{ "host": "test-host", "type": logType, }), @@ -126,7 +127,7 @@ func TestKafkaPublish(t *testing.T) { }, }, testTopic, - randMulti(1, 10, common.MapStr{ + randMulti(1, 10, mapstr.M{ "host": "test-host", "type": "log", }), @@ -139,7 +140,7 @@ func TestKafkaPublish(t *testing.T) { }, }, testTopic, - randMulti(1, 10, common.MapStr{ + randMulti(1, 10, mapstr.M{ "host": "test-host", "type": "log", }), @@ -150,7 +151,7 @@ func TestKafkaPublish(t *testing.T) { "partition.hash": map[string]interface{}{}, }, testTopic, - randMulti(1, 10, common.MapStr{ + randMulti(1, 10, mapstr.M{ "host": "test-host", "type": "log", }), @@ -163,7 +164,7 @@ func TestKafkaPublish(t *testing.T) { "partition.hash": map[string]interface{}{}, }, testTopic, - randMulti(1, 10, common.MapStr{ + randMulti(1, 10, mapstr.M{ "host": "test-host", "type": "log", }), @@ -179,7 +180,7 @@ func TestKafkaPublish(t *testing.T) { }, }, testTopic, - randMulti(1, 10, common.MapStr{ + randMulti(1, 10, mapstr.M{ "host": "test-host", "type": "log", }), @@ -188,7 +189,7 @@ func TestKafkaPublish(t *testing.T) { "publish single event to test topic", map[string]interface{}{}, testTopic, - single(common.MapStr{ + single(mapstr.M{ "host": "test-host", "message": id, }), @@ -210,7 +211,7 @@ func TestKafkaPublish(t *testing.T) { "password": "KafkaTest", }, testTopic, - single(common.MapStr{ + single(mapstr.M{ "host": "test-host", "message": id, }), @@ -234,7 +235,7 @@ func TestKafkaPublish(t *testing.T) { }, }, testTopic, - randMulti(5, 100, common.MapStr{ + randMulti(5, 100, mapstr.M{ "host": "test-host", }), }, @@ -526,7 +527,7 @@ func flatten(infos []eventInfo) []beat.Event { return out } -func single(fields common.MapStr) []eventInfo { +func single(fields mapstr.M) []eventInfo { return []eventInfo{ { events: []beat.Event{ @@ -536,12 +537,12 @@ func single(fields common.MapStr) []eventInfo { } } -func randMulti(batches, n int, event common.MapStr) []eventInfo { +func randMulti(batches, n int, event mapstr.M) []eventInfo { var out []eventInfo for i := 0; i < batches; i++ { var data []beat.Event for j := 0; j < n; j++ { - tmp := common.MapStr{} + tmp := mapstr.M{} for k, v := range event { tmp[k] = v } diff --git a/libbeat/outputs/kafka/partition.go b/libbeat/outputs/kafka/partition.go index b8723d58482..d8dc940d81b 100644 --- a/libbeat/outputs/kafka/partition.go +++ b/libbeat/outputs/kafka/partition.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type partitionBuilder func(*logp.Logger, *common.Config) (func() partitioner, error) @@ -276,7 +277,7 @@ func hash2Partition(hash uint32, numPartitions int32) (int32, error) { return p % numPartitions, nil } -func hashFieldValue(h hash.Hash32, event common.MapStr, field string) error { +func hashFieldValue(h hash.Hash32, event mapstr.M, field string) error { type stringer interface { String() string } diff --git a/libbeat/outputs/kafka/partition_test.go b/libbeat/outputs/kafka/partition_test.go index 81ded3009d0..75344d50b84 100644 --- a/libbeat/outputs/kafka/partition_test.go +++ b/libbeat/outputs/kafka/partition_test.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/publisher" + "github.com/elastic/elastic-agent-libs/mapstr" ) type partTestScenario func(*testing.T, bool, sarama.Partitioner) error @@ -229,7 +230,7 @@ func partTestSimple(N int, makeKey bool) partTestScenario { for i := 0; i <= N; i++ { ts := time.Now() - event := common.MapStr{ + event := mapstr.M{ "@timestamp": common.Time(ts), "message": randString(20), } @@ -281,7 +282,7 @@ func partTestHashInvariant(N int) partTestScenario { for i := 0; i <= N; i++ { ts := time.Now() - event := common.MapStr{ + event := mapstr.M{ "@timestamp": common.Time(ts), "message": randString(20), } diff --git a/libbeat/outputs/logstash/client_test.go b/libbeat/outputs/logstash/client_test.go index 424ffa6b3ce..72d73e9231e 100644 --- a/libbeat/outputs/logstash/client_test.go +++ b/libbeat/outputs/logstash/client_test.go @@ -28,10 +28,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport" "github.com/elastic/beats/v7/libbeat/common/transport/transptest" "github.com/elastic/beats/v7/libbeat/outputs/outest" + "github.com/elastic/elastic-agent-libs/mapstr" v2 "github.com/elastic/go-lumber/server/v2" ) @@ -106,7 +106,7 @@ func testSimpleEvent(t *testing.T, factory clientFactory) { defer client.Stop() event := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "name": "me", "line": 10, }, @@ -141,7 +141,7 @@ func testSimpleEventWithTTL(t *testing.T, factory clientFactory) { event := beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{"type": "test", "name": "me", "line": 10}, + Fields: mapstr.M{"type": "test", "name": "me", "line": 10}, } go client.Publish(outest.NewBatch(event)) @@ -161,7 +161,7 @@ func testSimpleEventWithTTL(t *testing.T, factory clientFactory) { event = beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{"type": "test", "name": "me", "line": 11}, + Fields: mapstr.M{"type": "test", "name": "me", "line": 11}, } go client.Publish(outest.NewBatch(event)) @@ -191,21 +191,21 @@ func testStructuredEvent(t *testing.T, factory clientFactory) { defer transp.Close() defer client.Stop() - event := beat.Event{Fields: common.MapStr{ + event := beat.Event{Fields: mapstr.M{ "type": "test", "name": "test", - "struct": common.MapStr{ + "struct": mapstr.M{ "field1": 1, "field2": true, "field3": []int{1, 2, 3}, "field4": []interface{}{ 1, "test", - common.MapStr{ + mapstr.M{ "sub": "field", }, }, - "field5": common.MapStr{ + "field5": mapstr.M{ "sub1": 2, }, }, diff --git a/libbeat/outputs/logstash/config_test.go b/libbeat/outputs/logstash/config_test.go index 572749bc1a3..d85ff92c1bc 100644 --- a/libbeat/outputs/logstash/config_test.go +++ b/libbeat/outputs/logstash/config_test.go @@ -23,6 +23,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -55,7 +56,7 @@ func TestConfig(t *testing.T) { }, }, "config given": { - config: common.MustNewConfigFrom(common.MapStr{ + config: common.MustNewConfigFrom(mapstr.M{ "index": "beat-index", "loadbalance": true, "bulk_max_size": 1024, @@ -79,7 +80,7 @@ func TestConfig(t *testing.T) { }, }, "removed config setting": { - config: common.MustNewConfigFrom(common.MapStr{ + config: common.MustNewConfigFrom(mapstr.M{ "port": "8080", }), expectedConfig: nil, diff --git a/libbeat/outputs/logstash/logstash_integration_test.go b/libbeat/outputs/logstash/logstash_integration_test.go index e76f4703e67..c07e52d3f9a 100644 --- a/libbeat/outputs/logstash/logstash_integration_test.go +++ b/libbeat/outputs/logstash/logstash_integration_test.go @@ -41,6 +41,7 @@ import ( _ "github.com/elastic/beats/v7/libbeat/outputs/elasticsearch" "github.com/elastic/beats/v7/libbeat/outputs/outest" "github.com/elastic/beats/v7/libbeat/outputs/outil" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -119,8 +120,8 @@ func esConnect(t *testing.T, index string) *esConnection { // try to drop old index if left over from failed test _, _, _ = client.Delete(index, "", "", nil) // ignore error - _, _, err = client.CreateIndex(index, common.MapStr{ - "settings": common.MapStr{ + _, _, err = client.CreateIndex(index, mapstr.M{ + "settings": mapstr.M{ "number_of_shards": 1, "number_of_replicas": 0, }, @@ -300,7 +301,7 @@ func testSendMessageViaLogstash(t *testing.T, name string, tls bool) { batch := outest.NewBatch( beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "host": "test-host", "message": "hello world", }, @@ -335,7 +336,7 @@ func testSendMultipleViaLogstash(t *testing.T, name string, tls bool) { for i := 0; i < 10; i++ { event := beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "host": "test-host", "type": "log", "message": fmt.Sprintf("hello world - %v", i), @@ -398,7 +399,7 @@ func testSendMultipleBatchesViaLogstash( for j := 0; j < batchSize; j++ { event := beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "host": "test-host", "type": "log", "message": fmt.Sprintf("batch hello world - %v", i*batchSize+j), @@ -448,7 +449,7 @@ func testLogstashElasticOutputPluginCompatibleMessage(t *testing.T, name string, ts := time.Now() event := beat.Event{ Timestamp: ts, - Fields: common.MapStr{ + Fields: mapstr.M{ "host": "test-host", "type": "log", "message": "hello world", @@ -505,7 +506,7 @@ func testLogstashElasticOutputPluginBulkCompatibleMessage(t *testing.T, name str events := []beat.Event{ { Timestamp: ts, - Fields: common.MapStr{ + Fields: mapstr.M{ "host": "test-host", "type": "log", "message": "hello world", diff --git a/libbeat/outputs/logstash/logstash_test.go b/libbeat/outputs/logstash/logstash_test.go index 7b8adeb8f43..3986033d8c0 100644 --- a/libbeat/outputs/logstash/logstash_test.go +++ b/libbeat/outputs/logstash/logstash_test.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/transport/transptest" "github.com/elastic/beats/v7/libbeat/outputs" "github.com/elastic/beats/v7/libbeat/outputs/outest" + "github.com/elastic/elastic-agent-libs/mapstr" v2 "github.com/elastic/go-lumber/server/v2" ) @@ -148,7 +149,7 @@ func testConnectionType( } func testEvent() beat.Event { - return beat.Event{Fields: common.MapStr{ + return beat.Event{Fields: mapstr.M{ "@timestamp": common.Time(time.Now()), "type": "log", "extra": 10, diff --git a/libbeat/outputs/outil/select_test.go b/libbeat/outputs/outil/select_test.go index 49ea63bbd4b..25ddbfc95a0 100644 --- a/libbeat/outputs/outil/select_test.go +++ b/libbeat/outputs/outil/select_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type node map[string]interface{} @@ -37,80 +38,80 @@ func TestSelector(t *testing.T) { tests := map[string]struct { config string - event common.MapStr + event mapstr.M want string settings func(Settings) Settings }{ "constant key": { config: `key: value`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "lowercase constant key": { config: `key: VaLuE`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", settings: useLowerCase, }, "do not lowercase constant key by default": { config: `key: VaLuE`, - event: common.MapStr{}, + event: mapstr.M{}, want: "VaLuE", }, "format string key": { config: `key: '%{[key]}'`, - event: common.MapStr{"key": "value"}, + event: mapstr.M{"key": "value"}, want: "value", }, "lowercase format string key": { config: `key: '%{[key]}'`, - event: common.MapStr{"key": "VaLuE"}, + event: mapstr.M{"key": "VaLuE"}, want: "value", settings: useLowerCase, }, "do not lowercase format string by default": { config: `key: '%{[key]}'`, - event: common.MapStr{"key": "VaLuE"}, + event: mapstr.M{"key": "VaLuE"}, want: "VaLuE", }, "key with empty keys": { config: `{key: value, keys: }`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "lowercase key with empty keys": { config: `{key: vAlUe, keys: }`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", settings: useLowerCase, }, "do not lowercase key with empty keys by default": { config: `{key: vAlUe, keys: }`, - event: common.MapStr{}, + event: mapstr.M{}, want: "vAlUe", }, "constant in multi key": { config: `keys: [key: 'value']`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "format string in multi key": { config: `keys: [key: '%{[key]}']`, - event: common.MapStr{"key": "value"}, + event: mapstr.M{"key": "value"}, want: "value", }, "missing format string key with default in rule": { config: `keys: - key: '%{[key]}' default: value`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "lowercase missing format string key with default in rule": { config: `keys: - key: '%{[key]}' default: vAlUe`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", settings: useLowerCase, }, @@ -118,21 +119,21 @@ func TestSelector(t *testing.T) { config: `keys: - key: '%{[key]}' default: vAlUe`, - event: common.MapStr{}, + event: mapstr.M{}, want: "vAlUe", }, "empty format string key with default in rule": { config: `keys: - key: '%{[key]}' default: value`, - event: common.MapStr{"key": ""}, + event: mapstr.M{"key": ""}, want: "value", }, "lowercase empty format string key with default in rule": { config: `keys: - key: '%{[key]}' default: vAluE`, - event: common.MapStr{"key": ""}, + event: mapstr.M{"key": ""}, want: "value", settings: useLowerCase, }, @@ -140,19 +141,19 @@ func TestSelector(t *testing.T) { config: `keys: - key: '%{[key]}' default: vAluE`, - event: common.MapStr{"key": ""}, + event: mapstr.M{"key": ""}, want: "vAluE", }, "missing format string key with constant in next rule": { config: `keys: - key: '%{[key]}' - key: value`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "missing format string key with constant in top-level rule": { config: `{ key: value, keys: [key: '%{[key]}']}`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "apply mapping": { @@ -160,7 +161,7 @@ func TestSelector(t *testing.T) { - key: '%{[key]}' mappings: v: value`, - event: common.MapStr{"key": "v"}, + event: mapstr.M{"key": "v"}, want: "value", }, "lowercase applied mapping": { @@ -168,7 +169,7 @@ func TestSelector(t *testing.T) { - key: '%{[key]}' mappings: v: vAlUe`, - event: common.MapStr{"key": "v"}, + event: mapstr.M{"key": "v"}, want: "value", settings: useLowerCase, }, @@ -177,7 +178,7 @@ func TestSelector(t *testing.T) { - key: '%{[key]}' mappings: v: vAlUe`, - event: common.MapStr{"key": "v"}, + event: mapstr.M{"key": "v"}, want: "vAlUe", }, "apply mapping with default on empty key": { @@ -186,7 +187,7 @@ func TestSelector(t *testing.T) { default: value mappings: v: 'v'`, - event: common.MapStr{"key": ""}, + event: mapstr.M{"key": ""}, want: "value", }, "lowercase apply mapping with default on empty key": { @@ -195,7 +196,7 @@ func TestSelector(t *testing.T) { default: vAluE mappings: v: 'v'`, - event: common.MapStr{"key": ""}, + event: mapstr.M{"key": ""}, want: "value", settings: useLowerCase, }, @@ -205,7 +206,7 @@ func TestSelector(t *testing.T) { default: vAluE mappings: v: 'v'`, - event: common.MapStr{"key": ""}, + event: mapstr.M{"key": ""}, want: "vAluE", }, "apply mapping with default on empty lookup": { @@ -214,7 +215,7 @@ func TestSelector(t *testing.T) { default: value mappings: v: ''`, - event: common.MapStr{"key": "v"}, + event: mapstr.M{"key": "v"}, want: "value", }, "apply mapping without match": { @@ -223,7 +224,7 @@ func TestSelector(t *testing.T) { mappings: v: '' - key: value`, - event: common.MapStr{"key": "x"}, + event: mapstr.M{"key": "x"}, want: "value", }, "mapping with constant key": { @@ -231,7 +232,7 @@ func TestSelector(t *testing.T) { - key: k mappings: k: value`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "mapping with missing constant key": { @@ -239,7 +240,7 @@ func TestSelector(t *testing.T) { - key: unknown mappings: {k: wrong} - key: value`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "mapping with missing constant key, but default": { @@ -247,14 +248,14 @@ func TestSelector(t *testing.T) { - key: unknown default: value mappings: {k: wrong}`, - event: common.MapStr{}, + event: mapstr.M{}, want: "value", }, "matching condition": { config: `keys: - key: value when.equals.test: test`, - event: common.MapStr{"test": "test"}, + event: mapstr.M{"test": "test"}, want: "value", }, "failing condition": { @@ -262,7 +263,7 @@ func TestSelector(t *testing.T) { - key: wrong when.equals.test: test - key: value`, - event: common.MapStr{"test": "x"}, + event: mapstr.M{"test": "x"}, want: "value", }, } diff --git a/libbeat/outputs/redis/client.go b/libbeat/outputs/redis/client.go index 914536bbb78..277c6aa08ce 100644 --- a/libbeat/outputs/redis/client.go +++ b/libbeat/outputs/redis/client.go @@ -28,13 +28,13 @@ import ( "github.com/gomodule/redigo/redis" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/outputs" "github.com/elastic/beats/v7/libbeat/outputs/codec" "github.com/elastic/beats/v7/libbeat/outputs/outil" "github.com/elastic/beats/v7/libbeat/publisher" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -222,7 +222,7 @@ func (c *client) makePublishPUBLISH(conn redis.Conn) (publishFn, error) { func (c *client) publishEventsBulk(conn redis.Conn, command string) publishFn { // XXX: requires key.IsConst() == true - dest, _ := c.key.Select(&beat.Event{Fields: common.MapStr{}}) + dest, _ := c.key.Select(&beat.Event{Fields: mapstr.M{}}) return func(_ outil.Selector, data []publisher.Event) ([]publisher.Event, error) { args := make([]interface{}, 1, len(data)+1) args[0] = dest diff --git a/libbeat/outputs/redis/redis_integration_test.go b/libbeat/outputs/redis/redis_integration_test.go index bc1f09f8bc5..a8366d3c849 100644 --- a/libbeat/outputs/redis/redis_integration_test.go +++ b/libbeat/outputs/redis/redis_integration_test.go @@ -38,6 +38,7 @@ import ( _ "github.com/elastic/beats/v7/libbeat/outputs/codec/format" _ "github.com/elastic/beats/v7/libbeat/outputs/codec/json" "github.com/elastic/beats/v7/libbeat/outputs/outest" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -362,10 +363,10 @@ func sendTestEvents(out outputs.Client, batches, N int) error { func createEvent(message int) beat.Event { return beat.Event{ Timestamp: time.Now(), - Meta: common.MapStr{ + Meta: mapstr.M{ "test": testMetaValue, }, - Fields: common.MapStr{"message": message}, + Fields: mapstr.M{"message": message}, } } diff --git a/libbeat/outputs/redis/redis_test.go b/libbeat/outputs/redis/redis_test.go index deffb1d8de5..0664e533c92 100644 --- a/libbeat/outputs/redis/redis_test.go +++ b/libbeat/outputs/redis/redis_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/outputs" _ "github.com/elastic/beats/v7/libbeat/outputs/codec/json" + "github.com/elastic/elastic-agent-libs/mapstr" ) type checker func(*testing.T, outputs.Group) @@ -148,14 +149,14 @@ func TestKeySelection(t *testing.T) { "use event field": { cfg: map[string]interface{}{"key": "test-%{[field]}"}, event: beat.Event{ - Fields: common.MapStr{"field": "from-event"}, + Fields: mapstr.M{"field": "from-event"}, }, want: "test-from-event", }, "use event field must keep case": { cfg: map[string]interface{}{"key": "Test-%{[field]}"}, event: beat.Event{ - Fields: common.MapStr{"field": "From-Event"}, + Fields: mapstr.M{"field": "From-Event"}, }, want: "Test-From-Event", }, diff --git a/libbeat/outputs/shipper/shipper.go b/libbeat/outputs/shipper/shipper.go index efdf928701d..bc98f92f447 100644 --- a/libbeat/outputs/shipper/shipper.go +++ b/libbeat/outputs/shipper/shipper.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/outputs" sc "github.com/elastic/beats/v7/libbeat/outputs/shipper/api" "github.com/elastic/beats/v7/libbeat/publisher" + "github.com/elastic/elastic-agent-libs/mapstr" "google.golang.org/grpc" "google.golang.org/grpc/backoff" @@ -189,7 +190,7 @@ func (c *shipper) String() string { return "shipper" } -func convertMapStr(m common.MapStr) (*structpb.Value, error) { +func convertMapStr(m mapstr.M) (*structpb.Value, error) { if m == nil { return structpb.NewNullValue(), nil } @@ -202,7 +203,7 @@ func convertMapStr(m common.MapStr) (*structpb.Value, error) { err error ) switch v := value.(type) { - case common.MapStr: + case mapstr.M: protoValue, err = convertMapStr(v) default: protoValue, err = structpb.NewValue(v) diff --git a/libbeat/outputs/shipper/shipper_test.go b/libbeat/outputs/shipper/shipper_test.go index 79a1ad6012a..ae12240075c 100644 --- a/libbeat/outputs/shipper/shipper_test.go +++ b/libbeat/outputs/shipper/shipper_test.go @@ -22,17 +22,17 @@ import ( "testing" "time" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/structpb" + + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConvertMapStr(t *testing.T) { cases := []struct { name string - value common.MapStr + value mapstr.M exp *structpb.Value expErr string }{ @@ -42,25 +42,25 @@ func TestConvertMapStr(t *testing.T) { }, { name: "empty map returns empty struct", - value: common.MapStr{}, + value: mapstr.M{}, exp: protoStruct(t, nil), }, { name: "returns error when type is not supported", - value: common.MapStr{ + value: mapstr.M{ "key": time.Now(), }, expErr: "invalid type: time.Time", }, { name: "values are preserved", - value: common.MapStr{ + value: mapstr.M{ "key1": "string", "key2": 42, "key3": 42.2, - "key4": common.MapStr{ + "key4": mapstr.M{ "subkey1": "string", - "subkey2": common.MapStr{ + "subkey2": mapstr.M{ "subsubkey1": "string", }, }, diff --git a/libbeat/processors/actions/add_fields.go b/libbeat/processors/actions/add_fields.go index 42fc2f2b443..eda3d876ff0 100644 --- a/libbeat/processors/actions/add_fields.go +++ b/libbeat/processors/actions/add_fields.go @@ -26,10 +26,11 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type addFields struct { - fields common.MapStr + fields mapstr.M shared bool overwrite bool } @@ -49,8 +50,8 @@ func init() { // CreateAddFields constructs an add_fields processor from config. func CreateAddFields(c *common.Config) (processors.Processor, error) { config := struct { - Fields common.MapStr `config:"fields" validate:"required"` - Target *string `config:"target"` + Fields mapstr.M `config:"fields" validate:"required"` + Target *string `config:"target"` }{} err := c.Unpack(&config) if err != nil { @@ -67,7 +68,7 @@ func CreateAddFields(c *common.Config) (processors.Processor, error) { // NewAddFields creates a new processor adding the given fields to events. // Set `shared` true if there is the chance of labels being changed/modified by // subsequent processors. -func NewAddFields(fields common.MapStr, shared bool, overwrite bool) processors.Processor { +func NewAddFields(fields mapstr.M, shared bool, overwrite bool) processors.Processor { return &addFields{fields: fields, shared: shared, overwrite: overwrite} } @@ -102,9 +103,9 @@ func optTarget(opt *string, def string) string { return *opt } -func makeFieldsProcessor(target string, fields common.MapStr, shared bool) processors.Processor { +func makeFieldsProcessor(target string, fields mapstr.M, shared bool) processors.Processor { if target != "" { - fields = common.MapStr{ + fields = mapstr.M{ target: fields, } } diff --git a/libbeat/processors/actions/add_fields_test.go b/libbeat/processors/actions/add_fields_test.go index 44bebb949b8..775d3f9edb1 100644 --- a/libbeat/processors/actions/add_fields_test.go +++ b/libbeat/processors/actions/add_fields_test.go @@ -20,7 +20,7 @@ package actions import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestAddFields(t *testing.T) { @@ -29,49 +29,49 @@ func TestAddFields(t *testing.T) { testProcessors(t, map[string]testCase{ "add field": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "fields": common.MapStr{"field": "test"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "fields": mapstr.M{"field": "test"}, }, cfg: single(`{add_fields: {fields: {field: test}}}`), }, "custom target": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "my": common.MapStr{"field": "test"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "my": mapstr.M{"field": "test"}, }, cfg: single(`{add_fields: {target: my, fields: {field: test}}}`), }, "overwrite existing field": { - eventFields: common.MapStr{ - "fields": common.MapStr{"field": "old"}, + eventFields: mapstr.M{ + "fields": mapstr.M{"field": "old"}, }, - wantFields: common.MapStr{"fields": common.MapStr{"field": "test"}}, + wantFields: mapstr.M{"fields": mapstr.M{"field": "test"}}, cfg: single(`{add_fields: {fields: {field: test}}}`), }, "merge with existing meta": { - eventMeta: common.MapStr{ + eventMeta: mapstr.M{ "_id": "unique", }, - wantMeta: common.MapStr{ + wantMeta: mapstr.M{ "_id": "unique", "op_type": "index", }, cfg: single(`{add_fields: {target: "@metadata", fields: {op_type: "index"}}}`), }, "merge with existing fields": { - eventFields: common.MapStr{ - "fields": common.MapStr{"existing": "a"}, + eventFields: mapstr.M{ + "fields": mapstr.M{"existing": "a"}, }, - wantFields: common.MapStr{ - "fields": common.MapStr{"existing": "a", "field": "test"}, + wantFields: mapstr.M{ + "fields": mapstr.M{"existing": "a", "field": "test"}, }, cfg: single(`{add_fields: {fields: {field: test}}}`), }, "combine 2 processors": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "fields": common.MapStr{ + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "fields": mapstr.M{ "l1": "a", "l2": "b", }, @@ -82,10 +82,10 @@ func TestAddFields(t *testing.T) { ), }, "different targets": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "a": common.MapStr{"l1": "a"}, - "b": common.MapStr{"l2": "b"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "a": mapstr.M{"l1": "a"}, + "b": mapstr.M{"l2": "b"}, }, cfg: multi( `{add_fields: {target: a, fields: {l1: a}}}`, @@ -93,31 +93,31 @@ func TestAddFields(t *testing.T) { ), }, "under root": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "a": common.MapStr{"b": "test"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "a": mapstr.M{"b": "test"}, }, cfg: single( `{add_fields: {target: "", fields: {a.b: test}}}`, ), }, "merge under root": { - eventFields: common.MapStr{ - "a": common.MapStr{"old": "value"}, + eventFields: mapstr.M{ + "a": mapstr.M{"old": "value"}, }, - wantFields: common.MapStr{ - "a": common.MapStr{"old": "value", "new": "test"}, + wantFields: mapstr.M{ + "a": mapstr.M{"old": "value", "new": "test"}, }, cfg: single( `{add_fields: {target: "", fields: {a.new: test}}}`, ), }, "overwrite existing under root": { - eventFields: common.MapStr{ - "a": common.MapStr{"keep": "value", "change": "a"}, + eventFields: mapstr.M{ + "a": mapstr.M{"keep": "value", "change": "a"}, }, - wantFields: common.MapStr{ - "a": common.MapStr{"keep": "value", "change": "b"}, + wantFields: mapstr.M{ + "a": mapstr.M{"keep": "value", "change": "b"}, }, cfg: single( `{add_fields: {target: "", fields: {a.change: b}}}`, @@ -125,8 +125,8 @@ func TestAddFields(t *testing.T) { }, "add fields to nil event": { eventFields: nil, - wantFields: common.MapStr{ - "fields": common.MapStr{"field": "test"}, + wantFields: mapstr.M{ + "fields": mapstr.M{"field": "test"}, }, cfg: single(`{add_fields: {fields: {field: test}}}`), }, diff --git a/libbeat/processors/actions/add_labels.go b/libbeat/processors/actions/add_labels.go index 93096898960..f58f1d8b88d 100644 --- a/libbeat/processors/actions/add_labels.go +++ b/libbeat/processors/actions/add_labels.go @@ -23,6 +23,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" + "github.com/elastic/elastic-agent-libs/mapstr" ) // LabelsKey is the default target key for the add_labels processor. @@ -37,7 +38,7 @@ func init() { func createAddLabels(c *common.Config) (processors.Processor, error) { config := struct { - Labels common.MapStr `config:"labels" validate:"required"` + Labels mapstr.M `config:"labels" validate:"required"` }{} err := c.Unpack(&config) if err != nil { @@ -58,25 +59,25 @@ func createAddLabels(c *common.Config) (processors.Processor, error) { // If labels contains nested objects, NewAddLabels will flatten keys into labels by // by joining names with a dot ('.') . // The labels will be inserted into the 'labels' field. -func NewAddLabels(labels common.MapStr, shared bool) (processors.Processor, error) { +func NewAddLabels(labels mapstr.M, shared bool) (processors.Processor, error) { flatLabels, err := flattenLabels(labels) if err != nil { return nil, fmt.Errorf("failed to flatten labels: %w", err) } - return NewAddFields(common.MapStr{ + return NewAddFields(mapstr.M{ LabelsKey: flatLabels, }, shared, true), nil } -func flattenLabels(labels common.MapStr) (common.MapStr, error) { +func flattenLabels(labels mapstr.M) (mapstr.M, error) { labelConfig, err := common.NewConfigFrom(labels) if err != nil { return nil, err } flatKeys := labelConfig.FlattenedKeys() - flatMap := make(common.MapStr, len(flatKeys)) + flatMap := make(mapstr.M, len(flatKeys)) for _, k := range flatKeys { v, err := labelConfig.String(k, -1) if err != nil { diff --git a/libbeat/processors/actions/add_labels_test.go b/libbeat/processors/actions/add_labels_test.go index 2b7b30aab15..70281fe413b 100644 --- a/libbeat/processors/actions/add_labels_test.go +++ b/libbeat/processors/actions/add_labels_test.go @@ -20,7 +20,7 @@ package actions import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestAddLabels(t *testing.T) { @@ -29,30 +29,30 @@ func TestAddLabels(t *testing.T) { testProcessors(t, map[string]testCase{ "add label": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "labels": common.MapStr{"label": "test"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "labels": mapstr.M{"label": "test"}, }, cfg: single(`{add_labels: {labels: {label: test}}}`), }, "add dotted label": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "labels": common.MapStr{"a.b": "test"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "labels": mapstr.M{"a.b": "test"}, }, cfg: single(`{add_labels: {labels: {a.b: test}}}`), }, "add nested labels": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "labels": common.MapStr{"a.b": "test", "a.c": "test2"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "labels": mapstr.M{"a.b": "test", "a.c": "test2"}, }, cfg: single(`{add_labels: {labels: {a: {b: test, c: test2}}}}`), }, "merge labels": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "labels": common.MapStr{"l1": "a", "l2": "b", "lc": "b"}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "labels": mapstr.M{"l1": "a", "l2": "b", "lc": "b"}, }, cfg: multi( `{add_labels.labels: {l1: a, lc: a}}`, @@ -60,9 +60,9 @@ func TestAddLabels(t *testing.T) { ), }, "add array": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{ - "labels": common.MapStr{ + eventFields: mapstr.M{}, + wantFields: mapstr.M{ + "labels": mapstr.M{ "array.0": "foo", "array.1": "bar", "array.2.hello": "world", diff --git a/libbeat/processors/actions/add_network_direction_test.go b/libbeat/processors/actions/add_network_direction_test.go index da507a18374..907d3e4f905 100644 --- a/libbeat/processors/actions/add_network_direction_test.go +++ b/libbeat/processors/actions/add_network_direction_test.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNetworkDirection(t *testing.T) { @@ -50,7 +51,7 @@ func TestNetworkDirection(t *testing.T) { for _, tt := range tests { t.Run(fmt.Sprintf("%v -> %v : %v", tt.Source, tt.Destination, tt.Direction), func(t *testing.T) { evt := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "source": tt.Source, "destination": tt.Destination, }, @@ -80,8 +81,8 @@ func TestNetworkDirection(t *testing.T) { t.Run("supports metadata as a target", func(t *testing.T) { evt := beat.Event{ - Meta: common.MapStr{}, - Fields: common.MapStr{ + Meta: mapstr.M{}, + Fields: mapstr.M{ "source": "1.1.1.1", "destination": "8.8.8.8", }, @@ -94,7 +95,7 @@ func TestNetworkDirection(t *testing.T) { })) require.NoError(t, err) - expectedMeta := common.MapStr{ + expectedMeta := mapstr.M{ "direction": "external", } diff --git a/libbeat/processors/actions/add_tags.go b/libbeat/processors/actions/add_tags.go index 15161ca8114..77c7a064527 100644 --- a/libbeat/processors/actions/add_tags.go +++ b/libbeat/processors/actions/add_tags.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" + "github.com/elastic/elastic-agent-libs/mapstr" ) type addTags struct { @@ -58,7 +59,7 @@ func createAddTags(c *common.Config) (processors.Processor, error) { // appended to the existing list of tags. func NewAddTags(target string, tags []string) processors.Processor { if target == "" { - target = common.TagsKey + target = mapstr.TagsKey } // make sure capacity == length such that different processors adding more tags @@ -73,7 +74,7 @@ func NewAddTags(target string, tags []string) processors.Processor { } func (at *addTags) Run(event *beat.Event) (*beat.Event, error) { - common.AddTagsWithKey(event.Fields, at.target, at.tags) + mapstr.AddTagsWithKey(event.Fields, at.target, at.tags) return event, nil } diff --git a/libbeat/processors/actions/add_tags_test.go b/libbeat/processors/actions/add_tags_test.go index 920fde65306..efb48a11c26 100644 --- a/libbeat/processors/actions/add_tags_test.go +++ b/libbeat/processors/actions/add_tags_test.go @@ -20,7 +20,7 @@ package actions import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestAddTags(t *testing.T) { @@ -29,39 +29,39 @@ func TestAddTags(t *testing.T) { testProcessors(t, map[string]testCase{ "create tags": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{"tags": []string{"t1", "t2"}}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{"tags": []string{"t1", "t2"}}, cfg: single(`{add_tags: {tags: [t1, t2]}}`), }, "append to tags": { - eventFields: common.MapStr{"tags": []string{"t1"}}, - wantFields: common.MapStr{"tags": []string{"t1", "t2", "t3"}}, + eventFields: mapstr.M{"tags": []string{"t1"}}, + wantFields: mapstr.M{"tags": []string{"t1", "t2", "t3"}}, cfg: single(`{add_tags: {tags: [t2, t3]}}`), }, "combine from 2 processors": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{"tags": []string{"t1", "t2", "t3", "t4"}}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{"tags": []string{"t1", "t2", "t3", "t4"}}, cfg: multi( `{add_tags: {tags: [t1, t2]}}`, `{add_tags: {tags: [t3, t4]}}`, ), }, "with custom target": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{"custom": []string{"t1", "t2"}}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{"custom": []string{"t1", "t2"}}, cfg: single(`{add_tags: {tags: [t1, t2], target: custom}}`), }, "different targets": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{"tags1": []string{"t1"}, "tags2": []string{"t2"}}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{"tags1": []string{"t1"}, "tags2": []string{"t2"}}, cfg: multi( `{add_tags: {target: tags1, tags: [t1]}}`, `{add_tags: {target: tags2, tags: [t2]}}`, ), }, "single tag config without array notation": { - eventFields: common.MapStr{}, - wantFields: common.MapStr{"tags": []string{"t1"}}, + eventFields: mapstr.M{}, + wantFields: mapstr.M{"tags": []string{"t1"}}, cfg: single(`{add_tags: {tags: t1}}`), }, }) diff --git a/libbeat/processors/actions/common_test.go b/libbeat/processors/actions/common_test.go index b4d30bfa9cd..dee60ded18d 100644 --- a/libbeat/processors/actions/common_test.go +++ b/libbeat/processors/actions/common_test.go @@ -25,13 +25,14 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" + "github.com/elastic/elastic-agent-libs/mapstr" ) type testCase struct { - eventFields common.MapStr - eventMeta common.MapStr - wantFields common.MapStr - wantMeta common.MapStr + eventFields mapstr.M + eventMeta mapstr.M + wantFields mapstr.M + wantMeta mapstr.M cfg []string } diff --git a/libbeat/processors/actions/copy_fields.go b/libbeat/processors/actions/copy_fields.go index 0b77da01450..0b3efe0a316 100644 --- a/libbeat/processors/actions/copy_fields.go +++ b/libbeat/processors/actions/copy_fields.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type copyFields struct { @@ -98,7 +99,7 @@ func (f *copyFields) copyField(from string, to string, event *beat.Event) error value, err := event.GetValue(from) if err != nil { - if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if f.config.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return nil } return fmt.Errorf("could not fetch value for key: %s, Error: %s", from, err) @@ -120,10 +121,10 @@ func (f *copyFields) String() string { // maps without doing any type conversions. func cloneValue(value interface{}) interface{} { switch v := value.(type) { - case common.MapStr: + case mapstr.M: return v.Clone() case map[string]interface{}: - return common.MapStr(v).Clone() + return mapstr.M(v).Clone() case []interface{}: len := len(v) newArr := make([]interface{}, len) diff --git a/libbeat/processors/actions/copy_fields_test.go b/libbeat/processors/actions/copy_fields_test.go index 5e890b5cbe6..bf591cfcc81 100644 --- a/libbeat/processors/actions/copy_fields_test.go +++ b/libbeat/processors/actions/copy_fields_test.go @@ -21,29 +21,29 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" ) func TestCopyFields(t *testing.T) { log := logp.NewLogger("copy_fields_test") var tests = map[string]struct { FromTo fromTo - Input common.MapStr - Expected common.MapStr + Input mapstr.M + Expected mapstr.M }{ "copy string from message to message_copied": { FromTo: fromTo{ From: "message", To: "message_copied", }, - Input: common.MapStr{ + Input: mapstr.M{ "message": "please copy this line", }, - Expected: common.MapStr{ + Expected: mapstr.M{ "message": "please copy this line", "message_copied": "please copy this line", }, @@ -53,13 +53,13 @@ func TestCopyFields(t *testing.T) { From: "nested.message", To: "message_copied", }, - Input: common.MapStr{ - "nested": common.MapStr{ + Input: mapstr.M{ + "nested": mapstr.M{ "message": "please copy this line", }, }, - Expected: common.MapStr{ - "nested": common.MapStr{ + Expected: mapstr.M{ + "nested": mapstr.M{ "message": "please copy this line", }, "message_copied": "please copy this line", @@ -70,10 +70,10 @@ func TestCopyFields(t *testing.T) { From: "dotted.message", To: "message_copied", }, - Input: common.MapStr{ + Input: mapstr.M{ "dotted.message": "please copy this line", }, - Expected: common.MapStr{ + Expected: mapstr.M{ "dotted.message": "please copy this line", "message_copied": "please copy this line", }, @@ -83,12 +83,12 @@ func TestCopyFields(t *testing.T) { From: "message.original", To: "message.copied", }, - Input: common.MapStr{ + Input: mapstr.M{ "message.original": 42, }, - Expected: common.MapStr{ + Expected: mapstr.M{ "message.original": 42, - "message": common.MapStr{ + "message": mapstr.M{ "copied": 42, }, }, @@ -98,13 +98,13 @@ func TestCopyFields(t *testing.T) { From: "message.original", To: "message", }, - Input: common.MapStr{ - "message": common.MapStr{ + Input: mapstr.M{ + "message": mapstr.M{ "original": 42, }, }, - Expected: common.MapStr{ - "message": common.MapStr{ + Expected: mapstr.M{ + "message": mapstr.M{ "original": 42, }, }, @@ -114,10 +114,10 @@ func TestCopyFields(t *testing.T) { From: "message.original", To: "message", }, - Input: common.MapStr{ + Input: mapstr.M{ "message.original": 42, }, - Expected: common.MapStr{ + Expected: mapstr.M{ "message.original": 42, "message": 42, }, @@ -127,20 +127,20 @@ func TestCopyFields(t *testing.T) { From: "message.original", To: "message_copied", }, - Input: common.MapStr{ - "message": common.MapStr{ - "original": common.MapStr{ + Input: mapstr.M{ + "message": mapstr.M{ + "original": mapstr.M{ "original": "original", }, }, }, - Expected: common.MapStr{ - "message": common.MapStr{ - "original": common.MapStr{ + Expected: mapstr.M{ + "message": mapstr.M{ + "original": mapstr.M{ "original": "original", }, }, - "message_copied": common.MapStr{ + "message_copied": mapstr.M{ "original": "original", }, }, @@ -183,12 +183,12 @@ func TestCopyFields(t *testing.T) { } event := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "message": "please copy this line", }, } - expMeta := common.MapStr{ + expMeta := mapstr.M{ "message": "please copy this line", "message_copied": "please copy this line", } diff --git a/libbeat/processors/actions/decode_base64_field.go b/libbeat/processors/actions/decode_base64_field.go index 63973be2129..ce134fe7273 100644 --- a/libbeat/processors/actions/decode_base64_field.go +++ b/libbeat/processors/actions/decode_base64_field.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -100,7 +101,7 @@ func (f decodeBase64Field) String() string { func (f *decodeBase64Field) decodeField(event *beat.Event) error { value, err := event.GetValue(f.config.Field.From) if err != nil { - if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if f.config.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return nil } return fmt.Errorf("could not fetch base64 value for key: %s, Error: %v", f.config.Field.From, err) diff --git a/libbeat/processors/actions/decode_base64_field_test.go b/libbeat/processors/actions/decode_base64_field_test.go index 3355400e9f0..d3a8af3011c 100644 --- a/libbeat/processors/actions/decode_base64_field_test.go +++ b/libbeat/processors/actions/decode_base64_field_test.go @@ -23,16 +23,16 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDecodeBase64Run(t *testing.T) { var testCases = []struct { description string config base64Config - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M error bool }{ { @@ -44,10 +44,10 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", "field2": "correct data", }, @@ -62,10 +62,10 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct data", }, error: false, @@ -79,10 +79,10 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct data", }, error: false, @@ -96,10 +96,10 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Y29ycmVjdCBwYWRkZWQgZGF0YQ==", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct padded data", }, error: false, @@ -113,10 +113,10 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "dW5wYWRkZWQgZGF0YQ", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "unpadded data", }, error: false, @@ -130,12 +130,12 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "bad data", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "bad data", - "error": common.MapStr{ + "error": mapstr.M{ "message": "failed to decode base64 fields in processor: error trying to decode bad data: illegal base64 data at input byte 3", }, }, @@ -150,10 +150,10 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: false, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "bad data", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "bad data", }, error: false, @@ -167,12 +167,12 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", - "error": common.MapStr{ + "error": mapstr.M{ "message": "failed to decode base64 fields in processor: could not fetch base64 value for key: field2, Error: key not found", }, }, @@ -187,10 +187,10 @@ func TestDecodeBase64Run(t *testing.T) { IgnoreMissing: true, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", }, error: false, @@ -231,8 +231,8 @@ func TestDecodeBase64Run(t *testing.T) { } event := &beat.Event{ - Meta: common.MapStr{}, - Fields: common.MapStr{ + Meta: mapstr.M{}, + Fields: mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", }, } @@ -242,10 +242,10 @@ func TestDecodeBase64Run(t *testing.T) { config: config, } - expectedFields := common.MapStr{ + expectedFields := mapstr.M{ "field1": "Y29ycmVjdCBkYXRh", } - expectedMeta := common.MapStr{ + expectedMeta := mapstr.M{ "field": "correct data", } diff --git a/libbeat/processors/actions/decode_json_fields.go b/libbeat/processors/actions/decode_json_fields.go index be852a47133..bc0d490d075 100644 --- a/libbeat/processors/actions/decode_json_fields.go +++ b/libbeat/processors/actions/decode_json_fields.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type decodeJSONFields struct { @@ -105,7 +106,7 @@ func (f *decodeJSONFields) Run(event *beat.Event) (*beat.Event, error) { for _, field := range f.fields { data, err := event.GetValue(field) - if err != nil && errors.Cause(err) != common.ErrKeyNotFound { + if err != nil && errors.Cause(err) != mapstr.ErrKeyNotFound { f.logger.Debugf("Error trying to GetValue for field : %s in event : %v", field, event) errs = append(errs, err.Error()) continue @@ -122,7 +123,7 @@ func (f *decodeJSONFields) Run(event *beat.Event) (*beat.Event, error) { if err != nil { f.logger.Debugf("Error trying to unmarshal %s", text) errs = append(errs, err.Error()) - event.SetErrorWithOption(common.MapStr{ + event.SetErrorWithOption(mapstr.M{ "message": "parsing input as JSON: " + err.Error(), "data": text, "field": field, @@ -138,10 +139,10 @@ func (f *decodeJSONFields) Run(event *beat.Event) (*beat.Event, error) { var id string if key := f.documentID; key != "" { if dict, ok := output.(map[string]interface{}); ok { - if tmp, err := common.MapStr(dict).GetValue(key); err == nil { + if tmp, err := mapstr.M(dict).GetValue(key); err == nil { if v, ok := tmp.(string); ok { id = v - common.MapStr(dict).Delete(key) + mapstr.M(dict).Delete(key) } } } @@ -166,7 +167,7 @@ func (f *decodeJSONFields) Run(event *beat.Event) (*beat.Event, error) { if id != "" { if event.Meta == nil { - event.Meta = common.MapStr{} + event.Meta = mapstr.M{} } event.Meta[events.FieldMetaID] = id } diff --git a/libbeat/processors/actions/decode_json_fields_test.go b/libbeat/processors/actions/decode_json_fields_test.go index 81d90dcf32d..c37e82dcf16 100644 --- a/libbeat/processors/actions/decode_json_fields_test.go +++ b/libbeat/processors/actions/decode_json_fields_test.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" + "github.com/elastic/elastic-agent-libs/mapstr" ) var fields = [1]string{"msg"} @@ -60,13 +61,13 @@ func TestDecodeJSONFieldsCheckConfig(t *testing.T) { } func TestMissingKey(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "pipeline": "us1", } actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "pipeline": "us1", } @@ -74,14 +75,14 @@ func TestMissingKey(t *testing.T) { } func TestFieldNotString(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": 123, "pipeline": "us1", } actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "msg": 123, "pipeline": "us1", } @@ -90,14 +91,14 @@ func TestFieldNotString(t *testing.T) { } func TestInvalidJSON(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3", "pipeline": "us1", } actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3", "pipeline": "us1", } @@ -105,14 +106,14 @@ func TestInvalidJSON(t *testing.T) { } func TestInvalidJSONMultiple(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": "11:38:04,323 |-INFO testing", "pipeline": "us1", } actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "msg": "11:38:04,323 |-INFO testing", "pipeline": "us1", } @@ -122,7 +123,7 @@ func TestInvalidJSONMultiple(t *testing.T) { func TestDocumentID(t *testing.T) { log := logp.NewLogger("decode_json_fields_test") - input := common.MapStr{ + input := mapstr.M{ "msg": `{"log": "message", "myid": "myDocumentID"}`, } @@ -140,10 +141,10 @@ func TestDocumentID(t *testing.T) { actual, err := p.Run(&beat.Event{Fields: input}) require.NoError(t, err) - wantFields := common.MapStr{ + wantFields := mapstr.M{ "msg": map[string]interface{}{"log": "message"}, } - wantMeta := common.MapStr{ + wantMeta := mapstr.M{ "_id": "myDocumentID", } @@ -152,14 +153,14 @@ func TestDocumentID(t *testing.T) { } func TestValidJSONDepthOne(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3}", "pipeline": "us1", } actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "msg": map[string]interface{}{ "log": "{\"level\":\"info\"}", "stream": "stderr", @@ -172,7 +173,7 @@ func TestValidJSONDepthOne(t *testing.T) { } func TestValidJSONDepthTwo(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3}", "pipeline": "us1", } @@ -185,7 +186,7 @@ func TestValidJSONDepthTwo(t *testing.T) { actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "msg": map[string]interface{}{ "log": map[string]interface{}{ "level": "info", @@ -200,7 +201,7 @@ func TestValidJSONDepthTwo(t *testing.T) { } func TestTargetOption(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3}", "pipeline": "us1", } @@ -214,7 +215,7 @@ func TestTargetOption(t *testing.T) { actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "doc": map[string]interface{}{ "log": map[string]interface{}{ "level": "info", @@ -230,7 +231,7 @@ func TestTargetOption(t *testing.T) { } func TestTargetRootOption(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3}", "pipeline": "us1", } @@ -244,7 +245,7 @@ func TestTargetRootOption(t *testing.T) { actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "log": map[string]interface{}{ "level": "info", }, @@ -259,11 +260,11 @@ func TestTargetRootOption(t *testing.T) { func TestTargetMetadata(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3}", "pipeline": "us1", }, - Meta: common.MapStr{}, + Meta: mapstr.M{}, } testConfig, _ = common.NewConfigFrom(map[string]interface{}{ @@ -283,7 +284,7 @@ func TestTargetMetadata(t *testing.T) { actual, _ := p.Run(event) - expectedMeta := common.MapStr{ + expectedMeta := mapstr.M{ "json": map[string]interface{}{ "log": map[string]interface{}{ "level": "info", @@ -300,12 +301,12 @@ func TestTargetMetadata(t *testing.T) { func TestNotJsonObjectOrArray(t *testing.T) { var cases = []struct { MaxDepth int - Expected common.MapStr + Expected mapstr.M }{ { MaxDepth: 1, - Expected: common.MapStr{ - "msg": common.MapStr{ + Expected: mapstr.M{ + "msg": mapstr.M{ "someDate": "2016-09-28T01:40:26.760+0000", "someNumber": 1475026826760, "someNumberAsString": "1475026826760", @@ -318,14 +319,14 @@ func TestNotJsonObjectOrArray(t *testing.T) { }, { MaxDepth: 10, - Expected: common.MapStr{ - "msg": common.MapStr{ + Expected: mapstr.M{ + "msg": mapstr.M{ "someDate": "2016-09-28T01:40:26.760+0000", "someNumber": 1475026826760, "someNumberAsString": "1475026826760", "someString": "foobar", "someString2": "2017 is awesome", - "someMap": common.MapStr{"a": "b"}, + "someMap": mapstr.M{"a": "b"}, "someArray": []int{1, 2, 3}, }, }, @@ -334,7 +335,7 @@ func TestNotJsonObjectOrArray(t *testing.T) { for _, testCase := range cases { t.Run(fmt.Sprintf("TestNotJsonObjectOrArrayDepth-%v", testCase.MaxDepth), func(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": `{ "someDate": "2016-09-28T01:40:26.760+0000", "someNumberAsString": "1475026826760", @@ -359,7 +360,7 @@ func TestNotJsonObjectOrArray(t *testing.T) { } func TestArrayWithArraysDisabled(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": `{ "arrayOfMap": "[{\"a\":\"b\"}]" }`, @@ -373,8 +374,8 @@ func TestArrayWithArraysDisabled(t *testing.T) { actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ - "msg": common.MapStr{ + expected := mapstr.M{ + "msg": mapstr.M{ "arrayOfMap": "[{\"a\":\"b\"}]", }, } @@ -383,7 +384,7 @@ func TestArrayWithArraysDisabled(t *testing.T) { } func TestArrayWithArraysEnabled(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": `{ "arrayOfMap": "[{\"a\":\"b\"}]" }`, @@ -397,9 +398,9 @@ func TestArrayWithArraysEnabled(t *testing.T) { actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ - "msg": common.MapStr{ - "arrayOfMap": []common.MapStr{common.MapStr{"a": "b"}}, + expected := mapstr.M{ + "msg": mapstr.M{ + "arrayOfMap": []mapstr.M{mapstr.M{"a": "b"}}, }, } @@ -407,7 +408,7 @@ func TestArrayWithArraysEnabled(t *testing.T) { } func TestArrayWithInvalidArray(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": `{ "arrayOfMap": "[]]" }`, @@ -421,8 +422,8 @@ func TestArrayWithInvalidArray(t *testing.T) { actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ - "msg": common.MapStr{ + expected := mapstr.M{ + "msg": mapstr.M{ "arrayOfMap": "[]]", }, } @@ -434,19 +435,19 @@ func TestAddErrKeyOption(t *testing.T) { tests := []struct { name string addErrOption bool - expectedOutput common.MapStr + expectedOutput mapstr.M }{ - {name: "With add_error_key option", addErrOption: true, expectedOutput: common.MapStr{ - "error": common.MapStr{"message": "@timestamp not overwritten (parse error on {})", "type": "json"}, + {name: "With add_error_key option", addErrOption: true, expectedOutput: mapstr.M{ + "error": mapstr.M{"message": "@timestamp not overwritten (parse error on {})", "type": "json"}, "msg": "{\"@timestamp\":\"{}\"}", }}, - {name: "Without add_error_key option", addErrOption: false, expectedOutput: common.MapStr{ + {name: "Without add_error_key option", addErrOption: false, expectedOutput: mapstr.M{ "msg": "{\"@timestamp\":\"{}\"}", }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - input := common.MapStr{ + input := mapstr.M{ "msg": "{\"@timestamp\":\"{}\"}", } @@ -470,10 +471,10 @@ func TestExpandKeys(t *testing.T) { "expand_keys": true, "target": "", }) - input := common.MapStr{"msg": `{"a.b": {"c": "c"}, "a.b.d": "d"}`} - expected := common.MapStr{ + input := mapstr.M{"msg": `{"a.b": {"c": "c"}, "a.b.d": "d"}`} + expected := mapstr.M{ "msg": `{"a.b": {"c": "c"}, "a.b.d": "d"}`, - "a": common.MapStr{ + "a": mapstr.M{ "b": map[string]interface{}{ "c": "c", "d": "d", @@ -491,10 +492,10 @@ func TestExpandKeysError(t *testing.T) { "add_error_key": true, "target": "", }) - input := common.MapStr{"msg": `{"a.b": "c", "a.b.c": "d"}`} - expected := common.MapStr{ + input := mapstr.M{"msg": `{"a.b": "c", "a.b.c": "d"}`} + expected := mapstr.M{ "msg": `{"a.b": "c", "a.b.c": "d"}`, - "error": common.MapStr{ + "error": mapstr.M{ "message": "cannot expand ...", "type": "json", }, @@ -502,7 +503,7 @@ func TestExpandKeysError(t *testing.T) { actual := getActualValue(t, testConfig, input) assert.Contains(t, actual, "error") - errorField := actual["error"].(common.MapStr) + errorField := actual["error"].(mapstr.M) assert.Contains(t, errorField, "message") // The order in which keys are processed is not defined, so the error @@ -519,11 +520,11 @@ func TestOverwriteMetadata(t *testing.T) { "overwrite_keys": true, }) - input := common.MapStr{ + input := mapstr.M{ "msg": "{\"@metadata\":{\"beat\":\"libbeat\"},\"msg\":\"overwrite metadata test\"}", } - expected := common.MapStr{ + expected := mapstr.M{ "msg": "overwrite metadata test", } actual := getActualValue(t, testConfig, input) @@ -537,13 +538,13 @@ func TestAddErrorToEventOnUnmarshalError(t *testing.T) { "add_error_key": true, }) - input := common.MapStr{ + input := mapstr.M{ "message": "Broken JSON [[", } actual := getActualValue(t, testConfig, input) - errObj, ok := actual["error"].(common.MapStr) + errObj, ok := actual["error"].(mapstr.M) require.True(t, ok, "'error' field not present or of invalid type") require.NotNil(t, actual["error"]) @@ -552,7 +553,7 @@ func TestAddErrorToEventOnUnmarshalError(t *testing.T) { assert.NotNil(t, errObj["message"]) } -func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr { +func getActualValue(t *testing.T, config *common.Config, input mapstr.M) mapstr.M { log := logp.NewLogger("decode_json_fields_test") p, err := NewDecodeJSONFields(config) diff --git a/libbeat/processors/actions/decompress_gzip_field.go b/libbeat/processors/actions/decompress_gzip_field.go index f8e77983d25..c8c15aaefd2 100644 --- a/libbeat/processors/actions/decompress_gzip_field.go +++ b/libbeat/processors/actions/decompress_gzip_field.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" + "github.com/elastic/elastic-agent-libs/mapstr" ) type decompressGzipField struct { @@ -88,7 +89,7 @@ func (f *decompressGzipField) Run(event *beat.Event) (*beat.Event, error) { func (f *decompressGzipField) decompressGzipField(event *beat.Event) error { data, err := event.GetValue(f.config.Field.From) if err != nil { - if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if f.config.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return nil } return fmt.Errorf("could not fetch value for key: %s, Error: %v", f.config.Field.From, err) diff --git a/libbeat/processors/actions/decompress_gzip_field_test.go b/libbeat/processors/actions/decompress_gzip_field_test.go index 8beaf8b71a0..4ef79b8f4c8 100644 --- a/libbeat/processors/actions/decompress_gzip_field_test.go +++ b/libbeat/processors/actions/decompress_gzip_field_test.go @@ -23,16 +23,16 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDecompressGzip(t *testing.T) { var testCases = []struct { description string config decompressGzipFieldConfig - input common.MapStr - output common.MapStr + input mapstr.M + output mapstr.M error bool }{ { @@ -44,10 +44,10 @@ func TestDecompressGzip(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - input: common.MapStr{ + input: mapstr.M{ "field1": []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 74, 73, 77, 206, 207, 45, 40, 74, 45, 46, 78, 77, 81, 72, 73, 44, 73, 4, 4, 0, 0, 255, 255, 108, 158, 105, 19, 17, 0, 0, 0}, }, - output: common.MapStr{ + output: mapstr.M{ "field1": []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 74, 73, 77, 206, 207, 45, 40, 74, 45, 46, 78, 77, 81, 72, 73, 44, 73, 4, 4, 0, 0, 255, 255, 108, 158, 105, 19, 17, 0, 0, 0}, "field2": "decompressed data", }, @@ -62,10 +62,10 @@ func TestDecompressGzip(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - input: common.MapStr{ + input: mapstr.M{ "field1": string([]byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 74, 73, 77, 206, 207, 45, 40, 74, 45, 46, 78, 77, 81, 72, 73, 44, 73, 4, 4, 0, 0, 255, 255, 108, 158, 105, 19, 17, 0, 0, 0}), }, - output: common.MapStr{ + output: mapstr.M{ "field1": string([]byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 74, 73, 77, 206, 207, 45, 40, 74, 45, 46, 78, 77, 81, 72, 73, 44, 73, 4, 4, 0, 0, 255, 255, 108, 158, 105, 19, 17, 0, 0, 0}), "field2": "decompressed data", }, @@ -80,10 +80,10 @@ func TestDecompressGzip(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - input: common.MapStr{ + input: mapstr.M{ "field1": []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 74, 73, 77, 206, 207, 45, 40, 74, 45, 46, 78, 77, 81, 72, 73, 44, 73, 4, 4, 0, 0, 255, 255, 108, 158, 105, 19, 17, 0, 0, 0}, }, - output: common.MapStr{ + output: mapstr.M{ "field1": "decompressed data", }, error: false, @@ -97,12 +97,12 @@ func TestDecompressGzip(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - input: common.MapStr{ + input: mapstr.M{ "field1": "invalid gzipped data", }, - output: common.MapStr{ + output: mapstr.M{ "field1": "invalid gzipped data", - "error": common.MapStr{ + "error": mapstr.M{ "message": "Failed to decompress field in decompress_gzip_field processor: error decompressing field field1: gzip: invalid header", }, }, @@ -117,10 +117,10 @@ func TestDecompressGzip(t *testing.T) { IgnoreMissing: false, FailOnError: false, }, - input: common.MapStr{ + input: mapstr.M{ "field1": "invalid gzipped data", }, - output: common.MapStr{ + output: mapstr.M{ "field1": "invalid gzipped data", }, error: false, @@ -134,12 +134,12 @@ func TestDecompressGzip(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - input: common.MapStr{ + input: mapstr.M{ "field1": "my value", }, - output: common.MapStr{ + output: mapstr.M{ "field1": "my value", - "error": common.MapStr{ + "error": mapstr.M{ "message": "Failed to decompress field in decompress_gzip_field processor: could not fetch value for key: field2, Error: key not found", }, }, @@ -154,10 +154,10 @@ func TestDecompressGzip(t *testing.T) { IgnoreMissing: true, FailOnError: true, }, - input: common.MapStr{ + input: mapstr.M{ "field1": "my value", }, - output: common.MapStr{ + output: mapstr.M{ "field1": "my value", }, error: false, @@ -193,13 +193,13 @@ func TestDecompressGzip(t *testing.T) { t.Parallel() event := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "field1": []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 74, 73, 77, 206, 207, 45, 40, 74, 45, 46, 78, 77, 81, 72, 73, 44, 73, 4, 4, 0, 0, 255, 255, 108, 158, 105, 19, 17, 0, 0, 0}, }, - Meta: common.MapStr{}, + Meta: mapstr.M{}, } - expectedMeta := common.MapStr{ + expectedMeta := mapstr.M{ "field": "decompressed data", } diff --git a/libbeat/processors/actions/detect_mime_type_test.go b/libbeat/processors/actions/detect_mime_type_test.go index 1c8bcecbd0f..a3e79be437f 100644 --- a/libbeat/processors/actions/detect_mime_type_test.go +++ b/libbeat/processors/actions/detect_mime_type_test.go @@ -24,11 +24,12 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestMimeTypeFromTo(t *testing.T) { evt := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "foo.bar.baz": "hello world!", }, } @@ -46,12 +47,12 @@ func TestMimeTypeFromTo(t *testing.T) { func TestMimeTypeFromToMetadata(t *testing.T) { evt := beat.Event{ - Meta: common.MapStr{}, - Fields: common.MapStr{ + Meta: mapstr.M{}, + Fields: mapstr.M{ "foo.bar.baz": "hello world!", }, } - expectedMeta := common.MapStr{ + expectedMeta := mapstr.M{ "field": "text/plain; charset=utf-8", } p, err := NewDetectMimeType(common.MustNewConfigFrom(map[string]interface{}{ @@ -68,7 +69,7 @@ func TestMimeTypeFromToMetadata(t *testing.T) { func TestMimeTypeTestNoMatch(t *testing.T) { evt := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "foo.bar.baz": string([]byte{0, 0}), }, } diff --git a/libbeat/processors/actions/drop_fields.go b/libbeat/processors/actions/drop_fields.go index 540880a750a..30aead42880 100644 --- a/libbeat/processors/actions/drop_fields.go +++ b/libbeat/processors/actions/drop_fields.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" + "github.com/elastic/elastic-agent-libs/mapstr" ) type dropFields struct { @@ -70,7 +71,7 @@ func (f *dropFields) Run(event *beat.Event) (*beat.Event, error) { for _, field := range f.Fields { if err := event.Delete(field); err != nil { - if f.IgnoreMissing && err == common.ErrKeyNotFound { + if f.IgnoreMissing && err == mapstr.ErrKeyNotFound { continue } errs = append(errs, errors.Wrapf(err, "failed to drop field [%v]", field)) diff --git a/libbeat/processors/actions/drop_fields_test.go b/libbeat/processors/actions/drop_fields_test.go index 8125564a6e1..7da44bfe413 100644 --- a/libbeat/processors/actions/drop_fields_test.go +++ b/libbeat/processors/actions/drop_fields_test.go @@ -23,15 +23,15 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDropFieldRun(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "field": "value", }, - Meta: common.MapStr{ + Meta: mapstr.M{ "meta_field": "value", }, } @@ -43,7 +43,7 @@ func TestDropFieldRun(t *testing.T) { newEvent, err := p.Run(event) assert.NoError(t, err) - assert.Equal(t, common.MapStr{}, newEvent.Fields) + assert.Equal(t, mapstr.M{}, newEvent.Fields) assert.Equal(t, event.Meta, newEvent.Meta) }) @@ -54,7 +54,7 @@ func TestDropFieldRun(t *testing.T) { newEvent, err := p.Run(event) assert.NoError(t, err) - assert.Equal(t, common.MapStr{}, newEvent.Meta) + assert.Equal(t, mapstr.M{}, newEvent.Meta) assert.Equal(t, event.Fields, newEvent.Fields) }) } diff --git a/libbeat/processors/actions/extract_field_test.go b/libbeat/processors/actions/extract_field_test.go index 4e95b66fd28..d7e966aaa5f 100644 --- a/libbeat/processors/actions/extract_field_test.go +++ b/libbeat/processors/actions/extract_field_test.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestCommonPaths(t *testing.T) { @@ -86,7 +87,7 @@ func TestCommonPaths(t *testing.T) { }) // Configure input to - input := common.MapStr{ + input := mapstr.M{ test.Field: test.Value, } @@ -116,16 +117,16 @@ func TestCommonPaths(t *testing.T) { }) event := &beat.Event{ - Meta: common.MapStr{}, - Fields: common.MapStr{ + Meta: mapstr.M{}, + Fields: mapstr.M{ "field": "/var/lib/foo/bar", }, } - expectedFields := common.MapStr{ + expectedFields := mapstr.M{ "field": "/var/lib/foo/bar", } - expectedMeta := common.MapStr{ + expectedMeta := mapstr.M{ "field": "bar", } @@ -141,7 +142,7 @@ func TestCommonPaths(t *testing.T) { }) } -func runExtractField(t *testing.T, config *common.Config, input common.MapStr) (*beat.Event, error) { +func runExtractField(t *testing.T, config *common.Config, input mapstr.M) (*beat.Event, error) { logp.TestingSetup() p, err := NewExtractField(config) diff --git a/libbeat/processors/actions/include_fields.go b/libbeat/processors/actions/include_fields.go index 32c404d3a78..64435930620 100644 --- a/libbeat/processors/actions/include_fields.go +++ b/libbeat/processors/actions/include_fields.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" + "github.com/elastic/elastic-agent-libs/mapstr" ) type includeFields struct { @@ -67,7 +68,7 @@ func newIncludeFields(c *common.Config) (processors.Processor, error) { } func (f *includeFields) Run(event *beat.Event) (*beat.Event, error) { - filtered := common.MapStr{} + filtered := mapstr.M{} var errs []string for _, field := range f.Fields { @@ -77,7 +78,7 @@ func (f *includeFields) Run(event *beat.Event) (*beat.Event, error) { } // Ignore ErrKeyNotFound errors - if err != nil && errors.Cause(err) != common.ErrKeyNotFound { + if err != nil && errors.Cause(err) != mapstr.ErrKeyNotFound { errs = append(errs, err.Error()) } } diff --git a/libbeat/processors/actions/include_fields_test.go b/libbeat/processors/actions/include_fields_test.go index 1432a2f32f7..9e5e6199fec 100644 --- a/libbeat/processors/actions/include_fields_test.go +++ b/libbeat/processors/actions/include_fields_test.go @@ -23,36 +23,36 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestIncludeFields(t *testing.T) { var tests = []struct { Fields []string - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M }{ { Fields: []string{"test"}, - Input: common.MapStr{ + Input: mapstr.M{ "hello": "world", "test": 17, }, - Output: common.MapStr{ + Output: mapstr.M{ "test": 17, }, }, { Fields: []string{"test", "a.b"}, - Input: common.MapStr{ + Input: mapstr.M{ "a.b": "b", "a.c": "c", "test": 17, }, - Output: common.MapStr{ + Output: mapstr.M{ "test": 17, - "a": common.MapStr{ + "a": mapstr.M{ "b": "b", }, }, diff --git a/libbeat/processors/actions/rename.go b/libbeat/processors/actions/rename.go index 170888d0b53..e8078366666 100644 --- a/libbeat/processors/actions/rename.go +++ b/libbeat/processors/actions/rename.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type renameFields struct { @@ -105,7 +106,7 @@ func (f *renameFields) renameField(from string, to string, event *beat.Event) er value, err := event.GetValue(from) if err != nil { // Ignore ErrKeyNotFound errors - if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if f.config.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return nil } return fmt.Errorf("could not fetch value for key: %s, Error: %s", from, err) diff --git a/libbeat/processors/actions/rename_test.go b/libbeat/processors/actions/rename_test.go index af9dfdd10c6..d7cce33a900 100644 --- a/libbeat/processors/actions/rename_test.go +++ b/libbeat/processors/actions/rename_test.go @@ -22,11 +22,11 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" ) func TestRenameRun(t *testing.T) { @@ -36,8 +36,8 @@ func TestRenameRun(t *testing.T) { Fields []fromTo IgnoreMissing bool FailOnError bool - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M error bool }{ { @@ -48,10 +48,10 @@ func TestRenameRun(t *testing.T) { To: "b", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a": "c", }, - Output: common.MapStr{ + Output: mapstr.M{ "b": "c", }, IgnoreMissing: false, @@ -66,12 +66,12 @@ func TestRenameRun(t *testing.T) { To: "a.b.c", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a.b": 1, }, - Output: common.MapStr{ - "a": common.MapStr{ - "b": common.MapStr{ + Output: mapstr.M{ + "a": mapstr.M{ + "b": mapstr.M{ "c": 1, }, }, @@ -88,14 +88,14 @@ func TestRenameRun(t *testing.T) { To: "b", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a": 2, "b": "q", }, - Output: common.MapStr{ + Output: mapstr.M{ "a": 2, "b": "q", - "error": common.MapStr{ + "error": mapstr.M{ "message": "Failed to rename fields in processor: target field b already exists, drop or rename this field first", }, }, @@ -115,11 +115,11 @@ func TestRenameRun(t *testing.T) { To: "b", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a": 2, "b": "q", }, - Output: common.MapStr{ + Output: mapstr.M{ "b": 2, "c": "q", }, @@ -135,13 +135,13 @@ func TestRenameRun(t *testing.T) { To: "a.value", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a": 5, "a.b": 6, }, - Output: common.MapStr{ + Output: mapstr.M{ "a.b": 6, - "a": common.MapStr{ + "a": mapstr.M{ "value": 5, }, }, @@ -161,12 +161,12 @@ func TestRenameRun(t *testing.T) { To: "a.c", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a": 7, "c": 8, }, - Output: common.MapStr{ - "a": common.MapStr{ + Output: mapstr.M{ + "a": mapstr.M{ "value": 7, "c": 8, }, @@ -187,14 +187,14 @@ func TestRenameRun(t *testing.T) { To: "a.value", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a": 9, "c": 10, }, - Output: common.MapStr{ + Output: mapstr.M{ "a": 9, "c": 10, - "error": common.MapStr{ + "error": mapstr.M{ "message": "Failed to rename fields in processor: could not put value: a.c: 10, expected map but type is int", }, }, @@ -214,12 +214,12 @@ func TestRenameRun(t *testing.T) { To: "a.value", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "a": 9, "c": 10, }, - Output: common.MapStr{ - "a": common.MapStr{ + Output: mapstr.M{ + "a": mapstr.M{ "value": 9, }, }, @@ -261,8 +261,8 @@ func TestRenameField(t *testing.T) { To string ignoreMissing bool failOnError bool - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M error bool description string }{ @@ -270,10 +270,10 @@ func TestRenameField(t *testing.T) { description: "simple rename of field", From: "a", To: "c", - Input: common.MapStr{ + Input: mapstr.M{ "a": "b", }, - Output: common.MapStr{ + Output: mapstr.M{ "c": "b", }, error: false, @@ -284,12 +284,12 @@ func TestRenameField(t *testing.T) { description: "Add hierarchy to event", From: "a.b", To: "a.b.c", - Input: common.MapStr{ + Input: mapstr.M{ "a.b": 1, }, - Output: common.MapStr{ - "a": common.MapStr{ - "b": common.MapStr{ + Output: mapstr.M{ + "a": mapstr.M{ + "b": mapstr.M{ "c": 1, }, }, @@ -302,11 +302,11 @@ func TestRenameField(t *testing.T) { description: "overwrite an existing field that should lead to an error", From: "a", To: "b", - Input: common.MapStr{ + Input: mapstr.M{ "a": 2, "b": "q", }, - Output: common.MapStr{ + Output: mapstr.M{ "a": 2, "b": "q", }, @@ -318,13 +318,13 @@ func TestRenameField(t *testing.T) { description: "resolve dotted event conflict", From: "a", To: "a.value", - Input: common.MapStr{ + Input: mapstr.M{ "a": 5, "a.b": 6, }, - Output: common.MapStr{ + Output: mapstr.M{ "a.b": 6, - "a": common.MapStr{ + "a": mapstr.M{ "value": 5, }, }, @@ -336,10 +336,10 @@ func TestRenameField(t *testing.T) { description: "try to rename no existing field with failOnError true", From: "a", To: "b", - Input: common.MapStr{ + Input: mapstr.M{ "c": 5, }, - Output: common.MapStr{ + Output: mapstr.M{ "c": 5, }, failOnError: true, @@ -369,12 +369,12 @@ func TestRenameField(t *testing.T) { t.Run("supports metadata as a target", func(t *testing.T) { event := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "a": "c", }, } - expMeta := common.MapStr{ + expMeta := mapstr.M{ "b": "c", } diff --git a/libbeat/processors/actions/replace.go b/libbeat/processors/actions/replace.go index 9dfd84ba9a8..8cad3cf7780 100644 --- a/libbeat/processors/actions/replace.go +++ b/libbeat/processors/actions/replace.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type replaceString struct { @@ -99,7 +100,7 @@ func (f *replaceString) replaceField(field string, pattern *regexp.Regexp, repla currentValue, err := event.GetValue(field) if err != nil { // Ignore ErrKeyNotFound errors - if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if f.config.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return nil } return fmt.Errorf("could not fetch value for key: %s, Error: %s", field, err) diff --git a/libbeat/processors/actions/replace_test.go b/libbeat/processors/actions/replace_test.go index bf54d528583..165a67b93a6 100644 --- a/libbeat/processors/actions/replace_test.go +++ b/libbeat/processors/actions/replace_test.go @@ -25,7 +25,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestReplaceRun(t *testing.T) { @@ -34,8 +34,8 @@ func TestReplaceRun(t *testing.T) { Fields []replaceConfig IgnoreMissing bool FailOnError bool - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M error bool }{ { @@ -47,10 +47,10 @@ func TestReplaceRun(t *testing.T) { Replacement: "b", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "f": "abc", }, - Output: common.MapStr{ + Output: mapstr.M{ "f": "bbc", }, error: false, @@ -66,13 +66,13 @@ func TestReplaceRun(t *testing.T) { Replacement: "b", }, }, - Input: common.MapStr{ - "f": common.MapStr{ + Input: mapstr.M{ + "f": mapstr.M{ "b": "abc", }, }, - Output: common.MapStr{ - "f": common.MapStr{ + Output: mapstr.M{ + "f": mapstr.M{ "b": "bbc", }, }, @@ -94,11 +94,11 @@ func TestReplaceRun(t *testing.T) { Replacement: "oor", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "f": "abbbc", "g": "def", }, - Output: common.MapStr{ + Output: mapstr.M{ "f": "cab", "g": "door", }, @@ -120,14 +120,14 @@ func TestReplaceRun(t *testing.T) { Replacement: "", }, }, - Input: common.MapStr{ + Input: mapstr.M{ "m": "abc", "n": "def", }, - Output: common.MapStr{ + Output: mapstr.M{ "m": "abc", "n": "def", - "error": common.MapStr{ + "error": mapstr.M{ "message": "Failed to replace fields in processor: could not fetch value for key: f, Error: key not found", }, }, @@ -169,8 +169,8 @@ func TestReplaceField(t *testing.T) { Replacement string ignoreMissing bool failOnError bool - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M error bool description string }{ @@ -179,10 +179,10 @@ func TestReplaceField(t *testing.T) { Field: "f", Pattern: regexp.MustCompile(`a`), Replacement: "b", - Input: common.MapStr{ + Input: mapstr.M{ "f": "abc", }, - Output: common.MapStr{ + Output: mapstr.M{ "f": "bbc", }, error: false, @@ -194,13 +194,13 @@ func TestReplaceField(t *testing.T) { Field: "f.b", Pattern: regexp.MustCompile(`a`), Replacement: "b", - Input: common.MapStr{ - "f": common.MapStr{ + Input: mapstr.M{ + "f": mapstr.M{ "b": "abc", }, }, - Output: common.MapStr{ - "f": common.MapStr{ + Output: mapstr.M{ + "f": mapstr.M{ "b": "bbc", }, }, @@ -213,11 +213,11 @@ func TestReplaceField(t *testing.T) { Field: "f", Pattern: regexp.MustCompile(`abc`), Replacement: "xyz", - Input: common.MapStr{ + Input: mapstr.M{ "m": "abc", "n": "def", }, - Output: common.MapStr{ + Output: mapstr.M{ "m": "abc", "n": "def", }, @@ -248,12 +248,12 @@ func TestReplaceField(t *testing.T) { t.Run("supports metadata as a target", func(t *testing.T) { event := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "f": "abc", }, } - expectedMeta := common.MapStr{ + expectedMeta := mapstr.M{ "f": "bbc", } diff --git a/libbeat/processors/actions/truncate_fields.go b/libbeat/processors/actions/truncate_fields.go index 227bbee10fc..e5603de8f28 100644 --- a/libbeat/processors/actions/truncate_fields.go +++ b/libbeat/processors/actions/truncate_fields.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type truncateFieldsConfig struct { @@ -104,7 +105,7 @@ func (f *truncateFields) Run(event *beat.Event) (*beat.Event, error) { func (f *truncateFields) truncateSingleField(field string, event *beat.Event) (*beat.Event, error) { v, err := event.GetValue(field) if err != nil { - if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if f.config.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return event, nil } return event, errors.Wrapf(err, "could not fetch value for key: %s", field) @@ -132,7 +133,7 @@ func (f *truncateFields) addTruncatedString(field, value string, event *beat.Eve } if isTruncated { - common.AddTagsWithKey(event.Fields, "log.flags", []string{"truncated"}) + mapstr.AddTagsWithKey(event.Fields, "log.flags", []string{"truncated"}) } return event, nil @@ -149,7 +150,7 @@ func (f *truncateFields) addTruncatedByte(field string, value []byte, event *bea } if isTruncated { - common.AddTagsWithKey(event.Fields, "log.flags", []string{"truncated"}) + mapstr.AddTagsWithKey(event.Fields, "log.flags", []string{"truncated"}) } return event, nil diff --git a/libbeat/processors/actions/truncate_fields_test.go b/libbeat/processors/actions/truncate_fields_test.go index aac7ee662cc..38a90310565 100644 --- a/libbeat/processors/actions/truncate_fields_test.go +++ b/libbeat/processors/actions/truncate_fields_test.go @@ -21,11 +21,11 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" ) func TestTruncateFields(t *testing.T) { @@ -33,19 +33,19 @@ func TestTruncateFields(t *testing.T) { var tests = map[string]struct { MaxBytes int MaxChars int - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M ShouldError bool TruncateFunc truncater }{ "truncate bytes of too long string line": { MaxBytes: 3, - Input: common.MapStr{ + Input: mapstr.M{ "message": "too long line", }, - Output: common.MapStr{ + Output: mapstr.M{ "message": "too", - "log": common.MapStr{ + "log": mapstr.M{ "flags": []string{"truncated"}, }, }, @@ -54,12 +54,12 @@ func TestTruncateFields(t *testing.T) { }, "truncate bytes of too long byte line": { MaxBytes: 3, - Input: common.MapStr{ + Input: mapstr.M{ "message": []byte("too long line"), }, - Output: common.MapStr{ + Output: mapstr.M{ "message": []byte("too"), - "log": common.MapStr{ + "log": mapstr.M{ "flags": []string{"truncated"}, }, }, @@ -68,10 +68,10 @@ func TestTruncateFields(t *testing.T) { }, "do not truncate short string line": { MaxBytes: 15, - Input: common.MapStr{ + Input: mapstr.M{ "message": "shorter line", }, - Output: common.MapStr{ + Output: mapstr.M{ "message": "shorter line", }, ShouldError: false, @@ -79,10 +79,10 @@ func TestTruncateFields(t *testing.T) { }, "do not truncate short byte line": { MaxBytes: 15, - Input: common.MapStr{ + Input: mapstr.M{ "message": []byte("shorter line"), }, - Output: common.MapStr{ + Output: mapstr.M{ "message": []byte("shorter line"), }, ShouldError: false, @@ -90,10 +90,10 @@ func TestTruncateFields(t *testing.T) { }, "try to truncate integer and get error": { MaxBytes: 5, - Input: common.MapStr{ + Input: mapstr.M{ "message": 42, }, - Output: common.MapStr{ + Output: mapstr.M{ "message": 42, }, ShouldError: true, @@ -101,10 +101,10 @@ func TestTruncateFields(t *testing.T) { }, "do not truncate characters of short byte line": { MaxChars: 6, - Input: common.MapStr{ + Input: mapstr.M{ "message": []byte("ez jó"), // this is good (hungarian) }, - Output: common.MapStr{ + Output: mapstr.M{ "message": []byte("ez jó"), // this is good (hungarian) }, ShouldError: false, @@ -112,10 +112,10 @@ func TestTruncateFields(t *testing.T) { }, "do not truncate bytes of short byte line with multibyte runes": { MaxBytes: 6, - Input: common.MapStr{ + Input: mapstr.M{ "message": []byte("ez jó"), // this is good (hungarian) }, - Output: common.MapStr{ + Output: mapstr.M{ "message": []byte("ez jó"), // this is good (hungarian) }, ShouldError: false, @@ -123,12 +123,12 @@ func TestTruncateFields(t *testing.T) { }, "truncate characters of too long byte line": { MaxChars: 10, - Input: common.MapStr{ + Input: mapstr.M{ "message": []byte("ez egy túl hosszú sor"), // this is a too long line (hungarian) }, - Output: common.MapStr{ + Output: mapstr.M{ "message": []byte("ez egy túl"), // this is a too (hungarian) - "log": common.MapStr{ + "log": mapstr.M{ "flags": []string{"truncated"}, }, }, @@ -137,12 +137,12 @@ func TestTruncateFields(t *testing.T) { }, "truncate bytes of too long byte line with multibyte runes": { MaxBytes: 10, - Input: common.MapStr{ + Input: mapstr.M{ "message": []byte("ez egy túl hosszú sor"), // this is a too long line (hungarian) }, - Output: common.MapStr{ + Output: mapstr.M{ "message": []byte("ez egy tú"), // this is a "to" (hungarian) - "log": common.MapStr{ + "log": mapstr.M{ "flags": []string{"truncated"}, }, }, @@ -191,19 +191,19 @@ func TestTruncateFields(t *testing.T) { } event := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "message": "too long line", }, - Fields: common.MapStr{}, + Fields: mapstr.M{}, } - expFields := common.MapStr{ - "log": common.MapStr{ + expFields := mapstr.M{ + "log": mapstr.M{ "flags": []string{"truncated"}, }, } - expMeta := common.MapStr{ + expMeta := mapstr.M{ "message": "too", } diff --git a/libbeat/processors/add_cloud_metadata/add_cloud_metadata.go b/libbeat/processors/add_cloud_metadata/add_cloud_metadata.go index 974b8f85bf9..57467cb2adf 100644 --- a/libbeat/processors/add_cloud_metadata/add_cloud_metadata.go +++ b/libbeat/processors/add_cloud_metadata/add_cloud_metadata.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -47,7 +48,7 @@ func init() { type addCloudMetadata struct { initOnce sync.Once initData *initData - metadata common.MapStr + metadata mapstr.M logger *logp.Logger } @@ -107,7 +108,7 @@ func (p *addCloudMetadata) init() { }) } -func (p *addCloudMetadata) getMeta() common.MapStr { +func (p *addCloudMetadata) getMeta() mapstr.M { p.init() return p.metadata.Clone() } @@ -129,7 +130,7 @@ func (p *addCloudMetadata) String() string { return "add_cloud_metadata=" + p.getMeta().String() } -func (p *addCloudMetadata) addMeta(event *beat.Event, meta common.MapStr) error { +func (p *addCloudMetadata) addMeta(event *beat.Event, meta mapstr.M) error { for key, metaVal := range meta { // If key exists in event already and overwrite flag is set to false, this processor will not overwrite the // meta fields. For example aws module writes cloud.instance.* to events already, with overwrite=false, diff --git a/libbeat/processors/add_cloud_metadata/http_fetcher.go b/libbeat/processors/add_cloud_metadata/http_fetcher.go index 0af5693526a..70d79bba466 100644 --- a/libbeat/processors/add_cloud_metadata/http_fetcher.go +++ b/libbeat/processors/add_cloud_metadata/http_fetcher.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/tlscommon" + "github.com/elastic/elastic-agent-libs/mapstr" ) type httpMetadataFetcher struct { @@ -41,7 +42,7 @@ type httpMetadataFetcher struct { // to the result according the HTTP response. type responseHandler func(all []byte, res *result) error -type schemaConv func(m map[string]interface{}) common.MapStr +type schemaConv func(m map[string]interface{}) mapstr.M // newMetadataFetcher return metadataFetcher with one pass JSON responseHandler. func newMetadataFetcher( @@ -65,7 +66,7 @@ func newMetadataFetcher( // Some providers require multiple HTTP requests to gather the whole metadata, // len(f.responseHandlers) > 1 indicates that multiple requests are needed. func (f *httpMetadataFetcher) fetchMetadata(ctx context.Context, client http.Client) result { - res := result{provider: f.provider, metadata: common.MapStr{}} + res := result{provider: f.provider, metadata: mapstr.M{}} for url, responseHandler := range f.responseHandlers { f.fetchRaw(ctx, client, url, responseHandler, &res) if res.err != nil { diff --git a/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud.go b/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud.go index cf82c2d621a..02a5e583b27 100644 --- a/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud.go +++ b/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud.go @@ -17,7 +17,10 @@ package add_cloud_metadata -import "github.com/elastic/beats/v7/libbeat/common" +import ( + "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" +) // Alibaba Cloud Metadata Service // Document https://help.aliyun.com/knowledge_detail/49122.html @@ -32,11 +35,11 @@ var alibabaCloudMetadataFetcher = provider{ ecsMetadataRegionURI := "/latest/meta-data/region-id" ecsMetadataZoneURI := "/latest/meta-data/zone-id" - ecsSchema := func(m map[string]interface{}) common.MapStr { - m["service"] = common.MapStr{ + ecsSchema := func(m map[string]interface{}) mapstr.M { + m["service"] = mapstr.M{ "name": "ECS", } - return common.MapStr{"cloud": m} + return mapstr.M{"cloud": m} } urls, err := getMetadataURLs(c, ecsMetadataHost, []string{ diff --git a/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud_test.go b/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud_test.go index 4adb41151b5..0b7be3da790 100644 --- a/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_alibaba_cloud_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func initECSTestServer() *httptest.Server { @@ -68,20 +69,20 @@ func TestRetrieveAlibabaCloudMetadata(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ "provider": "ecs", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "i-wz9g2hqiikg0aliyun2b", }, "region": "cn-shenzhen", "availability_zone": "cn-shenzhen-a", - "service": common.MapStr{ + "service": mapstr.M{ "name": "ECS", }, }, diff --git a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go index 49a49387f98..7c4cfeb806c 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2.go @@ -25,6 +25,7 @@ import ( "net/http" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" @@ -113,7 +114,7 @@ var ec2MetadataFetcher = provider{ Local: true, Create: func(_ string, config *common.Config) (metadataFetcher, error) { - ec2Schema := func(m map[string]interface{}) common.MapStr { + ec2Schema := func(m map[string]interface{}) mapstr.M { m["serviceName"] = "EC2" out, _ := s.Schema{ "instance": s.Object{"id": c.Str("instanceId")}, @@ -126,7 +127,7 @@ var ec2MetadataFetcher = provider{ "account": s.Object{"id": c.Str("accountId")}, "image": s.Object{"id": c.Str("imageId")}, }.Apply(m) - return common.MapStr{"cloud": out} + return mapstr.M{"cloud": out} } headers := make(map[string]string, 1) diff --git a/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go b/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go index 72053c67338..835ff1ff2ce 100644 --- a/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_aws_ec2_test.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func createEC2MockAPI(responseMap map[string]string) *httptest.Server { @@ -96,25 +97,25 @@ func TestRetrieveAWSMetadataEC2(t *testing.T) { testName string ec2ResponseMap map[string]string processorOverwrite bool - previousEvent common.MapStr + previousEvent mapstr.M - expectedEvent common.MapStr + expectedEvent mapstr.M }{ { testName: "all fields from processor", ec2ResponseMap: map[string]string{ec2InstanceIdentityURI: sampleEC2Doc1}, processorOverwrite: false, - previousEvent: common.MapStr{}, - expectedEvent: common.MapStr{ - "cloud": common.MapStr{ + previousEvent: mapstr.M{}, + expectedEvent: mapstr.M{ + "cloud": mapstr.M{ "provider": "aws", - "account": common.MapStr{"id": accountIDDoc1}, - "instance": common.MapStr{"id": instanceIDDoc1}, - "machine": common.MapStr{"type": instanceTypeDoc1}, - "image": common.MapStr{"id": imageIDDoc1}, + "account": mapstr.M{"id": accountIDDoc1}, + "instance": mapstr.M{"id": instanceIDDoc1}, + "machine": mapstr.M{"type": instanceTypeDoc1}, + "image": mapstr.M{"id": imageIDDoc1}, "region": regionDoc1, "availability_zone": availabilityZoneDoc1, - "service": common.MapStr{ + "service": mapstr.M{ "name": "EC2", }, }, @@ -125,14 +126,14 @@ func TestRetrieveAWSMetadataEC2(t *testing.T) { testName: "instanceId pre-informed, no overwrite", ec2ResponseMap: map[string]string{ec2InstanceIdentityURI: sampleEC2Doc1}, processorOverwrite: false, - previousEvent: common.MapStr{ - "cloud": common.MapStr{ - "instance": common.MapStr{"id": instanceIDDoc2}, + previousEvent: mapstr.M{ + "cloud": mapstr.M{ + "instance": mapstr.M{"id": instanceIDDoc2}, }, }, - expectedEvent: common.MapStr{ - "cloud": common.MapStr{ - "instance": common.MapStr{"id": instanceIDDoc2}, + expectedEvent: mapstr.M{ + "cloud": mapstr.M{ + "instance": mapstr.M{"id": instanceIDDoc2}, }, }, }, @@ -144,20 +145,20 @@ func TestRetrieveAWSMetadataEC2(t *testing.T) { testName: "only cloud.provider pre-informed, no overwrite", ec2ResponseMap: map[string]string{ec2InstanceIdentityURI: sampleEC2Doc1}, processorOverwrite: false, - previousEvent: common.MapStr{ + previousEvent: mapstr.M{ "cloud.provider": "aws", }, - expectedEvent: common.MapStr{ + expectedEvent: mapstr.M{ "cloud.provider": "aws", - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", - "account": common.MapStr{"id": accountIDDoc1}, - "instance": common.MapStr{"id": instanceIDDoc1}, - "machine": common.MapStr{"type": instanceTypeDoc1}, - "image": common.MapStr{"id": imageIDDoc1}, + "account": mapstr.M{"id": accountIDDoc1}, + "instance": mapstr.M{"id": instanceIDDoc1}, + "machine": mapstr.M{"type": instanceTypeDoc1}, + "image": mapstr.M{"id": imageIDDoc1}, "region": regionDoc1, "availability_zone": availabilityZoneDoc1, - "service": common.MapStr{ + "service": mapstr.M{ "name": "EC2", }, }, @@ -168,17 +169,17 @@ func TestRetrieveAWSMetadataEC2(t *testing.T) { testName: "all fields from processor, overwrite", ec2ResponseMap: map[string]string{ec2InstanceIdentityURI: sampleEC2Doc1}, processorOverwrite: true, - previousEvent: common.MapStr{}, - expectedEvent: common.MapStr{ - "cloud": common.MapStr{ + previousEvent: mapstr.M{}, + expectedEvent: mapstr.M{ + "cloud": mapstr.M{ "provider": "aws", - "account": common.MapStr{"id": accountIDDoc1}, - "instance": common.MapStr{"id": instanceIDDoc1}, - "machine": common.MapStr{"type": instanceTypeDoc1}, - "image": common.MapStr{"id": imageIDDoc1}, + "account": mapstr.M{"id": accountIDDoc1}, + "instance": mapstr.M{"id": instanceIDDoc1}, + "machine": mapstr.M{"type": instanceTypeDoc1}, + "image": mapstr.M{"id": imageIDDoc1}, "region": regionDoc1, "availability_zone": availabilityZoneDoc1, - "service": common.MapStr{ + "service": mapstr.M{ "name": "EC2", }, }, @@ -189,21 +190,21 @@ func TestRetrieveAWSMetadataEC2(t *testing.T) { testName: "instanceId pre-informed, overwrite", ec2ResponseMap: map[string]string{ec2InstanceIdentityURI: sampleEC2Doc1}, processorOverwrite: true, - previousEvent: common.MapStr{ - "cloud": common.MapStr{ - "instance": common.MapStr{"id": instanceIDDoc2}, + previousEvent: mapstr.M{ + "cloud": mapstr.M{ + "instance": mapstr.M{"id": instanceIDDoc2}, }, }, - expectedEvent: common.MapStr{ - "cloud": common.MapStr{ + expectedEvent: mapstr.M{ + "cloud": mapstr.M{ "provider": "aws", - "account": common.MapStr{"id": accountIDDoc1}, - "instance": common.MapStr{"id": instanceIDDoc1}, - "machine": common.MapStr{"type": instanceTypeDoc1}, - "image": common.MapStr{"id": imageIDDoc1}, + "account": mapstr.M{"id": accountIDDoc1}, + "instance": mapstr.M{"id": instanceIDDoc1}, + "machine": mapstr.M{"type": instanceTypeDoc1}, + "image": mapstr.M{"id": imageIDDoc1}, "region": regionDoc1, "availability_zone": availabilityZoneDoc1, - "service": common.MapStr{ + "service": mapstr.M{ "name": "EC2", }, }, @@ -214,20 +215,20 @@ func TestRetrieveAWSMetadataEC2(t *testing.T) { testName: "only cloud.provider pre-informed, overwrite", ec2ResponseMap: map[string]string{ec2InstanceIdentityURI: sampleEC2Doc1}, processorOverwrite: false, - previousEvent: common.MapStr{ + previousEvent: mapstr.M{ "cloud.provider": "aws", }, - expectedEvent: common.MapStr{ + expectedEvent: mapstr.M{ "cloud.provider": "aws", - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", - "account": common.MapStr{"id": accountIDDoc1}, - "instance": common.MapStr{"id": instanceIDDoc1}, - "machine": common.MapStr{"type": instanceTypeDoc1}, - "image": common.MapStr{"id": imageIDDoc1}, + "account": mapstr.M{"id": accountIDDoc1}, + "instance": mapstr.M{"id": instanceIDDoc1}, + "machine": mapstr.M{"type": instanceTypeDoc1}, + "image": mapstr.M{"id": imageIDDoc1}, "region": regionDoc1, "availability_zone": availabilityZoneDoc1, - "service": common.MapStr{ + "service": mapstr.M{ "name": "EC2", }, }, diff --git a/libbeat/processors/add_cloud_metadata/provider_azure_vm.go b/libbeat/processors/add_cloud_metadata/provider_azure_vm.go index 8ba8a2284b3..aa603965471 100644 --- a/libbeat/processors/add_cloud_metadata/provider_azure_vm.go +++ b/libbeat/processors/add_cloud_metadata/provider_azure_vm.go @@ -21,6 +21,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Azure VM Metadata Service @@ -32,7 +33,7 @@ var azureVMMetadataFetcher = provider{ Create: func(_ string, config *common.Config) (metadataFetcher, error) { azMetadataURI := "/metadata/instance/compute?api-version=2017-04-02" azHeaders := map[string]string{"Metadata": "true"} - azSchema := func(m map[string]interface{}) common.MapStr { + azSchema := func(m map[string]interface{}) mapstr.M { m["serviceName"] = "Virtual Machines" out, _ := s.Schema{ "account": s.Object{ @@ -50,7 +51,7 @@ var azureVMMetadataFetcher = provider{ }, "region": c.Str("location"), }.Apply(m) - return common.MapStr{"cloud": out} + return mapstr.M{"cloud": out} } fetcher, err := newMetadataFetcher(config, "azure", azHeaders, metadataHost, azSchema, azMetadataURI) diff --git a/libbeat/processors/add_cloud_metadata/provider_azure_vm_test.go b/libbeat/processors/add_cloud_metadata/provider_azure_vm_test.go index 5ebaad2c4a1..e629ac188fd 100644 --- a/libbeat/processors/add_cloud_metadata/provider_azure_vm_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_azure_vm_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const azInstanceIdentityDocument = `{ @@ -73,25 +74,25 @@ func TestRetrieveAzureMetadata(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ "provider": "azure", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "04ab04c3-63de-4709-a9f9-9ab8c0411d5e", "name": "test-az-vm", }, - "machine": common.MapStr{ + "machine": mapstr.M{ "type": "Standard_D3_v2", }, - "account": common.MapStr{ + "account": mapstr.M{ "id": "5tfb04c3-63de-4709-a9f9-9ab8c0411d5e", }, - "service": common.MapStr{ + "service": mapstr.M{ "name": "Virtual Machines", }, "region": "eastus2", diff --git a/libbeat/processors/add_cloud_metadata/provider_digital_ocean.go b/libbeat/processors/add_cloud_metadata/provider_digital_ocean.go index 63c90bc5a96..bf9470b08b7 100644 --- a/libbeat/processors/add_cloud_metadata/provider_digital_ocean.go +++ b/libbeat/processors/add_cloud_metadata/provider_digital_ocean.go @@ -21,6 +21,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" + "github.com/elastic/elastic-agent-libs/mapstr" ) // DigitalOcean Metadata Service @@ -30,7 +31,7 @@ var doMetadataFetcher = provider{ Local: true, Create: func(provider string, config *common.Config) (metadataFetcher, error) { - doSchema := func(m map[string]interface{}) common.MapStr { + doSchema := func(m map[string]interface{}) mapstr.M { m["serviceName"] = "Droplets" out, _ := s.Schema{ "instance": s.Object{ @@ -41,7 +42,7 @@ var doMetadataFetcher = provider{ "name": c.Str("serviceName"), }, }.Apply(m) - return common.MapStr{"cloud": out} + return mapstr.M{"cloud": out} } doMetadataURI := "/metadata/v1.json" diff --git a/libbeat/processors/add_cloud_metadata/provider_digital_ocean_test.go b/libbeat/processors/add_cloud_metadata/provider_digital_ocean_test.go index f39bafacc1a..4b8e50420ee 100644 --- a/libbeat/processors/add_cloud_metadata/provider_digital_ocean_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_digital_ocean_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const digitalOceanMetadataV1 = `{ @@ -106,18 +107,18 @@ func TestRetrieveDigitalOceanMetadata(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ "provider": "digitalocean", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "1111111", }, - "service": common.MapStr{ + "service": mapstr.M{ "name": "Droplets", }, "region": "nyc3", diff --git a/libbeat/processors/add_cloud_metadata/provider_google_gce.go b/libbeat/processors/add_cloud_metadata/provider_google_gce.go index d6cb4bb74c1..d05ab059a5e 100644 --- a/libbeat/processors/add_cloud_metadata/provider_google_gce.go +++ b/libbeat/processors/add_cloud_metadata/provider_google_gce.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" + "github.com/elastic/elastic-agent-libs/mapstr" ) type KubeConfig struct { @@ -48,13 +49,13 @@ var gceMetadataFetcher = provider{ Create: func(provider string, config *common.Config) (metadataFetcher, error) { gceMetadataURI := "/computeMetadata/v1/?recursive=true&alt=json" gceHeaders := map[string]string{"Metadata-Flavor": "Google"} - gceSchema := func(m map[string]interface{}) common.MapStr { - cloud := common.MapStr{ - "service": common.MapStr{ + gceSchema := func(m map[string]interface{}) mapstr.M { + cloud := mapstr.M{ + "service": mapstr.M{ "name": "GCE", }, } - meta := common.MapStr{} + meta := mapstr.M{} trimLeadingPath := func(key string) { v, err := cloud.GetValue(key) @@ -124,7 +125,7 @@ var gceMetadataFetcher = provider{ }.ApplyTo(cloud, project) } - meta.DeepUpdate(common.MapStr{"cloud": cloud}) + meta.DeepUpdate(mapstr.M{"cloud": cloud}) return meta } diff --git a/libbeat/processors/add_cloud_metadata/provider_google_gce_test.go b/libbeat/processors/add_cloud_metadata/provider_google_gce_test.go index a6a8754f805..32a16a088d4 100644 --- a/libbeat/processors/add_cloud_metadata/provider_google_gce_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_google_gce_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const gceMetadataV1 = `{ @@ -324,29 +325,29 @@ func TestRetrieveGCEMetadata(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ - "account": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ + "account": mapstr.M{ "id": "test-dev", }, "provider": "gcp", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "3910564293633576924", "name": "test-gce-dev", }, - "machine": common.MapStr{ + "machine": mapstr.M{ "type": "f1-micro", }, "availability_zone": "us-east1-b", - "project": common.MapStr{ + "project": mapstr.M{ "id": "test-dev", }, - "service": common.MapStr{ + "service": mapstr.M{ "name": "GCE", }, }, @@ -372,34 +373,34 @@ func TestRetrieveGCEMetadataInK8s(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ - "account": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ + "account": mapstr.M{ "id": "test-dev", }, "provider": "gcp", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "3910564293633576924", "name": "test-gce-dev", }, - "machine": common.MapStr{ + "machine": mapstr.M{ "type": "f1-micro", }, "availability_zone": "us-east1-b", - "project": common.MapStr{ + "project": mapstr.M{ "id": "test-dev", }, - "service": common.MapStr{ + "service": mapstr.M{ "name": "GCE", }, }, - "orchestrator": common.MapStr{ - "cluster": common.MapStr{ + "orchestrator": mapstr.M{ + "cluster": mapstr.M{ "name": "staging-marketing-k8s", "url": "https://35.223.150.34", }, @@ -428,9 +429,9 @@ func TestRetrieveGCEMetadataInK8sNotOverriden(t *testing.T) { actual, err := p.Run( &beat.Event{ - Fields: common.MapStr{ - "orchestrator": common.MapStr{ - "cluster": common.MapStr{ + Fields: mapstr.M{ + "orchestrator": mapstr.M{ + "cluster": mapstr.M{ "name": "production-marketing-k8s", "url": "https://35.223.150.35", }, @@ -442,29 +443,29 @@ func TestRetrieveGCEMetadataInK8sNotOverriden(t *testing.T) { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ - "account": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ + "account": mapstr.M{ "id": "test-dev", }, "provider": "gcp", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "3910564293633576924", "name": "test-gce-dev", }, - "machine": common.MapStr{ + "machine": mapstr.M{ "type": "f1-micro", }, "availability_zone": "us-east1-b", - "project": common.MapStr{ + "project": mapstr.M{ "id": "test-dev", }, - "service": common.MapStr{ + "service": mapstr.M{ "name": "GCE", }, }, - "orchestrator": common.MapStr{ - "cluster": common.MapStr{ + "orchestrator": mapstr.M{ + "cluster": mapstr.M{ "name": "production-marketing-k8s", "url": "https://35.223.150.35", }, @@ -491,29 +492,29 @@ func TestRetrieveGCEMetadataInK8sPartial(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ - "account": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ + "account": mapstr.M{ "id": "test-dev", }, "provider": "gcp", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "3910564293633576924", "name": "test-gce-dev", }, - "machine": common.MapStr{ + "machine": mapstr.M{ "type": "f1-micro", }, "availability_zone": "us-east1-b", - "project": common.MapStr{ + "project": mapstr.M{ "id": "test-dev", }, - "service": common.MapStr{ + "service": mapstr.M{ "name": "GCE", }, }, diff --git a/libbeat/processors/add_cloud_metadata/provider_huawei_cloud.go b/libbeat/processors/add_cloud_metadata/provider_huawei_cloud.go index 01a87b19c15..b5040a39bf9 100644 --- a/libbeat/processors/add_cloud_metadata/provider_huawei_cloud.go +++ b/libbeat/processors/add_cloud_metadata/provider_huawei_cloud.go @@ -21,6 +21,7 @@ import ( "encoding/json" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type hwMeta struct { @@ -48,11 +49,11 @@ var huaweiMetadataFetcher = provider{ metadataHost := "169.254.169.254" huaweiCloudMetadataJSONURI := "/openstack/latest/meta_data.json" - huaweiCloudSchema := func(m map[string]interface{}) common.MapStr { - m["service"] = common.MapStr{ + huaweiCloudSchema := func(m map[string]interface{}) mapstr.M { + m["service"] = mapstr.M{ "name": "ECS", } - return common.MapStr{"cloud": m} + return mapstr.M{"cloud": m} } urls, err := getMetadataURLs(c, metadataHost, []string{ diff --git a/libbeat/processors/add_cloud_metadata/provider_huawei_cloud_test.go b/libbeat/processors/add_cloud_metadata/provider_huawei_cloud_test.go index b6d9c20bb6d..8c722adb5bc 100644 --- a/libbeat/processors/add_cloud_metadata/provider_huawei_cloud_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_huawei_cloud_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func initHuaweiCloudTestServer() *httptest.Server { @@ -75,20 +76,20 @@ func TestRetrieveHuaweiCloudMetadata(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ "provider": "huawei", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "37da9890-8289-4c58-ba34-a8271c4a8216", }, "region": "cn-east-2", "availability_zone": "cn-east-2b", - "service": common.MapStr{ + "service": mapstr.M{ "name": "ECS", }, }, diff --git a/libbeat/processors/add_cloud_metadata/provider_openstack_nova.go b/libbeat/processors/add_cloud_metadata/provider_openstack_nova.go index 9922e853d2e..6d122cd3cb3 100644 --- a/libbeat/processors/add_cloud_metadata/provider_openstack_nova.go +++ b/libbeat/processors/add_cloud_metadata/provider_openstack_nova.go @@ -19,6 +19,7 @@ package add_cloud_metadata import ( "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -45,11 +46,11 @@ var openstackNovaSSLMetadataFetcher = provider{ func buildOpenstackNovaCreate(scheme string) func(provider string, c *common.Config) (metadataFetcher, error) { return func(provider string, c *common.Config) (metadataFetcher, error) { - osSchema := func(m map[string]interface{}) common.MapStr { - m["service"] = common.MapStr{ + osSchema := func(m map[string]interface{}) mapstr.M { + m["service"] = mapstr.M{ "name": "Nova", } - return common.MapStr{"cloud": m} + return mapstr.M{"cloud": m} } urls, err := getMetadataURLsWithScheme(c, scheme, metadataHost, []string{ diff --git a/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go b/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go index 31a4937343a..53f0852d432 100644 --- a/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_openstack_nova_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func openstackNovaMetadataHandler() http.HandlerFunc { @@ -93,23 +94,23 @@ func assertOpenstackNova(t *testing.T, config *common.Config) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ "provider": "openstack", - "instance": common.MapStr{"" + + "instance": mapstr.M{"" + "id": "i-0000ffac", "name": "testvm01.stack.cloud", }, - "machine": common.MapStr{ + "machine": mapstr.M{ "type": "m1.xlarge", }, "availability_zone": "az-test-2", - "service": common.MapStr{ + "service": mapstr.M{ "name": "Nova", }, }, diff --git a/libbeat/processors/add_cloud_metadata/provider_tencent_cloud.go b/libbeat/processors/add_cloud_metadata/provider_tencent_cloud.go index f562e2e9609..90ed190576e 100644 --- a/libbeat/processors/add_cloud_metadata/provider_tencent_cloud.go +++ b/libbeat/processors/add_cloud_metadata/provider_tencent_cloud.go @@ -17,7 +17,10 @@ package add_cloud_metadata -import "github.com/elastic/beats/v7/libbeat/common" +import ( + "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" +) // Tencent Cloud Metadata Service // Document https://www.qcloud.com/document/product/213/4934 @@ -32,11 +35,11 @@ var qcloudMetadataFetcher = provider{ qcloudMetadataRegionURI := "/meta-data/placement/region" qcloudMetadataZoneURI := "/meta-data/placement/zone" - qcloudSchema := func(m map[string]interface{}) common.MapStr { - m["service"] = common.MapStr{ + qcloudSchema := func(m map[string]interface{}) mapstr.M { + m["service"] = mapstr.M{ "name": "CVM", } - return common.MapStr{"cloud": m} + return mapstr.M{"cloud": m} } urls, err := getMetadataURLs(c, qcloudMetadataHost, []string{ diff --git a/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go b/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go index 5959bf57aef..b3882e06754 100644 --- a/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go +++ b/libbeat/processors/add_cloud_metadata/provider_tencent_cloud_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func initQCloudTestServer() *httptest.Server { @@ -68,20 +69,20 @@ func TestRetrieveQCloudMetadata(t *testing.T) { t.Fatal(err) } - actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) + actual, err := p.Run(&beat.Event{Fields: mapstr.M{}}) if err != nil { t.Fatal(err) } - expected := common.MapStr{ - "cloud": common.MapStr{ + expected := mapstr.M{ + "cloud": mapstr.M{ "provider": "qcloud", - "instance": common.MapStr{ + "instance": mapstr.M{ "id": "ins-qcloudv5", }, "region": "china-south-gz", "availability_zone": "gz-azone2", - "service": common.MapStr{ + "service": mapstr.M{ "name": "CVM", }, }, diff --git a/libbeat/processors/add_cloud_metadata/providers.go b/libbeat/processors/add_cloud_metadata/providers.go index 50346bcb126..72b7c06cce6 100644 --- a/libbeat/processors/add_cloud_metadata/providers.go +++ b/libbeat/processors/add_cloud_metadata/providers.go @@ -26,6 +26,7 @@ import ( "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type provider struct { @@ -45,9 +46,9 @@ type metadataFetcher interface { // result is the result of a query for a specific hosting provider's metadata. type result struct { - provider string // Hosting provider type. - err error // Error that occurred while fetching (if any). - metadata common.MapStr // A specific subset of the metadata received from the hosting provider. + provider string // Hosting provider type. + err error // Error that occurred while fetching (if any). + metadata mapstr.M // A specific subset of the metadata received from the hosting provider. } var cloudMetaProviders = map[string]provider{ diff --git a/libbeat/processors/add_data_stream/add_data_stream.go b/libbeat/processors/add_data_stream/add_data_stream.go index d27f9960b50..8677763963b 100644 --- a/libbeat/processors/add_data_stream/add_data_stream.go +++ b/libbeat/processors/add_data_stream/add_data_stream.go @@ -22,14 +22,14 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/beat/events" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const FieldMetaCustomDataset = "dataset" func SetEventDataset(event *beat.Event, ds string) { if event.Meta == nil { - event.Meta = common.MapStr{ + event.Meta = mapstr.M{ FieldMetaCustomDataset: ds, } } else { @@ -68,7 +68,7 @@ func New(ds DataStream) *AddDataStream { func (p *AddDataStream) Run(event *beat.Event) (*beat.Event, error) { eventDataStream := p.DataStream if event.Meta == nil { - event.Meta = common.MapStr{ + event.Meta = mapstr.M{ events.FieldMetaRawIndex: p.idxNameCache, } } else { @@ -81,7 +81,7 @@ func (p *AddDataStream) Run(event *beat.Event) (*beat.Event, error) { } } if event.Fields == nil { - event.Fields = common.MapStr{} + event.Fields = mapstr.M{} } event.PutValue("event.dataset", eventDataStream.Dataset) event.PutValue("data_stream", eventDataStream) diff --git a/libbeat/processors/add_data_stream/add_data_stream_test.go b/libbeat/processors/add_data_stream/add_data_stream_test.go index 71d981d6034..a87795935e3 100644 --- a/libbeat/processors/add_data_stream/add_data_stream_test.go +++ b/libbeat/processors/add_data_stream/add_data_stream_test.go @@ -24,7 +24,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/beat/events" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestAddDataStream(t *testing.T) { @@ -52,7 +52,7 @@ func TestAddDataStream(t *testing.T) { { "existing meta", simpleDs, - &beat.Event{Meta: common.MapStr{}}, + &beat.Event{Meta: mapstr.M{}}, "mytype-myds-myns", simpleDs, false, @@ -60,7 +60,7 @@ func TestAddDataStream(t *testing.T) { { "custom ds", simpleDs, - &beat.Event{Meta: common.MapStr{ + &beat.Event{Meta: mapstr.M{ FieldMetaCustomDataset: "custom-ds", }}, "mytype-custom-ds-myns", @@ -88,7 +88,7 @@ func TestAddDataStream(t *testing.T) { } require.Equal(t, tt.wantIndex, got.Meta[events.FieldMetaRawIndex]) require.Equal(t, tt.wantDataStream, got.Fields["data_stream"]) - require.Equal(t, tt.wantDataStream.Dataset, got.Fields["event"].(common.MapStr)["dataset"]) + require.Equal(t, tt.wantDataStream.Dataset, got.Fields["event"].(mapstr.M)["dataset"]) }) } } diff --git a/libbeat/processors/add_docker_metadata/add_docker_metadata.go b/libbeat/processors/add_docker_metadata/add_docker_metadata.go index 5282fc27e39..5dd1cd3d7d6 100644 --- a/libbeat/processors/add_docker_metadata/add_docker_metadata.go +++ b/libbeat/processors/add_docker_metadata/add_docker_metadata.go @@ -32,12 +32,13 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/docker" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/metric/system/cgroup" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/actions" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) const ( @@ -188,10 +189,10 @@ func (d *addDockerMetadata) Run(event *beat.Event) (*beat.Event, error) { container := d.watcher.Container(cid) if container != nil { - meta := common.MapStr{} + meta := mapstr.M{} if len(container.Labels) > 0 { - labels := common.MapStr{} + labels := mapstr.M{} for k, v := range container.Labels { if d.dedot { label := common.DeDot(k) diff --git a/libbeat/processors/add_docker_metadata/add_docker_metadata_integration_test.go b/libbeat/processors/add_docker_metadata/add_docker_metadata_integration_test.go index 95fa68e3023..b0f758a8497 100644 --- a/libbeat/processors/add_docker_metadata/add_docker_metadata_integration_test.go +++ b/libbeat/processors/add_docker_metadata/add_docker_metadata_integration_test.go @@ -35,6 +35,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" dockertest "github.com/elastic/beats/v7/libbeat/tests/docker" "github.com/elastic/beats/v7/libbeat/tests/resources" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestAddDockerMetadata(t *testing.T) { @@ -75,20 +76,20 @@ func TestAddDockerMetadata(t *testing.T) { require.NoError(t, err) t.Run("match container by container id", func(t *testing.T) { - input := &beat.Event{Fields: common.MapStr{ + input := &beat.Event{Fields: mapstr.M{ "cid": id, }} result, err := processor.Run(input) require.NoError(t, err) resultLabels, _ := result.Fields.GetValue("container.labels") - expectedLabels := common.MapStr{"label": "foo"} + expectedLabels := mapstr.M{"label": "foo"} assert.Equal(t, expectedLabels, resultLabels) assert.Equal(t, id, result.Fields["cid"]) }) t.Run("match container by process id", func(t *testing.T) { - input := &beat.Event{Fields: common.MapStr{ + input := &beat.Event{Fields: mapstr.M{ "cid": id, "process.pid": pid, }} @@ -96,13 +97,13 @@ func TestAddDockerMetadata(t *testing.T) { require.NoError(t, err) resultLabels, _ := result.Fields.GetValue("container.labels") - expectedLabels := common.MapStr{"label": "foo"} + expectedLabels := mapstr.M{"label": "foo"} assert.Equal(t, expectedLabels, resultLabels) assert.Equal(t, id, result.Fields["cid"]) }) t.Run("don't enrich non existing container", func(t *testing.T) { - input := &beat.Event{Fields: common.MapStr{ + input := &beat.Event{Fields: mapstr.M{ "cid": "notexists", }} result, err := processor.Run(input) diff --git a/libbeat/processors/add_docker_metadata/add_docker_metadata_test.go b/libbeat/processors/add_docker_metadata/add_docker_metadata_test.go index edde24f4184..3c87b2ebea1 100644 --- a/libbeat/processors/add_docker_metadata/add_docker_metadata_test.go +++ b/libbeat/processors/add_docker_metadata/add_docker_metadata_test.go @@ -36,6 +36,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/metric/system/cgroup" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -71,11 +72,11 @@ func TestInitializationNoDocker(t *testing.T) { p, err := buildDockerMetadataProcessor(logp.L(), testConfig, docker.NewWatcher) assert.NoError(t, err, "initializing add_docker_metadata processor") - input := common.MapStr{} + input := mapstr.M{} result, err := p.Run(&beat.Event{Fields: input}) assert.NoError(t, err, "processing an event") - assert.Equal(t, common.MapStr{}, result.Fields) + assert.Equal(t, mapstr.M{}, result.Fields) } func TestInitialization(t *testing.T) { @@ -84,11 +85,11 @@ func TestInitialization(t *testing.T) { p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil)) assert.NoError(t, err, "initializing add_docker_metadata processor") - input := common.MapStr{} + input := mapstr.M{} result, err := p.Run(&beat.Event{Fields: input}) assert.NoError(t, err, "processing an event") - assert.Equal(t, common.MapStr{}, result.Fields) + assert.Equal(t, mapstr.M{}, result.Fields) } func TestNoMatch(t *testing.T) { @@ -100,13 +101,13 @@ func TestNoMatch(t *testing.T) { p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil)) assert.NoError(t, err, "initializing add_docker_metadata processor") - input := common.MapStr{ + input := mapstr.M{ "field": "value", } result, err := p.Run(&beat.Event{Fields: input}) assert.NoError(t, err, "processing an event") - assert.Equal(t, common.MapStr{"field": "value"}, result.Fields) + assert.Equal(t, mapstr.M{"field": "value"}, result.Fields) } func TestMatchNoContainer(t *testing.T) { @@ -118,13 +119,13 @@ func TestMatchNoContainer(t *testing.T) { p, err := buildDockerMetadataProcessor(logp.L(), testConfig, MockWatcherFactory(nil)) assert.NoError(t, err, "initializing add_docker_metadata processor") - input := common.MapStr{ + input := mapstr.M{ "foo": "garbage", } result, err := p.Run(&beat.Event{Fields: input}) assert.NoError(t, err, "processing an event") - assert.Equal(t, common.MapStr{"foo": "garbage"}, result.Fields) + assert.Equal(t, mapstr.M{"foo": "garbage"}, result.Fields) } func TestMatchContainer(t *testing.T) { @@ -149,23 +150,23 @@ func TestMatchContainer(t *testing.T) { })) assert.NoError(t, err, "initializing add_docker_metadata processor") - input := common.MapStr{ + input := mapstr.M{ "foo": "container_id", } result, err := p.Run(&beat.Event{Fields: input}) assert.NoError(t, err, "processing an event") - assert.EqualValues(t, common.MapStr{ - "container": common.MapStr{ + assert.EqualValues(t, mapstr.M{ + "container": mapstr.M{ "id": "container_id", - "image": common.MapStr{ + "image": mapstr.M{ "name": "image", }, - "labels": common.MapStr{ - "a": common.MapStr{ + "labels": mapstr.M{ + "a": mapstr.M{ "x": "1", }, - "b": common.MapStr{ + "b": mapstr.M{ "value": "2", "foo": "3", }, @@ -197,19 +198,19 @@ func TestMatchContainerWithDedot(t *testing.T) { })) assert.NoError(t, err, "initializing add_docker_metadata processor") - input := common.MapStr{ + input := mapstr.M{ "foo": "container_id", } result, err := p.Run(&beat.Event{Fields: input}) assert.NoError(t, err, "processing an event") - assert.EqualValues(t, common.MapStr{ - "container": common.MapStr{ + assert.EqualValues(t, mapstr.M{ + "container": mapstr.M{ "id": "container_id", - "image": common.MapStr{ + "image": mapstr.M{ "name": "image", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "a_x": "1", "b": "2", "b_foo": "3", @@ -246,9 +247,9 @@ func TestMatchSource(t *testing.T) { default: inputSource = "/var/lib/docker/containers/8c147fdfab5a2608fe513d10294bf77cb502a231da9725093a155bd25cd1f14b/foo.log" } - input := common.MapStr{ - "log": common.MapStr{ - "file": common.MapStr{ + input := mapstr.M{ + "log": mapstr.M{ + "file": mapstr.M{ "path": inputSource, }, }, @@ -257,20 +258,20 @@ func TestMatchSource(t *testing.T) { result, err := p.Run(&beat.Event{Fields: input}) assert.NoError(t, err, "processing an event") - assert.EqualValues(t, common.MapStr{ - "container": common.MapStr{ + assert.EqualValues(t, mapstr.M{ + "container": mapstr.M{ "id": "8c147fdfab5a2608fe513d10294bf77cb502a231da9725093a155bd25cd1f14b", - "image": common.MapStr{ + "image": mapstr.M{ "name": "image", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "a": "1", "b": "2", }, "name": "name", }, - "log": common.MapStr{ - "file": common.MapStr{ + "log": mapstr.M{ + "file": mapstr.M{ "path": inputSource, }, }, @@ -298,7 +299,7 @@ func TestDisableSource(t *testing.T) { })) assert.NoError(t, err, "initializing add_docker_metadata processor") - input := common.MapStr{ + input := mapstr.M{ "source": "/var/lib/docker/containers/8c147fdfab5a2608fe513d10294bf77cb502a231da9725093a155bd25cd1f14b/foo.log", } result, err := p.Run(&beat.Event{Fields: input}) @@ -324,13 +325,13 @@ func TestMatchPIDs(t *testing.T) { )) assert.NoError(t, err, "initializing add_docker_metadata processor") - dockerMetadata := common.MapStr{ - "container": common.MapStr{ + dockerMetadata := mapstr.M{ + "container": mapstr.M{ "id": "8c147fdfab5a2608fe513d10294bf77cb502a231da9725093a155bd25cd1f14b", - "image": common.MapStr{ + "image": mapstr.M{ "name": "image", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "a": "1", "b": "2", }, @@ -339,11 +340,11 @@ func TestMatchPIDs(t *testing.T) { } t.Run("pid is not containerized", func(t *testing.T) { - input := common.MapStr{} + input := mapstr.M{} input.Put("process.pid", 2000) input.Put("process.parent.pid", 1000) - expected := common.MapStr{} + expected := mapstr.M{} expected.DeepUpdate(input) result, err := p.Run(&beat.Event{Fields: input}) @@ -352,10 +353,10 @@ func TestMatchPIDs(t *testing.T) { }) t.Run("pid does not exist", func(t *testing.T) { - input := common.MapStr{} + input := mapstr.M{} input.Put("process.pid", 9999) - expected := common.MapStr{} + expected := mapstr.M{} expected.DeepUpdate(input) result, err := p.Run(&beat.Event{Fields: input}) @@ -364,10 +365,10 @@ func TestMatchPIDs(t *testing.T) { }) t.Run("pid is containerized", func(t *testing.T) { - fields := common.MapStr{} + fields := mapstr.M{} fields.Put("process.pid", "1000") - expected := common.MapStr{} + expected := mapstr.M{} expected.DeepUpdate(dockerMetadata) expected.DeepUpdate(fields) @@ -377,11 +378,11 @@ func TestMatchPIDs(t *testing.T) { }) t.Run("pid exited and ppid is containerized", func(t *testing.T) { - fields := common.MapStr{} + fields := mapstr.M{} fields.Put("process.pid", 9999) fields.Put("process.parent.pid", 1000) - expected := common.MapStr{} + expected := mapstr.M{} expected.DeepUpdate(dockerMetadata) expected.DeepUpdate(fields) @@ -391,10 +392,10 @@ func TestMatchPIDs(t *testing.T) { }) t.Run("cgroup error", func(t *testing.T) { - fields := common.MapStr{} + fields := mapstr.M{} fields.Put("process.pid", 3000) - expected := common.MapStr{} + expected := mapstr.M{} expected.DeepUpdate(fields) result, err := p.Run(&beat.Event{Fields: fields}) diff --git a/libbeat/processors/add_formatted_index/add_formatted_index.go b/libbeat/processors/add_formatted_index/add_formatted_index.go index bd4e542b14f..9022b69cff9 100644 --- a/libbeat/processors/add_formatted_index/add_formatted_index.go +++ b/libbeat/processors/add_formatted_index/add_formatted_index.go @@ -22,8 +22,8 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/beat/events" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/fmtstr" + "github.com/elastic/elastic-agent-libs/mapstr" ) // AddFormattedIndex is a Processor to set an event's "raw_index" metadata field @@ -47,7 +47,7 @@ func (p *AddFormattedIndex) Run(event *beat.Event) (*beat.Event, error) { } if event.Meta == nil { - event.Meta = common.MapStr{} + event.Meta = mapstr.M{} } event.Meta[events.FieldMetaRawIndex] = index return event, nil diff --git a/libbeat/processors/add_host_metadata/add_host_metadata.go b/libbeat/processors/add_host_metadata/add_host_metadata.go index 69612bd651f..17eba5c5d56 100644 --- a/libbeat/processors/add_host_metadata/add_host_metadata.go +++ b/libbeat/processors/add_host_metadata/add_host_metadata.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" "github.com/elastic/beats/v7/libbeat/processors/util" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo" ) @@ -44,8 +45,8 @@ type addHostMetadata struct { time.Time sync.Mutex } - data common.MapStrPointer - geoData common.MapStr + data mapstr.Pointer + geoData mapstr.M config Config logger *logp.Logger } @@ -63,7 +64,7 @@ func New(cfg *common.Config) (processors.Processor, error) { p := &addHostMetadata{ config: config, - data: common.NewMapStrPointer(nil), + data: mapstr.NewPointer(nil), logger: logp.NewLogger("add_host_metadata"), } p.loadData() @@ -73,7 +74,7 @@ func New(cfg *common.Config) (processors.Processor, error) { if err != nil { return nil, err } - p.geoData = common.MapStr{"host": common.MapStr{"geo": geoFields}} + p.geoData = mapstr.M{"host": mapstr.M{"geo": geoFields}} } return p, nil @@ -162,7 +163,7 @@ func skipAddingHostMetadata(event *beat.Event) bool { } switch m := hostFields.(type) { - case common.MapStr: + case mapstr.M: // if "name" is the only field, don't skip hasName, _ := m.HasKey("name") if hasName && len(m) == 1 { @@ -170,7 +171,7 @@ func skipAddingHostMetadata(event *beat.Event) bool { } return true case map[string]interface{}: - hostMapStr := common.MapStr(m) + hostMapStr := mapstr.M(m) // if "name" is the only field, don't skip hasName, _ := hostMapStr.HasKey("name") if hasName && len(m) == 1 { diff --git a/libbeat/processors/add_host_metadata/add_host_metadata_test.go b/libbeat/processors/add_host_metadata/add_host_metadata_test.go index 6120269395c..f7f9b787514 100644 --- a/libbeat/processors/add_host_metadata/add_host_metadata_test.go +++ b/libbeat/processors/add_host_metadata/add_host_metadata_test.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo/types" ) @@ -38,7 +39,7 @@ var ( func TestConfigDefault(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } testConfig, err := common.NewConfigFrom(map[string]interface{}{}) @@ -83,7 +84,7 @@ func TestConfigDefault(t *testing.T) { func TestConfigNetInfoDisabled(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } testConfig, err := common.NewConfigFrom(map[string]interface{}{ @@ -130,7 +131,7 @@ func TestConfigNetInfoDisabled(t *testing.T) { func TestConfigName(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } @@ -158,7 +159,7 @@ func TestConfigName(t *testing.T) { func TestConfigGeoEnabled(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } @@ -190,7 +191,7 @@ func TestConfigGeoEnabled(t *testing.T) { func TestConfigGeoDisabled(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } @@ -236,8 +237,8 @@ func TestEventWithReplaceFieldsFalse(t *testing.T) { { "replace_fields=false with only host.name", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "name": hostName, }, }, @@ -249,8 +250,8 @@ func TestEventWithReplaceFieldsFalse(t *testing.T) { { "replace_fields=false with only host.id", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "id": hostID, }, }, @@ -262,8 +263,8 @@ func TestEventWithReplaceFieldsFalse(t *testing.T) { { "replace_fields=false with host.name and host.id", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "name": hostName, "id": hostID, }, @@ -282,10 +283,10 @@ func TestEventWithReplaceFieldsFalse(t *testing.T) { v, err := newEvent.GetValue("host") assert.NoError(t, err) - assert.Equal(t, c.hostLengthLargerThanOne, len(v.(common.MapStr)) > 1) - assert.Equal(t, c.hostLengthEqualsToOne, len(v.(common.MapStr)) == 1) + assert.Equal(t, c.hostLengthLargerThanOne, len(v.(mapstr.M)) > 1) + assert.Equal(t, c.hostLengthEqualsToOne, len(v.(mapstr.M)) == 1) if c.expectedHostFieldLength != -1 { - assert.Equal(t, c.expectedHostFieldLength, len(v.(common.MapStr))) + assert.Equal(t, c.expectedHostFieldLength, len(v.(mapstr.M))) } }) } @@ -315,8 +316,8 @@ func TestEventWithReplaceFieldsTrue(t *testing.T) { { "replace_fields=true with host.name", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "name": hostName, }, }, @@ -327,8 +328,8 @@ func TestEventWithReplaceFieldsTrue(t *testing.T) { { "replace_fields=true with host.id", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "id": hostID, }, }, @@ -339,8 +340,8 @@ func TestEventWithReplaceFieldsTrue(t *testing.T) { { "replace_fields=true with host.name and host.id", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "name": hostName, "id": hostID, }, @@ -358,8 +359,8 @@ func TestEventWithReplaceFieldsTrue(t *testing.T) { v, err := newEvent.GetValue("host") assert.NoError(t, err) - assert.Equal(t, c.hostLengthLargerThanOne, len(v.(common.MapStr)) > 1) - assert.Equal(t, c.hostLengthEqualsToOne, len(v.(common.MapStr)) == 1) + assert.Equal(t, c.hostLengthLargerThanOne, len(v.(mapstr.M)) > 1) + assert.Equal(t, c.hostLengthEqualsToOne, len(v.(mapstr.M)) == 1) }) } } @@ -383,8 +384,8 @@ func TestSkipAddingHostMetadata(t *testing.T) { { "event only with host.name", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "name": hostName, }, }, @@ -394,8 +395,8 @@ func TestSkipAddingHostMetadata(t *testing.T) { { "event only with host.id", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "id": hostID, }, }, @@ -405,8 +406,8 @@ func TestSkipAddingHostMetadata(t *testing.T) { { "event with host.name and host.id", beat.Event{ - Fields: common.MapStr{ - "host": common.MapStr{ + Fields: mapstr.M{ + "host": mapstr.M{ "name": hostName, "id": hostID, }, @@ -417,14 +418,14 @@ func TestSkipAddingHostMetadata(t *testing.T) { { "event without host field", beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, }, false, }, { "event with field type map[string]string hostID", beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "host": hostIDMap, }, }, @@ -433,7 +434,7 @@ func TestSkipAddingHostMetadata(t *testing.T) { { "event with field type map[string]string host name", beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "host": hostNameMap, }, }, @@ -442,7 +443,7 @@ func TestSkipAddingHostMetadata(t *testing.T) { { "event with field type map[string]string host ID and name", beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "host": hostIDNameMap, }, }, @@ -451,7 +452,7 @@ func TestSkipAddingHostMetadata(t *testing.T) { { "event with field type string", beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "host": "string", }, }, diff --git a/libbeat/processors/add_id/add_id_test.go b/libbeat/processors/add_id/add_id_test.go index a50349f22f0..2c6923ba8dc 100644 --- a/libbeat/processors/add_id/add_id_test.go +++ b/libbeat/processors/add_id/add_id_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/beat" @@ -42,14 +43,14 @@ func TestDefaultTargetField(t *testing.T) { } func TestNonDefaultTargetField(t *testing.T) { - cfg := common.MustNewConfigFrom(common.MapStr{ + cfg := common.MustNewConfigFrom(mapstr.M{ "target_field": "foo", }) p, err := New(cfg) assert.NoError(t, err) testEvent := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, } newEvent, err := p.Run(testEvent) @@ -65,14 +66,14 @@ func TestNonDefaultTargetField(t *testing.T) { } func TestNonDefaultMetadataTarget(t *testing.T) { - cfg := common.MustNewConfigFrom(common.MapStr{ + cfg := common.MustNewConfigFrom(mapstr.M{ "target_field": "@metadata.foo", }) p, err := New(cfg) assert.NoError(t, err) testEvent := &beat.Event{ - Meta: common.MapStr{}, + Meta: mapstr.M{}, } newEvent, err := p.Run(testEvent) diff --git a/libbeat/processors/add_kubernetes_metadata/cache.go b/libbeat/processors/add_kubernetes_metadata/cache.go index 95e6fd19b24..ed2307135fb 100644 --- a/libbeat/processors/add_kubernetes_metadata/cache.go +++ b/libbeat/processors/add_kubernetes_metadata/cache.go @@ -24,14 +24,14 @@ import ( "sync" "time" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type cache struct { sync.Mutex timeout time.Duration deleted map[string]time.Time // key -> when should this obj be deleted - metadata map[string]common.MapStr + metadata map[string]mapstr.M done chan struct{} } @@ -39,14 +39,14 @@ func newCache(cleanupTimeout time.Duration) *cache { c := &cache{ timeout: cleanupTimeout, deleted: make(map[string]time.Time), - metadata: make(map[string]common.MapStr), + metadata: make(map[string]mapstr.M), done: make(chan struct{}), } go c.cleanup() return c } -func (c *cache) get(key string) common.MapStr { +func (c *cache) get(key string) mapstr.M { c.Lock() defer c.Unlock() // add lifecycle if key was queried @@ -62,7 +62,7 @@ func (c *cache) delete(key string) { c.deleted[key] = time.Now().Add(c.timeout) } -func (c *cache) set(key string, data common.MapStr) { +func (c *cache) set(key string, data mapstr.M) { c.Lock() defer c.Unlock() delete(c.deleted, key) diff --git a/libbeat/processors/add_kubernetes_metadata/indexers.go b/libbeat/processors/add_kubernetes_metadata/indexers.go index 84e3c3dd877..1c770459af5 100644 --- a/libbeat/processors/add_kubernetes_metadata/indexers.go +++ b/libbeat/processors/add_kubernetes_metadata/indexers.go @@ -21,6 +21,7 @@ import ( "fmt" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" @@ -50,7 +51,7 @@ type Indexer interface { // MetadataIndex holds a pair of index -> metadata info type MetadataIndex struct { Index string - Data common.MapStr + Data mapstr.M } type Indexers struct { diff --git a/libbeat/processors/add_kubernetes_metadata/indexers_test.go b/libbeat/processors/add_kubernetes_metadata/indexers_test.go index 9897a6534de..e20f07db8b4 100644 --- a/libbeat/processors/add_kubernetes_metadata/indexers_test.go +++ b/libbeat/processors/add_kubernetes_metadata/indexers_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" @@ -64,18 +65,18 @@ func TestPodIndexer(t *testing.T) { assert.Equal(t, len(indexers), 1) assert.Equal(t, indexers[0].Index, fmt.Sprintf("%s/%s", ns, podName)) - expected := common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + expected := mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": "127.0.0.5", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "labelkey": "labelvalue", }, "namespace": "testns", - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, }, @@ -119,18 +120,18 @@ func TestPodUIDIndexer(t *testing.T) { assert.Equal(t, len(indexers), 1) assert.Equal(t, indexers[0].Index, uid) - expected := common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + expected := mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": "127.0.0.5", }, "namespace": "testns", - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "labelkey": "labelvalue", }, }, @@ -177,18 +178,18 @@ func TestContainerIndexer(t *testing.T) { indices := conIndexer.GetIndexes(&pod) assert.Equal(t, len(indexers), 0) assert.Equal(t, len(indices), 0) - expected := common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + expected := mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": "127.0.0.5", }, "namespace": "testns", - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "labelkey": "labelvalue", }, }, @@ -232,7 +233,7 @@ func TestContainerIndexer(t *testing.T) { assert.Equal(t, indices[2], "klmno") expected.Put("kubernetes.container", - common.MapStr{ + mapstr.M{ "name": container, "image": containerImage, "id": "abcde", @@ -241,7 +242,7 @@ func TestContainerIndexer(t *testing.T) { assert.Equal(t, expected.String(), indexers[0].Data.String()) expected.Put("kubernetes.container", - common.MapStr{ + mapstr.M{ "name": initContainer, "image": initContainerImage, "id": "fghij", @@ -250,7 +251,7 @@ func TestContainerIndexer(t *testing.T) { assert.Equal(t, expected.String(), indexers[1].Data.String()) expected.Put("kubernetes.container", - common.MapStr{ + mapstr.M{ "name": ephemeralContainer, "image": ephemeralContainerImage, "id": "klmno", @@ -289,7 +290,7 @@ func TestFilteredGenMeta(t *testing.T) { rawLabels, _ := indexers[0].Data.GetValue("kubernetes.labels") assert.NotNil(t, rawLabels) - labelMap, ok := rawLabels.(common.MapStr) + labelMap, ok := rawLabels.(mapstr.M) assert.Equal(t, ok, true) assert.Equal(t, len(labelMap), 2) @@ -313,7 +314,7 @@ func TestFilteredGenMeta(t *testing.T) { rawLabels, _ = indexers[0].Data.GetValue("kubernetes.labels") assert.NotNil(t, rawLabels) - labelMap, ok = rawLabels.(common.MapStr) + labelMap, ok = rawLabels.(mapstr.M) assert.Equal(t, ok, true) assert.Equal(t, len(labelMap), 1) @@ -322,7 +323,7 @@ func TestFilteredGenMeta(t *testing.T) { rawAnnotations, _ = indexers[0].Data.GetValue("kubernetes.annotations") assert.NotNil(t, rawAnnotations) - annotationsMap, ok := rawAnnotations.(common.MapStr) + annotationsMap, ok := rawAnnotations.(mapstr.M) assert.Equal(t, ok, true) assert.Equal(t, len(annotationsMap), 1) @@ -370,7 +371,7 @@ func TestFilteredGenMetaExclusion(t *testing.T) { rawLabels, _ := indexers[0].Data.GetValue("kubernetes.labels") assert.NotNil(t, rawLabels) - labelMap, ok := rawLabels.(common.MapStr) + labelMap, ok := rawLabels.(mapstr.M) assert.Equal(t, ok, true) assert.Equal(t, len(labelMap), 1) @@ -425,18 +426,18 @@ func TestIpPortIndexer(t *testing.T) { _, err = indexers[0].Data.GetValue("kubernetes.container.name") assert.Error(t, err) - expected := common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + expected := mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", "ip": "1.2.3.4", }, "namespace": "testns", - "node": common.MapStr{ + "node": mapstr.M{ "name": "testnode", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "labelkey": "labelvalue", }, }, @@ -477,7 +478,7 @@ func TestIpPortIndexer(t *testing.T) { assert.Equal(t, expected.String(), indexers[0].Data.String()) expected.Put("kubernetes.container", - common.MapStr{ + mapstr.M{ "name": container, "image": containerImage, "id": "foobar", diff --git a/libbeat/processors/add_kubernetes_metadata/kubernetes.go b/libbeat/processors/add_kubernetes_metadata/kubernetes.go index cdb182e4d31..33322f56c3f 100644 --- a/libbeat/processors/add_kubernetes_metadata/kubernetes.go +++ b/libbeat/processors/add_kubernetes_metadata/kubernetes.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -287,7 +288,7 @@ func (k *kubernetesAnnotator) Run(event *beat.Event) (*beat.Event, error) { } cmeta, err := metaClone.Clone().GetValue("kubernetes.container") if err == nil { - event.Fields.DeepUpdate(common.MapStr{ + event.Fields.DeepUpdate(mapstr.M{ "container": cmeta, }) } diff --git a/libbeat/processors/add_kubernetes_metadata/kubernetes_test.go b/libbeat/processors/add_kubernetes_metadata/kubernetes_test.go index 063beedcef1..5d64fb4f84c 100644 --- a/libbeat/processors/add_kubernetes_metadata/kubernetes_test.go +++ b/libbeat/processors/add_kubernetes_metadata/kubernetes_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Test Annotator is skipped if kubernetes metadata already exist @@ -49,10 +50,10 @@ func TestAnnotatorSkipped(t *testing.T) { } processor.cache.set("foo", - common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ - "labels": common.MapStr{ + mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ + "labels": mapstr.M{ "added": "should not", }, }, @@ -60,12 +61,12 @@ func TestAnnotatorSkipped(t *testing.T) { }) event, err := processor.Run(&beat.Event{ - Fields: common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + Fields: mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "foo", "id": "pod_id", - "metrics": common.MapStr{ + "metrics": mapstr.M{ "a": 1, "b": 2, }, @@ -75,12 +76,12 @@ func TestAnnotatorSkipped(t *testing.T) { }) assert.NoError(t, err) - assert.Equal(t, common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + assert.Equal(t, mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "foo", "id": "pod_id", - "metrics": common.MapStr{ + "metrics": mapstr.M{ "a": 1, "b": 2, }, @@ -107,12 +108,12 @@ func TestAnnotatorWithNoKubernetesAvailable(t *testing.T) { kubernetesAvailable: false, } - intialEventMap := common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + intialEventMap := mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": "foo", "id": "pod_id", - "metrics": common.MapStr{ + "metrics": mapstr.M{ "a": 1, "b": 2, }, diff --git a/libbeat/processors/add_kubernetes_metadata/matchers.go b/libbeat/processors/add_kubernetes_metadata/matchers.go index 291afafbd2a..1ff2f3553f7 100644 --- a/libbeat/processors/add_kubernetes_metadata/matchers.go +++ b/libbeat/processors/add_kubernetes_metadata/matchers.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/outputs/codec" "github.com/elastic/beats/v7/libbeat/outputs/codec/format" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -38,7 +39,7 @@ type Matcher interface { // MetadataIndex returns the index string to use in annotation lookups for the given // event. A previous indexer should have generated that index for this to work // This function can return "" if the event doesn't match - MetadataIndex(event common.MapStr) string + MetadataIndex(event mapstr.M) string } type Matchers struct { @@ -73,7 +74,7 @@ func NewMatchers(configs PluginConfig) *Matchers { } // MetadataIndex returns the index string for the first matcher from the Registry returning one -func (m *Matchers) MetadataIndex(event common.MapStr) string { +func (m *Matchers) MetadataIndex(event mapstr.M) string { for _, matcher := range m.matchers { index := matcher.MetadataIndex(event) if index != "" { @@ -114,7 +115,7 @@ func NewFieldMatcher(cfg common.Config) (Matcher, error) { return &FieldMatcher{MatchFields: config.LookupFields}, nil } -func (f *FieldMatcher) MetadataIndex(event common.MapStr) string { +func (f *FieldMatcher) MetadataIndex(event mapstr.M) string { for _, field := range f.MatchFields { keyIface, err := event.GetValue(field) if err == nil { @@ -152,7 +153,7 @@ func NewFieldFormatMatcher(cfg common.Config) (Matcher, error) { } -func (f *FieldFormatMatcher) MetadataIndex(event common.MapStr) string { +func (f *FieldFormatMatcher) MetadataIndex(event mapstr.M) string { bytes, err := f.Codec.Encode("", &beat.Event{ Fields: event, }) diff --git a/libbeat/processors/add_kubernetes_metadata/matchers_test.go b/libbeat/processors/add_kubernetes_metadata/matchers_test.go index c6b6a98d42c..f3c3814ceb3 100644 --- a/libbeat/processors/add_kubernetes_metadata/matchers_test.go +++ b/libbeat/processors/add_kubernetes_metadata/matchers_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFieldMatcher(t *testing.T) { @@ -42,14 +43,14 @@ func TestFieldMatcher(t *testing.T) { assert.NotNil(t, matcher) assert.NoError(t, err) - input := common.MapStr{ + input := mapstr.M{ "foo": "bar", } out := matcher.MetadataIndex(input) assert.Equal(t, out, "bar") - nonMatchInput := common.MapStr{ + nonMatchInput := mapstr.M{ "not": "match", } @@ -72,7 +73,7 @@ func TestFieldFormatMatcher(t *testing.T) { assert.NotNil(t, matcher) assert.NoError(t, err) - event := common.MapStr{ + event := mapstr.M{ "namespace": "foo", "pod": "bar", } @@ -80,7 +81,7 @@ func TestFieldFormatMatcher(t *testing.T) { out := matcher.MetadataIndex(event) assert.Equal(t, "foo/bar", out) - event = common.MapStr{ + event = mapstr.M{ "foo": "bar", } out = matcher.MetadataIndex(event) @@ -92,8 +93,8 @@ func TestFieldFormatMatcher(t *testing.T) { assert.NotNil(t, matcher) assert.NoError(t, err) - event = common.MapStr{ - "dimensions": common.MapStr{ + event = mapstr.M{ + "dimensions": mapstr.M{ "pod": "bar", "namespace": "foo", }, diff --git a/libbeat/processors/add_locale/add_locale_test.go b/libbeat/processors/add_locale/add_locale_test.go index d3bb8c71b2b..5f454146c58 100644 --- a/libbeat/processors/add_locale/add_locale_test.go +++ b/libbeat/processors/add_locale/add_locale_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestExportTimezone(t *testing.T) { @@ -37,13 +38,13 @@ func TestExportTimezone(t *testing.T) { t.Fatal(err) } - input := common.MapStr{} + input := mapstr.M{} zone, _ := time.Now().In(time.Local).Zone() actual := getActualValue(t, testConfig, input) - expected := common.MapStr{ + expected := mapstr.M{ "event": map[string]string{ "timezone": zone, }, @@ -84,7 +85,7 @@ func TestTimezoneFormat(t *testing.T) { assert.Regexp(t, regexp.MustCompile(`\-[\d]{2}\:[\d]{2}`), negVal) } -func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr { +func getActualValue(t *testing.T, config *common.Config, input mapstr.M) mapstr.M { log := logp.NewLogger("add_locale_test") p, err := New(config) if err != nil { @@ -99,7 +100,7 @@ func getActualValue(t *testing.T, config *common.Config, input common.MapStr) co func BenchmarkConstruct(b *testing.B) { var testConfig = common.NewConfig() - input := common.MapStr{} + input := mapstr.M{} p, err := New(testConfig) if err != nil { diff --git a/libbeat/processors/add_observer_metadata/add_observer_metadata.go b/libbeat/processors/add_observer_metadata/add_observer_metadata.go index ce16a9ca65f..9ddcd353e18 100644 --- a/libbeat/processors/add_observer_metadata/add_observer_metadata.go +++ b/libbeat/processors/add_observer_metadata/add_observer_metadata.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" "github.com/elastic/beats/v7/libbeat/processors/util" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo" ) @@ -43,8 +44,8 @@ type observerMetadata struct { time.Time sync.Mutex } - data common.MapStrPointer - geoData common.MapStr + data mapstr.Pointer + geoData mapstr.M config Config logger *logp.Logger } @@ -62,7 +63,7 @@ func New(cfg *common.Config) (processors.Processor, error) { p := &observerMetadata{ config: config, - data: common.NewMapStrPointer(nil), + data: mapstr.NewPointer(nil), logger: logp.NewLogger("add_observer_metadata"), } p.loadData() @@ -73,7 +74,7 @@ func New(cfg *common.Config) (processors.Processor, error) { return nil, err } - p.geoData = common.MapStr{"observer": common.MapStr{"geo": geoFields}} + p.geoData = mapstr.M{"observer": mapstr.M{"geo": geoFields}} } return p, nil @@ -128,8 +129,8 @@ func (p *observerMetadata) loadData() error { } hostInfo := h.Info() - data := common.MapStr{ - "observer": common.MapStr{ + data := mapstr.M{ + "observer": mapstr.M{ "hostname": hostInfo.Hostname, }, } diff --git a/libbeat/processors/add_observer_metadata/add_observer_metadata_test.go b/libbeat/processors/add_observer_metadata/add_observer_metadata_test.go index 3932d193d78..f7d670f37d7 100644 --- a/libbeat/processors/add_observer_metadata/add_observer_metadata_test.go +++ b/libbeat/processors/add_observer_metadata/add_observer_metadata_test.go @@ -26,11 +26,12 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConfigDefault(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } testConfig, err := common.NewConfigFrom(map[string]interface{}{}) @@ -52,7 +53,7 @@ func TestConfigDefault(t *testing.T) { func TestOverwriteFalse(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{"observer": common.MapStr{"foo": "bar"}}, + Fields: mapstr.M{"observer": mapstr.M{"foo": "bar"}}, Timestamp: time.Now(), } testConfig, err := common.NewConfigFrom(map[string]interface{}{}) @@ -65,12 +66,12 @@ func TestOverwriteFalse(t *testing.T) { v, err := newEvent.GetValue("observer") require.NoError(t, err) - assert.Equal(t, common.MapStr{"foo": "bar"}, v) + assert.Equal(t, mapstr.M{"foo": "bar"}, v) } func TestOverwriteTrue(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{"observer": common.MapStr{"foo": "bar"}}, + Fields: mapstr.M{"observer": mapstr.M{"foo": "bar"}}, Timestamp: time.Now(), } testConfig, err := common.NewConfigFrom(map[string]interface{}{"overwrite": true}) @@ -88,7 +89,7 @@ func TestOverwriteTrue(t *testing.T) { func TestConfigNetInfoDisabled(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } testConfig, err := common.NewConfigFrom(map[string]interface{}{ @@ -112,7 +113,7 @@ func TestConfigNetInfoDisabled(t *testing.T) { func TestConfigGeoEnabled(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } @@ -144,7 +145,7 @@ func TestConfigGeoEnabled(t *testing.T) { func TestConfigGeoDisabled(t *testing.T) { event := &beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, Timestamp: time.Now(), } diff --git a/libbeat/processors/add_process_metadata/add_process_metadata.go b/libbeat/processors/add_process_metadata/add_process_metadata.go index 769571a0de7..27fc8299e7c 100644 --- a/libbeat/processors/add_process_metadata/add_process_metadata.go +++ b/libbeat/processors/add_process_metadata/add_process_metadata.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/libbeat/processors" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -64,7 +65,7 @@ type addProcessMetadata struct { cgroupsCache *common.Cache cidProvider cidProvider log *logp.Logger - mappings common.MapStr + mappings mapstr.M } type processMetadata struct { @@ -74,7 +75,7 @@ type processMetadata struct { startTime time.Time pid, ppid int // - fields common.MapStr + fields mapstr.M } type processMetadataProvider interface { @@ -148,7 +149,7 @@ func newProcessMetadataProcessorWithProvider(cfg *common.Config, provider proces } // check if the value exist in mapping -func containsValue(m common.MapStr, v string) bool { +func containsValue(m mapstr.M, v string) bool { for _, x := range m { if x == v { return true @@ -163,7 +164,7 @@ func (p *addProcessMetadata) Run(event *beat.Event) (*beat.Event, error) { result, err := p.enrich(event, pidField) if err != nil { switch err { - case common.ErrKeyNotFound: + case mapstr.ErrKeyNotFound: continue case ErrNoProcess: return event, err @@ -218,13 +219,13 @@ func (p *addProcessMetadata) enrich(event *beat.Event, pidField string) (result return nil, errors.Wrapf(err, "cannot parse pid field '%s'", pidField) } - var meta common.MapStr + var meta mapstr.M metaPtr, err := p.provider.GetProcessMetadata(pid) if err != nil || metaPtr == nil { // no process metadata, lets still try to get container id p.log.Debugf("failed to get process metadata for PID=%d: %v", pid, err) - meta = common.MapStr{} + meta = mapstr.M{} } else { meta = metaPtr.fields } @@ -233,7 +234,7 @@ func (p *addProcessMetadata) enrich(event *beat.Event, pidField string) (result if cid == "" || err != nil { p.log.Debugf("failed to get container id for PID=%d: %v", pid, err) } else { - if _, err = meta.Put("container", common.MapStr{"id": cid}); err != nil { + if _, err = meta.Put("container", mapstr.M{"id": cid}); err != nil { return nil, err } } @@ -299,21 +300,21 @@ func (p *addProcessMetadata) String() string { p.config.OverwriteKeys, p.config.RestrictedFields, p.config.HostPath, p.config.CgroupPrefixes) } -func (p *processMetadata) toMap() common.MapStr { - process := common.MapStr{ +func (p *processMetadata) toMap() mapstr.M { + process := mapstr.M{ "name": p.name, "title": p.title, "executable": p.exe, "args": p.args, "env": p.env, "pid": p.pid, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": p.ppid, }, "start_time": p.startTime, } if p.username != "" || p.userid != "" { - user := common.MapStr{} + user := mapstr.M{} if p.username != "" { user["name"] = p.username } @@ -323,7 +324,7 @@ func (p *processMetadata) toMap() common.MapStr { process["owner"] = user } - return common.MapStr{ + return mapstr.M{ "process": process, } } diff --git a/libbeat/processors/add_process_metadata/add_process_metadata_test.go b/libbeat/processors/add_process_metadata/add_process_metadata_test.go index e0d64ec5f7f..2849a0a7af1 100644 --- a/libbeat/processors/add_process_metadata/add_process_metadata_test.go +++ b/libbeat/processors/add_process_metadata/add_process_metadata_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/metric/system/cgroup" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestAddProcessMetadata(t *testing.T) { @@ -120,66 +121,66 @@ func TestAddProcessMetadata(t *testing.T) { for _, test := range []struct { description string - config, event, expected common.MapStr + config, event, expected mapstr.M err, initErr error }{ { description: "default fields", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, - "process": common.MapStr{ + "process": mapstr.M{ "name": "systemd", "title": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22", "executable": "/usr/lib/systemd/systemd", "args": []string{"/usr/lib/systemd/systemd", "--switched-root", "--system", "--deserialize", "22"}, "pid": 1, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": 0, }, "start_time": startTime, - "owner": common.MapStr{ + "owner": mapstr.M{ "name": "root", "id": "0", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, }, { description: "single field", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, "target": "system.process.parent", "include_fields": []string{"process.name"}, }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", - "parent": common.MapStr{ + "parent": mapstr.M{ "name": "systemd", }, }, @@ -188,26 +189,26 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "multiple fields", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.other.pid", "system.process.ppid"}, "target": "extra", "include_fields": []string{"process.title", "process.start_time"}, }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, - "extra": common.MapStr{ - "process": common.MapStr{ + "extra": mapstr.M{ + "process": mapstr.M{ "title": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22", "start_time": startTime, }, @@ -216,32 +217,32 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "complete process info", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "target": "parent", }, - event: common.MapStr{ + event: mapstr.M{ "ppid": "1", }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": "1", - "parent": common.MapStr{ - "process": common.MapStr{ + "parent": mapstr.M{ + "process": mapstr.M{ "name": "systemd", "title": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22", "executable": "/usr/lib/systemd/systemd", "args": []string{"/usr/lib/systemd/systemd", "--switched-root", "--system", "--deserialize", "22"}, "pid": 1, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": 0, }, "start_time": startTime, - "owner": common.MapStr{ + "owner": mapstr.M{ "name": "root", "id": "0", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, @@ -249,24 +250,24 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "complete process info (restricted fields)", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "restricted_fields": true, "target": "parent", }, - event: common.MapStr{ + event: mapstr.M{ "ppid": "1", }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": "1", - "parent": common.MapStr{ - "process": common.MapStr{ + "parent": mapstr.M{ + "process": mapstr.M{ "name": "systemd", "title": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22", "executable": "/usr/lib/systemd/systemd", "args": []string{"/usr/lib/systemd/systemd", "--switched-root", "--system", "--deserialize", "22"}, "pid": 1, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": 0, }, "start_time": startTime, @@ -276,12 +277,12 @@ func TestAddProcessMetadata(t *testing.T) { "BOOT_IMAGE": "/boot/vmlinuz-4.11.8-300.fc26.x86_64", "LANG": "en_US.UTF-8", }, - "owner": common.MapStr{ + "owner": mapstr.M{ "name": "root", "id": "0", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, @@ -289,25 +290,25 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "complete process info (restricted fields - alt)", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "restricted_fields": true, "target": "parent", "include_fields": []string{"process"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": "1", }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": "1", - "parent": common.MapStr{ - "process": common.MapStr{ + "parent": mapstr.M{ + "process": mapstr.M{ "name": "systemd", "title": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22", "executable": "/usr/lib/systemd/systemd", "args": []string{"/usr/lib/systemd/systemd", "--switched-root", "--system", "--deserialize", "22"}, "pid": 1, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": 0, }, "start_time": startTime, @@ -317,7 +318,7 @@ func TestAddProcessMetadata(t *testing.T) { "BOOT_IMAGE": "/boot/vmlinuz-4.11.8-300.fc26.x86_64", "LANG": "en_US.UTF-8", }, - "owner": common.MapStr{ + "owner": mapstr.M{ "name": "root", "id": "0", }, @@ -327,18 +328,18 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "env field (restricted_fields: true)", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "restricted_fields": true, "target": "parent", "include_fields": []string{"process.env"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": "1", }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": "1", - "parent": common.MapStr{ + "parent": mapstr.M{ "env": map[string]string{ "HOME": "/", "TERM": "linux", @@ -350,12 +351,12 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "env field (restricted_fields: false)", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "target": "parent", "include_fields": []string{"process.env"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": "1", }, expected: nil, @@ -363,65 +364,65 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "fields not found (ignored)", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, }, - event: common.MapStr{ + event: mapstr.M{ "other": "field", }, - expected: common.MapStr{ + expected: mapstr.M{ "other": "field", }, }, { description: "fields not found (reported)", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "ignore_missing": false, }, - event: common.MapStr{ + event: mapstr.M{ "other": "field", }, - expected: common.MapStr{ + expected: mapstr.M{ "other": "field", }, err: ErrNoMatch, }, { description: "overwrite keys", - config: common.MapStr{ + config: mapstr.M{ "overwrite_keys": true, "match_pids": []string{"ppid"}, "include_fields": []string{"process.name"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": 1, - "process": common.MapStr{ + "process": mapstr.M{ "name": "other", }, }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": 1, - "process": common.MapStr{ + "process": mapstr.M{ "name": "systemd", }, }, }, { description: "overwrite keys error", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "include_fields": []string{"process.name"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": 1, - "process": common.MapStr{ + "process": mapstr.M{ "name": "other", }, }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": 1, - "process": common.MapStr{ + "process": mapstr.M{ "name": "other", }, }, @@ -429,53 +430,53 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "bad PID field cast", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": "a", }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": "a", }, err: errors.New("error applying add_process_metadata processor: cannot parse pid field 'ppid': error converting string to integer: strconv.Atoi: parsing \"a\": invalid syntax"), }, { description: "bad PID field type", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": false, }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": false, }, err: errors.New("error applying add_process_metadata processor: cannot parse pid field 'ppid': not an integer or string, but bool"), }, { description: "process not found", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": 42, }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": 42, }, err: ErrNoProcess, }, { description: "lookup first PID", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"nil", "ppid"}, }, - event: common.MapStr{ + event: mapstr.M{ "nil": 0, "ppid": 1, }, - expected: common.MapStr{ + expected: mapstr.M{ "nil": 0, "ppid": 1, }, @@ -483,158 +484,158 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "env field", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, - "process": common.MapStr{ + "process": mapstr.M{ "name": "systemd", "title": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22", "executable": "/usr/lib/systemd/systemd", "args": []string{"/usr/lib/systemd/systemd", "--switched-root", "--system", "--deserialize", "22"}, "pid": 1, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": 0, }, "start_time": startTime, - "owner": common.MapStr{ + "owner": mapstr.M{ "name": "root", "id": "0", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, }, { description: "env field (IncludeContainer id), process not found", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": 42, }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": 42, }, err: ErrNoProcess, }, { description: "container.id only", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, "include_fields": []string{"container.id"}, }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, }, { description: "container.id based on regex in config", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, "include_fields": []string{"container.id"}, "cgroup_regex": "\\/.+\\/.+\\/.+\\/([0-9a-f]{64}).*", }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, }, { description: "no process metadata available", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, "cgroup_regex": "\\/.+\\/.+\\/.+\\/([0-9a-f]{64}).*", }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "2", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "2", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, }, { description: "no container id available", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, "cgroup_regex": "\\/.+\\/.+\\/.+\\/([0-9a-f]{64}).*", }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "3", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "3", }, }, - "process": common.MapStr{ + "process": mapstr.M{ "name": "systemd", "title": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22", "executable": "/usr/lib/systemd/systemd", "args": []string{"/usr/lib/systemd/systemd", "--switched-root", "--system", "--deserialize", "22"}, "pid": 1, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": 0, }, "start_time": startTime, - "owner": common.MapStr{ + "owner": mapstr.M{ "name": "user", "id": "1001", }, @@ -643,68 +644,68 @@ func TestAddProcessMetadata(t *testing.T) { }, { description: "without cgroup cache", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, "include_fields": []string{"container.id"}, "cgroup_cache_expire_time": 0, }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, }, { description: "custom cache expire time", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"system.process.ppid"}, "include_fields": []string{"container.id"}, "cgroup_cache_expire_time": 10 * time.Second, }, - event: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + event: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, }, - expected: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + expected: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": "1", }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", }, }, }, { description: "only user", - config: common.MapStr{ + config: mapstr.M{ "match_pids": []string{"ppid"}, "target": "", "include_fields": []string{"process.owner"}, }, - event: common.MapStr{ + event: mapstr.M{ "ppid": "1", }, - expected: common.MapStr{ + expected: mapstr.M{ "ppid": "1", - "process": common.MapStr{ - "owner": common.MapStr{ + "process": mapstr.M{ + "owner": mapstr.M{ "id": "0", "name": "root", }, @@ -748,7 +749,7 @@ func TestAddProcessMetadata(t *testing.T) { } t.Run("supports metadata as a target", func(t *testing.T) { - c := common.MapStr{ + c := mapstr.M{ "match_pids": []string{"@metadata.system.ppid"}, "target": "@metadata", "include_fields": []string{"process.name"}, @@ -761,18 +762,18 @@ func TestAddProcessMetadata(t *testing.T) { assert.NoError(t, err) event := &beat.Event{ - Meta: common.MapStr{ - "system": common.MapStr{ + Meta: mapstr.M{ + "system": mapstr.M{ "ppid": "1", }, }, - Fields: common.MapStr{}, + Fields: mapstr.M{}, } - expMeta := common.MapStr{ - "system": common.MapStr{ + expMeta := mapstr.M{ + "system": mapstr.M{ "ppid": "1", }, - "process": common.MapStr{ + "process": mapstr.M{ "name": "systemd", }, } @@ -817,7 +818,7 @@ func TestUsingCache(t *testing.T) { return testMap[pid], nil } - config, err := common.NewConfigFrom(common.MapStr{ + config, err := common.NewConfigFrom(mapstr.M{ "match_pids": []string{"system.process.ppid"}, "include_fields": []string{"container.id"}, "target": "meta", @@ -831,9 +832,9 @@ func TestUsingCache(t *testing.T) { } ev := beat.Event{ - Fields: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + Fields: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": selfPID, }, }, @@ -853,9 +854,9 @@ func TestUsingCache(t *testing.T) { assert.Equal(t, "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1", containerID) ev = beat.Event{ - Fields: common.MapStr{ - "system": common.MapStr{ - "process": common.MapStr{ + Fields: mapstr.M{ + "system": mapstr.M{ + "process": mapstr.M{ "ppid": selfPID, }, }, @@ -877,7 +878,7 @@ func TestUsingCache(t *testing.T) { func TestSelf(t *testing.T) { logp.TestingSetup(logp.WithSelectors(processorName)) - config, err := common.NewConfigFrom(common.MapStr{ + config, err := common.NewConfigFrom(mapstr.M{ "match_pids": []string{"self_pid"}, "target": "self", }) @@ -890,7 +891,7 @@ func TestSelf(t *testing.T) { } selfPID := os.Getpid() ev := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "self_pid": selfPID, }, } @@ -910,7 +911,7 @@ func TestSelf(t *testing.T) { func TestBadProcess(t *testing.T) { logp.TestingSetup(logp.WithSelectors(processorName)) - config, err := common.NewConfigFrom(common.MapStr{ + config, err := common.NewConfigFrom(mapstr.M{ "match_pids": []string{"self_pid"}, "target": "self", }) @@ -922,7 +923,7 @@ func TestBadProcess(t *testing.T) { t.Fatal(err) } ev := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "self_pid": 0, }, } diff --git a/libbeat/processors/add_process_metadata/config.go b/libbeat/processors/add_process_metadata/config.go index 4d56f4540a0..dc8ce612da0 100644 --- a/libbeat/processors/add_process_metadata/config.go +++ b/libbeat/processors/add_process_metadata/config.go @@ -24,7 +24,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type config struct { @@ -61,31 +61,31 @@ type config struct { } // available fields by default -var defaultFields = common.MapStr{ - "process": common.MapStr{ +var defaultFields = mapstr.M{ + "process": mapstr.M{ "name": nil, "title": nil, "executable": nil, "args": nil, "pid": nil, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": nil, }, "start_time": nil, - "owner": common.MapStr{ + "owner": mapstr.M{ "name": nil, "id": nil, }, }, - "container": common.MapStr{ + "container": mapstr.M{ "id": nil, }, } // fields declared in here will only appear when requested explicitly // with `restricted_fields: true`. -var restrictedFields = common.MapStr{ - "process": common.MapStr{ +var restrictedFields = mapstr.M{ + "process": mapstr.M{ "env": nil, }, } @@ -106,8 +106,8 @@ func defaultConfig() config { } } -func (pf *config) getMappings() (mappings common.MapStr, err error) { - mappings = common.MapStr{} +func (pf *config) getMappings() (mappings mapstr.M, err error) { + mappings = mapstr.M{} validFields := defaultFields if pf.RestrictedFields { validFields = restrictedFields @@ -127,7 +127,7 @@ func (pf *config) getMappings() (mappings common.MapStr, err error) { return nil, fmt.Errorf("field '%v' not found", docSrc) } if reqField != nil { - for subField := range reqField.(common.MapStr) { + for subField := range reqField.(mapstr.M) { key := dstField + "." + subField val := docSrc + "." + subField if _, err = mappings.Put(key, val); err != nil { diff --git a/libbeat/processors/communityid/communityid_test.go b/libbeat/processors/communityid/communityid_test.go index 0ab643d05de..f8779f6762c 100644 --- a/libbeat/processors/communityid/communityid_test.go +++ b/libbeat/processors/communityid/communityid_test.go @@ -24,6 +24,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewDefaults(t *testing.T) { @@ -36,17 +37,17 @@ func TestNewDefaults(t *testing.T) { func TestRun(t *testing.T) { // From flowhash package testdata. // 1:LQU9qZlK+B5F3KDmev6m5PMibrg= | 128.232.110.120 66.35.250.204 6 34855 80 - evt := func() common.MapStr { - return common.MapStr{ - "source": common.MapStr{ + evt := func() mapstr.M { + return mapstr.M{ + "source": mapstr.M{ "ip": "128.232.110.120", "port": 34855, }, - "destination": common.MapStr{ + "destination": mapstr.M{ "ip": "66.35.250.204", "port": 80, }, - "network": common.MapStr{ + "network": mapstr.M{ "transport": "TCP", }, } @@ -151,7 +152,7 @@ func TestRun(t *testing.T) { t.Run("supports metadata as a target", func(t *testing.T) { event := &beat.Event{ Fields: evt(), - Meta: common.MapStr{}, + Meta: mapstr.M{}, } c := defaultConfig() c.Target = "@metadata.community_id" @@ -169,7 +170,7 @@ func TestRun(t *testing.T) { }) } -func testProcessor(t testing.TB, seed uint16, fields common.MapStr, expectedHash interface{}) { +func testProcessor(t testing.TB, seed uint16, fields mapstr.M, expectedHash interface{}) { t.Helper() c := defaultConfig() diff --git a/libbeat/processors/conditionals_test.go b/libbeat/processors/conditionals_test.go index 7521d3a72c1..767dbedff1b 100644 --- a/libbeat/processors/conditionals_test.go +++ b/libbeat/processors/conditionals_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type countFilter struct { @@ -45,37 +46,37 @@ func TestWhenProcessor(t *testing.T) { tests := []struct { title string filter config - events []common.MapStr + events []mapstr.M expected int }{ { "condition_matches", config{"when.equals.i": 10}, - []common.MapStr{{"i": 10}}, + []mapstr.M{{"i": 10}}, 1, }, { "condition_fails", config{"when.equals.i": 11}, - []common.MapStr{{"i": 10}}, + []mapstr.M{{"i": 10}}, 0, }, { "no_condition", config{}, - []common.MapStr{{"i": 10}}, + []mapstr.M{{"i": 10}}, 1, }, { "condition_matches", config{"when.has_fields": []string{"i"}}, - []common.MapStr{{"i": 10}}, + []mapstr.M{{"i": 10}}, 1, }, { "condition_fails", config{"when.has_fields": []string{"j"}}, - []common.MapStr{{"i": 10}}, + []mapstr.M{{"i": 10}}, 0, }, } @@ -124,8 +125,8 @@ func TestConditionRuleInitErrorPropagates(t *testing.T) { } type testCase struct { - event common.MapStr - want common.MapStr + event mapstr.M + want mapstr.M cfg string } @@ -199,33 +200,33 @@ func TestIfElseThenProcessor(t *testing.T) { testProcessors(t, map[string]testCase{ "if-then-true": { - event: common.MapStr{"uid": 411}, - want: common.MapStr{"uid": 411, "uid_type": "reserved"}, + event: mapstr.M{"uid": 411}, + want: mapstr.M{"uid": 411, "uid_type": "reserved"}, cfg: ifThen, }, "if-then-false": { - event: common.MapStr{"uid": 500}, - want: common.MapStr{"uid": 500}, + event: mapstr.M{"uid": 500}, + want: mapstr.M{"uid": 500}, cfg: ifThen, }, "if-then-else-true": { - event: common.MapStr{"uid": 411}, - want: common.MapStr{"uid": 411, "uid_type": "reserved"}, + event: mapstr.M{"uid": 411}, + want: mapstr.M{"uid": 411, "uid_type": "reserved"}, cfg: ifThenElse, }, "if-then-else-false": { - event: common.MapStr{"uid": 500}, - want: common.MapStr{"uid": 500, "uid_type": "user"}, + event: mapstr.M{"uid": 500}, + want: mapstr.M{"uid": 500, "uid_type": "user"}, cfg: ifThenElse, }, "if-then-else-false-single-processor": { - event: common.MapStr{"uid": 500}, - want: common.MapStr{"uid": 500, "uid_type": "user"}, + event: mapstr.M{"uid": 500}, + want: mapstr.M{"uid": 500, "uid_type": "user"}, cfg: ifThenElseSingleProcessor, }, "if-then-else-if": { - event: common.MapStr{"uid": 500}, - want: common.MapStr{"uid": 500, "uid_type": "eq_500"}, + event: mapstr.M{"uid": 500}, + want: mapstr.M{"uid": 500, "uid_type": "eq_500"}, cfg: ifThenElseIf, }, }) diff --git a/libbeat/processors/convert/convert.go b/libbeat/processors/convert/convert.go index 999a5abd418..5ec1b97f8f8 100644 --- a/libbeat/processors/convert/convert.go +++ b/libbeat/processors/convert/convert.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) const logName = "processor.convert" @@ -117,7 +118,7 @@ func (p *processor) convertFields(event *beat.Event, converted []interface{}) er func (p *processor) convertField(event *beat.Event, conversion field) (interface{}, error) { v, err := event.GetValue(conversion.From) if err != nil { - if p.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if p.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return ignoredFailure, nil } return nil, newConvertError(conversion, err, p.Tag, "field [%v] is missing", conversion.From) @@ -380,10 +381,10 @@ func newConvertError(conversion field, cause error, tag string, message string, // maps without doing any type conversions. func cloneValue(value interface{}) interface{} { switch v := value.(type) { - case common.MapStr: + case mapstr.M: return v.Clone() case map[string]interface{}: - return common.MapStr(v).Clone() + return mapstr.M(v).Clone() case []interface{}: len := len(v) newArr := make([]interface{}, len) diff --git a/libbeat/processors/convert/convert_test.go b/libbeat/processors/convert/convert_test.go index ea57413562f..39c1234b51a 100644 --- a/libbeat/processors/convert/convert_test.go +++ b/libbeat/processors/convert/convert_test.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConvert(t *testing.T) { @@ -37,7 +38,7 @@ func TestConvert(t *testing.T) { t.Fatal(err) } - evt := &beat.Event{Fields: common.MapStr{}} + evt := &beat.Event{Fields: mapstr.M{}} // Defaults. p.IgnoreMissing = false @@ -78,7 +79,7 @@ func TestConvert(t *testing.T) { t.Fatal(err) } - evt := &beat.Event{Fields: common.MapStr{"source": common.MapStr{"address": "host.local"}}} + evt := &beat.Event{Fields: mapstr.M{"source": mapstr.M{"address": "host.local"}}} _, err = p.Run(evt) if assert.Error(t, err) { @@ -102,7 +103,7 @@ func TestConvert(t *testing.T) { } const loopback = "127.0.0.1" - fields := common.MapStr{"source": common.MapStr{"address": loopback}} + fields := mapstr.M{"source": mapstr.M{"address": loopback}} t.Run("copy", func(t *testing.T) { evt := &beat.Event{Fields: fields.Clone()} @@ -153,11 +154,11 @@ func TestConvert(t *testing.T) { c.Fields = append(c.Fields, field{From: "@metadata.source", To: "@metadata.dest", Type: Integer}) evt := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "source": "1", }, } - expMeta := common.MapStr{ + expMeta := mapstr.M{ "source": "1", "dest": int32(1), } @@ -174,64 +175,64 @@ func TestConvert(t *testing.T) { func TestConvertRun(t *testing.T) { tests := map[string]struct { - config common.MapStr + config mapstr.M input beat.Event expected beat.Event fail bool errContains string }{ "missing field": { - config: common.MapStr{ - "fields": []common.MapStr{ + config: mapstr.M{ + "fields": []mapstr.M{ {"from": "port", "type": "integer"}, {"from": "address", "to": "ip", "type": "ip"}, }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "port": "80", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "port": "80", }, }, fail: true, }, "put error no clone": { - config: common.MapStr{ - "fields": []common.MapStr{ + config: mapstr.M{ + "fields": []mapstr.M{ {"from": "port", "to": "port.number", "type": "integer"}, }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "port": "80", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "port": "80", }, }, fail: true, }, "put error with clone": { - config: common.MapStr{ - "fields": []common.MapStr{ + config: mapstr.M{ + "fields": []mapstr.M{ {"from": "id", "to": "event.id", "type": "integer"}, {"from": "port", "to": "port.number", "type": "integer"}, }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "id": "32", "port": "80", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "id": "32", "port": "80", }, @@ -239,18 +240,18 @@ func TestConvertRun(t *testing.T) { fail: true, }, "invalid conversion": { - config: common.MapStr{ - "fields": []common.MapStr{ + config: mapstr.M{ + "fields": []mapstr.M{ {"from": "address", "to": "ip", "type": "ip"}, }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "address": "-", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "address": "-", }, }, @@ -435,7 +436,7 @@ func TestDataTypes(t *testing.T) { t.Fatal(err) } - event, err := p.Run(&beat.Event{Fields: common.MapStr{key: tc.In}}) + event, err := p.Run(&beat.Event{Fields: mapstr.M{key: tc.In}}) if tc.Err { assert.Error(t, err) return @@ -474,11 +475,11 @@ func BenchmarkTestConvertRun(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { event := &beat.Event{ - Fields: common.MapStr{ - "source": common.MapStr{ + Fields: mapstr.M{ + "source": mapstr.M{ "address": "192.51.100.1", }, - "destination": common.MapStr{ + "destination": mapstr.M{ "address": "192.0.2.51", }, }, diff --git a/libbeat/processors/decode_csv_fields/decode_csv_fields.go b/libbeat/processors/decode_csv_fields/decode_csv_fields.go index f971e7e8f5b..609c694a659 100644 --- a/libbeat/processors/decode_csv_fields/decode_csv_fields.go +++ b/libbeat/processors/decode_csv_fields/decode_csv_fields.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type decodeCSVFields struct { @@ -39,12 +40,12 @@ type decodeCSVFields struct { } type csvConfig struct { - Fields common.MapStr `config:"fields"` - IgnoreMissing bool `config:"ignore_missing"` - TrimLeadingSpace bool `config:"trim_leading_space"` - OverwriteKeys bool `config:"overwrite_keys"` - FailOnError bool `config:"fail_on_error"` - Separator string `config:"separator"` + Fields mapstr.M `config:"fields"` + IgnoreMissing bool `config:"ignore_missing"` + TrimLeadingSpace bool `config:"trim_leading_space"` + OverwriteKeys bool `config:"overwrite_keys"` + FailOnError bool `config:"fail_on_error"` + Separator string `config:"separator"` } var ( @@ -115,7 +116,7 @@ func (f *decodeCSVFields) Run(event *beat.Event) (*beat.Event, error) { func (f *decodeCSVFields) decodeCSVField(src, dest string, event *beat.Event) error { data, err := event.GetValue(src) if err != nil { - if f.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if f.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return nil } return errors.Wrapf(err, "could not fetch value for field %s", src) diff --git a/libbeat/processors/decode_csv_fields/decode_csv_fields_test.go b/libbeat/processors/decode_csv_fields/decode_csv_fields_test.go index e85d91873cf..0c0512f64a7 100644 --- a/libbeat/processors/decode_csv_fields/decode_csv_fields_test.go +++ b/libbeat/processors/decode_csv_fields/decode_csv_fields_test.go @@ -24,50 +24,51 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDecodeCSVField(t *testing.T) { tests := map[string]struct { - config common.MapStr + config mapstr.M input beat.Event expected beat.Event fail bool }{ "self target": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "message", }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "17,192.168.33.1,8.8.8.8", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": []string{"17", "192.168.33.1", "8.8.8.8"}, }, }, }, "alternative target": { - config: common.MapStr{ - "fields": common.MapStr{ - "my": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ + "my": mapstr.M{ "field": "message", }, }, }, input: beat.Event{ - Fields: common.MapStr{ - "my": common.MapStr{ + Fields: mapstr.M{ + "my": mapstr.M{ "field": "17,192.168.33.1,8.8.8.8", }, }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "my.field": "17,192.168.33.1,8.8.8.8", "message": []string{"17", "192.168.33.1", "8.8.8.8"}, }, @@ -75,8 +76,8 @@ func TestDecodeCSVField(t *testing.T) { }, "non existing field": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "field": "my.field", }, }, @@ -84,8 +85,8 @@ func TestDecodeCSVField(t *testing.T) { }, "ignore missing": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "my_field": "my_field", }, @@ -94,13 +95,13 @@ func TestDecodeCSVField(t *testing.T) { }, "overwrite keys failure": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "existing_field", }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": `"hello ""world"""`, "existing_field": 42, }, @@ -109,20 +110,20 @@ func TestDecodeCSVField(t *testing.T) { }, "overwrite keys": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "existing_field", }, "overwrite_keys": true, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": `"hello ""world"""`, "existing_field": 42, }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": `"hello ""world"""`, "existing_field": []string{`hello "world"`}, }, @@ -130,91 +131,91 @@ func TestDecodeCSVField(t *testing.T) { }, "custom separator": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "message", }, "separator": ";", }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "1.5;false;hello world;3", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": []string{"1.5", "false", "hello world", "3"}, }, }, }, "trim leading space": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "message", }, "trim_leading_space": true, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": " Here's, some, extra ,whitespace", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": []string{"Here's", "some", "extra ", "whitespace"}, }, }, }, "tab separator": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "message", }, "separator": "\t", "overwrite_keys": true, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "Tab\tin\tASCII\thas\tthe\t\"decimal\tcharacter\tcode\"\t9", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": []string{"Tab", "in", "ASCII", "has", "the", "decimal\tcharacter\tcode", "9"}, }, }, }, "unicode separator": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "message", }, "separator": "🍺", "overwrite_keys": true, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": `🐢🍺🌔🐈🍺🍺🐥🐲`, }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": []string{"🐢", "🌔🐈", "", "🐥🐲"}, }, }, }, "bad type": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "message": "message", }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": 42, }, }, @@ -222,20 +223,20 @@ func TestDecodeCSVField(t *testing.T) { }, "multiple fields": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "a": "a_csv", "b": "b_csv", }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "1,2", "b": "hello,world", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "1,2", "b": "hello,world", "a_csv": []string{"1", "2"}, @@ -245,20 +246,20 @@ func TestDecodeCSVField(t *testing.T) { }, "multiple fields failure": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "a": "a.csv", "b": "b.csv", }, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "1,2", "b": "hello,world", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "1,2", "b": "hello,world", }, @@ -267,8 +268,8 @@ func TestDecodeCSVField(t *testing.T) { }, "ignore errors": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "a": "a", "b": "b", "c": "a.b", @@ -276,14 +277,14 @@ func TestDecodeCSVField(t *testing.T) { "fail_on_error": false, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "1,2", "b": "hello,world", "c": ":)", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": []string{"1", "2"}, "b": []string{"hello", "world"}, "c": ":)", @@ -292,8 +293,8 @@ func TestDecodeCSVField(t *testing.T) { }, "restore on errors": { - config: common.MapStr{ - "fields": common.MapStr{ + config: mapstr.M{ + "fields": mapstr.M{ "a": "a", "b": "b", "c": "a.b", @@ -301,14 +302,14 @@ func TestDecodeCSVField(t *testing.T) { "fail_on_error": true, }, input: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "1,2", "b": "hello,world", "c": ":)", }, }, expected: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "1,2", "b": "hello,world", "c": ":)", @@ -340,21 +341,21 @@ func TestDecodeCSVField(t *testing.T) { } t.Run("supports metadata as a target", func(t *testing.T) { - config := common.MapStr{ - "fields": common.MapStr{ - "@metadata": common.MapStr{ + config := mapstr.M{ + "fields": mapstr.M{ + "@metadata": mapstr.M{ "field": "@metadata.message", }, }, } event := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "field": "17,192.168.33.1,8.8.8.8", }, - Fields: common.MapStr{}, + Fields: mapstr.M{}, } - expMeta := common.MapStr{ + expMeta := mapstr.M{ "field": "17,192.168.33.1,8.8.8.8", "message": []string{"17", "192.168.33.1", "8.8.8.8"}, } @@ -372,8 +373,8 @@ func TestDecodeCSVField(t *testing.T) { } func TestDecodeCSVField_String(t *testing.T) { - p, err := NewDecodeCSVField(common.MustNewConfigFrom(common.MapStr{ - "fields": common.MapStr{ + p, err := NewDecodeCSVField(common.MustNewConfigFrom(mapstr.M{ + "fields": mapstr.M{ "a": "csv.a", "b": "csv.b", }, diff --git a/libbeat/processors/decode_xml/decode_xml.go b/libbeat/processors/decode_xml/decode_xml.go index 5f36a71bf2e..24f07945c43 100644 --- a/libbeat/processors/decode_xml/decode_xml.go +++ b/libbeat/processors/decode_xml/decode_xml.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type decodeXML struct { @@ -96,7 +97,7 @@ func (x *decodeXML) Run(event *beat.Event) (*beat.Event, error) { func (x *decodeXML) run(event *beat.Event) error { data, err := event.GetValue(x.Field) if err != nil { - if x.IgnoreMissing && err == common.ErrKeyNotFound { + if x.IgnoreMissing && err == mapstr.ErrKeyNotFound { return nil } return err @@ -113,10 +114,10 @@ func (x *decodeXML) run(event *beat.Event) error { } var id string - if tmp, err := common.MapStr(xmlOutput).GetValue(x.DocumentID); err == nil { + if tmp, err := mapstr.M(xmlOutput).GetValue(x.DocumentID); err == nil { if v, ok := tmp.(string); ok { id = v - common.MapStr(xmlOutput).Delete(x.DocumentID) + mapstr.M(xmlOutput).Delete(x.DocumentID) } } @@ -135,7 +136,7 @@ func (x *decodeXML) run(event *beat.Event) error { return nil } -func (x *decodeXML) decode(p []byte) (common.MapStr, error) { +func (x *decodeXML) decode(p []byte) (mapstr.M, error) { dec := xml.NewDecoder(bytes.NewReader(p)) if x.ToLower { dec.LowercaseKeys() @@ -146,7 +147,7 @@ func (x *decodeXML) decode(p []byte) (common.MapStr, error) { return nil, err } - return common.MapStr(out), nil + return mapstr.M(out), nil } func (x *decodeXML) String() string { diff --git a/libbeat/processors/decode_xml/decode_xml_test.go b/libbeat/processors/decode_xml/decode_xml_test.go index 5a86787ae46..dfec7273a23 100644 --- a/libbeat/processors/decode_xml/decode_xml_test.go +++ b/libbeat/processors/decode_xml/decode_xml_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -36,8 +36,8 @@ func TestDecodeXML(t *testing.T) { var testCases = []struct { description string config decodeXMLConfig - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M error bool errorMessage string }{ @@ -47,7 +47,7 @@ func TestDecodeXML(t *testing.T) { Field: "message", Target: &testXMLTargetField, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` William H. Gaddis @@ -56,8 +56,8 @@ func TestDecodeXML(t *testing.T) { `, }, - Output: common.MapStr{ - "xml": common.MapStr{ + Output: mapstr.M{ + "xml": mapstr.M{ "catalog": map[string]interface{}{ "book": map[string]interface{}{ "author": "William H. Gaddis", @@ -82,7 +82,7 @@ func TestDecodeXML(t *testing.T) { Field: "message", Target: &testRootTargetField, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` William H. Gaddis @@ -91,8 +91,8 @@ func TestDecodeXML(t *testing.T) { `, }, - Output: common.MapStr{ - "catalog": common.MapStr{ + Output: mapstr.M{ + "catalog": mapstr.M{ "book": map[string]interface{}{ "author": "William H. Gaddis", "review": "One of the great seminal American novels of the 20th century.", @@ -114,7 +114,7 @@ func TestDecodeXML(t *testing.T) { config: decodeXMLConfig{ Field: "message", }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` @@ -124,8 +124,8 @@ func TestDecodeXML(t *testing.T) { `, }, - Output: common.MapStr{ - "message": common.MapStr{ + Output: mapstr.M{ + "message": mapstr.M{ "catalog": map[string]interface{}{ "book": map[string]interface{}{ "author": "William H. Gaddis", @@ -142,7 +142,7 @@ func TestDecodeXML(t *testing.T) { config: decodeXMLConfig{ Field: "message", }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` @@ -157,8 +157,8 @@ func TestDecodeXML(t *testing.T) { `, }, - Output: common.MapStr{ - "message": common.MapStr{ + Output: mapstr.M{ + "message": mapstr.M{ "catalog": map[string]interface{}{ "book": []interface{}{ map[string]interface{}{ @@ -182,7 +182,7 @@ func TestDecodeXML(t *testing.T) { Field: "message", ToLower: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` @@ -194,8 +194,8 @@ func TestDecodeXML(t *testing.T) { `, }, - Output: common.MapStr{ - "message": common.MapStr{ + Output: mapstr.M{ + "message": mapstr.M{ "auditbase": map[string]interface{}{ "contextcomponents": map[string]interface{}{ "component": []interface{}{ @@ -216,7 +216,7 @@ func TestDecodeXML(t *testing.T) { config: decodeXMLConfig{ Field: "message", }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` @@ -237,8 +237,8 @@ func TestDecodeXML(t *testing.T) { `, }, - Output: common.MapStr{ - "message": common.MapStr{ + Output: mapstr.M{ + "message": mapstr.M{ "catalog": map[string]interface{}{ "book": []interface{}{ map[string]interface{}{ @@ -269,7 +269,7 @@ func TestDecodeXML(t *testing.T) { Field: "message", IgnoreFailure: false, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` @@ -279,7 +279,7 @@ func TestDecodeXML(t *testing.T) { catalog>`, }, - Output: common.MapStr{ + Output: mapstr.M{ "message": ` @@ -288,7 +288,7 @@ func TestDecodeXML(t *testing.T) { One of the great seminal American novels of the 20th century. catalog>`, - "error": common.MapStr{"message": "failed in decode_xml on the \"message\" field: error decoding XML field: XML syntax error on line 7: element closed by "}, + "error": mapstr.M{"message": "failed in decode_xml on the \"message\" field: error decoding XML field: XML syntax error on line 7: element closed by "}, }, error: true, errorMessage: "error decoding XML field:", @@ -299,7 +299,7 @@ func TestDecodeXML(t *testing.T) { Field: "message", IgnoreFailure: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": ` @@ -309,7 +309,7 @@ func TestDecodeXML(t *testing.T) { catalog>`, }, - Output: common.MapStr{ + Output: mapstr.M{ "message": ` @@ -326,12 +326,12 @@ func TestDecodeXML(t *testing.T) { Field: "message2", IgnoreMissing: false, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": "testing message", }, - Output: common.MapStr{ + Output: mapstr.M{ "message": "testing message", - "error": common.MapStr{"message": "failed in decode_xml on the \"message2\" field: key not found"}, + "error": mapstr.M{"message": "failed in decode_xml on the \"message2\" field: key not found"}, }, error: true, errorMessage: "key not found", @@ -342,10 +342,10 @@ func TestDecodeXML(t *testing.T) { Field: "message2", IgnoreMissing: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": "testing message", }, - Output: common.MapStr{ + Output: mapstr.M{ "message": "testing message", }, }, @@ -355,12 +355,12 @@ func TestDecodeXML(t *testing.T) { Field: "message", IgnoreFailure: false, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": 1, }, - Output: common.MapStr{ + Output: mapstr.M{ "message": 1, - "error": common.MapStr{"message": "failed in decode_xml on the \"message\" field: field value is not a string"}, + "error": mapstr.M{"message": "failed in decode_xml on the \"message\" field: field value is not a string"}, }, error: true, errorMessage: "field value is not a string", @@ -371,10 +371,10 @@ func TestDecodeXML(t *testing.T) { Field: "message", IgnoreFailure: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "message": 1, }, - Output: common.MapStr{ + Output: mapstr.M{ "message": 1, }, }, @@ -415,7 +415,7 @@ func TestDecodeXML(t *testing.T) { require.NoError(t, err) event := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "message": ` William H. Gaddis @@ -425,8 +425,8 @@ func TestDecodeXML(t *testing.T) { `, }, } - expMeta := common.MapStr{ - "xml": common.MapStr{ + expMeta := mapstr.M{ + "xml": mapstr.M{ "catalog": map[string]interface{}{ "book": map[string]interface{}{ "author": "William H. Gaddis", @@ -518,7 +518,7 @@ func TestXMLToDocumentID(t *testing.T) { }) require.NoError(t, err) - input := common.MapStr{ + input := mapstr.M{ "message": ` William H. Gaddis @@ -530,8 +530,8 @@ func TestXMLToDocumentID(t *testing.T) { actual, err := p.Run(&beat.Event{Fields: input}) require.NoError(t, err) - wantFields := common.MapStr{ - "message": common.MapStr{ + wantFields := mapstr.M{ + "message": mapstr.M{ "catalog": map[string]interface{}{ "book": map[string]interface{}{ "author": "William H. Gaddis", @@ -541,7 +541,7 @@ func TestXMLToDocumentID(t *testing.T) { }, }, } - wantMeta := common.MapStr{ + wantMeta := mapstr.M{ "_id": "10", } diff --git a/libbeat/processors/decode_xml_wineventlog/decoder.go b/libbeat/processors/decode_xml_wineventlog/decoder.go index 51ad30d2b24..ff01382cb70 100644 --- a/libbeat/processors/decode_xml_wineventlog/decoder.go +++ b/libbeat/processors/decode_xml_wineventlog/decoder.go @@ -21,8 +21,8 @@ package decode_xml_wineventlog import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/winlogbeat/sys/winevent" + "github.com/elastic/elastic-agent-libs/mapstr" ) type nonWinDecoder struct{} @@ -31,7 +31,7 @@ func newDecoder() decoder { return nonWinDecoder{} } -func (nonWinDecoder) decode(data []byte) (common.MapStr, common.MapStr, error) { +func (nonWinDecoder) decode(data []byte) (mapstr.M, mapstr.M, error) { evt, err := winevent.UnmarshalXML(data) if err != nil { return nil, nil, err diff --git a/libbeat/processors/decode_xml_wineventlog/decoder_windows.go b/libbeat/processors/decode_xml_wineventlog/decoder_windows.go index 0ee26dbf068..89c4b334acc 100644 --- a/libbeat/processors/decode_xml_wineventlog/decoder_windows.go +++ b/libbeat/processors/decode_xml_wineventlog/decoder_windows.go @@ -23,10 +23,10 @@ package decode_xml_wineventlog import ( "sync" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/winlogbeat/sys/winevent" "github.com/elastic/beats/v7/winlogbeat/sys/wineventlog" + "github.com/elastic/elastic-agent-libs/mapstr" ) type winDecoder struct { @@ -42,7 +42,7 @@ func newDecoder() decoder { } } -func (dec *winDecoder) decode(data []byte) (common.MapStr, common.MapStr, error) { +func (dec *winDecoder) decode(data []byte) (mapstr.M, mapstr.M, error) { evt, err := winevent.UnmarshalXML(data) if err != nil { return nil, nil, err diff --git a/libbeat/processors/decode_xml_wineventlog/processor.go b/libbeat/processors/decode_xml_wineventlog/processor.go index c87dee5b515..8dd04963b35 100644 --- a/libbeat/processors/decode_xml_wineventlog/processor.go +++ b/libbeat/processors/decode_xml_wineventlog/processor.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" "github.com/elastic/beats/v7/winlogbeat/sys/winevent" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -63,7 +64,7 @@ type processor struct { } type decoder interface { - decode(data []byte) (win, ecs common.MapStr, err error) + decode(data []byte) (win, ecs mapstr.M, err error) } // New constructs a new decode_xml processor. @@ -99,7 +100,7 @@ func (p *processor) Run(event *beat.Event) (*beat.Event, error) { func (p *processor) run(event *beat.Event) error { data, err := event.GetValue(p.Field) if err != nil { - if p.IgnoreMissing && err == common.ErrKeyNotFound { + if p.IgnoreMissing && err == mapstr.ErrKeyNotFound { return nil } return err @@ -135,10 +136,10 @@ func (p *processor) String() string { return procName + "=" + string(json) } -func fields(evt winevent.Event) (common.MapStr, common.MapStr) { +func fields(evt winevent.Event) (mapstr.M, mapstr.M) { win := evt.Fields() - ecs := common.MapStr{} + ecs := mapstr.M{} eventCode, _ := win.GetValue("event_id") ecs.Put("event.code", eventCode) @@ -155,7 +156,7 @@ func fields(evt winevent.Event) (common.MapStr, common.MapStr) { return win, ecs } -func getValue(m common.MapStr, key string) interface{} { +func getValue(m mapstr.M, key string) interface{} { v, _ := m.GetValue(key) return v } diff --git a/libbeat/processors/decode_xml_wineventlog/processor_test.go b/libbeat/processors/decode_xml_wineventlog/processor_test.go index 4fc3c92d4e7..935c516dace 100644 --- a/libbeat/processors/decode_xml_wineventlog/processor_test.go +++ b/libbeat/processors/decode_xml_wineventlog/processor_test.go @@ -25,7 +25,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var testMessage = "" + @@ -39,21 +39,21 @@ var testMessage = "Oct 11 22:14:15 test-host su[1024]: this is the message`, }, - Want: common.MapStr{ - "log": common.MapStr{ - "syslog": common.MapStr{ + Want: mapstr.M{ + "log": mapstr.M{ + "syslog": mapstr.M{ "priority": 13, - "facility": common.MapStr{ + "facility": mapstr.M{ "code": 1, "name": "user-level", }, - "severity": common.MapStr{ + "severity": mapstr.M{ "code": 5, "name": "Notice", }, @@ -85,19 +86,19 @@ var syslogCases = map[string]struct { WantTime: mustParseTimeLoc(time.Stamp, "Oct 11 22:14:15", cfgtype.MustNewTimezone("America/Chicago").Location()), }, "rfc-5424": { - Cfg: common.MustNewConfigFrom(common.MapStr{}), - In: common.MapStr{ + Cfg: common.MustNewConfigFrom(mapstr.M{}), + In: mapstr.M{ "message": `<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog 1024 ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"][examplePriority@32473 class="high"] this is the message`, }, - Want: common.MapStr{ - "log": common.MapStr{ - "syslog": common.MapStr{ + Want: mapstr.M{ + "log": mapstr.M{ + "syslog": mapstr.M{ "priority": 165, - "facility": common.MapStr{ + "facility": mapstr.M{ "code": 20, "name": "local4", }, - "severity": common.MapStr{ + "severity": mapstr.M{ "code": 5, "name": "Notice", }, @@ -170,46 +171,46 @@ func BenchmarkSyslog(b *testing.B) { func TestAppendStringField(t *testing.T) { tests := map[string]struct { - InMap common.MapStr + InMap mapstr.M InField string InValue string - Want common.MapStr + Want mapstr.M }{ "nil": { - InMap: common.MapStr{}, + InMap: mapstr.M{}, InField: "error", InValue: "foo", - Want: common.MapStr{ + Want: mapstr.M{ "error": "foo", }, }, "string": { - InMap: common.MapStr{ + InMap: mapstr.M{ "error": "foo", }, InField: "error", InValue: "bar", - Want: common.MapStr{ + Want: mapstr.M{ "error": []string{"foo", "bar"}, }, }, "string-slice": { - InMap: common.MapStr{ + InMap: mapstr.M{ "error": []string{"foo", "bar"}, }, InField: "error", InValue: "some value", - Want: common.MapStr{ + Want: mapstr.M{ "error": []string{"foo", "bar", "some value"}, }, }, "interface-slice": { - InMap: common.MapStr{ + InMap: mapstr.M{ "error": []interface{}{"foo", "bar"}, }, InField: "error", InValue: "some value", - Want: common.MapStr{ + Want: mapstr.M{ "error": []interface{}{"foo", "bar", "some value"}, }, }, diff --git a/libbeat/processors/timeseries/timeseries.go b/libbeat/processors/timeseries/timeseries.go index 45b71ce79bf..6213b7695ef 100644 --- a/libbeat/processors/timeseries/timeseries.go +++ b/libbeat/processors/timeseries/timeseries.go @@ -21,10 +21,10 @@ import ( "strings" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/mapping" "github.com/elastic/beats/v7/libbeat/processors" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/mitchellh/hashstructure" ) @@ -67,7 +67,7 @@ func NewTimeSeriesProcessor(fields mapping.Fields) processors.Processor { func (t *timeseriesProcessor) Run(event *beat.Event) (*beat.Event, error) { if event.TimeSeries { - instanceFields := common.MapStr{} + instanceFields := mapstr.M{} // map all dimensions & values for k, v := range event.Fields.Flatten() { @@ -81,7 +81,7 @@ func (t *timeseriesProcessor) Run(event *beat.Event) (*beat.Event, error) { // this should not happen, keep the event in any case return event, err } - event.Fields["timeseries"] = common.MapStr{ + event.Fields["timeseries"] = mapstr.M{ "instance": h, } } diff --git a/libbeat/processors/timeseries/timeseries_test.go b/libbeat/processors/timeseries/timeseries_test.go index 18dedb5b88a..7c9b77ca02f 100644 --- a/libbeat/processors/timeseries/timeseries_test.go +++ b/libbeat/processors/timeseries/timeseries_test.go @@ -23,8 +23,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mapping" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -124,51 +124,51 @@ func TestTimesSeriesHashes(t *testing.T) { for _, test := range []struct { name string - in common.MapStr - expected common.MapStr + in mapstr.M + expected mapstr.M }{ { name: "simple fields", - in: common.MapStr{ - "context": common.MapStr{ + in: mapstr.M{ + "context": mapstr.M{ "first": 1, "second": "word2", "third": "word3", }, }, - expected: common.MapStr{ - "context": common.MapStr{ + expected: mapstr.M{ + "context": mapstr.M{ "first": 1, "second": "word2", "third": "word3", }, - "timeseries": common.MapStr{"instance": uint64(10259802856000774733)}, + "timeseries": mapstr.M{"instance": uint64(10259802856000774733)}, }, }, { name: "simple field - with one ignored field", - in: common.MapStr{ - "context": common.MapStr{ + in: mapstr.M{ + "context": mapstr.M{ "first": 1, "second": "word2", "third": "word3", }, "not-a-dimension": 1000, }, - expected: common.MapStr{ - "context": common.MapStr{ + expected: mapstr.M{ + "context": mapstr.M{ "first": 1, "second": "word2", "third": "word3", }, "not-a-dimension": 1000, - "timeseries": common.MapStr{"instance": uint64(10259802856000774733)}, // same as above + "timeseries": mapstr.M{"instance": uint64(10259802856000774733)}, // same as above }, }, { name: "simple fields and one ignored and one by default", - in: common.MapStr{ - "context": common.MapStr{ + in: mapstr.M{ + "context": mapstr.M{ "first": 1, "second": "word2", "third": "word3", @@ -176,15 +176,15 @@ func TestTimesSeriesHashes(t *testing.T) { "not-a-dimension": 1000, "dimension-by-default": "dimension1", }, - expected: common.MapStr{ - "context": common.MapStr{ + expected: mapstr.M{ + "context": mapstr.M{ "first": 1, "second": "word2", "third": "word3", }, "not-a-dimension": 1000, "dimension-by-default": "dimension1", - "timeseries": common.MapStr{"instance": uint64(17933311421196639387)}, + "timeseries": mapstr.M{"instance": uint64(17933311421196639387)}, }, }, } { diff --git a/libbeat/processors/timestamp/timestamp.go b/libbeat/processors/timestamp/timestamp.go index 7b147bbee1a..1e44f753663 100644 --- a/libbeat/processors/timestamp/timestamp.go +++ b/libbeat/processors/timestamp/timestamp.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) const logName = "processor.timestamp" @@ -87,7 +88,7 @@ func (p *processor) Run(event *beat.Event) (*beat.Event, error) { // Get the source field value. val, err := event.GetValue(p.Field) if err != nil { - if p.IgnoreFailure || (p.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound) { + if p.IgnoreFailure || (p.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound) { return event, nil } return event, errors.Wrapf(err, "failed to get time field %v", p.Field) diff --git a/libbeat/processors/timestamp/timestamp_test.go b/libbeat/processors/timestamp/timestamp_test.go index 54b7d8b9e95..b42fc151cef 100644 --- a/libbeat/processors/timestamp/timestamp_test.go +++ b/libbeat/processors/timestamp/timestamp_test.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgtype" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var expected = time.Date(2015, 3, 7, 11, 6, 39, 0, time.UTC) @@ -45,7 +46,7 @@ func TestParsePatterns(t *testing.T) { t.Fatal(err) } - evt := &beat.Event{Fields: common.MapStr{}} + evt := &beat.Event{Fields: mapstr.M{}} for name, format := range map[string]string{ "ANSIC": time.ANSIC, @@ -125,7 +126,7 @@ func TestParseNoYear(t *testing.T) { t.Fatal(err) } - evt := &beat.Event{Fields: common.MapStr{ + evt := &beat.Event{Fields: mapstr.M{ "ts": "Mar 7 11:06:39.002", }} @@ -153,7 +154,7 @@ func TestIgnoreMissing(t *testing.T) { t.Fatal(err) } - evt := &beat.Event{Fields: common.MapStr{}} + evt := &beat.Event{Fields: mapstr.M{}} _, err = p.Run(evt) if assert.Error(t, err) { @@ -175,7 +176,7 @@ func TestIgnoreFailure(t *testing.T) { t.Fatal(err) } - evt := &beat.Event{Fields: common.MapStr{"ts": expected.Format(time.Kitchen)}} + evt := &beat.Event{Fields: mapstr.M{"ts": expected.Format(time.Kitchen)}} _, err = p.Run(evt) if assert.Error(t, err) { @@ -291,7 +292,7 @@ func TestTimezone(t *testing.T) { t.Logf("Timezone: %s", c.Timezone) event := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "ts": originalTimestamp, }, } @@ -317,7 +318,7 @@ func TestMetadataTarget(t *testing.T) { } evt := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "time": datetime, }, } @@ -328,7 +329,7 @@ func TestMetadataTarget(t *testing.T) { expTs, err := time.Parse(time.RFC3339, datetime) assert.NoError(t, err) - expMeta := common.MapStr{ + expMeta := mapstr.M{ "time": datetime, "ts": expTs.UTC(), } diff --git a/libbeat/processors/translate_sid/translatesid.go b/libbeat/processors/translate_sid/translatesid.go index ece511cb24b..22f6ae6e8cd 100644 --- a/libbeat/processors/translate_sid/translatesid.go +++ b/libbeat/processors/translate_sid/translatesid.go @@ -34,6 +34,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" "github.com/elastic/beats/v7/winlogbeat/sys/winevent" + "github.com/elastic/elastic-agent-libs/mapstr" ) const logName = "processor.translate_sid" @@ -75,7 +76,7 @@ func (p *processor) String() string { func (p *processor) Run(event *beat.Event) (*beat.Event, error) { err := p.translateSID(event) - if err == nil || p.IgnoreFailure || (p.IgnoreMissing && common.ErrKeyNotFound == errors.Cause(err)) { + if err == nil || p.IgnoreFailure || (p.IgnoreMissing && mapstr.ErrKeyNotFound == errors.Cause(err)) { return event, nil } return event, err diff --git a/libbeat/processors/translate_sid/translatesid_test.go b/libbeat/processors/translate_sid/translatesid_test.go index 3f27b6569ce..58beaf502bd 100644 --- a/libbeat/processors/translate_sid/translatesid_test.go +++ b/libbeat/processors/translate_sid/translatesid_test.go @@ -30,8 +30,8 @@ import ( "golang.org/x/sys/windows" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/winlogbeat/sys/winevent" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestTranslateSID(t *testing.T) { @@ -94,12 +94,12 @@ func TestTranslateSID(t *testing.T) { }) assert.NoError(t, err) evt := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "sid": "S-1-5-7", }, } - expMeta := common.MapStr{ + expMeta := mapstr.M{ "sid": "S-1-5-32-544", "domain": "BUILTIN", "account": "Administrators", diff --git a/libbeat/processors/urldecode/urldecode.go b/libbeat/processors/urldecode/urldecode.go index 110b6387a2d..c6bff800936 100644 --- a/libbeat/processors/urldecode/urldecode.go +++ b/libbeat/processors/urldecode/urldecode.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/checks" jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor" + "github.com/elastic/elastic-agent-libs/mapstr" ) type urlDecode struct { @@ -97,7 +98,7 @@ func (p *urlDecode) Run(event *beat.Event) (*beat.Event, error) { func (p *urlDecode) decodeField(from string, to string, event *beat.Event) error { value, err := event.GetValue(from) if err != nil { - if p.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { + if p.config.IgnoreMissing && errors.Cause(err) == mapstr.ErrKeyNotFound { return nil } return fmt.Errorf("could not fetch value for key: %s, Error: %v", from, err) diff --git a/libbeat/processors/urldecode/urldecode_test.go b/libbeat/processors/urldecode/urldecode_test.go index 7e32aad3a56..e540c9f1c09 100644 --- a/libbeat/processors/urldecode/urldecode_test.go +++ b/libbeat/processors/urldecode/urldecode_test.go @@ -23,16 +23,16 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestURLDecode(t *testing.T) { var testCases = []struct { description string config urlDecodeConfig - Input common.MapStr - Output common.MapStr + Input mapstr.M + Output mapstr.M error bool }{ { @@ -44,10 +44,10 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "correct%20data", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct%20data", "field2": "correct data", }, @@ -63,11 +63,11 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "correct%20field1", "field3": "correct%20field3", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct%20field1", "field2": "correct field1", "field3": "correct%20field3", @@ -84,10 +84,10 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "correct%20data", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct data", }, error: false, @@ -101,10 +101,10 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "correct%20data", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct data", }, error: false, @@ -118,12 +118,12 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Hello G%ünter", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "Hello G%ünter", - "error": common.MapStr{ + "error": mapstr.M{ "message": "failed to decode fields in urldecode processor: error trying to URL-decode Hello G%ünter: invalid URL escape \"%ü\"", }, }, @@ -138,10 +138,10 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: false, FailOnError: false, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "Hello G%ünter", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "Hello G%ünter", }, error: false, @@ -155,12 +155,12 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: false, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "correct%20data", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct%20data", - "error": common.MapStr{ + "error": mapstr.M{ "message": "failed to decode fields in urldecode processor: could not fetch value for key: field2, Error: key not found", }, }, @@ -175,10 +175,10 @@ func TestURLDecode(t *testing.T) { IgnoreMissing: true, FailOnError: true, }, - Input: common.MapStr{ + Input: mapstr.M{ "field1": "correct%20data", }, - Output: common.MapStr{ + Output: mapstr.M{ "field1": "correct%20data", }, error: false, @@ -226,11 +226,11 @@ func TestURLDecode(t *testing.T) { } event := &beat.Event{ - Meta: common.MapStr{ + Meta: mapstr.M{ "field": "correct%20data", }, } - expMeta := common.MapStr{ + expMeta := mapstr.M{ "field": "correct%20data", "target": "correct data", } diff --git a/libbeat/processors/util/geo.go b/libbeat/processors/util/geo.go index f37a4b7bc97..377dc9ba606 100644 --- a/libbeat/processors/util/geo.go +++ b/libbeat/processors/util/geo.go @@ -21,7 +21,7 @@ import ( "fmt" "regexp" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // GeoConfig contains geo configuration data. @@ -36,8 +36,8 @@ type GeoConfig struct { CityName string `config:"city_name"` } -// GeoConfigToMap converts `geo` sections to a `common.MapStr`. -func GeoConfigToMap(config GeoConfig) (common.MapStr, error) { +// GeoConfigToMap converts `geo` sections to a `mapstr.M`. +func GeoConfigToMap(config GeoConfig) (mapstr.M, error) { if len(config.Location) > 0 { // Regexp matching a number with an optional decimal component // Valid numbers: '123', '123.23', etc. @@ -56,7 +56,7 @@ func GeoConfigToMap(config GeoConfig) (common.MapStr, error) { } } - geoFields := common.MapStr{ + geoFields := mapstr.M{ "name": config.Name, "location": config.Location, "continent_name": config.ContinentName, diff --git a/libbeat/processors/util/geo_test.go b/libbeat/processors/util/geo_test.go index aa8754c2c32..b4b7800f3c8 100644 --- a/libbeat/processors/util/geo_test.go +++ b/libbeat/processors/util/geo_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // parseGeoConfig converts the map into a GeoConfig. @@ -96,7 +97,7 @@ func TestGeoLocationValidation(t *testing.T) { for _, location := range locations { t.Run(fmt.Sprintf("Location %s validation should be %t", location.str, location.valid), func(t *testing.T) { - geoConfig := parseConfig(t, common.MapStr{"location": location.str}) + geoConfig := parseConfig(t, mapstr.M{"location": location.str}) geoMap, err := GeoConfigToMap(geoConfig) if location.valid { diff --git a/libbeat/publisher/event.go b/libbeat/publisher/event.go index e127d012d87..791fa14f3d8 100644 --- a/libbeat/publisher/event.go +++ b/libbeat/publisher/event.go @@ -19,7 +19,7 @@ package publisher import ( "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Batch is used to pass a batch of events to the outputs and asynchronously listening @@ -59,14 +59,14 @@ type EventFlags uint8 // EventCache provides a space for outputs to define per-event metadata // that's intended to be used only within the scope of an output type EventCache struct { - m common.MapStr + m mapstr.M } // Put lets outputs put key-value pairs into the event cache func (ec *EventCache) Put(key string, value interface{}) (interface{}, error) { if ec.m == nil { // uninitialized map - ec.m = common.MapStr{} + ec.m = mapstr.M{} } return ec.m.Put(key, value) @@ -76,7 +76,7 @@ func (ec *EventCache) Put(key string, value interface{}) (interface{}, error) { func (ec *EventCache) GetValue(key string) (interface{}, error) { if ec.m == nil { // uninitialized map - return nil, common.ErrKeyNotFound + return nil, mapstr.ErrKeyNotFound } return ec.m.GetValue(key) diff --git a/libbeat/publisher/pipeline/config.go b/libbeat/publisher/pipeline/config.go index 12633ff5b3e..285553ae422 100644 --- a/libbeat/publisher/pipeline/config.go +++ b/libbeat/publisher/pipeline/config.go @@ -24,12 +24,13 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Config object for loading a pipeline instance via Load. type Config struct { // Event processing configurations - common.EventMetadata `config:",inline"` // Fields and tags to add to each event. + mapstr.EventMetadata `config:",inline"` // Fields and tags to add to each event. Processors processors.PluginConfig `config:"processors"` // Event queue diff --git a/libbeat/publisher/pipeline/stress/gen.go b/libbeat/publisher/pipeline/stress/gen.go index 71bedc70fdc..31ebc0181bd 100644 --- a/libbeat/publisher/pipeline/stress/gen.go +++ b/libbeat/publisher/pipeline/stress/gen.go @@ -25,9 +25,9 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/acker" "github.com/elastic/beats/v7/libbeat/common/atomic" ) @@ -145,7 +145,7 @@ func generate( for cs.Active() { event := beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "id": id, "hello": "world", "count": count, diff --git a/libbeat/publisher/pipetool/pipetool.go b/libbeat/publisher/pipetool/pipetool.go index 46e77da3b1f..b4b479485a3 100644 --- a/libbeat/publisher/pipetool/pipetool.go +++ b/libbeat/publisher/pipetool/pipetool.go @@ -19,8 +19,8 @@ package pipetool import ( "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/acker" + "github.com/elastic/elastic-agent-libs/mapstr" ) // connectEditPipeline modifies the client configuration using edit before calling @@ -108,7 +108,7 @@ func WithClientWrapper(pipeline beat.PipelineConnector, wrap ClientWrapper) beat // WithDynamicFields ensures that dynamicFields from autodiscovery are setup // when connecting to the publisher pipeline. // Processing.DynamicFields will only be overwritten if not is not already set. -func WithDynamicFields(pipeline beat.PipelineConnector, dynamicFields *common.MapStrPointer) beat.PipelineConnector { +func WithDynamicFields(pipeline beat.PipelineConnector, dynamicFields *mapstr.Pointer) beat.PipelineConnector { if dynamicFields == nil { return pipeline } diff --git a/libbeat/publisher/processing/default.go b/libbeat/publisher/processing/default.go index 837b7f4b024..b5904b70cc3 100644 --- a/libbeat/publisher/processing/default.go +++ b/libbeat/publisher/processing/default.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/actions" "github.com/elastic/beats/v7/libbeat/processors/timeseries" + "github.com/elastic/elastic-agent-libs/mapstr" ) // builder is used to create the event processing pipeline in Beats. The @@ -44,8 +45,8 @@ type builder struct { // global pipeline fields and tags configurations modifiers []modifier - builtinMeta common.MapStr - fields common.MapStr + builtinMeta mapstr.M + fields mapstr.M tags []string // Time series id will be calculated for Events with the TimeSeries flag if this @@ -62,14 +63,14 @@ type builder struct { type modifier interface { // BuiltinFields defines global fields to be added to every event. - BuiltinFields(beat.Info) common.MapStr + BuiltinFields(beat.Info) mapstr.M // ClientFields defines connection local fields to be added to each event // of a pipeline client. - ClientFields(beat.Info, beat.ProcessingConfig) common.MapStr + ClientFields(beat.Info, beat.ProcessingConfig) mapstr.M } -type builtinModifier func(beat.Info) common.MapStr +type builtinModifier func(beat.Info) mapstr.M // MakeDefaultBeatSupport creates a new SupportFactory based on NewDefaultSupport. // MakeDefaultBeatSupport automatically adds the `ecs.version`, `host.name` and `agent.X` fields @@ -98,7 +99,7 @@ func MakeDefaultSupport( ) SupportFactory { return func(info beat.Info, log *logp.Logger, beatCfg *common.Config) (Supporter, error) { cfg := struct { - common.EventMetadata `config:",inline"` // Fields and tags to add to each event. + mapstr.EventMetadata `config:",inline"` // Fields and tags to add to each event. Processors processors.PluginConfig `config:"processors"` TimeSeries bool `config:"timeseries.enabled"` }{} @@ -116,23 +117,23 @@ func MakeDefaultSupport( } // WithFields creates a modifier with the given default builtin fields. -func WithFields(fields common.MapStr) modifier { - return builtinModifier(func(_ beat.Info) common.MapStr { +func WithFields(fields mapstr.M) modifier { + return builtinModifier(func(_ beat.Info) mapstr.M { return fields }) } // WithECS modifier adds `ecs.version` builtin fields to a processing pipeline. -var WithECS modifier = WithFields(common.MapStr{ - "ecs": common.MapStr{ +var WithECS modifier = WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) // WithHost modifier adds `host.name` builtin fields to a processing pipeline -var WithHost modifier = builtinModifier(func(info beat.Info) common.MapStr { - return common.MapStr{ - "host": common.MapStr{ +var WithHost modifier = builtinModifier(func(info beat.Info) mapstr.M { + return mapstr.M{ + "host": mapstr.M{ "name": info.Name, }, } @@ -141,8 +142,8 @@ var WithHost modifier = builtinModifier(func(info beat.Info) common.MapStr { // WithAgentMeta adds agent meta information as builtin fields to a processing // pipeline. func WithAgentMeta() modifier { - return builtinModifier(func(info beat.Info) common.MapStr { - metadata := common.MapStr{ + return builtinModifier(func(info beat.Info) mapstr.M { + metadata := mapstr.M{ "ephemeral_id": info.EphemeralID.String(), "id": info.ID.String(), "name": info.Hostname, @@ -152,15 +153,15 @@ func WithAgentMeta() modifier { if info.Name != "" { metadata["name"] = info.Name } - return common.MapStr{"agent": metadata} + return mapstr.M{"agent": metadata} }) } // WithObserverMeta adds beat meta information as builtin fields to a processing // pipeline. func WithObserverMeta() modifier { - return builtinModifier(func(info beat.Info) common.MapStr { - metadata := common.MapStr{ + return builtinModifier(func(info beat.Info) mapstr.M { + metadata := mapstr.M{ "type": info.Beat, // Per ECS this is not a valid type value. "ephemeral_id": info.EphemeralID.String(), // Not in ECS. "hostname": info.Hostname, @@ -170,7 +171,7 @@ func WithObserverMeta() modifier { if info.Name != info.Hostname { metadata.Put("name", info.Name) } - return common.MapStr{"observer": metadata} + return mapstr.M{"observer": metadata} }) } @@ -178,7 +179,7 @@ func newBuilder( info beat.Info, log *logp.Logger, processors *processors.Processors, - eventMeta common.EventMetadata, + eventMeta mapstr.EventMetadata, modifiers []modifier, skipNormalize bool, timeSeries bool, @@ -200,7 +201,7 @@ func newBuilder( b.processors = tmp } - builtin := common.MapStr{} + builtin := mapstr.M{} for _, mod := range modifiers { m := mod.BuiltinFields(info) if len(m) > 0 { @@ -212,8 +213,8 @@ func newBuilder( } if fields := eventMeta.Fields; len(fields) > 0 { - b.fields = common.MapStr{} - common.MergeFields(b.fields, fields.Clone(), eventMeta.FieldsUnderRoot) + b.fields = mapstr.M{} + mapstr.MergeFields(b.fields, fields.Clone(), eventMeta.FieldsUnderRoot) } if timeSeries { @@ -271,12 +272,12 @@ func (b *builder) Create(cfg beat.ProcessingConfig, drop bool) (beat.Processor, builtin = tmp } - var clientFields common.MapStr + var clientFields mapstr.M for _, mod := range b.modifiers { m := mod.ClientFields(b.info, cfg) if len(m) > 0 { if clientFields == nil { - clientFields = common.MapStr{} + clientFields = mapstr.M{} } clientFields.DeepUpdate(m.Clone()) } @@ -309,7 +310,7 @@ func (b *builder) Create(cfg beat.ProcessingConfig, drop bool) (beat.Processor, fields := cfg.Fields.Clone() fields.DeepUpdate(b.fields.Clone()) if em := cfg.EventMetadata; len(em.Fields) > 0 { - common.MergeFieldsDeep(fields, em.Fields.Clone(), em.FieldsUnderRoot) + mapstr.MergeFieldsDeep(fields, em.Fields.Clone(), em.FieldsUnderRoot) } if len(fields) > 0 { @@ -322,7 +323,7 @@ func (b *builder) Create(cfg beat.ProcessingConfig, drop bool) (beat.Processor, } if cfg.DynamicFields != nil { - checkCopy := func(m common.MapStr) bool { + checkCopy := func(m mapstr.M) bool { return needsCopy || hasKeyAnyOf(m, builtin) } processors.add(makeAddDynMetaProcessor("dynamicFields", cfg.DynamicFields, checkCopy)) @@ -381,10 +382,10 @@ func makeClientProcessors( return p } -func (b builtinModifier) BuiltinFields(info beat.Info) common.MapStr { +func (b builtinModifier) BuiltinFields(info beat.Info) mapstr.M { return b(info) } -func (b builtinModifier) ClientFields(_ beat.Info, _ beat.ProcessingConfig) common.MapStr { +func (b builtinModifier) ClientFields(_ beat.Info, _ beat.ProcessingConfig) mapstr.M { return nil } diff --git a/libbeat/publisher/processing/default_test.go b/libbeat/publisher/processing/default_test.go index c4cf8a07554..c62fb85263c 100644 --- a/libbeat/publisher/processing/default_test.go +++ b/libbeat/publisher/processing/default_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/actions" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestProcessorsConfigs(t *testing.T) { @@ -44,7 +45,7 @@ func TestProcessorsConfigs(t *testing.T) { Version: "0.1", } - ecsFields := common.MapStr{"version": ecs.Version} + ecsFields := mapstr.M{"version": ecs.Version} cases := map[string]struct { factory SupportFactory @@ -52,14 +53,14 @@ func TestProcessorsConfigs(t *testing.T) { local beat.ProcessingConfig drop bool event string - want common.MapStr - wantMeta common.MapStr + want mapstr.M + wantMeta mapstr.M infoMod func(beat.Info) beat.Info }{ "user global fields and tags": { global: "{fields: {global: 1}, fields_under_root: true, tags: [tag]}", event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "value": "abc", "global": uint64(1), "tags": []string{"tag"}, @@ -68,10 +69,10 @@ func TestProcessorsConfigs(t *testing.T) { "beat local fields": { global: "", local: beat.ProcessingConfig{ - Fields: common.MapStr{"local": 1}, + Fields: mapstr.M{"local": 1}, }, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "value": "abc", "local": 1, }, @@ -79,10 +80,10 @@ func TestProcessorsConfigs(t *testing.T) { "beat local and user global fields": { global: "{fields: {global: 1}, fields_under_root: true, tags: [tag]}", local: beat.ProcessingConfig{ - Fields: common.MapStr{"local": 1}, + Fields: mapstr.M{"local": 1}, }, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "value": "abc", "global": uint64(1), "local": 1, @@ -92,10 +93,10 @@ func TestProcessorsConfigs(t *testing.T) { "user global fields overwrite beat local fields": { global: "{fields: {global: a, shared: global}, fields_under_root: true}", local: beat.ProcessingConfig{ - Fields: common.MapStr{"local": "b", "shared": "local"}, + Fields: mapstr.M{"local": "b", "shared": "local"}, }, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "value": "abc", "local": "b", "global": "a", @@ -104,15 +105,15 @@ func TestProcessorsConfigs(t *testing.T) { }, "user local fields and tags": { local: beat.ProcessingConfig{ - EventMetadata: common.EventMetadata{ - Fields: common.MapStr{"local": "a"}, + EventMetadata: mapstr.EventMetadata{ + Fields: mapstr.M{"local": "a"}, Tags: []string{"tag"}, }, }, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "value": "abc", - "fields": common.MapStr{ + "fields": mapstr.M{ "local": "a", }, "tags": []string{"tag"}, @@ -120,14 +121,14 @@ func TestProcessorsConfigs(t *testing.T) { }, "user local fields (under root) and tags": { local: beat.ProcessingConfig{ - EventMetadata: common.EventMetadata{ - Fields: common.MapStr{"local": "a"}, + EventMetadata: mapstr.EventMetadata{ + Fields: mapstr.M{"local": "a"}, FieldsUnderRoot: true, Tags: []string{"tag"}, }, }, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "value": "abc", "local": "a", "tags": []string{"tag"}, @@ -136,8 +137,8 @@ func TestProcessorsConfigs(t *testing.T) { "user local fields overwrite user global fields": { global: `{fields: {global: a, shared: global}, fields_under_root: true, tags: [global]}`, local: beat.ProcessingConfig{ - EventMetadata: common.EventMetadata{ - Fields: common.MapStr{ + EventMetadata: mapstr.EventMetadata{ + Fields: mapstr.M{ "local": "a", "shared": "local", }, @@ -146,7 +147,7 @@ func TestProcessorsConfigs(t *testing.T) { }, }, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "value": "abc", "global": "a", "local": "a", @@ -156,33 +157,33 @@ func TestProcessorsConfigs(t *testing.T) { }, "with client metadata": { local: beat.ProcessingConfig{ - Meta: common.MapStr{"index": "test"}, + Meta: mapstr.M{"index": "test"}, }, event: `{"value": "abc"}`, - want: common.MapStr{"value": "abc"}, - wantMeta: common.MapStr{"index": "test"}, + want: mapstr.M{"value": "abc"}, + wantMeta: mapstr.M{"index": "test"}, }, "with client processor": { local: beat.ProcessingConfig{ Processor: func() beat.ProcessorList { g := newGroup("test", logp.L()) - g.add(actions.NewAddFields(common.MapStr{"custom": "value"}, true, true)) + g.add(actions.NewAddFields(mapstr.M{"custom": "value"}, true, true)) return g }(), }, event: `{"value": "abc"}`, - want: common.MapStr{"value": "abc", "custom": "value"}, + want: mapstr.M{"value": "abc", "custom": "value"}, }, "with beat default fields": { factory: MakeDefaultBeatSupport(true), global: `{fields: {global: a, agent.foo: bar}, fields_under_root: true, tags: [tag]}`, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "ecs": ecsFields, - "host": common.MapStr{ + "host": mapstr.M{ "name": "test.host.name", }, - "agent": common.MapStr{ + "agent": mapstr.M{ "ephemeral_id": "123e4567-e89b-12d3-a456-426655440000", "name": "test.host.name", "id": "123e4567-e89b-12d3-a456-426655440001", @@ -203,12 +204,12 @@ func TestProcessorsConfigs(t *testing.T) { info.Name = "other.test.host.name" return info }, - want: common.MapStr{ + want: mapstr.M{ "ecs": ecsFields, - "host": common.MapStr{ + "host": mapstr.M{ "name": "other.test.host.name", }, - "agent": common.MapStr{ + "agent": mapstr.M{ "ephemeral_id": "123e4567-e89b-12d3-a456-426655440000", "name": "other.test.host.name", "id": "123e4567-e89b-12d3-a456-426655440001", @@ -225,9 +226,9 @@ func TestProcessorsConfigs(t *testing.T) { factory: MakeDefaultObserverSupport(false), global: `{fields: {global: a, observer.foo: bar}, fields_under_root: true, tags: [tag]}`, event: `{"value": "abc"}`, - want: common.MapStr{ + want: mapstr.M{ "ecs": ecsFields, - "observer": common.MapStr{ + "observer": mapstr.M{ "ephemeral_id": "123e4567-e89b-12d3-a456-426655440000", "hostname": "test.host.name", "id": "123e4567-e89b-12d3-a456-426655440001", @@ -282,21 +283,21 @@ func TestProcessorsConfigs(t *testing.T) { func TestNormalization(t *testing.T) { cases := map[string]struct { normalize bool - in common.MapStr - mod common.MapStr - want common.MapStr + in mapstr.M + mod mapstr.M + want mapstr.M }{ "no sharing if normalized": { normalize: true, - in: common.MapStr{"a": "b"}, - mod: common.MapStr{"change": "x"}, - want: common.MapStr{"a": "b"}, + in: mapstr.M{"a": "b"}, + mod: mapstr.M{"change": "x"}, + want: mapstr.M{"a": "b"}, }, "data sharing if not normalized": { normalize: false, - in: common.MapStr{"a": "b"}, - mod: common.MapStr{"change": "x"}, - want: common.MapStr{"a": "b", "change": "x"}, + in: mapstr.M{"a": "b"}, + mod: mapstr.M{"change": "x"}, + want: mapstr.M{"a": "b", "change": "x"}, }, } @@ -332,7 +333,7 @@ func BenchmarkNormalization(b *testing.B) { prog, err := s.Create(beat.ProcessingConfig{}, false) require.NoError(b, err) - fields := common.MapStr{"a": "b"} + fields := mapstr.M{"a": "b"} for i := 0; i < b.N; i++ { f := fields.Clone() _, _ = prog.Run(&beat.Event{Fields: f}) @@ -358,20 +359,20 @@ func TestDynamicFields(t *testing.T) { factory, err := MakeDefaultSupport(true)(beat.Info{}, logp.L(), common.NewConfig()) require.NoError(t, err) - dynFields := common.NewMapStrPointer(common.MapStr{}) + dynFields := mapstr.NewPointer(mapstr.M{}) prog, err := factory.Create(beat.ProcessingConfig{ DynamicFields: &dynFields, }, false) require.NoError(t, err) - actual, err := prog.Run(&beat.Event{Fields: common.MapStr{"hello": "world"}}) + actual, err := prog.Run(&beat.Event{Fields: mapstr.M{"hello": "world"}}) require.NoError(t, err) - assert.Equal(t, common.MapStr{"hello": "world"}, actual.Fields) + assert.Equal(t, mapstr.M{"hello": "world"}, actual.Fields) - dynFields.Set(common.MapStr{"dyn": "field"}) - actual, err = prog.Run(&beat.Event{Fields: common.MapStr{"hello": "world"}}) + dynFields.Set(mapstr.M{"dyn": "field"}) + actual, err = prog.Run(&beat.Event{Fields: mapstr.M{"hello": "world"}}) require.NoError(t, err) - assert.Equal(t, common.MapStr{"hello": "world", "dyn": "field"}, actual.Fields) + assert.Equal(t, mapstr.M{"hello": "world", "dyn": "field"}, actual.Fields) err = factory.Close() require.NoError(t, err) @@ -401,7 +402,7 @@ func TestProcessingClose(t *testing.T) { // Check that both processors are called assert.False(t, factoryProcessor.called) assert.False(t, clientProcessor.called) - _, err = prog.Run(&beat.Event{Fields: common.MapStr{"hello": "world"}}) + _, err = prog.Run(&beat.Event{Fields: mapstr.M{"hello": "world"}}) require.NoError(t, err) assert.True(t, factoryProcessor.called) assert.True(t, clientProcessor.called) @@ -420,8 +421,8 @@ func TestProcessingClose(t *testing.T) { assert.True(t, factoryProcessor.closed) } -func fromJSON(in string) common.MapStr { - var tmp common.MapStr +func fromJSON(in string) mapstr.M { + var tmp mapstr.M err := json.Unmarshal([]byte(in), &tmp) if err != nil { panic(err) diff --git a/libbeat/publisher/processing/processors.go b/libbeat/publisher/processing/processors.go index cac9d356153..039a92ad707 100644 --- a/libbeat/publisher/processing/processors.go +++ b/libbeat/publisher/processing/processors.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/outputs/codec/json" "github.com/elastic/beats/v7/libbeat/processors" + "github.com/elastic/elastic-agent-libs/mapstr" ) type group struct { @@ -156,7 +157,7 @@ func newAnnotateProcessor(name string, fn func(*beat.Event)) *processorFn { func (p *processorFn) String() string { return p.name } func (p *processorFn) Run(e *beat.Event) (*beat.Event, error) { return p.fn(e) } -func clientEventMeta(meta common.MapStr, needsCopy bool) *processorFn { +func clientEventMeta(meta mapstr.M, needsCopy bool) *processorFn { fn := func(event *beat.Event) { addMeta(event, meta) } if needsCopy { fn = func(event *beat.Event) { addMeta(event, meta.Clone()) } @@ -164,7 +165,7 @@ func clientEventMeta(meta common.MapStr, needsCopy bool) *processorFn { return newAnnotateProcessor("@metadata", fn) } -func addMeta(event *beat.Event, meta common.MapStr) { +func addMeta(event *beat.Event, meta mapstr.M) { if event.Meta == nil { event.Meta = meta } else { @@ -175,8 +176,8 @@ func addMeta(event *beat.Event, meta common.MapStr) { func makeAddDynMetaProcessor( name string, - meta *common.MapStrPointer, - checkCopy func(m common.MapStr) bool, + meta *mapstr.Pointer, + checkCopy func(m mapstr.M) bool, ) *processorFn { return newAnnotateProcessor(name, func(event *beat.Event) { dynFields := meta.Get() @@ -211,12 +212,12 @@ func debugPrintProcessor(info beat.Info, log *logp.Logger) *processorFn { }) } -func hasKey(m common.MapStr, key string) bool { +func hasKey(m mapstr.M, key string) bool { _, exists := m[key] return exists } -func hasKeyAnyOf(m, builtin common.MapStr) bool { +func hasKeyAnyOf(m, builtin mapstr.M) bool { for k := range builtin { if hasKey(m, k) { return true diff --git a/libbeat/publisher/queue/diskqueue/serialize.go b/libbeat/publisher/queue/diskqueue/serialize.go index fc0c24de673..2680b4d4eee 100644 --- a/libbeat/publisher/queue/diskqueue/serialize.go +++ b/libbeat/publisher/queue/diskqueue/serialize.go @@ -25,9 +25,9 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/outputs/codec" "github.com/elastic/beats/v7/libbeat/publisher" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-structform/cborl" "github.com/elastic/go-structform/gotype" "github.com/elastic/go-structform/json" @@ -54,8 +54,8 @@ type eventDecoder struct { type entry struct { Timestamp int64 Flags uint8 - Meta common.MapStr - Fields common.MapStr + Meta mapstr.M + Fields mapstr.M } func newEventEncoder() *eventEncoder { diff --git a/libbeat/publisher/queue/diskqueue/serialize_test.go b/libbeat/publisher/queue/diskqueue/serialize_test.go index 888a42eca88..75a79fdefdc 100644 --- a/libbeat/publisher/queue/diskqueue/serialize_test.go +++ b/libbeat/publisher/queue/diskqueue/serialize_test.go @@ -23,8 +23,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/publisher" + "github.com/elastic/elastic-agent-libs/mapstr" ) // A test to make sure serialization works correctly on multi-byte characters. @@ -41,7 +41,7 @@ func TestSerialize(t *testing.T) { encoder := newEventEncoder() event := publisher.Event{ Content: beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "test_field": test.value, }, }, diff --git a/libbeat/publisher/queue/queuetest/event.go b/libbeat/publisher/queue/queuetest/event.go index 5a66fe050e2..dbe6841d6b6 100644 --- a/libbeat/publisher/queue/queuetest/event.go +++ b/libbeat/publisher/queue/queuetest/event.go @@ -21,11 +21,11 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/publisher" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func makeEvent(fields common.MapStr) publisher.Event { +func makeEvent(fields mapstr.M) publisher.Event { return publisher.Event{ Content: beat.Event{ Timestamp: time.Now(), diff --git a/libbeat/publisher/queue/queuetest/producer_cancel.go b/libbeat/publisher/queue/queuetest/producer_cancel.go index 195c2de79f7..5cfa2222a74 100644 --- a/libbeat/publisher/queue/queuetest/producer_cancel.go +++ b/libbeat/publisher/queue/queuetest/producer_cancel.go @@ -22,9 +22,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/publisher" "github.com/elastic/beats/v7/libbeat/publisher/queue" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TestSingleProducerConsumer tests buffered events for a producer getting @@ -53,7 +53,7 @@ func TestProducerCancelRemovesEvents(t *testing.T, factory QueueFactory) { for ; i < N1; i++ { log.Debugf("send event %v to first producer", i) - producer.Publish(makeEvent(common.MapStr{ + producer.Publish(makeEvent(mapstr.M{ "value": i, })) } @@ -67,7 +67,7 @@ func TestProducerCancelRemovesEvents(t *testing.T, factory QueueFactory) { producer = b.Producer(queue.ProducerConfig{}) for ; i < N2; i++ { log.Debugf("send event %v to new producer", i) - producer.Publish(makeEvent(common.MapStr{ + producer.Publish(makeEvent(mapstr.M{ "value": i, })) } diff --git a/libbeat/publisher/queue/queuetest/queuetest.go b/libbeat/publisher/queue/queuetest/queuetest.go index fb5b04666b9..e56adfcfaad 100644 --- a/libbeat/publisher/queue/queuetest/queuetest.go +++ b/libbeat/publisher/queue/queuetest/queuetest.go @@ -21,8 +21,8 @@ import ( "sync" "testing" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/publisher/queue" + "github.com/elastic/elastic-agent-libs/mapstr" ) // QueueFactory is used to create a per test queue instance. @@ -258,7 +258,7 @@ func multiple( func makeProducer( maxEvents int, waitACK bool, - makeFields func(int) common.MapStr, + makeFields func(int) mapstr.M, ) func(*sync.WaitGroup, interface{}, *TestLogger, queue.Queue) func() { return func(wg *sync.WaitGroup, info interface{}, log *TestLogger, b queue.Queue) func() { wg.Add(1) @@ -354,8 +354,8 @@ func multiConsumer(numConsumers, maxEvents, batchSize int) workerFactory { } } -func countEvent(i int) common.MapStr { - return common.MapStr{ +func countEvent(i int) mapstr.M { + return mapstr.M{ "count": i, } } diff --git a/libbeat/publisher/testing/testing_test.go b/libbeat/publisher/testing/testing_test.go index a7bffe0bb36..2098306b096 100644 --- a/libbeat/publisher/testing/testing_test.go +++ b/libbeat/publisher/testing/testing_test.go @@ -23,14 +23,14 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var cnt = 0 func testEvent() beat.Event { event := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "test", "idx": cnt, }, diff --git a/libbeat/reader/message.go b/libbeat/reader/message.go index e8bd1202729..4450e4f7c9a 100644 --- a/libbeat/reader/message.go +++ b/libbeat/reader/message.go @@ -21,17 +21,17 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Message represents a reader event with timestamp, content and actual number // of bytes read from input before decoding. type Message struct { - Ts time.Time // timestamp the content was read - Content []byte // actual content read - Bytes int // total number of bytes read to generate the message - Fields common.MapStr // optional fields that can be added by reader - Meta common.MapStr // deprecated + Ts time.Time // timestamp the content was read + Content []byte // actual content read + Bytes int // total number of bytes read to generate the message + Fields mapstr.M // optional fields that can be added by reader + Meta mapstr.M // deprecated Private interface{} } @@ -53,13 +53,13 @@ func (m *Message) IsEmpty() bool { } // AddFields adds fields to the message. -func (m *Message) AddFields(fields common.MapStr) { +func (m *Message) AddFields(fields mapstr.M) { if fields == nil { return } if m.Fields == nil { - m.Fields = common.MapStr{} + m.Fields = mapstr.M{} } m.Fields.Update(fields) } @@ -72,10 +72,10 @@ func (m *Message) AddFlagsWithKey(key string, flags ...string) error { } if m.Fields == nil { - m.Fields = common.MapStr{} + m.Fields = mapstr.M{} } - return common.AddTagsWithKey(m.Fields, key, flags) + return mapstr.AddTagsWithKey(m.Fields, key, flags) } // ToEvent converts a Message to an Event that can be published @@ -84,7 +84,7 @@ func (m *Message) ToEvent() beat.Event { if len(m.Content) > 0 { if m.Fields == nil { - m.Fields = common.MapStr{} + m.Fields = mapstr.M{} } m.Fields["message"] = string(m.Content) } diff --git a/libbeat/reader/message_test.go b/libbeat/reader/message_test.go index 95891724719..fd62a3a687e 100644 --- a/libbeat/reader/message_test.go +++ b/libbeat/reader/message_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestToEvent(t *testing.T) { @@ -37,36 +37,36 @@ func TestToEvent(t *testing.T) { beat.Event{}, }, "empty content, one field": { - Message{Fields: common.MapStr{"my_field": "my_value"}}, - beat.Event{Fields: common.MapStr{"my_field": "my_value"}}, + Message{Fields: mapstr.M{"my_field": "my_value"}}, + beat.Event{Fields: mapstr.M{"my_field": "my_value"}}, }, "content, no field": { Message{Content: []byte("my message")}, - beat.Event{Fields: common.MapStr{"message": "my message"}}, + beat.Event{Fields: mapstr.M{"message": "my message"}}, }, "content, one field": { - Message{Content: []byte("my message"), Fields: common.MapStr{"my_field": "my_value"}}, - beat.Event{Fields: common.MapStr{"message": "my message", "my_field": "my_value"}}, + Message{Content: []byte("my message"), Fields: mapstr.M{"my_field": "my_value"}}, + beat.Event{Fields: mapstr.M{"message": "my message", "my_field": "my_value"}}, }, "content, message field": { - Message{Content: []byte("my message"), Fields: common.MapStr{"message": "my_message_value"}}, - beat.Event{Fields: common.MapStr{"message": "my message"}}, + Message{Content: []byte("my message"), Fields: mapstr.M{"message": "my_message_value"}}, + beat.Event{Fields: mapstr.M{"message": "my message"}}, }, "content, meta, message field": { - Message{Content: []byte("my message"), Fields: common.MapStr{"my_field": "my_value"}, Meta: common.MapStr{"meta": "id"}}, - beat.Event{Fields: common.MapStr{"message": "my message", "my_field": "my_value"}, Meta: common.MapStr{"meta": "id"}}, + Message{Content: []byte("my message"), Fields: mapstr.M{"my_field": "my_value"}, Meta: mapstr.M{"meta": "id"}}, + beat.Event{Fields: mapstr.M{"message": "my message", "my_field": "my_value"}, Meta: mapstr.M{"meta": "id"}}, }, "content, meta, message and private fields": { Message{ Ts: time.Date(2022, 1, 9, 10, 42, 0, 0, time.UTC), Content: []byte("my message"), - Meta: common.MapStr{"foo": "bar"}, + Meta: mapstr.M{"foo": "bar"}, Private: 42, }, beat.Event{ Timestamp: time.Date(2022, 1, 9, 10, 42, 0, 0, time.UTC), - Fields: common.MapStr{"message": "my message"}, - Meta: common.MapStr{"foo": "bar"}, + Fields: mapstr.M{"message": "my message"}, + Meta: mapstr.M{"foo": "bar"}, Private: 42, }, }, diff --git a/libbeat/reader/multiline/message_buffer_test.go b/libbeat/reader/multiline/message_buffer_test.go index 4816ef01a37..fff9c61b6d8 100644 --- a/libbeat/reader/multiline/message_buffer_test.go +++ b/libbeat/reader/multiline/message_buffer_test.go @@ -22,8 +22,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestMessageBufferAddLine(t *testing.T) { @@ -90,7 +90,7 @@ func TestFinalizeMessage(t *testing.T) { }, expected: reader.Message{ Content: []byte("tooooooooooooooooooo"), - Fields: common.MapStr{"log": common.MapStr{"flags": []string{"truncated"}}}, + Fields: mapstr.M{"log": mapstr.M{"flags": []string{"truncated"}}}, }, }, "untruncated multiline message": { @@ -101,7 +101,7 @@ func TestFinalizeMessage(t *testing.T) { }, expected: reader.Message{ Content: []byte("line1\nline2"), - Fields: common.MapStr{"log": common.MapStr{"flags": []string{"multiline"}}}, + Fields: mapstr.M{"log": mapstr.M{"flags": []string{"multiline"}}}, }, }, "truncated multiline message": { @@ -112,7 +112,7 @@ func TestFinalizeMessage(t *testing.T) { }, expected: reader.Message{ Content: []byte("line1\nli"), - Fields: common.MapStr{"log": common.MapStr{"flags": []string{"truncated", "multiline"}}}, + Fields: mapstr.M{"log": mapstr.M{"flags": []string{"truncated", "multiline"}}}, }, }, } diff --git a/libbeat/reader/parser/parser_test.go b/libbeat/reader/parser/parser_test.go index dcbc322c93d..8dda0a39930 100644 --- a/libbeat/reader/parser/parser_test.go +++ b/libbeat/reader/parser/parser_test.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/reader/multiline" "github.com/elastic/beats/v7/libbeat/reader/readfile" "github.com/elastic/beats/v7/libbeat/reader/readfile/encoding" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestParsersConfigSuffix(t *testing.T) { @@ -348,7 +349,7 @@ func TestJSONParsersWithFields(t *testing.T) { "JSON post processer with keys_under_root": { message: reader.Message{ Content: []byte("{\"key\":\"value\"}"), - Fields: common.MapStr{}, + Fields: mapstr.M{}, }, config: map[string]interface{}{ "parsers": []map[string]interface{}{ @@ -361,7 +362,7 @@ func TestJSONParsersWithFields(t *testing.T) { }, expectedMessage: reader.Message{ Content: []byte(""), - Fields: common.MapStr{ + Fields: mapstr.M{ "key": "value", }, }, @@ -369,7 +370,7 @@ func TestJSONParsersWithFields(t *testing.T) { "JSON post processer with document ID": { message: reader.Message{ Content: []byte("{\"key\":\"value\", \"my-id-field\":\"my-id\"}"), - Fields: common.MapStr{}, + Fields: mapstr.M{}, }, config: map[string]interface{}{ "parsers": []map[string]interface{}{ @@ -383,10 +384,10 @@ func TestJSONParsersWithFields(t *testing.T) { }, expectedMessage: reader.Message{ Content: []byte(""), - Fields: common.MapStr{ + Fields: mapstr.M{ "key": "value", }, - Meta: common.MapStr{ + Meta: mapstr.M{ "_id": "my-id", }, }, @@ -394,7 +395,7 @@ func TestJSONParsersWithFields(t *testing.T) { "JSON post processer with overwrite keys and under root": { message: reader.Message{ Content: []byte("{\"key\": \"value\"}"), - Fields: common.MapStr{ + Fields: mapstr.M{ "key": "another-value", "other-key": "other-value", }, @@ -411,7 +412,7 @@ func TestJSONParsersWithFields(t *testing.T) { }, expectedMessage: reader.Message{ Content: []byte(""), - Fields: common.MapStr{ + Fields: mapstr.M{ "key": "value", "other-key": "other-value", }, @@ -459,25 +460,25 @@ func TestContainerParser(t *testing.T) { expectedMessages: []reader.Message{ reader.Message{ Content: []byte("Fetching main repository github.com/elastic/beats...\n"), - Fields: common.MapStr{ + Fields: mapstr.M{ "stream": "stdout", }, }, reader.Message{ Content: []byte("Fetching dependencies...\n"), - Fields: common.MapStr{ + Fields: mapstr.M{ "stream": "stdout", }, }, reader.Message{ Content: []byte("Execute /scripts/packetbeat_before_build.sh\n"), - Fields: common.MapStr{ + Fields: mapstr.M{ "stream": "stdout", }, }, reader.Message{ Content: []byte("patching file vendor/github.com/tsg/gopacket/pcap/pcap.go\n"), - Fields: common.MapStr{ + Fields: mapstr.M{ "stream": "stdout", }, }, @@ -498,7 +499,7 @@ func TestContainerParser(t *testing.T) { expectedMessages: []reader.Message{ reader.Message{ Content: []byte("2017-09-12 22:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache\n"), - Fields: common.MapStr{ + Fields: mapstr.M{ "stream": "stdout", }, }, @@ -519,13 +520,13 @@ func TestContainerParser(t *testing.T) { expectedMessages: []reader.Message{ reader.Message{ Content: []byte("Fetching main repository github.com/elastic/beats...\n"), - Fields: common.MapStr{ + Fields: mapstr.M{ "stream": "stdout", }, }, reader.Message{ Content: []byte("Execute /scripts/packetbeat_before_build.sh\n"), - Fields: common.MapStr{ + Fields: mapstr.M{ "stream": "stdout", }, }, diff --git a/libbeat/reader/readfile/encode.go b/libbeat/reader/readfile/encode.go index 13b5c2d2712..3b63c43aa35 100644 --- a/libbeat/reader/readfile/encode.go +++ b/libbeat/reader/readfile/encode.go @@ -22,9 +22,9 @@ import ( "io" "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/reader" "github.com/elastic/beats/v7/libbeat/reader/readfile/encoding" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Reader produces lines by reading lines from an io.Reader @@ -58,7 +58,7 @@ func (r EncoderReader) Next() (reader.Message, error) { Ts: time.Now(), Content: bytes.Trim(c, "\xef\xbb\xbf"), Bytes: sz, - Fields: common.MapStr{}, + Fields: mapstr.M{}, }, err } diff --git a/libbeat/reader/readfile/metafields.go b/libbeat/reader/readfile/metafields.go index 1b5cb8bed20..5117bc6ec9b 100644 --- a/libbeat/reader/readfile/metafields.go +++ b/libbeat/reader/readfile/metafields.go @@ -18,8 +18,8 @@ package readfile import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Reader produces lines by reading lines from an io.Reader @@ -47,10 +47,10 @@ func (r *FileMetaReader) Next() (reader.Message, error) { return message, err } - message.Fields.DeepUpdate(common.MapStr{ - "log": common.MapStr{ + message.Fields.DeepUpdate(mapstr.M{ + "log": mapstr.M{ "offset": r.offset, - "file": common.MapStr{ + "file": mapstr.M{ "path": r.path, }, }, diff --git a/libbeat/reader/readfile/metafields_test.go b/libbeat/reader/readfile/metafields_test.go index a1480e6291b..0bc4a2d72ec 100644 --- a/libbeat/reader/readfile/metafields_test.go +++ b/libbeat/reader/readfile/metafields_test.go @@ -23,8 +23,8 @@ import ( "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestMetaFieldsOffset(t *testing.T) { @@ -32,17 +32,17 @@ func TestMetaFieldsOffset(t *testing.T) { reader.Message{ Content: []byte("my line"), Bytes: 7, - Fields: common.MapStr{}, + Fields: mapstr.M{}, }, reader.Message{ Content: []byte("my line again"), Bytes: 13, - Fields: common.MapStr{}, + Fields: mapstr.M{}, }, reader.Message{ Content: []byte(""), Bytes: 10, - Fields: common.MapStr{}, + Fields: mapstr.M{}, }, } @@ -55,11 +55,11 @@ func TestMetaFieldsOffset(t *testing.T) { break } - expectedFields := common.MapStr{} + expectedFields := mapstr.M{} if len(msg.Content) != 0 { - expectedFields = common.MapStr{ - "log": common.MapStr{ - "file": common.MapStr{ + expectedFields = mapstr.M{ + "log": mapstr.M{ + "file": mapstr.M{ "path": path, }, "offset": offset, diff --git a/libbeat/reader/readjson/docker_json.go b/libbeat/reader/readjson/docker_json.go index d182ed7a187..a332ffbaa3c 100644 --- a/libbeat/reader/readjson/docker_json.go +++ b/libbeat/reader/readjson/docker_json.go @@ -26,9 +26,9 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) // DockerJSONReader processor renames a given field @@ -157,7 +157,7 @@ func (p *DockerJSONReader) parseCRILog(message *reader.Message, msg *logLine) er } msg.Partial = partial - message.AddFields(common.MapStr{ + message.AddFields(mapstr.M{ "stream": msg.Stream, }) // Remove \n ending for partial messages @@ -186,13 +186,13 @@ func (p *DockerJSONReader) parseDockerJSONLog(message *reader.Message, msg *logL } message.Ts = ts - message.AddFields(common.MapStr{ + message.AddFields(mapstr.M{ "stream": msg.Stream, }) if len(msg.Attrs) > 0 { - message.AddFields(common.MapStr{ - "docker": common.MapStr{ + message.AddFields(mapstr.M{ + "docker": mapstr.M{ "attrs": msg.Attrs, }, }) diff --git a/libbeat/reader/readjson/docker_json_test.go b/libbeat/reader/readjson/docker_json_test.go index de03b87da81..06ed7649ea6 100644 --- a/libbeat/reader/readjson/docker_json_test.go +++ b/libbeat/reader/readjson/docker_json_test.go @@ -24,8 +24,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDockerJSON(t *testing.T) { @@ -45,7 +45,7 @@ func TestDockerJSON(t *testing.T) { stream: "all", expectedMessage: reader.Message{ Content: []byte("1:M 09 Nov 13:27:36.276 # User requested shutdown...\n"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 11, 9, 13, 27, 36, 277747246, time.UTC), Bytes: 122, }, @@ -65,7 +65,7 @@ func TestDockerJSON(t *testing.T) { stream: "all", expectedMessage: reader.Message{ Content: []byte(""), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 11, 9, 13, 27, 36, 277747246, time.UTC), Bytes: 68, }, @@ -103,7 +103,7 @@ func TestDockerJSON(t *testing.T) { stream: "all", expectedMessage: reader.Message{ Content: []byte("2017-09-12 22:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 9, 12, 22, 32, 21, 212861448, time.UTC), Bytes: 115, }, @@ -115,7 +115,7 @@ func TestDockerJSON(t *testing.T) { stream: "all", expectedMessage: reader.Message{ Content: []byte("2017-09-12 22:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 9, 12, 22, 32, 21, 212861448, time.UTC), Bytes: 117, }, @@ -131,7 +131,7 @@ func TestDockerJSON(t *testing.T) { stream: "stderr", expectedMessage: reader.Message{ Content: []byte("unfiltered\n"), - Fields: common.MapStr{"stream": "stderr"}, + Fields: mapstr.M{"stream": "stderr"}, Ts: time.Date(2017, 11, 9, 13, 27, 36, 277747246, time.UTC), Bytes: 158, }, @@ -146,7 +146,7 @@ func TestDockerJSON(t *testing.T) { stream: "stderr", expectedMessage: reader.Message{ Content: []byte("2017-11-12 23:32:21.212 [ERROR][77] table.go 111: error"), - Fields: common.MapStr{"stream": "stderr"}, + Fields: mapstr.M{"stream": "stderr"}, Ts: time.Date(2017, 11, 12, 23, 32, 21, 212771448, time.UTC), Bytes: 212, }, @@ -162,7 +162,7 @@ func TestDockerJSON(t *testing.T) { partial: true, expectedMessage: reader.Message{ Content: []byte("1:M 09 Nov 13:27:36.276 # User requested shutdown...\n"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 11, 9, 13, 27, 36, 277747246, time.UTC), Bytes: 190, }, @@ -177,7 +177,7 @@ func TestDockerJSON(t *testing.T) { partial: true, expectedMessage: reader.Message{ Content: []byte("2017-10-12 13:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache error"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 10, 12, 13, 32, 21, 232861448, time.UTC), Bytes: 163, }, @@ -192,7 +192,7 @@ func TestDockerJSON(t *testing.T) { stream: "stdout", expectedMessage: reader.Message{ Content: []byte("2017-10-12 13:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache error"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 10, 12, 13, 32, 21, 232861448, time.UTC), Bytes: 164, }, @@ -209,7 +209,7 @@ func TestDockerJSON(t *testing.T) { partial: false, expectedMessage: reader.Message{ Content: []byte("1:M 09 Nov 13:27:36.276 # User requested "), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 11, 9, 13, 27, 36, 277747246, time.UTC), Bytes: 109, }, @@ -240,7 +240,7 @@ func TestDockerJSON(t *testing.T) { stream: "all", expectedMessage: reader.Message{ Content: []byte("2017-09-12 22:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 9, 12, 22, 32, 21, 212861448, time.UTC), Bytes: 115, }, @@ -253,7 +253,7 @@ func TestDockerJSON(t *testing.T) { stream: "all", expectedMessage: reader.Message{ Content: []byte("2017-09-12 22:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 9, 12, 22, 32, 21, 212861448, time.UTC), Bytes: 117, }, @@ -270,7 +270,7 @@ func TestDockerJSON(t *testing.T) { partial: true, expectedMessage: reader.Message{ Content: []byte("2017-10-12 13:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache error"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 10, 12, 13, 32, 21, 232861448, time.UTC), Bytes: 163, }, @@ -286,7 +286,7 @@ func TestDockerJSON(t *testing.T) { stream: "stdout", expectedMessage: reader.Message{ Content: []byte("2017-10-12 13:32:21.212 [INFO][88] table.go 710: Invalidating dataplane cache error"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 10, 12, 13, 32, 21, 232861448, time.UTC), Bytes: 164, }, @@ -316,7 +316,7 @@ func TestDockerJSON(t *testing.T) { partial: true, expectedMessage: reader.Message{ Content: []byte("hello\n"), - Fields: common.MapStr{"docker": common.MapStr{"attrs": map[string]string{"KEY1": "value1", "KEY2": "value2"}}, "stream": "stdout"}, + Fields: mapstr.M{"docker": mapstr.M{"attrs": map[string]string{"KEY1": "value1", "KEY2": "value2"}}, "stream": "stdout"}, Ts: time.Date(2017, 11, 9, 13, 27, 36, 277747246, time.UTC), Bytes: 117, }, @@ -339,7 +339,7 @@ func TestDockerJSON(t *testing.T) { stream: "all", expectedMessage: reader.Message{ Content: []byte("1:M 09 Nov 13:27:36.276 # User requested"), - Fields: common.MapStr{"stream": "stdout"}, + Fields: mapstr.M{"stream": "stdout"}, Ts: time.Date(2017, 11, 9, 13, 27, 36, 277747246, time.UTC), Bytes: 205, }, diff --git a/libbeat/reader/readjson/json.go b/libbeat/reader/readjson/json.go index e17deeb4896..0613bc5bfb8 100644 --- a/libbeat/reader/readjson/json.go +++ b/libbeat/reader/readjson/json.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/jsontransform" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) // JSONReader parses JSON inputs @@ -65,7 +66,7 @@ func NewJSONParser(r reader.Reader, cfg *ParserConfig) *JSONParser { // decodeJSON unmarshals the text parameter into a MapStr and // returns the new text column if one was requested. -func (r *JSONReader) decode(text []byte) ([]byte, common.MapStr) { +func (r *JSONReader) decode(text []byte) ([]byte, mapstr.M) { var jsonFields map[string]interface{} err := unmarshal(text, &jsonFields) @@ -74,7 +75,7 @@ func (r *JSONReader) decode(text []byte) ([]byte, common.MapStr) { r.logger.Errorf("Error decoding JSON: %v", err) } if r.cfg.AddErrorKey { - jsonFields = common.MapStr{"error": createJSONError(fmt.Sprintf("Error decoding JSON: %v", err))} + jsonFields = mapstr.M{"error": createJSONError(fmt.Sprintf("Error decoding JSON: %v", err))} } return text, jsonFields } @@ -122,9 +123,9 @@ func (r *JSONReader) Next() (reader.Message, error) { return message, err } - var fields common.MapStr + var fields mapstr.M message.Content, fields = r.decode(message.Content) - message.AddFields(common.MapStr{"json": fields}) + message.AddFields(mapstr.M{"json": fields}) return message, nil } @@ -132,8 +133,8 @@ func (r *JSONReader) Close() error { return r.reader.Close() } -func createJSONError(message string) common.MapStr { - return common.MapStr{"message": message, "type": "json"} +func createJSONError(message string) mapstr.M { + return mapstr.M{"message": message, "type": "json"} } // Next decodes JSON and returns the filled Line object. @@ -151,7 +152,7 @@ func (p *JSONParser) Next() (reader.Message, error) { return message, fmt.Errorf("cannot decode JSON message, missing key: %s", p.field) } } - var jsonFields common.MapStr + var jsonFields mapstr.M message.Content, jsonFields = p.JSONReader.decode(from) if len(jsonFields) == 0 { @@ -176,7 +177,7 @@ func (p *JSONParser) Next() (reader.Message, error) { jsonFields.Delete(key) if message.Meta == nil { - message.Meta = common.MapStr{} + message.Meta = mapstr.M{} } message.Meta["_id"] = id } @@ -194,7 +195,7 @@ func (p *JSONParser) Next() (reader.Message, error) { message.Fields = event.Fields message.Meta = event.Meta } else { - message.AddFields(common.MapStr{p.target: jsonFields}) + message.AddFields(mapstr.M{p.target: jsonFields}) } return message, err @@ -204,7 +205,7 @@ func (p *JSONParser) Next() (reader.Message, error) { // respecting the KeysUnderRoot, ExpandKeys, and OverwriteKeys configuration options. // If MessageKey is defined, the Text value from the event always // takes precedence. -func MergeJSONFields(data common.MapStr, jsonFields common.MapStr, text *string, config Config) (string, time.Time) { +func MergeJSONFields(data mapstr.M, jsonFields mapstr.M, text *string, config Config) (string, time.Time) { // The message key might have been modified by multiline if len(config.MessageKey) > 0 && text != nil { jsonFields[config.MessageKey] = *text diff --git a/libbeat/reader/readjson/json_test.go b/libbeat/reader/readjson/json_test.go index 68c88f6297c..15db300ef5c 100644 --- a/libbeat/reader/readjson/json_test.go +++ b/libbeat/reader/readjson/json_test.go @@ -22,6 +22,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" @@ -106,31 +107,31 @@ func TestDecodeJSON(t *testing.T) { Text string Config Config ExpectedText string - ExpectedMap common.MapStr + ExpectedMap mapstr.M }{ { Text: `{"message": "test", "value": 1}`, Config: Config{MessageKey: "message"}, ExpectedText: "test", - ExpectedMap: common.MapStr{"message": "test", "value": int64(1)}, + ExpectedMap: mapstr.M{"message": "test", "value": int64(1)}, }, { Text: `{"message": "test", "value": 1}`, Config: Config{MessageKey: "message1"}, ExpectedText: "", - ExpectedMap: common.MapStr{"message": "test", "value": int64(1)}, + ExpectedMap: mapstr.M{"message": "test", "value": int64(1)}, }, { Text: `{"message": "test", "value": 1}`, Config: Config{MessageKey: "value"}, ExpectedText: "", - ExpectedMap: common.MapStr{"message": "test", "value": int64(1)}, + ExpectedMap: mapstr.M{"message": "test", "value": int64(1)}, }, { Text: `{"message": "test", "value": "1"}`, Config: Config{MessageKey: "value"}, ExpectedText: "1", - ExpectedMap: common.MapStr{"message": "test", "value": "1"}, + ExpectedMap: mapstr.M{"message": "test", "value": "1"}, }, { // in case of JSON decoding errors, the text is passed as is @@ -144,35 +145,35 @@ func TestDecodeJSON(t *testing.T) { Text: `null`, Config: Config{MessageKey: "value", AddErrorKey: true}, ExpectedText: `null`, - ExpectedMap: common.MapStr{"error": common.MapStr{"message": "Error decoding JSON: ", "type": "json"}}, + ExpectedMap: mapstr.M{"error": mapstr.M{"message": "Error decoding JSON: ", "type": "json"}}, }, { // Add key error helps debugging this Text: `{"message": "test", "value": "`, Config: Config{MessageKey: "value", AddErrorKey: true}, ExpectedText: `{"message": "test", "value": "`, - ExpectedMap: common.MapStr{"error": common.MapStr{"message": "Error decoding JSON: unexpected EOF", "type": "json"}}, + ExpectedMap: mapstr.M{"error": mapstr.M{"message": "Error decoding JSON: unexpected EOF", "type": "json"}}, }, { // If the text key is not found, put an error Text: `{"message": "test", "value": "1"}`, Config: Config{MessageKey: "hello", AddErrorKey: true}, ExpectedText: ``, - ExpectedMap: common.MapStr{"message": "test", "value": "1", "error": common.MapStr{"message": "Key 'hello' not found", "type": "json"}}, + ExpectedMap: mapstr.M{"message": "test", "value": "1", "error": mapstr.M{"message": "Key 'hello' not found", "type": "json"}}, }, { // If the text key is found, but not a string, put an error Text: `{"message": "test", "value": 1}`, Config: Config{MessageKey: "value", AddErrorKey: true}, ExpectedText: ``, - ExpectedMap: common.MapStr{"message": "test", "value": int64(1), "error": common.MapStr{"message": "Value of key 'value' is not a string", "type": "json"}}, + ExpectedMap: mapstr.M{"message": "test", "value": int64(1), "error": mapstr.M{"message": "Value of key 'value' is not a string", "type": "json"}}, }, { // Without a text key, simple return the json and an empty text Text: `{"message": "test", "value": 1}`, Config: Config{AddErrorKey: true}, ExpectedText: ``, - ExpectedMap: common.MapStr{"message": "test", "value": int64(1)}, + ExpectedMap: mapstr.M{"message": "test", "value": int64(1)}, }, { // If AddErrorKey set to false, error event should not be set. @@ -203,28 +204,28 @@ func TestMergeJSONFields(t *testing.T) { now := time.Now().UTC() tests := map[string]struct { - Data common.MapStr + Data mapstr.M Text *string JSONConfig Config - ExpectedItems common.MapStr + ExpectedItems mapstr.M ExpectedTimestamp time.Time ExpectedID string }{ "default: do not overwrite": { - Data: common.MapStr{"type": "test_type", "json": common.MapStr{"type": "test", "text": "hello"}}, + Data: mapstr.M{"type": "test_type", "json": mapstr.M{"type": "test", "text": "hello"}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test_type", "text": "hello", }, ExpectedTimestamp: time.Time{}, }, "overwrite keys if configured": { - Data: common.MapStr{"type": "test_type", "json": common.MapStr{"type": "test", "text": "hello"}}, + Data: mapstr.M{"type": "test_type", "json": mapstr.M{"type": "test", "text": "hello"}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test", "text": "hello", }, @@ -232,22 +233,22 @@ func TestMergeJSONFields(t *testing.T) { }, "use json namespace w/o keys_under_root": { // without keys_under_root, put everything in a json key - Data: common.MapStr{"type": "test_type", "json": common.MapStr{"type": "test", "text": "hello"}}, + Data: mapstr.M{"type": "test_type", "json": mapstr.M{"type": "test", "text": "hello"}}, Text: &text, JSONConfig: Config{}, - ExpectedItems: common.MapStr{ - "json": common.MapStr{"type": "test", "text": "hello"}, + ExpectedItems: mapstr.M{ + "json": mapstr.M{"type": "test", "text": "hello"}, }, ExpectedTimestamp: time.Time{}, }, "write result to message_key field": { // when MessageKey is defined, the Text overwrites the value of that key - Data: common.MapStr{"type": "test_type", "json": common.MapStr{"type": "test", "text": "hi"}}, + Data: mapstr.M{"type": "test_type", "json": mapstr.M{"type": "test", "text": "hi"}}, Text: &text, JSONConfig: Config{MessageKey: "text"}, - ExpectedItems: common.MapStr{ - "json": common.MapStr{"type": "test", "text": "hello"}, + ExpectedItems: mapstr.M{ + "json": mapstr.M{"type": "test", "text": "hello"}, "type": "test_type", }, ExpectedTimestamp: time.Time{}, @@ -255,10 +256,10 @@ func TestMergeJSONFields(t *testing.T) { "parse @timestamp": { // when @timestamp is in JSON and overwrite_keys is true, parse it // in a common.Time - Data: common.MapStr{"@timestamp": now, "type": "test_type", "json": common.MapStr{"type": "test", "@timestamp": "2016-04-05T18:47:18.444Z"}}, + Data: mapstr.M{"@timestamp": now, "type": "test_type", "json": mapstr.M{"type": "test", "@timestamp": "2016-04-05T18:47:18.444Z"}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test", }, ExpectedTimestamp: time.Time(common.MustParseTime("2016-04-05T18:47:18.444Z")), @@ -266,12 +267,12 @@ func TestMergeJSONFields(t *testing.T) { "fail to parse @timestamp": { // when the parsing on @timestamp fails, leave the existing value and add an error key // in a common.Time - Data: common.MapStr{"@timestamp": common.Time(now), "type": "test_type", "json": common.MapStr{"type": "test", "@timestamp": "2016-04-05T18:47:18.44XX4Z"}}, + Data: mapstr.M{"@timestamp": common.Time(now), "type": "test_type", "json": mapstr.M{"type": "test", "@timestamp": "2016-04-05T18:47:18.44XX4Z"}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true, AddErrorKey: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test", - "error": common.MapStr{"type": "json", "message": "@timestamp not overwritten (parse error on 2016-04-05T18:47:18.44XX4Z)"}, + "error": mapstr.M{"type": "json", "message": "@timestamp not overwritten (parse error on 2016-04-05T18:47:18.44XX4Z)"}, }, ExpectedTimestamp: time.Time{}, }, @@ -279,54 +280,54 @@ func TestMergeJSONFields(t *testing.T) { "wrong @timestamp format": { // when the @timestamp has the wrong type, leave the existing value and add an error key // in a common.Time - Data: common.MapStr{"@timestamp": common.Time(now), "type": "test_type", "json": common.MapStr{"type": "test", "@timestamp": 42}}, + Data: mapstr.M{"@timestamp": common.Time(now), "type": "test_type", "json": mapstr.M{"type": "test", "@timestamp": 42}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true, AddErrorKey: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test", - "error": common.MapStr{"type": "json", "message": "@timestamp not overwritten (not string)"}, + "error": mapstr.M{"type": "json", "message": "@timestamp not overwritten (not string)"}, }, ExpectedTimestamp: time.Time{}, }, "ignore non-string type field": { // if overwrite_keys is true, but the `type` key in json is not a string, ignore it - Data: common.MapStr{"type": "test_type", "json": common.MapStr{"type": 42}}, + Data: mapstr.M{"type": "test_type", "json": mapstr.M{"type": 42}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true, AddErrorKey: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test_type", - "error": common.MapStr{"type": "json", "message": "type not overwritten (not string)"}, + "error": mapstr.M{"type": "json", "message": "type not overwritten (not string)"}, }, ExpectedTimestamp: time.Time{}, }, "ignore empty type field": { // if overwrite_keys is true, but the `type` key in json is empty, ignore it - Data: common.MapStr{"type": "test_type", "json": common.MapStr{"type": ""}}, + Data: mapstr.M{"type": "test_type", "json": mapstr.M{"type": ""}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true, AddErrorKey: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test_type", - "error": common.MapStr{"type": "json", "message": "type not overwritten (invalid value [])"}, + "error": mapstr.M{"type": "json", "message": "type not overwritten (invalid value [])"}, }, ExpectedTimestamp: time.Time{}, }, "ignore type names starting with underscore": { // if overwrite_keys is true, but the `type` key in json starts with _, ignore it - Data: common.MapStr{"@timestamp": common.Time(now), "type": "test_type", "json": common.MapStr{"type": "_type"}}, + Data: mapstr.M{"@timestamp": common.Time(now), "type": "test_type", "json": mapstr.M{"type": "_type"}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true, AddErrorKey: true}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test_type", - "error": common.MapStr{"type": "json", "message": "type not overwritten (invalid value [_type])"}, + "error": mapstr.M{"type": "json", "message": "type not overwritten (invalid value [_type])"}, }, ExpectedTimestamp: time.Time{}, }, "do not set error if AddErrorKey is false": { - Data: common.MapStr{"@timestamp": common.Time(now), "type": "test_type", "json": common.MapStr{"type": "_type"}}, + Data: mapstr.M{"@timestamp": common.Time(now), "type": "test_type", "json": mapstr.M{"type": "_type"}}, Text: &text, JSONConfig: Config{KeysUnderRoot: true, OverwriteKeys: true, AddErrorKey: false}, - ExpectedItems: common.MapStr{ + ExpectedItems: mapstr.M{ "type": "test_type", "error": nil, }, @@ -334,28 +335,28 @@ func TestMergeJSONFields(t *testing.T) { }, "extract event id": { // if document_id is set, extract the ID from the event - Data: common.MapStr{"@timestamp": common.Time(now), "json": common.MapStr{"id": "test_id"}}, + Data: mapstr.M{"@timestamp": common.Time(now), "json": mapstr.M{"id": "test_id"}}, JSONConfig: Config{DocumentID: "id"}, ExpectedID: "test_id", }, "extract event id with wrong type": { // if document_id is set, extract the ID from the event - Data: common.MapStr{"@timestamp": common.Time(now), "json": common.MapStr{"id": 42}}, + Data: mapstr.M{"@timestamp": common.Time(now), "json": mapstr.M{"id": 42}}, JSONConfig: Config{DocumentID: "id"}, ExpectedID: "", }, "expand dotted fields": { - Data: common.MapStr{"json": common.MapStr{"a.b": common.MapStr{"c": "c"}, "a.b.d": "d"}}, + Data: mapstr.M{"json": mapstr.M{"a.b": mapstr.M{"c": "c"}, "a.b.d": "d"}}, JSONConfig: Config{ExpandKeys: true, KeysUnderRoot: true}, - ExpectedItems: common.MapStr{"a": common.MapStr{"b": common.MapStr{"c": "c", "d": "d"}}}, + ExpectedItems: mapstr.M{"a": mapstr.M{"b": mapstr.M{"c": "c", "d": "d"}}}, }, } for name, test := range tests { t.Run(name, func(t *testing.T) { - var jsonFields common.MapStr + var jsonFields mapstr.M if fields, ok := test.Data["json"]; ok { - jsonFields = fields.(common.MapStr) + jsonFields = fields.(mapstr.M) } id, ts := MergeJSONFields(test.Data, jsonFields, test.Text, test.JSONConfig) diff --git a/libbeat/reader/syslog/message.go b/libbeat/reader/syslog/message.go index 3753d73444d..3401d0a2abe 100644 --- a/libbeat/reader/syslog/message.go +++ b/libbeat/reader/syslog/message.go @@ -21,7 +21,7 @@ import ( "strings" "time" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -173,8 +173,8 @@ func (m *message) setDataValue(id, key, value string) { } // fields produces fields from the message. -func (m message) fields() common.MapStr { - f := common.MapStr{} +func (m message) fields() mapstr.M { + f := mapstr.M{} // Syslog fields. _, _ = f.Put("log.syslog.priority", m.priority) diff --git a/libbeat/reader/syslog/message_test.go b/libbeat/reader/syslog/message_test.go index 81b86ede9d2..94d7a72890a 100644 --- a/libbeat/reader/syslog/message_test.go +++ b/libbeat/reader/syslog/message_test.go @@ -23,7 +23,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func mustParseTime(layout string, value string) time.Time { @@ -463,7 +463,7 @@ func TestMessage_SetData(t *testing.T) { func TestMessage_Fields(t *testing.T) { cases := map[string]struct { In *message - Want common.MapStr + Want mapstr.M }{ "valid": { In: &message{ @@ -483,15 +483,15 @@ func TestMessage_Fields(t *testing.T) { }, }, }, - Want: common.MapStr{ - "log": common.MapStr{ - "syslog": common.MapStr{ + Want: mapstr.M{ + "log": mapstr.M{ + "syslog": mapstr.M{ "priority": 13, - "facility": common.MapStr{ + "facility": mapstr.M{ "code": 1, "name": "user-level", }, - "severity": common.MapStr{ + "severity": mapstr.M{ "code": 5, "name": "Notice", }, diff --git a/libbeat/reader/syslog/syslog.go b/libbeat/reader/syslog/syslog.go index 2bd3f358c43..0354bd96e46 100644 --- a/libbeat/reader/syslog/syslog.go +++ b/libbeat/reader/syslog/syslog.go @@ -22,10 +22,10 @@ import ( "fmt" "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgtype" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) //go:generate ragel -Z -G2 -o rfc3164_gen.go parser/parser_rfc3164.rl @@ -97,7 +97,7 @@ func DefaultConfig() Config { // ParseMessage will parse syslog message data formatted as format into fields. loc is used to enrich // timestamps that lack a time zone. -func ParseMessage(data string, format Format, loc *time.Location) (common.MapStr, time.Time, error) { +func ParseMessage(data string, format Format, loc *time.Location) (mapstr.M, time.Time, error) { var m message var err error @@ -114,7 +114,7 @@ func ParseMessage(data string, format Format, loc *time.Location) (common.MapStr m, err = parseRFC5424(data) } if err != nil { - return common.MapStr{}, time.Time{}, err + return mapstr.M{}, time.Time{}, err } return m.fields(), m.timestamp, nil diff --git a/libbeat/reader/syslog/syslog_test.go b/libbeat/reader/syslog/syslog_test.go index bc61c6d2bcf..3394c05b769 100644 --- a/libbeat/reader/syslog/syslog_test.go +++ b/libbeat/reader/syslog/syslog_test.go @@ -25,8 +25,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/reader" + "github.com/elastic/elastic-agent-libs/mapstr" ) var _ reader.Reader = &testReader{} @@ -50,7 +50,7 @@ func (t *testReader) Next() (reader.Message, error) { Ts: t.referenceTime, Content: t.messages[t.currentLine], Bytes: len(t.messages[t.currentLine]), - Fields: common.MapStr{}, + Fields: mapstr.M{}, } t.currentLine++ @@ -61,7 +61,7 @@ func TestNewParser(t *testing.T) { type testResult struct { Timestamp time.Time Content []byte - Fields common.MapStr + Fields mapstr.M WantErr bool } @@ -82,15 +82,15 @@ func TestNewParser(t *testing.T) { { Timestamp: mustParseTime(time.RFC3339Nano, "2003-10-11T22:14:15.003Z"), Content: []byte("this is the message"), - Fields: common.MapStr{ - "log": common.MapStr{ - "syslog": common.MapStr{ + Fields: mapstr.M{ + "log": mapstr.M{ + "syslog": mapstr.M{ "priority": 165, - "facility": common.MapStr{ + "facility": mapstr.M{ "code": 20, "name": "local4", }, - "severity": common.MapStr{ + "severity": mapstr.M{ "code": 5, "name": "Notice", }, @@ -117,15 +117,15 @@ func TestNewParser(t *testing.T) { { Timestamp: mustParseTimeLoc(time.Stamp, "Oct 11 22:14:15", time.Local), Content: []byte("this is the message"), - Fields: common.MapStr{ - "log": common.MapStr{ - "syslog": common.MapStr{ + Fields: mapstr.M{ + "log": mapstr.M{ + "syslog": mapstr.M{ "priority": 13, - "facility": common.MapStr{ + "facility": mapstr.M{ "code": 1, "name": "user-level", }, - "severity": common.MapStr{ + "severity": mapstr.M{ "code": 5, "name": "Notice", }, diff --git a/libbeat/reader/syslog/util.go b/libbeat/reader/syslog/util.go index 7b846a0041f..5153d99aa97 100644 --- a/libbeat/reader/syslog/util.go +++ b/libbeat/reader/syslog/util.go @@ -20,7 +20,7 @@ package syslog import ( "strings" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // stringToInt converts a string, assumed to be ASCII numeric characters, to an int. @@ -104,7 +104,7 @@ func mapIndexToString(idx int, values []string) (string, bool) { // value will be converted to a string slice. If the existing field is a string slice or // interface slice, then the new value will be appended. If the existing value is some // other type, then this function does nothing. -func appendStringField(m common.MapStr, field, value string) { +func appendStringField(m mapstr.M, field, value string) { v, _ := m.GetValue(field) switch t := v.(type) { case nil: diff --git a/libbeat/reader/syslog/util_test.go b/libbeat/reader/syslog/util_test.go index 8c86e6cef10..d68a944c9d9 100644 --- a/libbeat/reader/syslog/util_test.go +++ b/libbeat/reader/syslog/util_test.go @@ -22,7 +22,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestStringToInt(t *testing.T) { @@ -200,46 +200,46 @@ func TestMapIndexToString(t *testing.T) { func TestAppendStringField(t *testing.T) { tests := map[string]struct { - InMap common.MapStr + InMap mapstr.M InField string InValue string - Want common.MapStr + Want mapstr.M }{ "nil": { - InMap: common.MapStr{}, + InMap: mapstr.M{}, InField: "error", InValue: "foo", - Want: common.MapStr{ + Want: mapstr.M{ "error": "foo", }, }, "string": { - InMap: common.MapStr{ + InMap: mapstr.M{ "error": "foo", }, InField: "error", InValue: "bar", - Want: common.MapStr{ + Want: mapstr.M{ "error": []string{"foo", "bar"}, }, }, "string-slice": { - InMap: common.MapStr{ + InMap: mapstr.M{ "error": []string{"foo", "bar"}, }, InField: "error", InValue: "some value", - Want: common.MapStr{ + Want: mapstr.M{ "error": []string{"foo", "bar", "some value"}, }, }, "interface-slice": { - InMap: common.MapStr{ + InMap: mapstr.M{ "error": []interface{}{"foo", "bar"}, }, InField: "error", InValue: "some value", - Want: common.MapStr{ + Want: mapstr.M{ "error": []interface{}{"foo", "bar", "some value"}, }, }, diff --git a/libbeat/statestore/backend/memlog/diskstore.go b/libbeat/statestore/backend/memlog/diskstore.go index 0fc79859abc..1a0ec266f64 100644 --- a/libbeat/statestore/backend/memlog/diskstore.go +++ b/libbeat/statestore/backend/memlog/diskstore.go @@ -29,9 +29,9 @@ import ( "sort" "strconv" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cleanup" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // diskstore manages the on-disk state of the memlog store. @@ -79,8 +79,8 @@ type dataFileInfo struct { // storeEntry is used to write entries to the checkpoint file only. type storeEntry struct { - Key string `struct:"_key"` - Fields common.MapStr `struct:",inline"` + Key string `struct:"_key"` + Fields mapstr.M `struct:",inline"` } // storeMeta is read from the meta file. @@ -520,7 +520,7 @@ func loadDataFile(path string, tbl map[string]entry) error { return nil } - err := readDataFile(path, func(key string, state common.MapStr) { + err := readDataFile(path, func(key string, state mapstr.M) { tbl[key] = entry{value: state} }) return err @@ -528,7 +528,7 @@ func loadDataFile(path string, tbl map[string]entry) error { var ErrCorruptStore = errors.New("corrupted data file") -func readDataFile(path string, fn func(string, common.MapStr)) error { +func readDataFile(path string, fn func(string, mapstr.M)) error { f, err := os.Open(path) if err != nil { return err @@ -549,7 +549,7 @@ func readDataFile(path string, fn func(string, common.MapStr)) error { } delete(state, keyField) - fn(key, common.MapStr(state)) + fn(key, mapstr.M(state)) } return nil diff --git a/libbeat/statestore/backend/memlog/op.go b/libbeat/statestore/backend/memlog/op.go index d30ce975750..e4bf91dec46 100644 --- a/libbeat/statestore/backend/memlog/op.go +++ b/libbeat/statestore/backend/memlog/op.go @@ -17,7 +17,7 @@ package memlog -import "github.com/elastic/beats/v7/libbeat/common" +import "github.com/elastic/elastic-agent-libs/mapstr" type ( op interface { @@ -27,7 +27,7 @@ type ( // opSet encodes the 'Set' operations in the update log. opSet struct { K string - V common.MapStr + V mapstr.M } // opRemove encodes the 'Remove' operation in the update log. diff --git a/libbeat/statestore/backend/memlog/store.go b/libbeat/statestore/backend/memlog/store.go index 41120083624..6327baf8a6c 100644 --- a/libbeat/statestore/backend/memlog/store.go +++ b/libbeat/statestore/backend/memlog/store.go @@ -24,10 +24,10 @@ import ( "path/filepath" "sync" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transform/typeconv" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/statestore/backend" + "github.com/elastic/elastic-agent-libs/mapstr" ) // store implements an actual memlog based store. @@ -184,7 +184,7 @@ func (s *store) Get(key string, to interface{}) error { // If encoding was successful the in-memory state will be updated and a // set-operation is logged to the diskstore. func (s *store) Set(key string, value interface{}) error { - var tmp common.MapStr + var tmp mapstr.M if err := typeconv.Convert(&tmp, value); err != nil { return err } @@ -263,7 +263,7 @@ func (m *memstore) Get(key string) backend.ValueDecoder { return entry } -func (m *memstore) Set(key string, value common.MapStr) { +func (m *memstore) Set(key string, value mapstr.M) { m.table[key] = entry{value: value} } diff --git a/libbeat/template/load.go b/libbeat/template/load.go index ffce2c6db9c..de2bdafb3f1 100644 --- a/libbeat/template/load.go +++ b/libbeat/template/load.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/paths" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Loader interface for loading templates. @@ -239,7 +240,7 @@ func (b *templateBuilder) template(config TemplateConfig, info beat.Info, esVers return tmpl, nil } -func (b *templateBuilder) buildBody(tmpl *Template, config TemplateConfig, fields []byte) (common.MapStr, error) { +func (b *templateBuilder) buildBody(tmpl *Template, config TemplateConfig, fields []byte) (mapstr.M, error) { if config.Overwrite { b.log.Info("Existing template will be overwritten, as overwrite is enabled.") } @@ -257,7 +258,7 @@ func (b *templateBuilder) buildBody(tmpl *Template, config TemplateConfig, field return b.buildBodyFromFields(tmpl, fields) } -func (b *templateBuilder) buildBodyFromJSON(config TemplateConfig) (common.MapStr, error) { +func (b *templateBuilder) buildBodyFromJSON(config TemplateConfig) (mapstr.M, error) { jsonPath := paths.Resolve(paths.Config, config.JSON.Path) if _, err := os.Stat(jsonPath); err != nil { return nil, fmt.Errorf("error checking json file %s for template: %w", jsonPath, err) @@ -276,7 +277,7 @@ func (b *templateBuilder) buildBodyFromJSON(config TemplateConfig) (common.MapSt return body, nil } -func (b *templateBuilder) buildBodyFromFile(tmpl *Template, config TemplateConfig) (common.MapStr, error) { +func (b *templateBuilder) buildBodyFromFile(tmpl *Template, config TemplateConfig) (mapstr.M, error) { b.log.Debugf("Load fields.yml from file: %s", config.Fields) fieldsPath := paths.Resolve(paths.Config, config.Fields) body, err := tmpl.LoadFile(fieldsPath) @@ -286,7 +287,7 @@ func (b *templateBuilder) buildBodyFromFile(tmpl *Template, config TemplateConfi return body, nil } -func (b *templateBuilder) buildBodyFromFields(tmpl *Template, fields []byte) (common.MapStr, error) { +func (b *templateBuilder) buildBodyFromFields(tmpl *Template, fields []byte) (mapstr.M, error) { b.log.Debug("Load default fields") body, err := tmpl.LoadBytes(fields) if err != nil { diff --git a/libbeat/template/load_integration_test.go b/libbeat/template/load_integration_test.go index 3390e168056..ed39ad0cc53 100644 --- a/libbeat/template/load_integration_test.go +++ b/libbeat/template/load_integration_test.go @@ -37,11 +37,11 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/httpcommon" "github.com/elastic/beats/v7/libbeat/esleg/eslegclient" "github.com/elastic/beats/v7/libbeat/esleg/eslegtest" "github.com/elastic/beats/v7/libbeat/version" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -51,7 +51,7 @@ func init() { type testTemplate struct { t *testing.T client ESClient - common.MapStr + mapstr.M } type testSetup struct { @@ -414,8 +414,8 @@ func TestLoadBeatsTemplate_fromFile(t *testing.T) { func TestTemplateSettings(t *testing.T) { settings := TemplateSettings{ - Index: common.MapStr{"number_of_shards": 1}, - Source: common.MapStr{"enabled": false}, + Index: mapstr.M{"number_of_shards": 1}, + Source: mapstr.M{"enabled": false}, } setup := newTestSetup(t, TemplateConfig{Settings: settings, Enabled: true}) setup.mustLoadFromFile([]string{"..", "fields.yml"}) @@ -427,15 +427,15 @@ func TestTemplateSettings(t *testing.T) { } var dataTests = []struct { - data common.MapStr + data mapstr.M error bool }{ { - data: common.MapStr{ + data: mapstr.M{ "@timestamp": time.Now(), "keyword": "test keyword", "array": [...]int{1, 2, 3}, - "object": common.MapStr{ + "object": mapstr.M{ "hello": "world", }, }, @@ -443,8 +443,8 @@ var dataTests = []struct { }, { // Invalid array - data: common.MapStr{ - "array": common.MapStr{ + data: mapstr.M{ + "array": mapstr.M{ "hello": "world", }, }, @@ -452,17 +452,17 @@ var dataTests = []struct { }, { // Invalid object - data: common.MapStr{ + data: mapstr.M{ "object": [...]int{1, 2, 3}, }, error: true, }, { // tests enabled: false values - data: common.MapStr{ + data: mapstr.M{ "@timestamp": time.Now(), "array_disabled": [...]int{1, 2, 3}, - "object_disabled": common.MapStr{ + "object_disabled": mapstr.M{ "hello": "world", }, }, @@ -492,7 +492,7 @@ func getTemplate(t *testing.T, client ESClient, templateName string) testTemplat require.NoError(t, err) require.Equal(t, status, 200) - var response common.MapStr + var response mapstr.M err = json.Unmarshal(body, &response) require.NoError(t, err) require.NotNil(t, response) @@ -504,7 +504,7 @@ func getTemplate(t *testing.T, client ESClient, templateName string) testTemplat return testTemplate{ t: t, client: client, - MapStr: common.MapStr(templateElem["index_template"].(map[string]interface{})), + M: mapstr.M(templateElem["index_template"].(map[string]interface{})), } } @@ -519,7 +519,7 @@ func (tt *testTemplate) SourceEnabled() bool { val, err := tt.GetValue(key) if !assert.NoError(tt.t, err) { - doc, _ := json.MarshalIndent(tt.MapStr, "", " ") + doc, _ := json.MarshalIndent(tt.M, "", " ") tt.t.Fatal(fmt.Sprintf("failed to read '%v' in %s", key, doc)) } diff --git a/libbeat/template/load_test.go b/libbeat/template/load_test.go index fd66fa2e603..46341cfc558 100644 --- a/libbeat/template/load_test.go +++ b/libbeat/template/load_test.go @@ -24,6 +24,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -37,41 +38,41 @@ func TestFileLoader_Load(t *testing.T) { for name, test := range map[string]struct { settings TemplateSettings - body common.MapStr + body mapstr.M fields []byte - want common.MapStr + want mapstr.M wantErr error }{ "load minimal config info": { - body: common.MapStr{ + body: mapstr.M{ "index_patterns": []string{"mock-7.0.0"}, "data_stream": struct{}{}, "priority": 150, - "template": common.MapStr{ - "settings": common.MapStr{"index": nil}}, + "template": mapstr.M{ + "settings": mapstr.M{"index": nil}}, }, }, "load minimal config with index settings": { - settings: TemplateSettings{Index: common.MapStr{"code": "best_compression"}}, - body: common.MapStr{ + settings: TemplateSettings{Index: mapstr.M{"code": "best_compression"}}, + body: mapstr.M{ "index_patterns": []string{"mock-7.0.0"}, "data_stream": struct{}{}, "priority": 150, - "template": common.MapStr{ - "settings": common.MapStr{"index": common.MapStr{"code": "best_compression"}}}, + "template": mapstr.M{ + "settings": mapstr.M{"index": mapstr.M{"code": "best_compression"}}}, }, }, "load minimal config with source settings": { - settings: TemplateSettings{Source: common.MapStr{"enabled": false}}, - body: common.MapStr{ + settings: TemplateSettings{Source: mapstr.M{"enabled": false}}, + body: mapstr.M{ "index_patterns": []string{"mock-7.0.0"}, "data_stream": struct{}{}, "priority": 150, - "template": common.MapStr{ - "settings": common.MapStr{"index": nil}, - "mappings": common.MapStr{ - "_source": common.MapStr{"enabled": false}, - "_meta": common.MapStr{"beat": prefix, "version": ver}, + "template": mapstr.M{ + "settings": mapstr.M{"index": nil}, + "mappings": mapstr.M{ + "_source": mapstr.M{"enabled": false}, + "_meta": mapstr.M{"beat": prefix, "version": ver}, "date_detection": false, "dynamic_templates": nil, "properties": nil, @@ -79,12 +80,12 @@ func TestFileLoader_Load(t *testing.T) { }, }, "load config and in-line analyzer fields": { - body: common.MapStr{ + body: mapstr.M{ "index_patterns": []string{"mock-7.0.0"}, "data_stream": struct{}{}, "priority": 150, - "template": common.MapStr{ - "settings": common.MapStr{"index": nil}}, + "template": mapstr.M{ + "settings": mapstr.M{"index": nil}}, }, fields: []byte(`- key: test title: Test fields.yml with analyzer @@ -109,21 +110,21 @@ func TestFileLoader_Load(t *testing.T) { type: text analyzer: simple `), - want: common.MapStr{ + want: mapstr.M{ "index_patterns": []string{"mock-7.0.0"}, "data_stream": struct{}{}, "priority": 150, - "template": common.MapStr{ - "mappings": common.MapStr{ - "_meta": common.MapStr{ + "template": mapstr.M{ + "mappings": mapstr.M{ + "_meta": mapstr.M{ "version": "7.0.0", "beat": "mock", }, "date_detection": false, - "dynamic_templates": []common.MapStr{ + "dynamic_templates": []mapstr.M{ { - "strings_as_keyword": common.MapStr{ - "mapping": common.MapStr{ + "strings_as_keyword": mapstr.M{ + "mapping": mapstr.M{ "ignore_above": 1024, "type": "keyword", }, @@ -131,41 +132,41 @@ func TestFileLoader_Load(t *testing.T) { }, }, }, - "properties": common.MapStr{ - "code_block_text": common.MapStr{ + "properties": mapstr.M{ + "code_block_text": mapstr.M{ "type": "text", "norms": false, "analyzer": "test_powershell", }, - "script_block_text": common.MapStr{ + "script_block_text": mapstr.M{ "type": "text", "norms": false, "analyzer": "test_powershell", }, - "standard_text": common.MapStr{ + "standard_text": mapstr.M{ "type": "text", "norms": false, "analyzer": "simple", }, }, }, - "settings": common.MapStr{ - "index": common.MapStr{ + "settings": mapstr.M{ + "index": mapstr.M{ "refresh_interval": "5s", - "mapping": common.MapStr{ - "total_fields": common.MapStr{ + "mapping": mapstr.M{ + "total_fields": mapstr.M{ "limit": 10000, }, }, - "query": common.MapStr{ + "query": mapstr.M{ "default_field": []string{ "fields.*", }, }, "max_docvalue_fields_search": 200, }, - "analysis": common.MapStr{ - "analyzer": common.MapStr{ + "analysis": mapstr.M{ + "analyzer": mapstr.M{ "test_powershell": map[string]interface{}{ "type": "pattern", "pattern": "[\\W&&[^-]]+", @@ -177,9 +178,9 @@ func TestFileLoader_Load(t *testing.T) { }, }, "load config and in-line analyzer fields with name collision": { - body: common.MapStr{ + body: mapstr.M{ "index_patterns": []string{"mock-7.0.0"}, - "settings": common.MapStr{"index": nil}, + "settings": mapstr.M{"index": nil}, }, fields: []byte(`- key: test title: Test fields.yml with analyzer diff --git a/libbeat/template/processor.go b/libbeat/template/processor.go index 4186aa8ac6e..c75ee2025ca 100644 --- a/libbeat/template/processor.go +++ b/libbeat/template/processor.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mapping" + "github.com/elastic/elastic-agent-libs/mapstr" ) // DefaultField controls the default value for the default_field flag. @@ -45,9 +46,9 @@ type Processor struct { ElasticLicensed bool // dynamicTemplatesMap records which dynamic templates have been added, to prevent duplicates. - dynamicTemplatesMap map[dynamicTemplateKey]common.MapStr + dynamicTemplatesMap map[dynamicTemplateKey]mapstr.M // dynamicTemplates records the dynamic templates in the order they were added. - dynamicTemplates []common.MapStr + dynamicTemplates []mapstr.M } var ( @@ -63,7 +64,7 @@ type fieldState struct { } // Process recursively processes the given fields and writes the template in the given output -func (p *Processor) Process(fields mapping.Fields, state *fieldState, output, analyzers common.MapStr) error { +func (p *Processor) Process(fields mapping.Fields, state *fieldState, output, analyzers mapstr.M) error { if state == nil { // Set the defaults. state = &fieldState{DefaultField: DefaultField} @@ -79,7 +80,7 @@ func (p *Processor) Process(fields mapping.Fields, state *fieldState, output, an field.DefaultField = &state.DefaultField } var ( - indexMapping common.MapStr + indexMapping mapstr.M analyzer, searchAnalyzer mapping.Analyzer ) @@ -184,7 +185,7 @@ func addToDefaultFields(f *mapping.Field) { } } -func (p *Processor) other(f *mapping.Field) common.MapStr { +func (p *Processor) other(f *mapping.Field) mapstr.M { property := p.getDefaultProperties(f) if f.Type != "" { property["type"] = f.Type @@ -193,13 +194,13 @@ func (p *Processor) other(f *mapping.Field) common.MapStr { return property } -func (p *Processor) integer(f *mapping.Field) common.MapStr { +func (p *Processor) integer(f *mapping.Field) mapstr.M { property := p.getDefaultProperties(f) property["type"] = "long" return property } -func (p *Processor) scaledFloat(f *mapping.Field, params ...common.MapStr) common.MapStr { +func (p *Processor) scaledFloat(f *mapping.Field, params ...mapstr.M) mapstr.M { property := p.getDefaultProperties(f) property["type"] = "scaled_float" @@ -219,7 +220,7 @@ func (p *Processor) scaledFloat(f *mapping.Field, params ...common.MapStr) commo return property } -func (p *Processor) nested(f *mapping.Field, output, analyzers common.MapStr) (common.MapStr, error) { +func (p *Processor) nested(f *mapping.Field, output, analyzers mapstr.M) (mapstr.M, error) { mapping, err := p.group(f, output, analyzers) if err != nil { return nil, err @@ -228,19 +229,19 @@ func (p *Processor) nested(f *mapping.Field, output, analyzers common.MapStr) (c return mapping, nil } -func (p *Processor) group(f *mapping.Field, output, analyzers common.MapStr) (common.MapStr, error) { - indexMapping := common.MapStr{} +func (p *Processor) group(f *mapping.Field, output, analyzers mapstr.M) (mapstr.M, error) { + indexMapping := mapstr.M{} if f.Dynamic.Value != nil { indexMapping["dynamic"] = f.Dynamic.Value } // Combine properties with previous field definitions (if any) - properties := common.MapStr{} + properties := mapstr.M{} key := mapping.GenerateKey(f.Name) + ".properties" currentProperties, err := output.GetValue(key) if err == nil { var ok bool - properties, ok = currentProperties.(common.MapStr) + properties, ok = currentProperties.(mapstr.M) if !ok { // This should never happen return nil, errors.New(key + " is expected to be a MapStr") @@ -260,14 +261,14 @@ func (p *Processor) group(f *mapping.Field, output, analyzers common.MapStr) (co return indexMapping, nil } -func (p *Processor) halfFloat(f *mapping.Field) common.MapStr { +func (p *Processor) halfFloat(f *mapping.Field) mapstr.M { property := p.getDefaultProperties(f) property["type"] = "half_float" return property } -func (p *Processor) ip(f *mapping.Field) common.MapStr { +func (p *Processor) ip(f *mapping.Field) mapstr.M { property := p.getDefaultProperties(f) property["type"] = "ip" return property @@ -290,7 +291,7 @@ func stateFromField(f *mapping.Field) *fieldState { return st } -func (p *Processor) keyword(f *mapping.Field, analyzers common.MapStr) common.MapStr { +func (p *Processor) keyword(f *mapping.Field, analyzers mapstr.M) mapstr.M { property := p.getDefaultProperties(f) property["type"] = "keyword" @@ -304,7 +305,7 @@ func (p *Processor) keyword(f *mapping.Field, analyzers common.MapStr) common.Ma } if len(f.MultiFields) > 0 { - fields := common.MapStr{} + fields := mapstr.M{} p.Process(f.MultiFields, stateFromField(f), fields, analyzers) property["fields"] = fields } @@ -312,7 +313,7 @@ func (p *Processor) keyword(f *mapping.Field, analyzers common.MapStr) common.Ma return property } -func (p *Processor) wildcard(f *mapping.Field, analyzers common.MapStr) common.MapStr { +func (p *Processor) wildcard(f *mapping.Field, analyzers mapstr.M) mapstr.M { property := p.getDefaultProperties(f) property["type"] = "wildcard" @@ -326,7 +327,7 @@ func (p *Processor) wildcard(f *mapping.Field, analyzers common.MapStr) common.M } if len(f.MultiFields) > 0 { - fields := common.MapStr{} + fields := mapstr.M{} p.Process(f.MultiFields, stateFromField(f), fields, analyzers) property["fields"] = fields } @@ -334,7 +335,7 @@ func (p *Processor) wildcard(f *mapping.Field, analyzers common.MapStr) common.M return property } -func (p *Processor) text(f *mapping.Field, analyzers common.MapStr) (properties common.MapStr, analyzer, searchAnalyzer mapping.Analyzer) { +func (p *Processor) text(f *mapping.Field, analyzers mapstr.M) (properties mapstr.M, analyzer, searchAnalyzer mapping.Analyzer) { properties = p.getDefaultProperties(f) properties["type"] = "text" @@ -354,7 +355,7 @@ func (p *Processor) text(f *mapping.Field, analyzers common.MapStr) (properties } if len(f.MultiFields) > 0 { - fields := common.MapStr{} + fields := mapstr.M{} p.Process(f.MultiFields, stateFromField(f), fields, analyzers) properties["fields"] = fields } @@ -362,7 +363,7 @@ func (p *Processor) text(f *mapping.Field, analyzers common.MapStr) (properties return properties, analyzer, searchAnalyzer } -func (p *Processor) matchOnlyText(f *mapping.Field, analyzers common.MapStr) (properties common.MapStr, analyzer, searchAnalyzer mapping.Analyzer) { +func (p *Processor) matchOnlyText(f *mapping.Field, analyzers mapstr.M) (properties mapstr.M, analyzer, searchAnalyzer mapping.Analyzer) { properties = p.getDefaultProperties(f) properties["type"] = "match_only_text" @@ -378,7 +379,7 @@ func (p *Processor) matchOnlyText(f *mapping.Field, analyzers common.MapStr) (pr } if len(f.MultiFields) > 0 { - fields := common.MapStr{} + fields := mapstr.M{} p.Process(f.MultiFields, nil, fields, analyzers) properties["fields"] = fields } @@ -386,7 +387,7 @@ func (p *Processor) matchOnlyText(f *mapping.Field, analyzers common.MapStr) (pr return properties, analyzer, searchAnalyzer } -func (p *Processor) array(f *mapping.Field) common.MapStr { +func (p *Processor) array(f *mapping.Field) mapstr.M { properties := p.getDefaultProperties(f) if f.ObjectType != "" { properties["type"] = f.ObjectType @@ -394,7 +395,7 @@ func (p *Processor) array(f *mapping.Field) common.MapStr { return properties } -func (p *Processor) alias(f *mapping.Field) common.MapStr { +func (p *Processor) alias(f *mapping.Field) mapstr.M { // In case migration is disabled and it's a migration alias, field is not created if !p.Migration && f.MigrationAlias { return nil @@ -406,7 +407,7 @@ func (p *Processor) alias(f *mapping.Field) common.MapStr { return properties } -func (p *Processor) histogram(f *mapping.Field) common.MapStr { +func (p *Processor) histogram(f *mapping.Field) mapstr.M { // Histograms were introduced in Elasticsearch 7.6, ignore if unsupported if p.EsVersion.LessThan(minVersionHistogram) { return nil @@ -418,7 +419,7 @@ func (p *Processor) histogram(f *mapping.Field) common.MapStr { return properties } -func (p *Processor) object(f *mapping.Field) common.MapStr { +func (p *Processor) object(f *mapping.Field) mapstr.M { matchType := func(onlyType string, mt string) string { if mt != "" { return mt @@ -443,7 +444,7 @@ func (p *Processor) object(f *mapping.Field) common.MapStr { switch otp.ObjectType { case "scaled_float": - dynProperties = p.scaledFloat(f, common.MapStr{scalingFactorKey: otp.ScalingFactor}) + dynProperties = p.scaledFloat(f, mapstr.M{scalingFactorKey: otp.ScalingFactor}) matchingType = matchType("*", otp.ObjectTypeMappingType) case "text": dynProperties["type"] = "text" @@ -500,21 +501,21 @@ type dynamicTemplateKey struct { matchType string } -func (p *Processor) addDynamicTemplate(name, pathMatch, matchType string, properties common.MapStr) { +func (p *Processor) addDynamicTemplate(name, pathMatch, matchType string, properties mapstr.M) { key := dynamicTemplateKey{ name: name, pathMatch: pathMatch, matchType: matchType, } if p.dynamicTemplatesMap == nil { - p.dynamicTemplatesMap = make(map[dynamicTemplateKey]common.MapStr) + p.dynamicTemplatesMap = make(map[dynamicTemplateKey]mapstr.M) } else { if _, ok := p.dynamicTemplatesMap[key]; ok { // Dynamic template already added. return } } - dynamicTemplateProperties := common.MapStr{ + dynamicTemplateProperties := mapstr.M{ "mapping": properties, } if matchType != "" { @@ -523,16 +524,16 @@ func (p *Processor) addDynamicTemplate(name, pathMatch, matchType string, proper if pathMatch != "" { dynamicTemplateProperties["path_match"] = pathMatch } - dynamicTemplate := common.MapStr{ + dynamicTemplate := mapstr.M{ name: dynamicTemplateProperties, } p.dynamicTemplatesMap[key] = dynamicTemplate p.dynamicTemplates = append(p.dynamicTemplates, dynamicTemplate) } -func (p *Processor) getDefaultProperties(f *mapping.Field) common.MapStr { +func (p *Processor) getDefaultProperties(f *mapping.Field) mapstr.M { // Currently no defaults exist - properties := common.MapStr{} + properties := mapstr.M{} if f.Index != nil { properties["index"] = *f.Index @@ -548,7 +549,7 @@ func (p *Processor) getDefaultProperties(f *mapping.Field) common.MapStr { if !p.EsVersion.LessThan(minVersionFieldMeta) { if f.MetricType != "" || f.Unit != "" { - meta := common.MapStr{} + meta := mapstr.M{} if f.MetricType != "" { meta["metric_type"] = f.MetricType } diff --git a/libbeat/template/processor_test.go b/libbeat/template/processor_test.go index 9f40529c7ac..c4a4f352b8b 100644 --- a/libbeat/template/processor_test.go +++ b/libbeat/template/processor_test.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mapping" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestProcessor(t *testing.T) { @@ -36,84 +37,84 @@ func TestProcessor(t *testing.T) { pEsVersion76 := &Processor{EsVersion: *common.MustNewVersion("7.6.0")} tests := []struct { - output common.MapStr - expected common.MapStr + output mapstr.M + expected mapstr.M }{ { output: p.other(&mapping.Field{Type: "long"}), - expected: common.MapStr{"type": "long"}, + expected: mapstr.M{"type": "long"}, }, { output: p.scaledFloat(&mapping.Field{Type: "scaled_float"}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "scaled_float", "scaling_factor": 1000, }, }, { output: p.scaledFloat(&mapping.Field{Type: "scaled_float", ScalingFactor: 100}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "scaled_float", "scaling_factor": 100, }, }, { - output: p.scaledFloat(&mapping.Field{Type: "scaled_float"}, common.MapStr{scalingFactorKey: 0}), - expected: common.MapStr{ + output: p.scaledFloat(&mapping.Field{Type: "scaled_float"}, mapstr.M{scalingFactorKey: 0}), + expected: mapstr.M{ "type": "scaled_float", "scaling_factor": 1000, }, }, { - output: p.scaledFloat(&mapping.Field{Type: "scaled_float"}, common.MapStr{"someKey": 10}), - expected: common.MapStr{ + output: p.scaledFloat(&mapping.Field{Type: "scaled_float"}, mapstr.M{"someKey": 10}), + expected: mapstr.M{ "type": "scaled_float", "scaling_factor": 1000, }, }, { - output: p.scaledFloat(&mapping.Field{Type: "scaled_float"}, common.MapStr{scalingFactorKey: 10}), - expected: common.MapStr{ + output: p.scaledFloat(&mapping.Field{Type: "scaled_float"}, mapstr.M{scalingFactorKey: 10}), + expected: mapstr.M{ "type": "scaled_float", "scaling_factor": 10, }, }, { output: p.object(&mapping.Field{Type: "object", Enabled: &falseVar}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "object", "enabled": false, }, }, { output: p.integer(&mapping.Field{Type: "long", CopyTo: "hello.world"}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "long", "copy_to": "hello.world", }, }, { output: p.array(&mapping.Field{Type: "array"}), - expected: common.MapStr{}, + expected: mapstr.M{}, }, { output: p.array(&mapping.Field{Type: "array", ObjectType: "text"}), - expected: common.MapStr{"type": "text"}, + expected: mapstr.M{"type": "text"}, }, { output: p.array(&mapping.Field{Type: "array", Index: &falseVar, ObjectType: "keyword"}), - expected: common.MapStr{"index": false, "type": "keyword"}, + expected: mapstr.M{"index": false, "type": "keyword"}, }, { output: p.object(&mapping.Field{Type: "object", Enabled: &falseVar}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "object", "enabled": false, }, }, { output: fieldsOnly(p.text(&mapping.Field{Type: "text", Analyzer: mapping.Analyzer{Name: "autocomplete"}}, nil)), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", "analyzer": "autocomplete", "norms": false, @@ -121,21 +122,21 @@ func TestProcessor(t *testing.T) { }, { output: fieldsOnly(p.text(&mapping.Field{Type: "text", Analyzer: mapping.Analyzer{Name: "autocomplete"}, Norms: true}, nil)), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", "analyzer": "autocomplete", }, }, { output: fieldsOnly(p.text(&mapping.Field{Type: "text", SearchAnalyzer: mapping.Analyzer{Name: "standard"}, Norms: true}, nil)), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", "search_analyzer": "standard", }, }, { output: fieldsOnly(p.text(&mapping.Field{Type: "text", Analyzer: mapping.Analyzer{Name: "autocomplete"}, SearchAnalyzer: mapping.Analyzer{Name: "standard"}, Norms: true}, nil)), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", "analyzer": "autocomplete", "search_analyzer": "standard", @@ -143,10 +144,10 @@ func TestProcessor(t *testing.T) { }, { output: fieldsOnly(p.text(&mapping.Field{Type: "text", MultiFields: mapping.Fields{mapping.Field{Name: "raw", Type: "keyword"}}, Norms: true}, nil)), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", - "fields": common.MapStr{ - "raw": common.MapStr{ + "fields": mapstr.M{ + "raw": mapstr.M{ "type": "keyword", "ignore_above": 1024, }, @@ -155,11 +156,11 @@ func TestProcessor(t *testing.T) { }, { output: p.keyword(&mapping.Field{Type: "keyword", MultiFields: mapping.Fields{mapping.Field{Name: "analyzed", Type: "text", Norms: true}}}, nil), - expected: common.MapStr{ + expected: mapstr.M{ "type": "keyword", "ignore_above": 1024, - "fields": common.MapStr{ - "analyzed": common.MapStr{ + "fields": mapstr.M{ + "analyzed": mapstr.M{ "type": "text", }, }, @@ -167,20 +168,20 @@ func TestProcessor(t *testing.T) { }, { output: p.keyword(&mapping.Field{Type: "keyword", IgnoreAbove: 256}, nil), - expected: common.MapStr{ + expected: mapstr.M{ "type": "keyword", "ignore_above": 256, }, }, { output: p.keyword(&mapping.Field{Type: "keyword", IgnoreAbove: -1}, nil), - expected: common.MapStr{ + expected: mapstr.M{ "type": "keyword", }, }, { output: p.keyword(&mapping.Field{Type: "keyword"}, nil), - expected: common.MapStr{ + expected: mapstr.M{ "type": "keyword", "ignore_above": 1024, }, @@ -190,14 +191,14 @@ func TestProcessor(t *testing.T) { mapping.Field{Name: "raw", Type: "keyword"}, mapping.Field{Name: "indexed", Type: "text"}, }, Norms: true}, nil)), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", - "fields": common.MapStr{ - "raw": common.MapStr{ + "fields": mapstr.M{ + "raw": mapstr.M{ "type": "keyword", "ignore_above": 1024, }, - "indexed": common.MapStr{ + "indexed": mapstr.M{ "type": "text", "norms": false, }, @@ -209,14 +210,14 @@ func TestProcessor(t *testing.T) { mapping.Field{Name: "raw", Type: "keyword"}, mapping.Field{Name: "indexed", Type: "text"}, }, Norms: true}, nil)), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", - "fields": common.MapStr{ - "raw": common.MapStr{ + "fields": mapstr.M{ + "raw": mapstr.M{ "type": "keyword", "ignore_above": 1024, }, - "indexed": common.MapStr{ + "indexed": mapstr.M{ "type": "text", "norms": false, }, @@ -225,55 +226,55 @@ func TestProcessor(t *testing.T) { }, { output: p.object(&mapping.Field{Dynamic: mapping.DynamicType{Value: false}}), - expected: common.MapStr{ + expected: mapstr.M{ "dynamic": false, "type": "object", }, }, { output: p.object(&mapping.Field{Dynamic: mapping.DynamicType{Value: true}}), - expected: common.MapStr{ + expected: mapstr.M{ "dynamic": true, "type": "object", }, }, { output: p.object(&mapping.Field{Dynamic: mapping.DynamicType{Value: "strict"}}), - expected: common.MapStr{ + expected: mapstr.M{ "dynamic": "strict", "type": "object", }, }, { output: p.other(&mapping.Field{Type: "long", Index: &falseVar}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "long", "index": false, }, }, { output: p.other(&mapping.Field{Type: "text", Index: &trueVar}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", "index": true, }, }, { output: p.other(&mapping.Field{Type: "long", DocValues: &falseVar}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "long", "doc_values": false, }, }, { output: p.other(&mapping.Field{Type: "double", DocValues: &falseVar}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "double", "doc_values": false, }, }, { output: p.other(&mapping.Field{Type: "text", DocValues: &trueVar}), - expected: common.MapStr{ + expected: mapstr.M{ "type": "text", "doc_values": true, }, }, { output: p.alias(&mapping.Field{Type: "alias", AliasPath: "a.c", MigrationAlias: false}), - expected: common.MapStr{"path": "a.c", "type": "alias"}, + expected: mapstr.M{"path": "a.c", "type": "alias"}, }, { output: p.alias(&mapping.Field{Type: "alias", AliasPath: "a.d", MigrationAlias: true}), @@ -281,11 +282,11 @@ func TestProcessor(t *testing.T) { }, { output: migrationP.alias(&mapping.Field{Type: "alias", AliasPath: "a.e", MigrationAlias: false}), - expected: common.MapStr{"path": "a.e", "type": "alias"}, + expected: mapstr.M{"path": "a.e", "type": "alias"}, }, { output: migrationP.alias(&mapping.Field{Type: "alias", AliasPath: "a.f", MigrationAlias: true}), - expected: common.MapStr{"path": "a.f", "type": "alias"}, + expected: mapstr.M{"path": "a.f", "type": "alias"}, }, { output: p.histogram(&mapping.Field{Type: "histogram"}), @@ -293,24 +294,24 @@ func TestProcessor(t *testing.T) { }, { output: pEsVersion76.histogram(&mapping.Field{Type: "histogram"}), - expected: common.MapStr{"type": "histogram"}, + expected: mapstr.M{"type": "histogram"}, }, { // "p" has EsVersion 7.0.0; field metadata requires ES 7.6.0+ output: p.other(&mapping.Field{Type: "long", MetricType: "gauge", Unit: "nanos"}), - expected: common.MapStr{"type": "long"}, + expected: mapstr.M{"type": "long"}, }, { output: pEsVersion76.other(&mapping.Field{Type: "long", MetricType: "gauge"}), - expected: common.MapStr{"type": "long", "meta": common.MapStr{"metric_type": "gauge"}}, + expected: mapstr.M{"type": "long", "meta": mapstr.M{"metric_type": "gauge"}}, }, { output: pEsVersion76.other(&mapping.Field{Type: "long", Unit: "nanos"}), - expected: common.MapStr{"type": "long", "meta": common.MapStr{"unit": "nanos"}}, + expected: mapstr.M{"type": "long", "meta": mapstr.M{"unit": "nanos"}}, }, { output: pEsVersion76.other(&mapping.Field{Type: "long", MetricType: "gauge", Unit: "nanos"}), - expected: common.MapStr{"type": "long", "meta": common.MapStr{"metric_type": "gauge", "unit": "nanos"}}, + expected: mapstr.M{"type": "long", "meta": mapstr.M{"metric_type": "gauge", "unit": "nanos"}}, }, } @@ -319,24 +320,24 @@ func TestProcessor(t *testing.T) { } } -func fieldsOnly(f common.MapStr, _, _ mapping.Analyzer) common.MapStr { +func fieldsOnly(f mapstr.M, _, _ mapping.Analyzer) mapstr.M { return f } func TestDynamicTemplates(t *testing.T) { tests := []struct { field mapping.Field - expected []common.MapStr + expected []mapstr.M }{ { field: mapping.Field{ Type: "object", ObjectType: "keyword", Name: "context", }, - expected: []common.MapStr{ - common.MapStr{ - "context": common.MapStr{ - "mapping": common.MapStr{"type": "keyword"}, + expected: []mapstr.M{ + mapstr.M{ + "context": mapstr.M{ + "mapping": mapstr.M{"type": "keyword"}, "match_mapping_type": "string", "path_match": "context.*", }, @@ -348,10 +349,10 @@ func TestDynamicTemplates(t *testing.T) { Type: "object", ObjectType: "long", ObjectTypeMappingType: "futuretype", Path: "language", Name: "english", }, - expected: []common.MapStr{ - common.MapStr{ - "language.english": common.MapStr{ - "mapping": common.MapStr{"type": "long"}, + expected: []mapstr.M{ + mapstr.M{ + "language.english": mapstr.M{ + "mapping": mapstr.M{"type": "long"}, "match_mapping_type": "futuretype", "path_match": "language.english.*", }, @@ -363,10 +364,10 @@ func TestDynamicTemplates(t *testing.T) { Type: "object", ObjectType: "long", ObjectTypeMappingType: "*", Path: "language", Name: "english", }, - expected: []common.MapStr{ - common.MapStr{ - "language.english": common.MapStr{ - "mapping": common.MapStr{"type": "long"}, + expected: []mapstr.M{ + mapstr.M{ + "language.english": mapstr.M{ + "mapping": mapstr.M{"type": "long"}, "match_mapping_type": "*", "path_match": "language.english.*", }, @@ -378,10 +379,10 @@ func TestDynamicTemplates(t *testing.T) { Type: "object", ObjectType: "long", Path: "language", Name: "english", }, - expected: []common.MapStr{ - common.MapStr{ - "language.english": common.MapStr{ - "mapping": common.MapStr{"type": "long"}, + expected: []mapstr.M{ + mapstr.M{ + "language.english": mapstr.M{ + "mapping": mapstr.M{"type": "long"}, "match_mapping_type": "long", "path_match": "language.english.*", }, @@ -393,10 +394,10 @@ func TestDynamicTemplates(t *testing.T) { Type: "object", ObjectType: "text", Path: "language", Name: "english", }, - expected: []common.MapStr{ - common.MapStr{ - "language.english": common.MapStr{ - "mapping": common.MapStr{"type": "text"}, + expected: []mapstr.M{ + mapstr.M{ + "language.english": mapstr.M{ + "mapping": mapstr.M{"type": "text"}, "match_mapping_type": "string", "path_match": "language.english.*", }, @@ -408,10 +409,10 @@ func TestDynamicTemplates(t *testing.T) { Type: "object", ObjectType: "scaled_float", Name: "core.*.pct", }, - expected: []common.MapStr{ - common.MapStr{ - "core.*.pct": common.MapStr{ - "mapping": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "core.*.pct": mapstr.M{ + "mapping": mapstr.M{ "type": "scaled_float", "scaling_factor": defaultScalingFactor, }, @@ -426,10 +427,10 @@ func TestDynamicTemplates(t *testing.T) { Type: "object", ObjectType: "scaled_float", Name: "core.*.pct", ScalingFactor: 100, ObjectTypeMappingType: "float", }, - expected: []common.MapStr{ - common.MapStr{ - "core.*.pct": common.MapStr{ - "mapping": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "core.*.pct": mapstr.M{ + "mapping": mapstr.M{ "type": "scaled_float", "scaling_factor": 100, }, @@ -448,24 +449,24 @@ func TestDynamicTemplates(t *testing.T) { }, Name: "context", }, - expected: []common.MapStr{ + expected: []mapstr.M{ { - "context_float": common.MapStr{ - "mapping": common.MapStr{"type": "float"}, + "context_float": mapstr.M{ + "mapping": mapstr.M{"type": "float"}, "match_mapping_type": "float", "path_match": "context.*", }, }, { - "context_boolean": common.MapStr{ - "mapping": common.MapStr{"type": "boolean"}, + "context_boolean": mapstr.M{ + "mapping": mapstr.M{"type": "boolean"}, "match_mapping_type": "boolean", "path_match": "context.*", }, }, { - "context_*": common.MapStr{ - "mapping": common.MapStr{"type": "scaled_float", "scaling_factor": 10000}, + "context_*": mapstr.M{ + "mapping": mapstr.M{"type": "scaled_float", "scaling_factor": 10000}, "match_mapping_type": "*", "path_match": "context.*", }, @@ -478,10 +479,10 @@ func TestDynamicTemplates(t *testing.T) { Type: "histogram", DynamicTemplate: true, }, - expected: []common.MapStr{ + expected: []mapstr.M{ { - "dynamic_histogram": common.MapStr{ - "mapping": common.MapStr{"type": "histogram"}, + "dynamic_histogram": mapstr.M{ + "mapping": mapstr.M{"type": "histogram"}, }, }, }, @@ -491,16 +492,16 @@ func TestDynamicTemplates(t *testing.T) { for _, numericType := range []string{"byte", "double", "float", "long", "short", "boolean"} { gen := struct { field mapping.Field - expected []common.MapStr + expected []mapstr.M }{ field: mapping.Field{ Type: "object", ObjectType: numericType, Name: "somefield", ObjectTypeMappingType: "long", }, - expected: []common.MapStr{ - common.MapStr{ - "somefield": common.MapStr{ - "mapping": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "somefield": mapstr.M{ + "mapping": mapstr.M{ "type": numericType, }, "match_mapping_type": "long", @@ -513,8 +514,8 @@ func TestDynamicTemplates(t *testing.T) { } for _, test := range tests { - output := make(common.MapStr) - analyzers := make(common.MapStr) + output := make(mapstr.M) + analyzers := make(mapstr.M) p := &Processor{EsVersion: *common.MustNewVersion("8.0.0")} err := p.Process(mapping.Fields{ test.field, @@ -550,8 +551,8 @@ func TestPropertiesCombine(t *testing.T) { }, } - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} version, err := common.NewVersion("6.0.0") if err != nil { t.Fatal(err) @@ -572,8 +573,8 @@ func TestPropertiesCombine(t *testing.T) { t.Fatal(err) } - assert.Equal(t, v1, common.MapStr{"type": "text", "norms": false}) - assert.Equal(t, v2, common.MapStr{"type": "text", "norms": false}) + assert.Equal(t, v1, mapstr.M{"type": "text", "norms": false}) + assert.Equal(t, v2, mapstr.M{"type": "text", "norms": false}) } func TestProcessNoName(t *testing.T) { @@ -599,8 +600,8 @@ func TestProcessNoName(t *testing.T) { }, } - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} version, err := common.NewVersion("6.0.0") if err != nil { t.Fatal(err) @@ -613,10 +614,10 @@ func TestProcessNoName(t *testing.T) { } // Make sure fields without a name are skipped during template generation - expectedOutput := common.MapStr{ - "test": common.MapStr{ - "properties": common.MapStr{ - "two": common.MapStr{ + expectedOutput := mapstr.M{ + "test": mapstr.M{ + "properties": mapstr.M{ + "two": mapstr.M{ "norms": false, "type": "text", }, @@ -739,8 +740,8 @@ func TestProcessDefaultField(t *testing.T) { } p := Processor{EsVersion: *version} - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} if err = p.Process(fields, nil, output, analyzers); err != nil { t.Fatal(err) } @@ -776,8 +777,8 @@ func TestProcessWildcardOSS(t *testing.T) { }, } - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} version, err := common.NewVersion("8.0.0") if err != nil { t.Fatal(err) @@ -790,10 +791,10 @@ func TestProcessWildcardOSS(t *testing.T) { } // Make sure fields without a name are skipped during template generation - expectedOutput := common.MapStr{ - "test": common.MapStr{ - "properties": common.MapStr{ - "one": common.MapStr{ + expectedOutput := mapstr.M{ + "test": mapstr.M{ + "properties": mapstr.M{ + "one": mapstr.M{ "ignore_above": 1024, "type": "keyword", }, @@ -808,7 +809,7 @@ func TestProcessWildcardElastic(t *testing.T) { for _, test := range []struct { title string fields mapping.Fields - expected common.MapStr + expected mapstr.M }{ { title: "default", @@ -824,10 +825,10 @@ func TestProcessWildcardElastic(t *testing.T) { }, }, }, - expected: common.MapStr{ - "test": common.MapStr{ - "properties": common.MapStr{ - "one": common.MapStr{ + expected: mapstr.M{ + "test": mapstr.M{ + "properties": mapstr.M{ + "one": mapstr.M{ "type": "wildcard", }, }, @@ -849,10 +850,10 @@ func TestProcessWildcardElastic(t *testing.T) { }, }, }, - expected: common.MapStr{ - "test": common.MapStr{ - "properties": common.MapStr{ - "one": common.MapStr{ + expected: mapstr.M{ + "test": mapstr.M{ + "properties": mapstr.M{ + "one": mapstr.M{ "ignore_above": 4096, "type": "wildcard", }, @@ -862,8 +863,8 @@ func TestProcessWildcardElastic(t *testing.T) { }, } { t.Run(test.title, func(t *testing.T) { - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} version, err := common.NewVersion("8.0.0") if err != nil { t.Fatal(err) @@ -893,8 +894,8 @@ func TestProcessWildcardPreSupport(t *testing.T) { }, } - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} version, err := common.NewVersion("7.8.0") if err != nil { t.Fatal(err) @@ -907,10 +908,10 @@ func TestProcessWildcardPreSupport(t *testing.T) { } // Make sure fields without a name are skipped during template generation - expectedOutput := common.MapStr{ - "test": common.MapStr{ - "properties": common.MapStr{ - "one": common.MapStr{ + expectedOutput := mapstr.M{ + "test": mapstr.M{ + "properties": mapstr.M{ + "one": mapstr.M{ "ignore_above": 1024, "type": "keyword", }, @@ -935,8 +936,8 @@ func TestProcessNestedSupport(t *testing.T) { }, } - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} version, err := common.NewVersion("7.8.0") if err != nil { t.Fatal(err) @@ -948,11 +949,11 @@ func TestProcessNestedSupport(t *testing.T) { t.Fatal(err) } - expectedOutput := common.MapStr{ - "test": common.MapStr{ + expectedOutput := mapstr.M{ + "test": mapstr.M{ "type": "nested", - "properties": common.MapStr{ - "one": common.MapStr{ + "properties": mapstr.M{ + "one": mapstr.M{ "ignore_above": 1024, "type": "keyword", }, @@ -971,8 +972,8 @@ func TestProcessNestedSupportNoSubfields(t *testing.T) { }, } - output := common.MapStr{} - analyzers := common.MapStr{} + output := mapstr.M{} + analyzers := mapstr.M{} version, err := common.NewVersion("7.8.0") if err != nil { t.Fatal(err) @@ -984,8 +985,8 @@ func TestProcessNestedSupportNoSubfields(t *testing.T) { t.Fatal(err) } - expectedOutput := common.MapStr{ - "test": common.MapStr{ + expectedOutput := mapstr.M{ + "test": mapstr.M{ "type": "nested", }, } diff --git a/libbeat/template/template.go b/libbeat/template/template.go index af032487303..98c9cc89972 100644 --- a/libbeat/template/template.go +++ b/libbeat/template/template.go @@ -22,6 +22,7 @@ import ( "sync" "time" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg/yaml" "github.com/elastic/beats/v7/libbeat/beat" @@ -84,18 +85,18 @@ func New( } event := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ // beat object was left in for backward compatibility reason for older configs. - "beat": common.MapStr{ + "beat": mapstr.M{ "name": beatName, "version": bV.String(), }, - "agent": common.MapStr{ + "agent": mapstr.M{ "name": beatName, "version": bV.String(), }, // For the Beats that have an observer role - "observer": common.MapStr{ + "observer": mapstr.M{ "name": beatName, "version": bV.String(), }, @@ -139,7 +140,7 @@ func New( }, nil } -func (t *Template) load(fields mapping.Fields) (common.MapStr, error) { +func (t *Template) load(fields mapping.Fields) (mapstr.M, error) { // Locking to make sure dynamicTemplates and defaultFields is not accessed in parallel t.Lock() @@ -156,8 +157,8 @@ func (t *Template) load(fields mapping.Fields) (common.MapStr, error) { } // Start processing at the root - properties := common.MapStr{} - analyzers := common.MapStr{} + properties := mapstr.M{} + analyzers := mapstr.M{} processor := Processor{EsVersion: t.esVersion, ElasticLicensed: t.elasticLicensed, Migration: t.migration} if err := processor.Process(fields, nil, properties, analyzers); err != nil { return nil, err @@ -169,7 +170,7 @@ func (t *Template) load(fields mapping.Fields) (common.MapStr, error) { } // LoadFile loads the the template from the given file path -func (t *Template) LoadFile(file string) (common.MapStr, error) { +func (t *Template) LoadFile(file string) (mapstr.M, error) { fields, err := mapping.LoadFieldsYaml(file) if err != nil { return nil, err @@ -179,7 +180,7 @@ func (t *Template) LoadFile(file string) (common.MapStr, error) { } // LoadBytes loads the template from the given byte array -func (t *Template) LoadBytes(data []byte) (common.MapStr, error) { +func (t *Template) LoadBytes(data []byte) (mapstr.M, error) { fields, err := loadYamlByte(data) if err != nil { return nil, err @@ -189,18 +190,18 @@ func (t *Template) LoadBytes(data []byte) (common.MapStr, error) { } // LoadMinimal loads the template only with the given configuration -func (t *Template) LoadMinimal() common.MapStr { - templ := common.MapStr{} +func (t *Template) LoadMinimal() mapstr.M { + templ := mapstr.M{} if t.config.Settings.Source != nil { templ["mappings"] = buildMappings( t.beatVersion, t.beatName, nil, nil, - common.MapStr(t.config.Settings.Source)) + mapstr.M(t.config.Settings.Source)) } - templ["settings"] = common.MapStr{ + templ["settings"] = mapstr.M{ "index": t.config.Settings.Index, } - return common.MapStr{ + return mapstr.M{ "template": templ, "data_stream": struct{}{}, "priority": t.priority, @@ -220,7 +221,7 @@ func (t *Template) GetPattern() string { // Generate generates the full template // The default values are taken from the default variable. -func (t *Template) Generate(properties, analyzers common.MapStr, dynamicTemplates []common.MapStr) common.MapStr { +func (t *Template) Generate(properties, analyzers mapstr.M, dynamicTemplates []mapstr.M) mapstr.M { tmpl := t.generateComponent(properties, analyzers, dynamicTemplates) tmpl["data_stream"] = struct{}{} tmpl["priority"] = t.priority @@ -229,15 +230,15 @@ func (t *Template) Generate(properties, analyzers common.MapStr, dynamicTemplate } -func (t *Template) generateComponent(properties, analyzers common.MapStr, dynamicTemplates []common.MapStr) common.MapStr { - m := common.MapStr{ - "template": common.MapStr{ +func (t *Template) generateComponent(properties, analyzers mapstr.M, dynamicTemplates []mapstr.M) mapstr.M { + m := mapstr.M{ + "template": mapstr.M{ "mappings": buildMappings( t.beatVersion, t.beatName, properties, append(dynamicTemplates, buildDynTmpl(t.esVersion)), - common.MapStr(t.config.Settings.Source)), - "settings": common.MapStr{ + mapstr.M(t.config.Settings.Source)), + "settings": mapstr.M{ "index": buildIdxSettings( t.esVersion, t.config.Settings.Index, @@ -254,12 +255,12 @@ func (t *Template) generateComponent(properties, analyzers common.MapStr, dynami func buildMappings( beatVersion common.Version, beatName string, - properties common.MapStr, - dynTmpls []common.MapStr, - source common.MapStr, -) common.MapStr { - mapping := common.MapStr{ - "_meta": common.MapStr{ + properties mapstr.M, + dynTmpls []mapstr.M, + source mapstr.M, +) mapstr.M { + mapping := mapstr.M{ + "_meta": mapstr.M{ "version": beatVersion.String(), "beat": beatName, }, @@ -275,10 +276,10 @@ func buildMappings( return mapping } -func buildDynTmpl(ver common.Version) common.MapStr { - return common.MapStr{ - "strings_as_keyword": common.MapStr{ - "mapping": common.MapStr{ +func buildDynTmpl(ver common.Version) mapstr.M { + return mapstr.M{ + "strings_as_keyword": mapstr.M{ + "mapping": mapstr.M{ "ignore_above": 1024, "type": "keyword", }, @@ -287,11 +288,11 @@ func buildDynTmpl(ver common.Version) common.MapStr { } } -func buildIdxSettings(ver common.Version, userSettings common.MapStr) common.MapStr { - indexSettings := common.MapStr{ +func buildIdxSettings(ver common.Version, userSettings mapstr.M) mapstr.M { + indexSettings := mapstr.M{ "refresh_interval": "5s", - "mapping": common.MapStr{ - "total_fields": common.MapStr{ + "mapping": mapstr.M{ + "total_fields": mapstr.M{ "limit": defaultTotalFieldsLimit, }, }, diff --git a/libbeat/template/template_test.go b/libbeat/template/template_test.go index e2430e25ede..5aa638006fe 100644 --- a/libbeat/template/template_test.go +++ b/libbeat/template/template_test.go @@ -29,12 +29,13 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/version" + "github.com/elastic/elastic-agent-libs/mapstr" ) type testTemplate struct { t *testing.T tmpl *Template - data common.MapStr + data mapstr.M } func TestNumberOfRoutingShards(t *testing.T) { @@ -97,14 +98,14 @@ func TestTemplate(t *testing.T) { t.Run("for ES 7.x", func(t *testing.T) { template := createTestTemplate(t, currentVersion, "7.10.0", DefaultConfig(info)) template.Assert("index_patterns", []string{"testbeat-" + currentVersion}) - template.Assert("template.mappings._meta", common.MapStr{"beat": "testbeat", "version": currentVersion}) + template.Assert("template.mappings._meta", mapstr.M{"beat": "testbeat", "version": currentVersion}) template.Assert("template.settings.index.max_docvalue_fields_search", 200) }) t.Run("for ES 8.x", func(t *testing.T) { template := createTestTemplate(t, currentVersion, "8.0.0", DefaultConfig(info)) template.Assert("index_patterns", []string{"testbeat-" + currentVersion}) - template.Assert("template.mappings._meta", common.MapStr{"beat": "testbeat", "version": currentVersion}) + template.Assert("template.mappings._meta", mapstr.M{"beat": "testbeat", "version": currentVersion}) template.Assert("template.settings.index.max_docvalue_fields_search", 200) }) } @@ -124,7 +125,7 @@ func createTestTemplate(t *testing.T, beatVersion, esVersion string, config Temp func (t *testTemplate) Has(path string) bool { t.t.Helper() has, err := t.data.HasKey(path) - if err != nil && err != common.ErrKeyNotFound { + if err != nil && err != mapstr.ErrKeyNotFound { serialized, _ := json.MarshalIndent(t.data, "", " ") t.t.Fatalf("error accessing '%v': %v\ntemplate: %s", path, err, serialized) } diff --git a/metricbeat/autodiscover/appender/kubernetes/token/token.go b/metricbeat/autodiscover/appender/kubernetes/token/token.go index 9f4f99c4583..91489d6361d 100644 --- a/metricbeat/autodiscover/appender/kubernetes/token/token.go +++ b/metricbeat/autodiscover/appender/kubernetes/token/token.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/conditions" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -80,7 +81,7 @@ func (t *tokenAppender) Append(event bus.Event) { } // Check if the condition is met. Attempt to append only if that is the case. - if t.Condition == nil || t.Condition.Check(common.MapStr(event)) == true { + if t.Condition == nil || t.Condition.Check(mapstr.M(event)) == true { tok := t.getAuthHeaderFromToken() // If token is empty then just return if tok == "" { @@ -89,20 +90,20 @@ func (t *tokenAppender) Append(event bus.Event) { for i := 0; i < len(cfgs); i++ { // Unpack the config cfg := cfgs[i] - c := common.MapStr{} + c := mapstr.M{} err := cfg.Unpack(&c) if err != nil { logp.Debug("kubernetes.config", "unable to unpack config due to error: %v", err) continue } - var headers common.MapStr + var headers mapstr.M if hRaw, ok := c["headers"]; ok { // If headers is not a map then continue to next config - if headers, ok = hRaw.(common.MapStr); !ok { + if headers, ok = hRaw.(mapstr.M); !ok { continue } } else { - headers = common.MapStr{} + headers = mapstr.M{} } // Assign authorization header and add it back to the config diff --git a/metricbeat/autodiscover/appender/kubernetes/token/token_test.go b/metricbeat/autodiscover/appender/kubernetes/token/token_test.go index d5c1ec54da4..7c86c12126b 100644 --- a/metricbeat/autodiscover/appender/kubernetes/token/token_test.go +++ b/metricbeat/autodiscover/appender/kubernetes/token/token_test.go @@ -26,20 +26,21 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestTokenAppender(t *testing.T) { tests := []struct { eventConfig string event bus.Event - result common.MapStr + result mapstr.M config string }{ // Appender without a condition should apply the config regardless // Empty event config should return a config with only the headers { event: bus.Event{}, - result: common.MapStr{ + result: mapstr.M{ "headers": map[string]interface{}{ "Authorization": "Bearer foo bar", }, @@ -52,7 +53,7 @@ token_path: "test" // Metricbeat module config should return a config that has headers section { event: bus.Event{}, - result: common.MapStr{ + result: mapstr.M{ "module": "prometheus", "hosts": []interface{}{"1.2.3.4:8080"}, "headers": map[string]interface{}{ @@ -91,7 +92,7 @@ token_path: "test" cfgs, _ := test.event["config"].([]*common.Config) assert.Equal(t, len(cfgs), 1) - out := common.MapStr{} + out := mapstr.M{} cfgs[0].Unpack(&out) assert.Equal(t, out, test.result) diff --git a/metricbeat/autodiscover/builder/hints/metrics.go b/metricbeat/autodiscover/builder/hints/metrics.go index 4b32b522b08..9ffe92f88b7 100644 --- a/metricbeat/autodiscover/builder/hints/metrics.go +++ b/metricbeat/autodiscover/builder/hints/metrics.go @@ -22,6 +22,7 @@ import ( "strconv" "strings" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" "github.com/elastic/beats/v7/libbeat/autodiscover" @@ -89,7 +90,7 @@ func (m *metricHints) CreateConfig(event bus.Event, options ...ucfg.Option) []*c noPort = true } - hints, ok := event["hints"].(common.MapStr) + hints, ok := event["hints"].(mapstr.M) if !ok { return configs } @@ -131,7 +132,7 @@ func (m *metricHints) CreateConfig(event bus.Event, options ...ucfg.Option) []*c username := m.getUsername(hint) password := m.getPassword(hint) - moduleConfig := common.MapStr{ + moduleConfig := mapstr.M{ "module": mod, "metricsets": msets, "timeout": tout, @@ -190,7 +191,7 @@ func (m *metricHints) CreateConfig(event bus.Event, options ...ucfg.Option) []*c return template.ApplyConfigTemplate(event, configs, options...) } -func (m *metricHints) generateConfig(mod common.MapStr) *common.Config { +func (m *metricHints) generateConfig(mod mapstr.M) *common.Config { cfg, err := common.NewConfigFrom(mod) if err != nil { logp.Debug("hints.builder", "config merge failed with error: %v", err) @@ -199,11 +200,11 @@ func (m *metricHints) generateConfig(mod common.MapStr) *common.Config { return cfg } -func (m *metricHints) getModule(hints common.MapStr) string { +func (m *metricHints) getModule(hints mapstr.M) string { return builder.GetHintString(hints, m.Key, module) } -func (m *metricHints) getMetricSets(hints common.MapStr, module string) []string { +func (m *metricHints) getMetricSets(hints mapstr.M, module string) []string { var msets []string var err error msets = builder.GetHintAsList(hints, m.Key, metricsets) @@ -220,7 +221,7 @@ func (m *metricHints) getMetricSets(hints common.MapStr, module string) []string return msets } -func (m *metricHints) getHostsWithPort(hints common.MapStr, port int, noPort bool) ([]string, bool) { +func (m *metricHints) getHostsWithPort(hints mapstr.M, port int, noPort bool) ([]string, bool) { var result []string thosts := builder.GetHintAsList(hints, m.Key, hosts) @@ -270,23 +271,23 @@ func (m *metricHints) checkHostPort(h string, p int) bool { return h[end] < '0' || h[end] > '9' } -func (m *metricHints) getNamespace(hints common.MapStr) string { +func (m *metricHints) getNamespace(hints mapstr.M) string { return builder.GetHintString(hints, m.Key, namespace) } -func (m *metricHints) getMetricPath(hints common.MapStr) string { +func (m *metricHints) getMetricPath(hints mapstr.M) string { return builder.GetHintString(hints, m.Key, metricspath) } -func (m *metricHints) getUsername(hints common.MapStr) string { +func (m *metricHints) getUsername(hints mapstr.M) string { return builder.GetHintString(hints, m.Key, username) } -func (m *metricHints) getPassword(hints common.MapStr) string { +func (m *metricHints) getPassword(hints mapstr.M) string { return builder.GetHintString(hints, m.Key, password) } -func (m *metricHints) getPeriod(hints common.MapStr) string { +func (m *metricHints) getPeriod(hints mapstr.M) string { if ival := builder.GetHintString(hints, m.Key, period); ival != "" { return ival } @@ -294,40 +295,40 @@ func (m *metricHints) getPeriod(hints common.MapStr) string { return defaultPeriod } -func (m *metricHints) getTimeout(hints common.MapStr) string { +func (m *metricHints) getTimeout(hints mapstr.M) string { if tout := builder.GetHintString(hints, m.Key, timeout); tout != "" { return tout } return defaultTimeout } -func (m *metricHints) getSSLConfig(hints common.MapStr) common.MapStr { +func (m *metricHints) getSSLConfig(hints mapstr.M) mapstr.M { return builder.GetHintMapStr(hints, m.Key, ssl) } -func (m *metricHints) getMetricsFilters(hints common.MapStr) common.MapStr { - mf := common.MapStr{} +func (m *metricHints) getMetricsFilters(hints mapstr.M) mapstr.M { + mf := mapstr.M{} for k := range builder.GetHintMapStr(hints, m.Key, metricsfilters) { mf[k] = builder.GetHintAsList(hints, m.Key, metricsfilters+"."+k) } return mf } -func (m *metricHints) getModuleConfigs(hints common.MapStr) []common.MapStr { +func (m *metricHints) getModuleConfigs(hints mapstr.M) []mapstr.M { return builder.GetHintAsConfigs(hints, m.Key) } -func (m *metricHints) getProcessors(hints common.MapStr) []common.MapStr { +func (m *metricHints) getProcessors(hints mapstr.M) []mapstr.M { return builder.GetProcessors(hints, m.Key) } -func (m *metricHints) getModules(hints common.MapStr) []common.MapStr { +func (m *metricHints) getModules(hints mapstr.M) []mapstr.M { modules := builder.GetHintsAsList(hints, m.Key) - var output []common.MapStr + var output []mapstr.M for _, mod := range modules { - output = append(output, common.MapStr{ + output = append(output, mapstr.M{ m.Key: mod, }) } diff --git a/metricbeat/autodiscover/builder/hints/metrics_test.go b/metricbeat/autodiscover/builder/hints/metrics_test.go index 2ea5103288f..416c8038350 100644 --- a/metricbeat/autodiscover/builder/hints/metrics_test.go +++ b/metricbeat/autodiscover/builder/hints/metrics_test.go @@ -26,11 +26,11 @@ import ( "github.com/docker/docker/pkg/ioutils" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints(t *testing.T) { @@ -38,69 +38,69 @@ func TestGenerateHints(t *testing.T) { message string event bus.Event len int - result []common.MapStr + result []mapstr.M }{ { message: "Empty event hints should return empty config", event: bus.Event{ "host": "1.2.3.4", - "kubernetes": common.MapStr{ - "container": common.MapStr{ + "kubernetes": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "name": "foobar", "id": "abc", }, }, }, len: 0, - result: []common.MapStr{}, + result: []mapstr.M{}, }, { message: "Hints without host should return nothing", event: bus.Event{ - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmodule", }, }, }, len: 0, - result: []common.MapStr{}, + result: []mapstr.M{}, }, { message: "Hints without matching port should return nothing", event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "hosts": "${data.host}:8888", }, }, }, len: 0, - result: []common.MapStr{}, + result: []mapstr.M{}, }, { message: "Hints with multiple hosts return only the matching one", event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "hosts": "${data.host}:8888,${data.host}:9090", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "metricsets": []string{"default"}, @@ -116,15 +116,15 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "hosts": "${data.host}:8888,${data.host}:${data.port}", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "metricsets": []string{"default"}, @@ -139,14 +139,14 @@ func TestGenerateHints(t *testing.T) { message: "Only module hint should return all metricsets", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmodule", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmodule", "metricsets": []string{"one", "two"}, @@ -160,15 +160,15 @@ func TestGenerateHints(t *testing.T) { message: "Metricsets hint works", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmodule", "metricsets": "one", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmodule", "metricsets": []string{"one"}, @@ -182,14 +182,14 @@ func TestGenerateHints(t *testing.T) { message: "Only module, it should return defaults", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "metricsets": []string{"default"}, @@ -203,14 +203,14 @@ func TestGenerateHints(t *testing.T) { message: "Module defined in modules as a JSON string should return a config", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "raw": "{\"enabled\":true,\"metricsets\":[\"default\"],\"module\":\"mockmoduledefaults\",\"period\":\"1m\",\"timeout\":\"3s\"}", }, }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "metricsets": []string{"default"}, @@ -225,8 +225,8 @@ func TestGenerateHints(t *testing.T) { "docker host network scenario", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:9090", @@ -234,7 +234,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -250,13 +250,13 @@ func TestGenerateHints(t *testing.T) { message: "Module with processor config must return an module having the processor defined", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:9090", - "processors": common.MapStr{ - "add_locale": common.MapStr{ + "processors": mapstr.M{ + "add_locale": mapstr.M{ "abbrevation": "MST", }, }, @@ -264,7 +264,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -288,8 +288,8 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 0, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:9090", @@ -303,8 +303,8 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:9090", @@ -312,7 +312,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -329,9 +329,9 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 80, - "hints": []common.MapStr{ + "hints": []mapstr.M{ { - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:8080", @@ -340,15 +340,15 @@ func TestGenerateHints(t *testing.T) { }, }, len: 0, - result: []common.MapStr{}, + result: []mapstr.M{}, }, { message: "Non http URLs with valid host port combination should return a valid config", event: bus.Event{ "host": "1.2.3.4", "port": 3306, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "tcp(${data.host}:3306)/", @@ -356,7 +356,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -372,9 +372,9 @@ func TestGenerateHints(t *testing.T) { message: "Named port in the hints should return the corresponding container port", event: bus.Event{ "host": "1.2.3.4", - "ports": common.MapStr{"some": 3306}, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "ports": mapstr.M{"some": 3306}, + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:${data.ports.some}", @@ -382,7 +382,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -398,9 +398,9 @@ func TestGenerateHints(t *testing.T) { message: "Named port in the hints should return the corresponding container port for complex hosts", event: bus.Event{ "host": "1.2.3.4", - "ports": common.MapStr{"prometheus": 3306}, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "ports": mapstr.M{"prometheus": 3306}, + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "http://${data.host}:${data.ports.prometheus}/metrics", @@ -408,7 +408,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -425,9 +425,9 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 3306, - "ports": common.MapStr{"prometheus": 3306}, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "ports": mapstr.M{"prometheus": 3306}, + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:${data.port}", @@ -435,7 +435,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -451,14 +451,14 @@ func TestGenerateHints(t *testing.T) { message: "Module with mutliple sets of hints must return the right configs", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ - "1": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ + "1": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:9090", }, - "2": common.MapStr{ + "2": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test1", "hosts": "${data.host}:9090/fake", @@ -467,7 +467,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 2, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -492,8 +492,8 @@ func TestGenerateHints(t *testing.T) { message: "Module with multiple hosts returns the right number of hints. Pod level hints need to be one per host", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:9090, ${data.host}:9091", @@ -501,7 +501,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 2, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -527,8 +527,8 @@ func TestGenerateHints(t *testing.T) { event: bus.Event{ "host": "1.2.3.4", "port": 9091, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "namespace": "test", "hosts": "${data.host}:9090, ${data.host}:9091", @@ -536,7 +536,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "mockmoduledefaults", "namespace": "test", @@ -552,12 +552,12 @@ func TestGenerateHints(t *testing.T) { message: "exclude/exclude in metrics filters are parsed as a list", event: bus.Event{ "host": "1.2.3.4", - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "prometheus", "namespace": "test", "hosts": "${data.host}:9090", - "metrics_filters": common.MapStr{ + "metrics_filters": mapstr.M{ "exclude": "foo, bar", "include": "xxx, yyy", }, @@ -565,7 +565,7 @@ func TestGenerateHints(t *testing.T) { }, }, len: 1, - result: []common.MapStr{ + result: []mapstr.M{ { "module": "prometheus", "namespace": "test", @@ -602,9 +602,9 @@ func TestGenerateHints(t *testing.T) { if len(cfgs) == 0 { continue } - configs := make([]common.MapStr, 0) + configs := make([]mapstr.M, 0) for _, cfg := range cfgs { - config := common.MapStr{} + config := mapstr.M{} err := cfg.Unpack(&config) ok := assert.Nil(t, err, test.message) if !ok { @@ -639,15 +639,15 @@ func TestGenerateHintsDoesNotAccessGlobalKeystore(t *testing.T) { message string event bus.Event len int - result common.MapStr + result mapstr.M }{ { message: "Module, namespace, host hint should return valid config", event: bus.Event{ "host": "1.2.3.4", "port": 9090, - "hints": common.MapStr{ - "metrics": common.MapStr{ + "hints": mapstr.M{ + "metrics": mapstr.M{ "module": "mockmoduledefaults", "hosts": "${data.host}:9090", "password": "${PASSWORD}", @@ -656,7 +656,7 @@ func TestGenerateHintsDoesNotAccessGlobalKeystore(t *testing.T) { "keystore": keystore, }, len: 1, - result: common.MapStr{ + result: mapstr.M{ "module": "mockmoduledefaults", "metricsets": []string{"default"}, "hosts": []interface{}{"1.2.3.4:9090"}, @@ -679,7 +679,7 @@ func TestGenerateHintsDoesNotAccessGlobalKeystore(t *testing.T) { cfgs := m.CreateConfig(test.event) assert.Equal(t, len(cfgs), test.len) if len(cfgs) != 0 { - config := common.MapStr{} + config := mapstr.M{} err := cfgs[0].Unpack(&config) assert.Nil(t, err, test.message) diff --git a/metricbeat/cmd/root.go b/metricbeat/cmd/root.go index 9adbd0542a7..ffc3169ad02 100644 --- a/metricbeat/cmd/root.go +++ b/metricbeat/cmd/root.go @@ -24,11 +24,11 @@ import ( "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/publisher/processing" "github.com/elastic/beats/v7/metricbeat/beater" "github.com/elastic/beats/v7/metricbeat/cmd/test" + "github.com/elastic/elastic-agent-libs/mapstr" // import modules _ "github.com/elastic/beats/v7/metricbeat/include" @@ -44,8 +44,8 @@ const ( var RootCmd *cmd.BeatsRootCmd // withECSVersion is a modifier that adds ecs.version to events. -var withECSVersion = processing.WithFields(common.MapStr{ - "ecs": common.MapStr{ +var withECSVersion = processing.WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) diff --git a/metricbeat/helper/elastic/elastic.go b/metricbeat/helper/elastic/elastic.go index c51179747e1..972c595362b 100644 --- a/metricbeat/helper/elastic/elastic.go +++ b/metricbeat/helper/elastic/elastic.go @@ -24,6 +24,7 @@ import ( "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" @@ -118,9 +119,9 @@ func ReportAndLogError(err error, r mb.ReporterV2, l *logp.Logger) { // int, so that it is not serialized in scientific notation in the event. This is because // Elasticsearch cannot accepts scientific notation to represent millis-since-epoch values // for it's date fields: https://github.com/elastic/elasticsearch/pull/36691 -func FixTimestampField(m common.MapStr, field string) error { +func FixTimestampField(m mapstr.M, field string) error { v, err := m.GetValue(field) - if err == common.ErrKeyNotFound { + if err == mapstr.ErrKeyNotFound { return nil } if err != nil { @@ -151,7 +152,7 @@ func NewModule(base *mb.BaseModule, xpackEnabledMetricsets []string, logger *log return base, nil } - var raw common.MapStr + var raw mapstr.M if err := base.UnpackConfig(&raw); err != nil { return nil, errors.Wrapf(err, "could not unpack configuration for module %v", moduleName) } diff --git a/metricbeat/helper/labelhash/hash.go b/metricbeat/helper/labelhash/hash.go index c26665fbdba..8006c2830b6 100644 --- a/metricbeat/helper/labelhash/hash.go +++ b/metricbeat/helper/labelhash/hash.go @@ -25,7 +25,7 @@ import ( "github.com/cespare/xxhash/v2" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const sep = '\xff' @@ -48,7 +48,7 @@ func (ls labels) Swap(i, j int) { ls[i], ls[j] = ls[j], ls[i] } func (ls labels) Less(i, j int) bool { return ls[i].key < ls[j].key } // LabelHash hashes the labels map and returns a string -func LabelHash(labelMap common.MapStr) string { +func LabelHash(labelMap mapstr.M) string { ls := make(labels, len(labelMap)) for k, v := range labelMap { diff --git a/metricbeat/helper/labelhash/hash_benchmark_test.go b/metricbeat/helper/labelhash/hash_benchmark_test.go index fabf6391cf3..0c7efec2cd3 100644 --- a/metricbeat/helper/labelhash/hash_benchmark_test.go +++ b/metricbeat/helper/labelhash/hash_benchmark_test.go @@ -20,11 +20,11 @@ package labelhash import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) -var testLabels = common.MapStr{ - "request": common.MapStr{ +var testLabels = mapstr.M{ + "request": mapstr.M{ "component": "apiserver", "resource": "configmaps", "scope": "cluster", @@ -33,7 +33,7 @@ var testLabels = common.MapStr{ }, } -var testLabelsFlat = common.MapStr{ +var testLabelsFlat = mapstr.M{ "component": "apiserver", "resource": "configmaps", "scope": "cluster", diff --git a/metricbeat/helper/openmetrics/metric.go b/metricbeat/helper/openmetrics/metric.go index 4907ab59c9e..d2083ca4a6c 100644 --- a/metricbeat/helper/openmetrics/metric.go +++ b/metricbeat/helper/openmetrics/metric.go @@ -25,6 +25,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // MetricMap defines the mapping from Openmetrics metric to a Metricbeat field @@ -69,13 +70,13 @@ type Configuration struct { MetricProcessingOptions []MetricOption // ExtraFields is used to add fields to the // event where this metric is included - ExtraFields common.MapStr + ExtraFields mapstr.M } // MetricOption adds settings to Metric objects behavior type MetricOption interface { // Process a tuple of field, value and labels from a metric, return the same tuple updated - Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) + Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) } // OpFilterMap only processes metrics matching the given filter @@ -244,14 +245,14 @@ func (m *commonMetric) GetValue(metric *OpenMetric) interface{} { summary := metric.GetSummary() if summary != nil { - value := common.MapStr{} + value := mapstr.M{} if !math.IsNaN(summary.GetSampleSum()) && !math.IsInf(summary.GetSampleSum(), 0) { value["sum"] = summary.GetSampleSum() value["count"] = summary.GetSampleCount() } quantiles := summary.GetQuantile() - percentileMap := common.MapStr{} + percentileMap := mapstr.M{} for _, quantile := range quantiles { if !math.IsNaN(quantile.GetValue()) && !math.IsInf(quantile.GetValue(), 0) { key := strconv.FormatFloat(100*quantile.GetQuantile(), 'f', -1, 64) @@ -268,14 +269,14 @@ func (m *commonMetric) GetValue(metric *OpenMetric) interface{} { histogram := metric.GetHistogram() if histogram != nil { - value := common.MapStr{} + value := mapstr.M{} if !math.IsNaN(histogram.GetSampleSum()) && !math.IsInf(histogram.GetSampleSum(), 0) { value["sum"] = histogram.GetSampleSum() value["count"] = histogram.GetSampleCount() } buckets := histogram.GetBucket() - bucketMap := common.MapStr{} + bucketMap := mapstr.M{} for _, bucket := range buckets { if bucket.GetCumulativeCount() != uint64(math.NaN()) && bucket.GetCumulativeCount() != uint64(math.Inf(0)) { key := strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64) @@ -292,14 +293,14 @@ func (m *commonMetric) GetValue(metric *OpenMetric) interface{} { gaugehistogram := metric.GetGaugeHistogram() if gaugehistogram != nil { - value := common.MapStr{} + value := mapstr.M{} if !math.IsNaN(gaugehistogram.GetSampleSum()) && !math.IsInf(gaugehistogram.GetSampleSum(), 0) { value["gsum"] = gaugehistogram.GetSampleSum() value["gcount"] = gaugehistogram.GetSampleCount() } buckets := gaugehistogram.GetBucket() - bucketMap := common.MapStr{} + bucketMap := mapstr.M{} for _, bucket := range buckets { if bucket.GetCumulativeCount() != uint64(math.NaN()) && bucket.GetCumulativeCount() != uint64(math.Inf(0)) { key := strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64) @@ -388,7 +389,7 @@ type opFilterMap struct { // Check whether the value of the specified label is allowed and, if yes, return the metric via the specified mapped field // Else, if the specified label does not match the filter, return nil // This is useful in cases where multiple Metricbeat fields need to be defined per Openmetrics metric, based on label values -func (o opFilterMap) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opFilterMap) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { for k, v := range o.filterMap { if labels[o.label] == k { return fmt.Sprintf("%v.%v", field, v), value, labels @@ -400,7 +401,7 @@ func (o opFilterMap) Process(field string, value interface{}, labels common.MapS type opLowercaseValue struct{} // Process will lowercase the given value if it's a string -func (o opLowercaseValue) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opLowercaseValue) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { if val, ok := value.(string); ok { value = strings.ToLower(val) } @@ -412,12 +413,12 @@ type opMultiplyBuckets struct { } // Process will multiply the bucket labels if it is an histogram with numeric labels -func (o opMultiplyBuckets) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { - histogram, ok := value.(common.MapStr) +func (o opMultiplyBuckets) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { + histogram, ok := value.(mapstr.M) if !ok { return field, value, labels } - bucket, ok := histogram["bucket"].(common.MapStr) + bucket, ok := histogram["bucket"].(mapstr.M) if !ok { return field, value, labels } @@ -425,7 +426,7 @@ func (o opMultiplyBuckets) Process(field string, value interface{}, labels commo if !ok { return field, value, labels } - multiplied := common.MapStr{} + multiplied := mapstr.M{} for k, v := range bucket { if f, err := strconv.ParseFloat(k, 64); err == nil { key := strconv.FormatFloat(f*o.multiplier, 'f', -1, 64) @@ -444,7 +445,7 @@ type opSetNumericMetricSuffix struct { } // Process will extend the field's name with the given suffix -func (o opSetNumericMetricSuffix) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opSetNumericMetricSuffix) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { _, ok := value.(float64) if !ok { return field, value, labels @@ -457,7 +458,7 @@ type opUnixTimestampValue struct { } // Process converts a value in seconds into an unix time -func (o opUnixTimestampValue) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opUnixTimestampValue) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { return field, common.Time(time.Unix(int64(value.(float64)), 0)), labels } @@ -475,7 +476,7 @@ type opLabelKeyPrefixRemover struct { // For each label, if the key is found a new key will be created hosting the same value and the // old key will be deleted. // Fields, values and not prefixed labels will remain unmodified. -func (o opLabelKeyPrefixRemover) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opLabelKeyPrefixRemover) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { renameKeys := []string{} for k := range labels { if len(k) < len(o.Prefix) { diff --git a/metricbeat/helper/openmetrics/openmetrics.go b/metricbeat/helper/openmetrics/openmetrics.go index 9e4abc6428b..c99b8185b51 100644 --- a/metricbeat/helper/openmetrics/openmetrics.go +++ b/metricbeat/helper/openmetrics/openmetrics.go @@ -39,10 +39,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) const acceptHeader = `application/openmetrics-text; version=1.0.0; charset=utf-8,text/plain` @@ -330,9 +330,9 @@ type OpenMetrics interface { // GetFamilies requests metric families from openmetrics endpoint and returns them GetFamilies() ([]*OpenMetricFamily, error) - GetProcessedMetrics(mapping *MetricsMapping) ([]common.MapStr, error) + GetProcessedMetrics(mapping *MetricsMapping) ([]mapstr.M, error) - ProcessMetrics(families []*OpenMetricFamily, mapping *MetricsMapping) ([]common.MapStr, error) + ProcessMetrics(families []*OpenMetricFamily, mapping *MetricsMapping) ([]mapstr.M, error) ReportProcessedMetrics(mapping *MetricsMapping, r mb.ReporterV2) error } @@ -766,9 +766,9 @@ type MetricsMapping struct { ExtraFields map[string]string } -func (p *openmetrics) ProcessMetrics(families []*OpenMetricFamily, mapping *MetricsMapping) ([]common.MapStr, error) { +func (p *openmetrics) ProcessMetrics(families []*OpenMetricFamily, mapping *MetricsMapping) ([]mapstr.M, error) { - eventsMap := map[string]common.MapStr{} + eventsMap := map[string]mapstr.M{} infoMetrics := []*infoMetricData{} for _, family := range families { for _, metric := range family.GetMetric() { @@ -788,7 +788,7 @@ func (p *openmetrics) ProcessMetrics(families []*OpenMetricFamily, mapping *Metr storeAllLabels := false labelsLocation := "" - var extraFields common.MapStr + var extraFields mapstr.M if m != nil { c := m.GetConfiguration() storeAllLabels = c.StoreNonMappedLabels @@ -803,8 +803,8 @@ func (p *openmetrics) ProcessMetrics(families []*OpenMetricFamily, mapping *Metr } // Convert labels - labels := common.MapStr{} - keyLabels := common.MapStr{} + labels := mapstr.M{} + keyLabels := mapstr.M{} for k, v := range allLabels { if l, ok := mapping.Labels[k]; ok { if l.IsKey() { @@ -840,7 +840,7 @@ func (p *openmetrics) ProcessMetrics(families []*OpenMetricFamily, mapping *Metr if field != "" { event := getEvent(eventsMap, keyLabels) - update := common.MapStr{} + update := mapstr.M{} update.Put(field, value) // value may be a mapstr (for histograms and summaries), do a deep update to avoid smashing existing fields event.DeepUpdate(update) @@ -851,7 +851,7 @@ func (p *openmetrics) ProcessMetrics(families []*OpenMetricFamily, mapping *Metr } // populate events array from values in eventsMap - events := make([]common.MapStr, 0, len(eventsMap)) + events := make([]mapstr.M, 0, len(eventsMap)) for _, event := range eventsMap { // Add extra fields for k, v := range mapping.ExtraFields { @@ -882,7 +882,7 @@ func (p *openmetrics) ProcessMetrics(families []*OpenMetricFamily, mapping *Metr return events, nil } -func (p *openmetrics) GetProcessedMetrics(mapping *MetricsMapping) ([]common.MapStr, error) { +func (p *openmetrics) GetProcessedMetrics(mapping *MetricsMapping) ([]mapstr.M, error) { families, err := p.GetFamilies() if err != nil { return nil, err @@ -892,8 +892,8 @@ func (p *openmetrics) GetProcessedMetrics(mapping *MetricsMapping) ([]common.Map // infoMetricData keeps data about an infoMetric type infoMetricData struct { - Labels common.MapStr - Meta common.MapStr + Labels mapstr.M + Meta mapstr.M } func (p *openmetrics) ReportProcessedMetrics(mapping *MetricsMapping, r mb.ReporterV2) error { @@ -911,7 +911,7 @@ func (p *openmetrics) ReportProcessedMetrics(mapping *MetricsMapping, r mb.Repor return nil } -func getEvent(m map[string]common.MapStr, labels common.MapStr) common.MapStr { +func getEvent(m map[string]mapstr.M, labels mapstr.M) mapstr.M { hash := labels.String() res, ok := m[hash] if !ok { @@ -921,8 +921,8 @@ func getEvent(m map[string]common.MapStr, labels common.MapStr) common.MapStr { return res } -func getLabels(metric *OpenMetric) common.MapStr { - labels := common.MapStr{} +func getLabels(metric *OpenMetric) mapstr.M { + labels := mapstr.M{} for _, label := range metric.GetLabel() { if label.Name != "" && label.Value != "" { labels.Put(label.Name, label.Value) diff --git a/metricbeat/helper/openmetrics/openmetrics_test.go b/metricbeat/helper/openmetrics/openmetrics_test.go index 5ebf1903c0f..6c75d9333e4 100644 --- a/metricbeat/helper/openmetrics/openmetrics_test.go +++ b/metricbeat/helper/openmetrics/openmetrics_test.go @@ -27,9 +27,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -206,7 +206,7 @@ func TestOpenMetrics(t *testing.T) { tests := []struct { mapping *MetricsMapping msg string - expected []common.MapStr + expected []mapstr.M }{ { msg: "Simple field map", @@ -215,9 +215,9 @@ func TestOpenMetrics(t *testing.T) { "first_metric": Metric("first.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, }, @@ -234,12 +234,12 @@ func TestOpenMetrics(t *testing.T) { "label2": Label("labels.label2"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", "label2": "value2", }, @@ -257,20 +257,20 @@ func TestOpenMetrics(t *testing.T) { "label3": KeyLabel("labels.label3"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label3": "Value3", }, }, - common.MapStr{ - "second": common.MapStr{ + mapstr.M{ + "second": mapstr.M{ "metric": 0.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label3": "othervalue", }, }, @@ -288,15 +288,15 @@ func TestOpenMetrics(t *testing.T) { "label2": Label("labels.label2"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, - "second": common.MapStr{ + "second": mapstr.M{ "metric": 0.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", "label2": "value2", }, @@ -314,12 +314,12 @@ func TestOpenMetrics(t *testing.T) { "label1": KeyLabel("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": "works", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -336,15 +336,15 @@ func TestOpenMetrics(t *testing.T) { "label1": KeyLabel("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": true, }, - "second": common.MapStr{ + "second": mapstr.M{ "metric": false, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -360,12 +360,12 @@ func TestOpenMetrics(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": "Value3", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -381,12 +381,12 @@ func TestOpenMetrics(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": "foo", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -405,14 +405,14 @@ func TestOpenMetrics(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ + "metric": mapstr.M{ "foo": "FOO", }, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -431,7 +431,7 @@ func TestOpenMetrics(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{}, + expected: []mapstr.M{}, }, { msg: "Summary metric", @@ -440,13 +440,13 @@ func TestOpenMetrics(t *testing.T) { "summary_metric": Metric("summary.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "summary": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "summary": mapstr.M{ + "metric": mapstr.M{ "sum": 234892394.0, "count": uint64(44000), - "percentile": common.MapStr{ + "percentile": mapstr.M{ "50": 29735.0, "90": 47103.0, "99": 50681.0, @@ -463,12 +463,12 @@ func TestOpenMetrics(t *testing.T) { "histogram_metric": Metric("histogram.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "histogram": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "histogram": mapstr.M{ + "metric": mapstr.M{ "count": uint64(1), - "bucket": common.MapStr{ + "bucket": mapstr.M{ "1000000000": uint64(1), "+Inf": uint64(1), "1000": uint64(1), @@ -490,12 +490,12 @@ func TestOpenMetrics(t *testing.T) { "histogram_decimal_metric": Metric("histogram.metric", OpMultiplyBuckets(1000)), }, }, - expected: []common.MapStr{ - common.MapStr{ - "histogram": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "histogram": mapstr.M{ + "metric": mapstr.M{ "count": uint64(5), - "bucket": common.MapStr{ + "bucket": mapstr.M{ "1": uint64(1), "10": uint64(1), "100": uint64(2), @@ -515,12 +515,12 @@ func TestOpenMetrics(t *testing.T) { "gaugehistogram_metric": Metric("gaugehistogram.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "gaugehistogram": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "gaugehistogram": mapstr.M{ + "metric": mapstr.M{ "gcount": uint64(42), - "bucket": common.MapStr{ + "bucket": mapstr.M{ "0.01": uint64(20), "0.1": uint64(25), "1": uint64(34), @@ -540,9 +540,9 @@ func TestOpenMetrics(t *testing.T) { "target_info": Metric("target_info.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "target_info": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "target_info": mapstr.M{ "metric": int64(1), }, }, @@ -559,12 +559,12 @@ func TestOpenMetrics(t *testing.T) { "hostname": Label("labels.hostname"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "target_with_labels_info": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "target_with_labels_info": mapstr.M{ "metric": int64(1), }, - "labels": common.MapStr{ + "labels": mapstr.M{ "env": "prod", "hostname": "myhost", }, @@ -597,7 +597,7 @@ func TestOpenMetricsKeyLabels(t *testing.T) { testName string openmetricsResponse string mapping *MetricsMapping - expectedEvents []common.MapStr + expectedEvents []mapstr.M }{ { testName: "Test gauge with KeyLabel", @@ -612,34 +612,34 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 1.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 2.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "john", "surname": "williams", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 3.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -664,23 +664,23 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 0.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 3.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -704,34 +704,34 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(1), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(2), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "john", "surname": "williams", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(3), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -756,23 +756,23 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(1), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(3), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -795,14 +795,14 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "alive": KeyLabel("metrics.one.midichlorians.alive"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ - "midichlorians": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ + "midichlorians": mapstr.M{ "count": uint64(86), "sum": 1000001.0, - "bucket": common.MapStr{ + "bucket": mapstr.M{ "2000": uint64(52), "4000": uint64(70), "8000": uint64(78), @@ -817,13 +817,13 @@ func TestOpenMetricsKeyLabels(t *testing.T) { }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ - "midichlorians": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ + "midichlorians": mapstr.M{ "count": uint64(28), "sum": 800001.0, - "bucket": common.MapStr{ + "bucket": mapstr.M{ "2000": uint64(16), "4000": uint64(20), "8000": uint64(23), @@ -852,14 +852,14 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "alive": KeyLabel("metrics.one.midichlorians.alive"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ - "midichlorians": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ + "midichlorians": mapstr.M{ "count": uint64(86), "sum": 1000001.0, - "bucket": common.MapStr{ + "bucket": mapstr.M{ "16000": uint64(84), "32000": uint64(86), "+Inf": uint64(86), @@ -885,22 +885,22 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "kind": KeyLabel("metrics.force.propagation.ms.labels.kind"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "force": common.MapStr{ - "propagation": common.MapStr{ - "ms": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "force": mapstr.M{ + "propagation": mapstr.M{ + "ms": mapstr.M{ "count": uint64(651), "sum": 89.0, - "percentile": common.MapStr{ + "percentile": mapstr.M{ "0": 35.0, "25": 22.0, "50": 7.0, "75": 20.0, "100": 30.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "kind": "jedi", }, }, @@ -908,21 +908,21 @@ func TestOpenMetricsKeyLabels(t *testing.T) { }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "force": common.MapStr{ - "propagation": common.MapStr{ - "ms": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "force": mapstr.M{ + "propagation": mapstr.M{ + "ms": mapstr.M{ "count": uint64(711), "sum": 112.0, - "percentile": common.MapStr{ + "percentile": mapstr.M{ "0": 30.0, "25": 20.0, "50": 12.0, "75": 21.0, "100": 29.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "kind": "sith", }, }, @@ -944,19 +944,19 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "kind": KeyLabel("metrics.force.propagation.ms.labels.kind"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "force": common.MapStr{ - "propagation": common.MapStr{ - "ms": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "force": mapstr.M{ + "propagation": mapstr.M{ + "ms": mapstr.M{ "count": uint64(651), "sum": 50.0, - "percentile": common.MapStr{ + "percentile": mapstr.M{ "75": 20.0, "100": 30.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "kind": "jedi", }, }, @@ -979,12 +979,12 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "label1": KeyLabel("metrics.label1"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ "value": 20.0, "label1": "I am 1", - "other_labels": common.MapStr{ + "other_labels": mapstr.M{ "label2": "I am 2", "label3": "I am 3", }, @@ -1000,7 +1000,7 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "metrics_that_inform_labels": ExtendedInfoMetric(Configuration{ StoreNonMappedLabels: true, NonMappedLabelsPlacement: "metrics.other_labels", - ExtraFields: common.MapStr{ + ExtraFields: mapstr.M{ "metrics.extra.field1": "extra1", "metrics.extra.field2": "extra2", }}), @@ -1010,16 +1010,16 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "label1": KeyLabel("metrics.label1"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ "value": 20.0, "label1": "I am 1", - "other_labels": common.MapStr{ + "other_labels": mapstr.M{ "label2": "I am 2", "label3": "I am 3", }, - "extra": common.MapStr{ + "extra": mapstr.M{ "field1": "extra1", "field2": "extra2", }, @@ -1038,19 +1038,19 @@ func TestOpenMetricsKeyLabels(t *testing.T) { "category": KeyLabel("metrics.labels.category"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ "count": int64(0), - "labels": common.MapStr{ + "labels": mapstr.M{ "category": "shoes", }, }, }, - common.MapStr{ - "metrics": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ "count": int64(1), - "labels": common.MapStr{ + "labels": mapstr.M{ "category": "collectibles", }, }, diff --git a/metricbeat/helper/prometheus/metric.go b/metricbeat/helper/prometheus/metric.go index c182d531750..815881c5b58 100644 --- a/metricbeat/helper/prometheus/metric.go +++ b/metricbeat/helper/prometheus/metric.go @@ -25,6 +25,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" dto "github.com/prometheus/client_model/go" ) @@ -70,13 +71,13 @@ type Configuration struct { MetricProcessingOptions []MetricOption // ExtraFields is used to add fields to the // event where this metric is included - ExtraFields common.MapStr + ExtraFields mapstr.M } // MetricOption adds settings to Metric objects behavior type MetricOption interface { // Process a tuple of field, value and labels from a metric, return the same tuple updated - Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) + Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) } // OpFilterMap only processes metrics matching the given filter @@ -221,14 +222,14 @@ func (m *commonMetric) GetValue(metric *dto.Metric) interface{} { summary := metric.GetSummary() if summary != nil { - value := common.MapStr{} + value := mapstr.M{} if !math.IsNaN(summary.GetSampleSum()) && !math.IsInf(summary.GetSampleSum(), 0) { value["sum"] = summary.GetSampleSum() value["count"] = summary.GetSampleCount() } quantiles := summary.GetQuantile() - percentileMap := common.MapStr{} + percentileMap := mapstr.M{} for _, quantile := range quantiles { if !math.IsNaN(quantile.GetValue()) && !math.IsInf(quantile.GetValue(), 0) { key := strconv.FormatFloat(100*quantile.GetQuantile(), 'f', -1, 64) @@ -245,14 +246,14 @@ func (m *commonMetric) GetValue(metric *dto.Metric) interface{} { histogram := metric.GetHistogram() if histogram != nil { - value := common.MapStr{} + value := mapstr.M{} if !math.IsNaN(histogram.GetSampleSum()) && !math.IsInf(histogram.GetSampleSum(), 0) { value["sum"] = histogram.GetSampleSum() value["count"] = histogram.GetSampleCount() } buckets := histogram.GetBucket() - bucketMap := common.MapStr{} + bucketMap := mapstr.M{} for _, bucket := range buckets { if bucket.GetCumulativeCount() != uint64(math.NaN()) && bucket.GetCumulativeCount() != uint64(math.Inf(0)) { key := strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64) @@ -341,7 +342,7 @@ type opFilterMap struct { // Check whether the value of the specified label is allowed and, if yes, return the metric via the specified mapped field // Else, if the specified label does not match the filter, return nil // This is useful in cases where multiple Metricbeat fields need to be defined per Prometheus metric, based on label values -func (o opFilterMap) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opFilterMap) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { for k, v := range o.filterMap { if labels[o.label] == k { if field == "" { @@ -357,7 +358,7 @@ func (o opFilterMap) Process(field string, value interface{}, labels common.MapS type opLowercaseValue struct{} // Process will lowercase the given value if it's a string -func (o opLowercaseValue) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opLowercaseValue) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { if val, ok := value.(string); ok { value = strings.ToLower(val) } @@ -369,12 +370,12 @@ type opMultiplyBuckets struct { } // Process will multiply the bucket labels if it is an histogram with numeric labels -func (o opMultiplyBuckets) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { - histogram, ok := value.(common.MapStr) +func (o opMultiplyBuckets) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { + histogram, ok := value.(mapstr.M) if !ok { return field, value, labels } - bucket, ok := histogram["bucket"].(common.MapStr) + bucket, ok := histogram["bucket"].(mapstr.M) if !ok { return field, value, labels } @@ -382,7 +383,7 @@ func (o opMultiplyBuckets) Process(field string, value interface{}, labels commo if !ok { return field, value, labels } - multiplied := common.MapStr{} + multiplied := mapstr.M{} for k, v := range bucket { if f, err := strconv.ParseFloat(k, 64); err == nil { key := strconv.FormatFloat(f*o.multiplier, 'f', -1, 64) @@ -401,7 +402,7 @@ type opSetNumericMetricSuffix struct { } // Process will extend the field's name with the given suffix -func (o opSetNumericMetricSuffix) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opSetNumericMetricSuffix) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { _, ok := value.(float64) if !ok { return field, value, labels @@ -414,7 +415,7 @@ type opUnixTimestampValue struct { } // Process converts a value in seconds into an unix time -func (o opUnixTimestampValue) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opUnixTimestampValue) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { return field, common.Time(time.Unix(int64(value.(float64)), 0)), labels } @@ -432,7 +433,7 @@ type opLabelKeyPrefixRemover struct { // For each label, if the key is found a new key will be created hosting the same value and the // old key will be deleted. // Fields, values and not prefixed labels will remain unmodified. -func (o opLabelKeyPrefixRemover) Process(field string, value interface{}, labels common.MapStr) (string, interface{}, common.MapStr) { +func (o opLabelKeyPrefixRemover) Process(field string, value interface{}, labels mapstr.M) (string, interface{}, mapstr.M) { renameKeys := []string{} for k := range labels { if len(k) < len(o.Prefix) { diff --git a/metricbeat/helper/prometheus/prometheus.go b/metricbeat/helper/prometheus/prometheus.go index 591ef1c212b..5a4640a7e7c 100644 --- a/metricbeat/helper/prometheus/prometheus.go +++ b/metricbeat/helper/prometheus/prometheus.go @@ -29,10 +29,10 @@ import ( dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/expfmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) const acceptHeader = `text/plain;version=0.0.4;q=0.5,*/*;q=0.1` @@ -42,9 +42,9 @@ type Prometheus interface { // GetFamilies requests metric families from prometheus endpoint and returns them GetFamilies() ([]*dto.MetricFamily, error) - GetProcessedMetrics(mapping *MetricsMapping) ([]common.MapStr, error) + GetProcessedMetrics(mapping *MetricsMapping) ([]mapstr.M, error) - ProcessMetrics(families []*dto.MetricFamily, mapping *MetricsMapping) ([]common.MapStr, error) + ProcessMetrics(families []*dto.MetricFamily, mapping *MetricsMapping) ([]mapstr.M, error) ReportProcessedMetrics(mapping *MetricsMapping, r mb.ReporterV2) error } @@ -141,9 +141,9 @@ type MetricsMapping struct { ExtraFields map[string]string } -func (p *prometheus) ProcessMetrics(families []*dto.MetricFamily, mapping *MetricsMapping) ([]common.MapStr, error) { +func (p *prometheus) ProcessMetrics(families []*dto.MetricFamily, mapping *MetricsMapping) ([]mapstr.M, error) { - eventsMap := map[string]common.MapStr{} + eventsMap := map[string]mapstr.M{} infoMetrics := []*infoMetricData{} for _, family := range families { for _, metric := range family.GetMetric() { @@ -163,7 +163,7 @@ func (p *prometheus) ProcessMetrics(families []*dto.MetricFamily, mapping *Metri storeAllLabels := false labelsLocation := "" - var extraFields common.MapStr + var extraFields mapstr.M if m != nil { c := m.GetConfiguration() storeAllLabels = c.StoreNonMappedLabels @@ -178,8 +178,8 @@ func (p *prometheus) ProcessMetrics(families []*dto.MetricFamily, mapping *Metri } // Convert labels - labels := common.MapStr{} - keyLabels := common.MapStr{} + labels := mapstr.M{} + keyLabels := mapstr.M{} for k, v := range allLabels { if l, ok := mapping.Labels[k]; ok { if l.IsKey() { @@ -216,7 +216,7 @@ func (p *prometheus) ProcessMetrics(families []*dto.MetricFamily, mapping *Metri if field != "" { event := getEvent(eventsMap, keyLabels) - update := common.MapStr{} + update := mapstr.M{} update.Put(field, value) // value may be a mapstr (for histograms and summaries), do a deep update to avoid smashing existing fields event.DeepUpdate(update) @@ -227,7 +227,7 @@ func (p *prometheus) ProcessMetrics(families []*dto.MetricFamily, mapping *Metri } // populate events array from values in eventsMap - events := make([]common.MapStr, 0, len(eventsMap)) + events := make([]mapstr.M, 0, len(eventsMap)) for _, event := range eventsMap { // Add extra fields for k, v := range mapping.ExtraFields { @@ -258,7 +258,7 @@ func (p *prometheus) ProcessMetrics(families []*dto.MetricFamily, mapping *Metri return events, nil } -func (p *prometheus) GetProcessedMetrics(mapping *MetricsMapping) ([]common.MapStr, error) { +func (p *prometheus) GetProcessedMetrics(mapping *MetricsMapping) ([]mapstr.M, error) { families, err := p.GetFamilies() if err != nil { return nil, err @@ -268,8 +268,8 @@ func (p *prometheus) GetProcessedMetrics(mapping *MetricsMapping) ([]common.MapS // infoMetricData keeps data about an infoMetric type infoMetricData struct { - Labels common.MapStr - Meta common.MapStr + Labels mapstr.M + Meta mapstr.M } func (p *prometheus) ReportProcessedMetrics(mapping *MetricsMapping, r mb.ReporterV2) error { @@ -287,7 +287,7 @@ func (p *prometheus) ReportProcessedMetrics(mapping *MetricsMapping, r mb.Report return nil } -func getEvent(m map[string]common.MapStr, labels common.MapStr) common.MapStr { +func getEvent(m map[string]mapstr.M, labels mapstr.M) mapstr.M { hash := labels.String() res, ok := m[hash] if !ok { @@ -297,8 +297,8 @@ func getEvent(m map[string]common.MapStr, labels common.MapStr) common.MapStr { return res } -func getLabels(metric *dto.Metric) common.MapStr { - labels := common.MapStr{} +func getLabels(metric *dto.Metric) mapstr.M { + labels := mapstr.M{} for _, label := range metric.GetLabel() { if label.GetName() != "" && label.GetValue() != "" { labels.Put(label.GetName(), label.GetValue()) diff --git a/metricbeat/helper/prometheus/prometheus_test.go b/metricbeat/helper/prometheus/prometheus_test.go index f044c2f422f..ffbf205753c 100644 --- a/metricbeat/helper/prometheus/prometheus_test.go +++ b/metricbeat/helper/prometheus/prometheus_test.go @@ -27,9 +27,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -207,7 +207,7 @@ func TestPrometheus(t *testing.T) { tests := []struct { mapping *MetricsMapping msg string - expected []common.MapStr + expected []mapstr.M }{ { msg: "Simple field map", @@ -216,9 +216,9 @@ func TestPrometheus(t *testing.T) { "first_metric": Metric("first.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, }, @@ -235,12 +235,12 @@ func TestPrometheus(t *testing.T) { "label2": Label("labels.label2"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", "label2": "value2", }, @@ -258,20 +258,20 @@ func TestPrometheus(t *testing.T) { "label3": KeyLabel("labels.label3"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label3": "Value3", }, }, - common.MapStr{ - "second": common.MapStr{ + mapstr.M{ + "second": mapstr.M{ "metric": 0.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label3": "othervalue", }, }, @@ -289,15 +289,15 @@ func TestPrometheus(t *testing.T) { "label2": Label("labels.label2"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": 1.0, }, - "second": common.MapStr{ + "second": mapstr.M{ "metric": 0.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", "label2": "value2", }, @@ -315,12 +315,12 @@ func TestPrometheus(t *testing.T) { "label1": KeyLabel("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": "works", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -337,15 +337,15 @@ func TestPrometheus(t *testing.T) { "label1": KeyLabel("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": true, }, - "second": common.MapStr{ + "second": mapstr.M{ "metric": false, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -361,12 +361,12 @@ func TestPrometheus(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": "Value3", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -382,12 +382,12 @@ func TestPrometheus(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ "metric": "foo", }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -406,14 +406,14 @@ func TestPrometheus(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "first": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "first": mapstr.M{ + "metric": mapstr.M{ "foo": "FOO", }, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "label1": "value1", }, }, @@ -432,7 +432,7 @@ func TestPrometheus(t *testing.T) { "label1": Label("labels.label1"), }, }, - expected: []common.MapStr{}, + expected: []mapstr.M{}, }, { msg: "Summary metric", @@ -441,13 +441,13 @@ func TestPrometheus(t *testing.T) { "summary_metric": Metric("summary.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "summary": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "summary": mapstr.M{ + "metric": mapstr.M{ "sum": 234892394.0, "count": uint64(44000), - "percentile": common.MapStr{ + "percentile": mapstr.M{ "50": 29735.0, "90": 47103.0, "99": 50681.0, @@ -464,12 +464,12 @@ func TestPrometheus(t *testing.T) { "histogram_metric": Metric("histogram.metric"), }, }, - expected: []common.MapStr{ - common.MapStr{ - "histogram": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "histogram": mapstr.M{ + "metric": mapstr.M{ "count": uint64(1), - "bucket": common.MapStr{ + "bucket": mapstr.M{ "1000000000": uint64(1), "+Inf": uint64(1), "1000": uint64(1), @@ -491,12 +491,12 @@ func TestPrometheus(t *testing.T) { "histogram_decimal_metric": Metric("histogram.metric", OpMultiplyBuckets(1000)), }, }, - expected: []common.MapStr{ - common.MapStr{ - "histogram": common.MapStr{ - "metric": common.MapStr{ + expected: []mapstr.M{ + mapstr.M{ + "histogram": mapstr.M{ + "metric": mapstr.M{ "count": uint64(5), - "bucket": common.MapStr{ + "bucket": mapstr.M{ "1": uint64(1), "10": uint64(1), "100": uint64(2), @@ -535,7 +535,7 @@ func TestPrometheusKeyLabels(t *testing.T) { testName string prometheusResponse string mapping *MetricsMapping - expectedEvents []common.MapStr + expectedEvents []mapstr.M }{ { testName: "Test gauge with KeyLabel", @@ -550,34 +550,34 @@ func TestPrometheusKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 1.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 2.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "john", "surname": "williams", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 3.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -602,23 +602,23 @@ func TestPrometheusKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 0.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": 3.0, - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -642,34 +642,34 @@ func TestPrometheusKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(1), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(2), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "john", "surname": "williams", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(3), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -694,23 +694,23 @@ func TestPrometheusKeyLabels(t *testing.T) { "age": KeyLabel("metrics.one.labels.age"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(1), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jane", "surname": "foster", }, }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ "count": int64(3), - "labels": common.MapStr{ + "labels": mapstr.M{ "name": "jahn", "surname": "baldwin", "age": "30", @@ -733,14 +733,14 @@ func TestPrometheusKeyLabels(t *testing.T) { "alive": KeyLabel("metrics.one.midichlorians.alive"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ - "midichlorians": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ + "midichlorians": mapstr.M{ "count": uint64(86), "sum": 1000001.0, - "bucket": common.MapStr{ + "bucket": mapstr.M{ "2000": uint64(52), "4000": uint64(70), "8000": uint64(78), @@ -755,13 +755,13 @@ func TestPrometheusKeyLabels(t *testing.T) { }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ - "midichlorians": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ + "midichlorians": mapstr.M{ "count": uint64(28), "sum": 800001.0, - "bucket": common.MapStr{ + "bucket": mapstr.M{ "2000": uint64(16), "4000": uint64(20), "8000": uint64(23), @@ -790,14 +790,14 @@ func TestPrometheusKeyLabels(t *testing.T) { "alive": KeyLabel("metrics.one.midichlorians.alive"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "one": common.MapStr{ - "midichlorians": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "one": mapstr.M{ + "midichlorians": mapstr.M{ "count": uint64(86), "sum": 1000001.0, - "bucket": common.MapStr{ + "bucket": mapstr.M{ "16000": uint64(84), "32000": uint64(86), "+Inf": uint64(86), @@ -823,22 +823,22 @@ func TestPrometheusKeyLabels(t *testing.T) { "kind": KeyLabel("metrics.force.propagation.ms.labels.kind"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "force": common.MapStr{ - "propagation": common.MapStr{ - "ms": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "force": mapstr.M{ + "propagation": mapstr.M{ + "ms": mapstr.M{ "count": uint64(651), "sum": 89.0, - "percentile": common.MapStr{ + "percentile": mapstr.M{ "0": 35.0, "25": 22.0, "50": 7.0, "75": 20.0, "100": 30.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "kind": "jedi", }, }, @@ -846,21 +846,21 @@ func TestPrometheusKeyLabels(t *testing.T) { }, }, }, - common.MapStr{ - "metrics": common.MapStr{ - "force": common.MapStr{ - "propagation": common.MapStr{ - "ms": common.MapStr{ + mapstr.M{ + "metrics": mapstr.M{ + "force": mapstr.M{ + "propagation": mapstr.M{ + "ms": mapstr.M{ "count": uint64(711), "sum": 112.0, - "percentile": common.MapStr{ + "percentile": mapstr.M{ "0": 30.0, "25": 20.0, "50": 12.0, "75": 21.0, "100": 29.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "kind": "sith", }, }, @@ -882,19 +882,19 @@ func TestPrometheusKeyLabels(t *testing.T) { "kind": KeyLabel("metrics.force.propagation.ms.labels.kind"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ - "force": common.MapStr{ - "propagation": common.MapStr{ - "ms": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ + "force": mapstr.M{ + "propagation": mapstr.M{ + "ms": mapstr.M{ "count": uint64(651), "sum": 50.0, - "percentile": common.MapStr{ + "percentile": mapstr.M{ "75": 20.0, "100": 30.0, }, - "labels": common.MapStr{ + "labels": mapstr.M{ "kind": "jedi", }, }, @@ -917,12 +917,12 @@ func TestPrometheusKeyLabels(t *testing.T) { "label1": KeyLabel("metrics.label1"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ "value": 20.0, "label1": "I am 1", - "other_labels": common.MapStr{ + "other_labels": mapstr.M{ "label2": "I am 2", "label3": "I am 3", }, @@ -939,7 +939,7 @@ func TestPrometheusKeyLabels(t *testing.T) { "metrics_that_inform_labels": ExtendedInfoMetric(Configuration{ StoreNonMappedLabels: true, NonMappedLabelsPlacement: "metrics.other_labels", - ExtraFields: common.MapStr{ + ExtraFields: mapstr.M{ "metrics.extra.field1": "extra1", "metrics.extra.field2": "extra2", }}), @@ -949,16 +949,16 @@ func TestPrometheusKeyLabels(t *testing.T) { "label1": KeyLabel("metrics.label1"), }, }, - expectedEvents: []common.MapStr{ - common.MapStr{ - "metrics": common.MapStr{ + expectedEvents: []mapstr.M{ + mapstr.M{ + "metrics": mapstr.M{ "value": 20.0, "label1": "I am 1", - "other_labels": common.MapStr{ + "other_labels": mapstr.M{ "label2": "I am 2", "label3": "I am 3", }, - "extra": common.MapStr{ + "extra": mapstr.M{ "field1": "extra1", "field2": "extra2", }, diff --git a/metricbeat/helper/server/http/http.go b/metricbeat/helper/server/http/http.go index 3fe02001d7a..3c8a9dc25f0 100644 --- a/metricbeat/helper/server/http/http.go +++ b/metricbeat/helper/server/http/http.go @@ -24,11 +24,11 @@ import ( "net/http" "strconv" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/tlscommon" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper/server" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type HttpServer struct { @@ -40,11 +40,11 @@ type HttpServer struct { } type HttpEvent struct { - event common.MapStr + event mapstr.M meta server.Meta } -func (h *HttpEvent) GetEvent() common.MapStr { +func (h *HttpEvent) GetEvent() mapstr.M { return h.event } @@ -154,7 +154,7 @@ func (h *HttpServer) handleFunc(writer http.ResponseWriter, req *http.Request) { return } - payload := common.MapStr{ + payload := mapstr.M{ server.EventDataKey: body, } diff --git a/metricbeat/helper/server/server.go b/metricbeat/helper/server/server.go index 3f3fb52c491..8109b427cc6 100644 --- a/metricbeat/helper/server/server.go +++ b/metricbeat/helper/server/server.go @@ -17,9 +17,9 @@ package server -import "github.com/elastic/beats/v7/libbeat/common" +import "github.com/elastic/elastic-agent-libs/mapstr" -type Meta common.MapStr +type Meta mapstr.M const ( EventDataKey = "data" @@ -38,7 +38,7 @@ type Server interface { // Event is an interface that can be used to get the event and event source related information. type Event interface { // Get the raw bytes of the event. - GetEvent() common.MapStr + GetEvent() mapstr.M // Get any metadata associated with the data that was received. Ex: client IP for udp message, // request/response headers for HTTP call. GetMeta() Meta diff --git a/metricbeat/helper/server/tcp/tcp.go b/metricbeat/helper/server/tcp/tcp.go index a0afc51fb09..7c68da50917 100644 --- a/metricbeat/helper/server/tcp/tcp.go +++ b/metricbeat/helper/server/tcp/tcp.go @@ -24,10 +24,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper/server" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type TcpServer struct { @@ -40,10 +40,10 @@ type TcpServer struct { } type TcpEvent struct { - event common.MapStr + event mapstr.M } -func (m *TcpEvent) GetEvent() common.MapStr { +func (m *TcpEvent) GetEvent() mapstr.M { return m.event } @@ -131,7 +131,7 @@ func (g *TcpServer) handle(conn net.Conn) { // Drop the delimiter and send the data if len(bytes) > 0 { g.eventQueue <- &TcpEvent{ - event: common.MapStr{ + event: mapstr.M{ server.EventDataKey: bytes[:len(bytes)-1], }, } diff --git a/metricbeat/helper/server/udp/udp.go b/metricbeat/helper/server/udp/udp.go index 6bec8489b46..c72cb0922c4 100644 --- a/metricbeat/helper/server/udp/udp.go +++ b/metricbeat/helper/server/udp/udp.go @@ -23,10 +23,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper/server" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type UdpServer struct { @@ -38,11 +38,11 @@ type UdpServer struct { } type UdpEvent struct { - event common.MapStr + event mapstr.M meta server.Meta } -func (u *UdpEvent) GetEvent() common.MapStr { +func (u *UdpEvent) GetEvent() mapstr.M { return u.event } @@ -107,7 +107,7 @@ func (g *UdpServer) watchMetrics() { copy(bufCopy, buffer) g.eventQueue <- &UdpEvent{ - event: common.MapStr{ + event: mapstr.M{ server.EventDataKey: bufCopy, }, meta: server.Meta{ diff --git a/metricbeat/helper/sql/sql.go b/metricbeat/helper/sql/sql.go index 521295b1b2d..67b888cb86b 100644 --- a/metricbeat/helper/sql/sql.go +++ b/metricbeat/helper/sql/sql.go @@ -28,8 +28,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type DbClient struct { @@ -62,7 +62,7 @@ func NewDBClient(driver, uri string, l *logp.Logger) (*DbClient, error) { } // fetchTableMode scan the rows and publishes the event for querys that return the response in a table format. -func (d *DbClient) FetchTableMode(ctx context.Context, q string) ([]common.MapStr, error) { +func (d *DbClient) FetchTableMode(ctx context.Context, q string) ([]mapstr.M, error) { rows, err := d.QueryContext(ctx, q) if err != nil { return nil, err @@ -71,7 +71,7 @@ func (d *DbClient) FetchTableMode(ctx context.Context, q string) ([]common.MapSt } // fetchTableMode scan the rows and publishes the event for querys that return the response in a table format. -func (d *DbClient) fetchTableMode(rows sqlRow) ([]common.MapStr, error) { +func (d *DbClient) fetchTableMode(rows sqlRow) ([]mapstr.M, error) { // Extracted from // https://stackoverflow.com/questions/23507531/is-golangs-sql-package-incapable-of-ad-hoc-exploratory-queries/23507765#23507765 cols, err := rows.Columns() @@ -88,7 +88,7 @@ func (d *DbClient) fetchTableMode(rows sqlRow) ([]common.MapStr, error) { vals[i] = new(interface{}) } - rr := make([]common.MapStr, 0) + rr := make([]mapstr.M, 0) for rows.Next() { err = rows.Scan(vals...) if err != nil { @@ -96,7 +96,7 @@ func (d *DbClient) fetchTableMode(rows sqlRow) ([]common.MapStr, error) { continue } - r := common.MapStr{} + r := mapstr.M{} for i, c := range cols { value := getValue(vals[i].(*interface{})) @@ -114,7 +114,7 @@ func (d *DbClient) fetchTableMode(rows sqlRow) ([]common.MapStr, error) { } // fetchTableMode scan the rows and publishes the event for querys that return the response in a table format. -func (d *DbClient) FetchVariableMode(ctx context.Context, q string) (common.MapStr, error) { +func (d *DbClient) FetchVariableMode(ctx context.Context, q string) (mapstr.M, error) { rows, err := d.QueryContext(ctx, q) if err != nil { return nil, err @@ -123,8 +123,8 @@ func (d *DbClient) FetchVariableMode(ctx context.Context, q string) (common.MapS } // fetchVariableMode scan the rows and publishes the event for querys that return the response in a key/value format. -func (d *DbClient) fetchVariableMode(rows sqlRow) (common.MapStr, error) { - data := common.MapStr{} +func (d *DbClient) fetchVariableMode(rows sqlRow) (mapstr.M, error) { + data := mapstr.M{} for rows.Next() { var key string @@ -143,7 +143,7 @@ func (d *DbClient) fetchVariableMode(rows sqlRow) (common.MapStr, error) { d.logger.Debug(errors.Wrap(err, "error trying to read rows")) } - r := common.MapStr{} + r := mapstr.M{} for key, value := range data { value := getValue(&value) @@ -155,8 +155,8 @@ func (d *DbClient) fetchVariableMode(rows sqlRow) (common.MapStr, error) { // ReplaceUnderscores takes the root keys of a common.Mapstr and rewrites them replacing underscores with dots. Check tests // to see an example. -func ReplaceUnderscores(ms common.MapStr) common.MapStr { - dotMap := common.MapStr{} +func ReplaceUnderscores(ms mapstr.M) mapstr.M { + dotMap := mapstr.M{} for k, v := range ms { dotMap.Put(strings.Replace(k, "_", ".", -1), v) } diff --git a/metricbeat/helper/sql/sql_test.go b/metricbeat/helper/sql/sql_test.go index 713837a96fa..783fff993a2 100644 --- a/metricbeat/helper/sql/sql_test.go +++ b/metricbeat/helper/sql/sql_test.go @@ -28,8 +28,8 @@ import ( "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/resources" + "github.com/elastic/elastic-agent-libs/mapstr" ) type kv struct { @@ -136,7 +136,7 @@ func TestFetchTableMode(t *testing.T) { } } -func checkValue(t *testing.T, res kv, ms common.MapStr) { +func checkValue(t *testing.T, res kv, ms mapstr.M) { switch v := res.v.(type) { case string, bool: if ms[res.k] != v { @@ -186,10 +186,10 @@ func checkValue(t *testing.T, res kv, ms common.MapStr) { } func TestToDotKeys(t *testing.T) { - ms := common.MapStr{"key_value": "value"} + ms := mapstr.M{"key_value": "value"} ms = ReplaceUnderscores(ms) - if ms["key"].(common.MapStr)["value"] != "value" { + if ms["key"].(mapstr.M)["value"] != "value" { t.Fail() } } diff --git a/metricbeat/internal/metrics/cpu/metrics.go b/metricbeat/internal/metrics/cpu/metrics.go index 75fa38d62d7..5f3c623c0bd 100644 --- a/metricbeat/internal/metrics/cpu/metrics.go +++ b/metricbeat/internal/metrics/cpu/metrics.go @@ -23,6 +23,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/libbeat/opt" + "github.com/elastic/elastic-agent-libs/mapstr" ) // CPU manages the CPU metrics from /proc/stat @@ -128,7 +129,7 @@ type Metrics struct { } // Format returns the final MapStr data object for the metrics. -func (metric Metrics) Format(opts MetricOpts) (common.MapStr, error) { +func (metric Metrics) Format(opts MetricOpts) (mapstr.M, error) { timeDelta := metric.currentSample.Total() - metric.previousSample.Total() if timeDelta <= 0 { @@ -139,7 +140,7 @@ func (metric Metrics) Format(opts MetricOpts) (common.MapStr, error) { normCPU = 1 } - formattedMetrics := common.MapStr{} + formattedMetrics := mapstr.M{} reportOptMetric := func(name string, current, previous opt.Uint, norm int) { if !current.IsZero() { @@ -176,8 +177,8 @@ func createTotal(prev, cur CPU, timeDelta uint64, numCPU int) float64 { return common.Round(float64(numCPU)-idleTime, common.DefaultDecimalPlacesCount) } -func fillMetric(opts MetricOpts, cur, prev opt.Uint, timeDelta uint64, numCPU int) common.MapStr { - event := common.MapStr{} +func fillMetric(opts MetricOpts, cur, prev opt.Uint, timeDelta uint64, numCPU int) mapstr.M { + event := mapstr.M{} if opts.Ticks { event.Put("ticks", cur.ValueOr(0)) } diff --git a/metricbeat/internal/metrics/cpu/metrics_test.go b/metricbeat/internal/metrics/cpu/metrics_test.go index 91a3f302375..156a87442b0 100644 --- a/metricbeat/internal/metrics/cpu/metrics_test.go +++ b/metricbeat/internal/metrics/cpu/metrics_test.go @@ -22,9 +22,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/libbeat/opt" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestMonitorSample(t *testing.T) { @@ -51,7 +51,7 @@ func TestCoresMonitorSample(t *testing.T) { } for _, s := range sample { - evt := common.MapStr{} + evt := mapstr.M{} metricOpts := MetricOpts{Percentages: true, Ticks: true} evt, err := s.Format(metricOpts) assert.NoError(t, err, "error in Format") @@ -59,7 +59,7 @@ func TestCoresMonitorSample(t *testing.T) { } } -func testPopulatedEvent(evt common.MapStr, t *testing.T, norm bool) { +func testPopulatedEvent(evt mapstr.M, t *testing.T, norm bool) { user, err := evt.GetValue("user.pct") assert.NoError(t, err, "error getting user.pct") system, err := evt.GetValue("system.pct") diff --git a/metricbeat/mb/event.go b/metricbeat/mb/event.go index f92615462fa..0ee5c2a35a9 100644 --- a/metricbeat/mb/event.go +++ b/metricbeat/mb/event.go @@ -23,6 +23,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // EventModifier is a function that can modifies an Event. This is typically @@ -32,9 +33,9 @@ type EventModifier func(module, metricset string, event *Event) // Event contains the data generated by a MetricSet. type Event struct { - RootFields common.MapStr // Fields that will be added to the root of the event. - ModuleFields common.MapStr // Fields that will be namespaced under [module]. - MetricSetFields common.MapStr // Fields that will be namespaced under [module].[metricset]. + RootFields mapstr.M // Fields that will be added to the root of the event. + ModuleFields mapstr.M // Fields that will be namespaced under [module]. + MetricSetFields mapstr.M // Fields that will be namespaced under [module].[metricset]. Index string // Index name prefix. If set overwrites the default prefix. ID string // ID of event. If set, overwrites the default ID. @@ -53,7 +54,7 @@ type Event struct { // mutate the underlying data in the Event. func (e *Event) BeatEvent(module, metricSet string, modifiers ...EventModifier) beat.Event { if e.RootFields == nil { - e.RootFields = common.MapStr{} + e.RootFields = mapstr.M{} } for _, modify := range modifiers { @@ -93,7 +94,7 @@ func (e *Event) BeatEvent(module, metricSet string, modifiers ...EventModifier) // Set index prefix to overwrite default if e.Index != "" { - b.Meta = common.MapStr{"index": e.Index} + b.Meta = mapstr.M{"index": e.Index} } if e.ID != "" { @@ -101,7 +102,7 @@ func (e *Event) BeatEvent(module, metricSet string, modifiers ...EventModifier) } if e.Error != nil { - b.Fields["error"] = common.MapStr{ + b.Fields["error"] = mapstr.M{ "message": e.Error.Error(), } } @@ -135,13 +136,13 @@ func AddMetricSetInfo(module, metricset string, event *Event) { event.Namespace = fmt.Sprintf("%s.%s", module, metricset) } - e := common.MapStr{ - "event": common.MapStr{ + e := mapstr.M{ + "event": mapstr.M{ "dataset": event.Namespace, "module": module, }, // TODO: This should only be sent if migration layer is enabled - "metricset": common.MapStr{ + "metricset": mapstr.M{ "name": metricset, }, } @@ -162,13 +163,13 @@ func AddMetricSetInfo(module, metricset string, event *Event) { } } -// TransformMapStrToEvent transforms a common.MapStr produced by MetricSet +// TransformMapStrToEvent transforms a mapstr.M produced by MetricSet // (like any MetricSet that does not natively produce a mb.Event). It accounts // for the special key names and routes the data stored under those keys to the // correct location in the event. -func TransformMapStrToEvent(module string, m common.MapStr, err error) Event { +func TransformMapStrToEvent(module string, m mapstr.M, err error) Event { var ( - event = Event{RootFields: common.MapStr{}, Error: err} + event = Event{RootFields: mapstr.M{}, Error: err} ) for k, v := range m { @@ -204,12 +205,12 @@ func TransformMapStrToEvent(module string, m common.MapStr, err error) Event { return event } -func tryToMapStr(v interface{}) (common.MapStr, bool) { +func tryToMapStr(v interface{}) (mapstr.M, bool) { switch m := v.(type) { - case common.MapStr: + case mapstr.M: return m, true case map[string]interface{}: - return common.MapStr(m), true + return mapstr.M(m), true default: return nil, false } diff --git a/metricbeat/mb/event_test.go b/metricbeat/mb/event_test.go index 89cd8ac5a1b..97150c7b2e5 100644 --- a/metricbeat/mb/event_test.go +++ b/metricbeat/mb/event_test.go @@ -28,7 +28,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat/events" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEventConversionToBeatEvent(t *testing.T) { @@ -41,31 +41,31 @@ func TestEventConversionToBeatEvent(t *testing.T) { t.Run("all levels", func(t *testing.T) { e := (&Event{ Timestamp: timestamp, - RootFields: common.MapStr{ + RootFields: mapstr.M{ "type": "docker", }, - ModuleFields: common.MapStr{ - "container": common.MapStr{ + ModuleFields: mapstr.M{ + "container": mapstr.M{ "name": "wordpress", }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "ms": 1000, }, }).BeatEvent(module, metricSet) assert.Equal(t, timestamp, e.Timestamp) - assert.Equal(t, common.MapStr{ + assert.Equal(t, mapstr.M{ "type": "docker", - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "name": "wordpress", }, - "uptime": common.MapStr{ + "uptime": mapstr.M{ "ms": 1000, }, }, - "service": common.MapStr{ + "service": mapstr.M{ "type": "docker", }, }, e.Fields) @@ -74,15 +74,15 @@ func TestEventConversionToBeatEvent(t *testing.T) { t.Run("idempotent", func(t *testing.T) { mbEvent := &Event{ Timestamp: timestamp, - RootFields: common.MapStr{ + RootFields: mapstr.M{ "type": "docker", }, - ModuleFields: common.MapStr{ - "container": common.MapStr{ + ModuleFields: mapstr.M{ + "container": mapstr.M{ "name": "wordpress", }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "ms": 1000, }, } @@ -90,17 +90,17 @@ func TestEventConversionToBeatEvent(t *testing.T) { e = mbEvent.BeatEvent(module, metricSet) assert.Equal(t, timestamp, e.Timestamp) - assert.Equal(t, common.MapStr{ + assert.Equal(t, mapstr.M{ "type": "docker", - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "name": "wordpress", }, - "uptime": common.MapStr{ + "uptime": mapstr.M{ "ms": 1000, }, }, - "service": common.MapStr{ + "service": mapstr.M{ "type": "docker", }, }, e.Fields) @@ -113,10 +113,10 @@ func TestEventConversionToBeatEvent(t *testing.T) { } e := (&Event{}).BeatEvent(module, metricSet, modifier) - assert.Equal(t, common.MapStr{ + assert.Equal(t, mapstr.M{ "module": module, "metricset": metricSet, - "service": common.MapStr{ + "service": mapstr.M{ "type": "docker", }, }, e.Fields) @@ -126,15 +126,15 @@ func TestEventConversionToBeatEvent(t *testing.T) { mbEvent := &Event{ ID: "foobar", Timestamp: timestamp, - RootFields: common.MapStr{ + RootFields: mapstr.M{ "type": "docker", }, - ModuleFields: common.MapStr{ - "container": common.MapStr{ + ModuleFields: mapstr.M{ + "container": mapstr.M{ "name": "wordpress", }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "ms": 1000, }, } @@ -143,17 +143,17 @@ func TestEventConversionToBeatEvent(t *testing.T) { assert.Equal(t, "foobar", e.Meta[events.FieldMetaID]) assert.Equal(t, timestamp, e.Timestamp) - assert.Equal(t, common.MapStr{ + assert.Equal(t, mapstr.M{ "type": "docker", - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "name": "wordpress", }, - "uptime": common.MapStr{ + "uptime": mapstr.M{ "ms": 1000, }, }, - "service": common.MapStr{ + "service": mapstr.M{ "type": "docker", }, }, e.Fields) @@ -187,16 +187,16 @@ func TestAddMetricSetInfo(t *testing.T) { AddMetricSetInfo(moduleName, metricSetName, &e) - assert.Equal(t, common.MapStr{ - "event": common.MapStr{ + assert.Equal(t, mapstr.M{ + "event": mapstr.M{ "module": moduleName, "dataset": moduleName + "." + metricSetName, "duration": time.Duration(500000000), }, - "service": common.MapStr{ + "service": mapstr.M{ "address": host, }, - "metricset": common.MapStr{ + "metricset": mapstr.M{ "name": metricSetName, }, }, e.RootFields) @@ -207,12 +207,12 @@ func TestAddMetricSetInfo(t *testing.T) { AddMetricSetInfo(moduleName, metricSetName, &e) - assert.Equal(t, common.MapStr{ - "event": common.MapStr{ + assert.Equal(t, mapstr.M{ + "event": mapstr.M{ "module": moduleName, "dataset": moduleName + "." + metricSetName, }, - "metricset": common.MapStr{ + "metricset": mapstr.M{ "name": metricSetName, }, }, e.RootFields) @@ -223,16 +223,16 @@ func TestTransformMapStrToEvent(t *testing.T) { var ( timestamp = time.Now() took = time.Duration(1) - moduleData = common.MapStr{ + moduleData = mapstr.M{ "container_id": "busybox", } - metricSetData = common.MapStr{ + metricSetData = mapstr.M{ "uptime": "1 day", } failure = errors.New("failed") ) - m := common.MapStr{ + m := mapstr.M{ TimestampKey: timestamp, RTTKey: took, ModuleDataKey: moduleData, diff --git a/metricbeat/mb/example_metricset_test.go b/metricbeat/mb/example_metricset_test.go index af7a2dfcb6a..6a266478996 100644 --- a/metricbeat/mb/example_metricset_test.go +++ b/metricbeat/mb/example_metricset_test.go @@ -20,9 +20,9 @@ package mb_test import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) var hostParser = parse.URLHostParserBuilder{ @@ -48,7 +48,7 @@ func NewMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { // Fetch will be called periodically by the framework. func (ms *MetricSet) Fetch(report mb.Reporter) { // Fetch data from the host at ms.HostData().URI and return the data. - data, err := common.MapStr{ + data, err := mapstr.M{ "some_metric": 18.0, "answer_to_everything": 42, }, error(nil) diff --git a/metricbeat/mb/lightmetricset_test.go b/metricbeat/mb/lightmetricset_test.go index ebdf9e1bf75..f27d62ade8b 100644 --- a/metricbeat/mb/lightmetricset_test.go +++ b/metricbeat/mb/lightmetricset_test.go @@ -24,6 +24,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestLightMetricSetRegistration(t *testing.T) { @@ -75,8 +76,8 @@ func TestLightMetricSetRegistration(t *testing.T) { } ms.Input.Module = c.module ms.Input.MetricSet = c.metricSet - ms.Input.Defaults = common.MapStr{ - "query": common.MapStr{ + ms.Input.Defaults = mapstr.M{ + "query": mapstr.M{ "extra": "something", }, } diff --git a/metricbeat/mb/lightmodules_test.go b/metricbeat/mb/lightmodules_test.go index f74764b7423..0366079c4af 100644 --- a/metricbeat/mb/lightmodules_test.go +++ b/metricbeat/mb/lightmodules_test.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" _ "github.com/elastic/beats/v7/libbeat/processors/add_id" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TestLightModulesAsModuleSource checks that registry correctly lists @@ -187,62 +188,62 @@ func TestNewModuleFromConfig(t *testing.T) { logp.TestingSetup() cases := map[string]struct { - config common.MapStr + config mapstr.M err bool expectedOption string expectedQuery QueryParams expectedPeriod time.Duration }{ "normal module": { - config: common.MapStr{"module": "foo", "metricsets": []string{"bar"}}, + config: mapstr.M{"module": "foo", "metricsets": []string{"bar"}}, expectedOption: "default", expectedQuery: nil, }, "light module": { - config: common.MapStr{"module": "service", "metricsets": []string{"metricset"}}, + config: mapstr.M{"module": "service", "metricsets": []string{"metricset"}}, expectedOption: "test", expectedQuery: nil, }, "light module default metricset": { - config: common.MapStr{"module": "service"}, + config: mapstr.M{"module": "service"}, expectedOption: "test", expectedQuery: nil, }, "light module override option": { - config: common.MapStr{"module": "service", "option": "overriden"}, + config: mapstr.M{"module": "service", "option": "overriden"}, expectedOption: "overriden", expectedQuery: nil, }, "light module with query": { - config: common.MapStr{"module": "service", "query": common.MapStr{"param": "foo"}}, + config: mapstr.M{"module": "service", "query": mapstr.M{"param": "foo"}}, expectedOption: "test", expectedQuery: QueryParams{"param": "foo"}, }, "light module with custom period": { - config: common.MapStr{"module": "service", "period": "42s"}, + config: mapstr.M{"module": "service", "period": "42s"}, expectedOption: "test", expectedPeriod: 42 * time.Second, expectedQuery: nil, }, "light module is broken": { - config: common.MapStr{"module": "broken"}, + config: mapstr.M{"module": "broken"}, err: true, }, "light metric set doesn't exist": { - config: common.MapStr{"module": "service", "metricsets": []string{"notexists"}}, + config: mapstr.M{"module": "service", "metricsets": []string{"notexists"}}, err: true, }, "disabled light module": { - config: common.MapStr{"module": "service", "enabled": false}, + config: mapstr.M{"module": "service", "enabled": false}, err: true, }, "mixed module with standard and light metricsets": { - config: common.MapStr{"module": "mixed", "metricsets": []string{"standard", "light"}}, + config: mapstr.M{"module": "mixed", "metricsets": []string{"standard", "light"}}, expectedOption: "default", expectedQuery: nil, }, "mixed module with unregistered and light metricsets": { - config: common.MapStr{"module": "mixedbroken", "metricsets": []string{"unregistered", "light"}}, + config: mapstr.M{"module": "mixedbroken", "metricsets": []string{"unregistered", "light"}}, err: true, }, } @@ -308,7 +309,7 @@ func TestLightMetricSet_VerifyHostDataURI(t *testing.T) { r.SetSecondarySource(NewLightModulesSource("testdata/lightmodules")) config, err := common.NewConfigFrom( - common.MapStr{ + mapstr.M{ "module": "httpextended", "metricsets": []string{"extends"}, "hosts": []string{sampleHttpsEndpoint}, @@ -331,7 +332,7 @@ func TestLightMetricSet_WithoutHostParser(t *testing.T) { r.SetSecondarySource(NewLightModulesSource("testdata/lightmodules")) config, err := common.NewConfigFrom( - common.MapStr{ + mapstr.M{ "module": "httpextended", "metricsets": []string{"extends"}, "hosts": []string{sampleHttpsEndpoint}, @@ -364,7 +365,7 @@ func TestLightMetricSet_VerifyHostDataURI_NonParsableHost(t *testing.T) { r.SetSecondarySource(NewLightModulesSource("testdata/lightmodules")) config, err := common.NewConfigFrom( - common.MapStr{ + mapstr.M{ "module": "httpextended", "metricsets": []string{"extends"}, "hosts": []string{postgresEndpoint}, @@ -392,7 +393,7 @@ func TestNewModulesCallModuleFactory(t *testing.T) { return DefaultModuleFactory(base) }) - config, err := common.NewConfigFrom(common.MapStr{"module": "service"}) + config, err := common.NewConfigFrom(mapstr.M{"module": "service"}) require.NoError(t, err) _, _, err = NewModule(config, r) diff --git a/metricbeat/mb/mb.go b/metricbeat/mb/mb.go index c86380fdcf1..43e0909acf5 100644 --- a/metricbeat/mb/mb.go +++ b/metricbeat/mb/mb.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" "github.com/elastic/beats/v7/metricbeat/helper/dialer" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -43,7 +44,7 @@ const ( // ModuleDataKey is the key used in events created by MetricSets to add data // to an event that is common to the module. The data must be a - // common.MapStr and when the final event is built the object will be stored + // mapstr.M and when the final event is built the object will be stored // in the event under a key that is the module name. ModuleDataKey string = "_module" @@ -159,9 +160,9 @@ type Closer interface { // // Deprecated: Use ReporterV2. type Reporter interface { - Event(event common.MapStr) bool // Event reports a single successful event. - ErrorWith(err error, meta common.MapStr) bool // ErrorWith reports a single error event with the additional metadata. - Error(err error) bool // Error reports a single error event. + Event(event mapstr.M) bool // Event reports a single successful event. + ErrorWith(err error, meta mapstr.M) bool // ErrorWith reports a single error event with the additional metadata. + Error(err error) bool // Error reports a single error event. } // ReportingMetricSet is a MetricSet that reports events or errors through the diff --git a/metricbeat/mb/module/connector.go b/metricbeat/mb/module/connector.go index bff016fff4c..c6bdb72e89d 100644 --- a/metricbeat/mb/module/connector.go +++ b/metricbeat/mb/module/connector.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/fmtstr" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/add_formatted_index" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Connector configures and establishes a beat.Client for publishing events @@ -32,7 +33,7 @@ import ( type Connector struct { pipeline beat.PipelineConnector processors *processors.Processors - eventMeta common.EventMetadata + eventMeta mapstr.EventMetadata timeSeries bool keepNull bool } @@ -45,7 +46,7 @@ type connectorConfig struct { // KeepNull determines whether published events will keep null values or omit them. KeepNull bool `config:"keep_null"` - common.EventMetadata `config:",inline"` // Fields and tags to add to events. + mapstr.EventMetadata `config:",inline"` // Fields and tags to add to events. } type metricSetRegister interface { diff --git a/metricbeat/mb/module/connector_test.go b/metricbeat/mb/module/connector_test.go index 3695d1c61be..0fd4f517675 100644 --- a/metricbeat/mb/module/connector_test.go +++ b/metricbeat/mb/module/connector_test.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestProcessorsForConfig(t *testing.T) { @@ -54,7 +55,7 @@ func TestProcessorsForConfig(t *testing.T) { } for description, test := range testCases { if test.event.Fields == nil { - test.event.Fields = common.MapStr{} + test.event.Fields = mapstr.M{} } config, err := connectorConfigFromString(test.configStr) if err != nil { diff --git a/metricbeat/mb/module/options.go b/metricbeat/mb/module/options.go index 273db92b718..24e5b101e99 100644 --- a/metricbeat/mb/module/options.go +++ b/metricbeat/mb/module/options.go @@ -20,8 +20,8 @@ package module import ( "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Option specifies some optional arguments used for configuring the behavior @@ -76,7 +76,7 @@ func WithServiceName() Option { return } if event.RootFields == nil { - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} } else if current, err := event.RootFields.GetValue("service.name"); err == nil && current != "" { // Already set by the metricset, don't overwrite return diff --git a/metricbeat/mb/module/wrapper.go b/metricbeat/mb/module/wrapper.go index c3d279d2859..47f39c6dac8 100644 --- a/metricbeat/mb/module/wrapper.go +++ b/metricbeat/mb/module/wrapper.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/monitoring" "github.com/elastic/beats/v7/libbeat/testing" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Expvar metric names. @@ -335,10 +336,10 @@ type reporterV1 struct { module string } -func (r reporterV1) Done() <-chan struct{} { return r.v2.Done() } -func (r reporterV1) Event(event common.MapStr) bool { return r.ErrorWith(nil, event) } -func (r reporterV1) Error(err error) bool { return r.ErrorWith(err, nil) } -func (r reporterV1) ErrorWith(err error, meta common.MapStr) bool { +func (r reporterV1) Done() <-chan struct{} { return r.v2.Done() } +func (r reporterV1) Event(event mapstr.M) bool { return r.ErrorWith(nil, event) } +func (r reporterV1) Error(err error) bool { return r.ErrorWith(err, nil) } +func (r reporterV1) ErrorWith(err error, meta mapstr.M) bool { // Skip nil events without error if err == nil && meta == nil { return true diff --git a/metricbeat/mb/module/wrapper_test.go b/metricbeat/mb/module/wrapper_test.go index 130ddb9e249..a8bfc49e895 100644 --- a/metricbeat/mb/module/wrapper_test.go +++ b/metricbeat/mb/module/wrapper_test.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/module" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -53,7 +54,7 @@ type fakeReportingFetcher struct { func (ms *fakeReportingFetcher) Fetch(r mb.Reporter) { t, _ := time.Parse(time.RFC3339, "2016-05-10T23:27:58.485Z") - r.Event(common.MapStr{"@timestamp": common.Time(t), "metric": 1}) + r.Event(mapstr.M{"@timestamp": common.Time(t), "metric": 1}) } func newFakeReportingFetcher(base mb.BaseMetricSet) (mb.MetricSet, error) { @@ -69,7 +70,7 @@ type fakePushMetricSet struct { func (ms *fakePushMetricSet) Run(r mb.PushReporter) { t, _ := time.Parse(time.RFC3339, "2016-05-10T23:27:58.485Z") - event := common.MapStr{"@timestamp": common.Time(t), "metric": 1} + event := mapstr.M{"@timestamp": common.Time(t), "metric": 1} r.Event(event) <-r.Done() } diff --git a/metricbeat/mb/testing/data_generator.go b/metricbeat/mb/testing/data_generator.go index abad9d5e205..00dce1ea7f0 100644 --- a/metricbeat/mb/testing/data_generator.go +++ b/metricbeat/mb/testing/data_generator.go @@ -27,9 +27,9 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/testing/flags" + "github.com/elastic/elastic-agent-libs/mapstr" ) // WriteEventsReporterV2 fetches events and writes the first event to a ./_meta/data.json @@ -52,7 +52,7 @@ func WriteEventsReporterV2WithContext(f mb.ReportingMetricSetV2WithContext, t te // WriteEventsReporterV2Cond fetches events and writes the first event that matches // the condition to a file. -func WriteEventsReporterV2Cond(f mb.ReportingMetricSetV2, t testing.TB, path string, cond func(common.MapStr) bool) error { +func WriteEventsReporterV2Cond(f mb.ReportingMetricSetV2, t testing.TB, path string, cond func(mapstr.M) bool) error { if !*flags.DataFlag { t.Skip("skip data generation tests") } @@ -67,7 +67,7 @@ func WriteEventsReporterV2Cond(f mb.ReportingMetricSetV2, t testing.TB, path str // WriteEventsReporterV2ErrorCond fetches events and writes the first event that matches // the condition to a file. -func WriteEventsReporterV2ErrorCond(f mb.ReportingMetricSetV2Error, t testing.TB, path string, cond func(common.MapStr) bool) error { +func WriteEventsReporterV2ErrorCond(f mb.ReportingMetricSetV2Error, t testing.TB, path string, cond func(mapstr.M) bool) error { if !*flags.DataFlag { t.Skip("skip data generation tests") } @@ -82,7 +82,7 @@ func WriteEventsReporterV2ErrorCond(f mb.ReportingMetricSetV2Error, t testing.TB // WriteEventsReporterV2WithContextCond fetches events and writes the first event that matches // the condition to a file. -func WriteEventsReporterV2WithContextCond(f mb.ReportingMetricSetV2WithContext, t testing.TB, path string, cond func(common.MapStr) bool) error { +func WriteEventsReporterV2WithContextCond(f mb.ReportingMetricSetV2WithContext, t testing.TB, path string, cond func(mapstr.M) bool) error { if !*flags.DataFlag { t.Skip("skip data generation tests") } @@ -95,7 +95,7 @@ func WriteEventsReporterV2WithContextCond(f mb.ReportingMetricSetV2WithContext, return writeEvent(events, f, t, path, cond) } -func writeEvent(events []mb.Event, f mb.MetricSet, t testing.TB, path string, cond func(common.MapStr) bool) error { +func writeEvent(events []mb.Event, f mb.MetricSet, t testing.TB, path string, cond func(mapstr.M) bool) error { if len(events) == 0 { return fmt.Errorf("no events were generated") } @@ -114,7 +114,7 @@ func writeEvent(events []mb.Event, f mb.MetricSet, t testing.TB, path string, co // CreateFullEvent builds a full event given the data generated by a MetricSet. // This simulates the output of Metricbeat as if it were // 2016-05-23T08:05:34.853Z and the hostname is host.example.com. -func CreateFullEvent(ms mb.MetricSet, metricSetData common.MapStr) beat.Event { +func CreateFullEvent(ms mb.MetricSet, metricSetData mapstr.M) beat.Event { return StandardizeEvent( ms, mb.TransformMapStrToEvent(ms.Module().Name(), metricSetData, nil), @@ -190,7 +190,7 @@ func WriteEventToDataJSON(t testing.TB, fullEvent beat.Event, postfixPath string } // SelectEvent selects the first event that matches an specific condition -func SelectEvent(events []common.MapStr, cond func(e common.MapStr) bool) (common.MapStr, error) { +func SelectEvent(events []mapstr.M, cond func(e mapstr.M) bool) (mapstr.M, error) { if cond == nil && len(events) > 0 { return events[0], nil } @@ -203,7 +203,7 @@ func SelectEvent(events []common.MapStr, cond func(e common.MapStr) bool) (commo } // SelectEventV2 selects the first event that matches an specific condition -func SelectEventV2(f mb.MetricSet, events []mb.Event, cond func(e common.MapStr) bool) (mb.Event, error) { +func SelectEventV2(f mb.MetricSet, events []mb.Event, cond func(e mapstr.M) bool) (mb.Event, error) { if cond == nil && len(events) > 0 { return events[0], nil } diff --git a/metricbeat/mb/testing/fetcher.go b/metricbeat/mb/testing/fetcher.go index aa2a427d80e..f02dc8d8dd7 100644 --- a/metricbeat/mb/testing/fetcher.go +++ b/metricbeat/mb/testing/fetcher.go @@ -21,8 +21,8 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Fetcher is an interface implemented by all fetchers for testing purpouses @@ -32,7 +32,7 @@ type Fetcher interface { FetchEvents() ([]mb.Event, []error) WriteEvents(testing.TB, string) - WriteEventsCond(testing.TB, string, func(common.MapStr) bool) + WriteEventsCond(testing.TB, string, func(mapstr.M) bool) StandardizeEvent(mb.Event, ...mb.EventModifier) beat.Event } @@ -68,7 +68,7 @@ func (f *reportingMetricSetV2Fetcher) WriteEvents(t testing.TB, path string) { f.WriteEventsCond(t, path, nil) } -func (f *reportingMetricSetV2Fetcher) WriteEventsCond(t testing.TB, path string, cond func(common.MapStr) bool) { +func (f *reportingMetricSetV2Fetcher) WriteEventsCond(t testing.TB, path string, cond func(mapstr.M) bool) { err := WriteEventsReporterV2Cond(f, t, path, cond) if err != nil { t.Fatal("writing events", err) @@ -95,7 +95,7 @@ func (f *reportingMetricSetV2FetcherError) WriteEvents(t testing.TB, path string f.WriteEventsCond(t, path, nil) } -func (f *reportingMetricSetV2FetcherError) WriteEventsCond(t testing.TB, path string, cond func(common.MapStr) bool) { +func (f *reportingMetricSetV2FetcherError) WriteEventsCond(t testing.TB, path string, cond func(mapstr.M) bool) { t.Helper() err := WriteEventsReporterV2ErrorCond(f, t, path, cond) @@ -124,7 +124,7 @@ func (f *reportingMetricSetV2FetcherWithContext) WriteEvents(t testing.TB, path f.WriteEventsCond(t, path, nil) } -func (f *reportingMetricSetV2FetcherWithContext) WriteEventsCond(t testing.TB, path string, cond func(common.MapStr) bool) { +func (f *reportingMetricSetV2FetcherWithContext) WriteEventsCond(t testing.TB, path string, cond func(mapstr.M) bool) { err := WriteEventsReporterV2WithContextCond(f, t, path, cond) if err != nil { t.Fatal("writing events", err) diff --git a/metricbeat/mb/testing/lightmodules_test.go b/metricbeat/mb/testing/lightmodules_test.go index b07cedefc63..642924e2c46 100644 --- a/metricbeat/mb/testing/lightmodules_test.go +++ b/metricbeat/mb/testing/lightmodules_test.go @@ -33,8 +33,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" // Processor in the light module _ "github.com/elastic/beats/v7/libbeat/processors/actions" @@ -67,18 +67,18 @@ func TestFetchLightModuleWithProcessors(t *testing.T) { assert.Empty(t, errs) assert.NotEmpty(t, events) - expected := common.MapStr{ - "http": common.MapStr{ - "test": common.MapStr{ + expected := mapstr.M{ + "http": mapstr.M{ + "test": mapstr.M{ "foo": "bar", }, }, - "service": common.MapStr{ + "service": mapstr.M{ "type": "test", }, // From the processor in the light module - "fields": common.MapStr{ + "fields": mapstr.M{ "test": "fromprocessor", }, } diff --git a/metricbeat/mb/testing/modules.go b/metricbeat/mb/testing/modules.go index c5dd3ecbcdd..293db11b20e 100644 --- a/metricbeat/mb/testing/modules.go +++ b/metricbeat/mb/testing/modules.go @@ -63,6 +63,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type TestModule struct { @@ -132,7 +133,7 @@ func NewReportingMetricSet(t testing.TB, config interface{}) mb.ReportingMetricS // ReportingFetch runs the given reporting metricset and returns all of the // events and errors that occur during that period. -func ReportingFetch(metricSet mb.ReportingMetricSet) ([]common.MapStr, []error) { +func ReportingFetch(metricSet mb.ReportingMetricSet) ([]mapstr.M, []error) { r := &capturingReporter{} metricSet.Fetch(r) return r.events, r.errs @@ -266,17 +267,17 @@ func NewPushMetricSet(t testing.TB, config interface{}) mb.PushMetricSet { } type capturingReporter struct { - events []common.MapStr + events []mapstr.M errs []error done chan struct{} } -func (r *capturingReporter) Event(event common.MapStr) bool { +func (r *capturingReporter) Event(event mapstr.M) bool { r.events = append(r.events, event) return true } -func (r *capturingReporter) ErrorWith(err error, meta common.MapStr) bool { +func (r *capturingReporter) ErrorWith(err error, meta mapstr.M) bool { r.events = append(r.events, meta) r.errs = append(r.errs, err) return true @@ -293,7 +294,7 @@ func (r *capturingReporter) Done() <-chan struct{} { // RunPushMetricSet run the given push metricset for the specific amount of time // and returns all of the events and errors that occur during that period. -func RunPushMetricSet(duration time.Duration, metricSet mb.PushMetricSet) ([]common.MapStr, []error) { +func RunPushMetricSet(duration time.Duration, metricSet mb.PushMetricSet) ([]mapstr.M, []error) { r := &capturingReporter{done: make(chan struct{})} // Run the metricset. diff --git a/metricbeat/mb/testing/testdata.go b/metricbeat/mb/testing/testdata.go index e5b853d570e..c05c7333f65 100644 --- a/metricbeat/mb/testing/testdata.go +++ b/metricbeat/mb/testing/testdata.go @@ -33,10 +33,10 @@ import ( "gopkg.in/yaml.v2" "github.com/elastic/beats/v7/libbeat/asset" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/mapping" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/testing/flags" + "github.com/elastic/elastic-agent-libs/mapstr" _ "github.com/elastic/beats/v7/metricbeat/include/fields" ) @@ -178,7 +178,7 @@ func TestDataFilesWithConfig(t *testing.T, module, metricSet string, config Data // TestMetricsetFieldsDocumented checks metricset fields are documented from metricsets that cannot run `TestDataFiles` test which contains this check func TestMetricsetFieldsDocumented(t *testing.T, metricSet mb.MetricSet, events []mb.Event) { - var data []common.MapStr + var data []mapstr.M for _, e := range events { beatEvent := StandardizeEvent(metricSet, e, mb.AddMetricSetInfo) data = append(data, beatEvent.Fields) @@ -220,7 +220,7 @@ func runTest(t *testing.T, file string, module, metricSetName string, config Dat events = append(events, mb.Event{Error: e}) } - var data []common.MapStr + var data []mapstr.M for _, e := range events { beatEvent := StandardizeEvent(metricSet, e, mb.AddMetricSetInfo) @@ -259,7 +259,7 @@ func runTest(t *testing.T, file string, module, metricSetName string, config Dat t.Fatalf("could not read file: %s", err) } - expectedMap := []common.MapStr{} + expectedMap := []mapstr.M{} if err := json.Unmarshal(expected, &expectedMap); err != nil { t.Fatal(err) } @@ -311,7 +311,7 @@ func runTest(t *testing.T, file string, module, metricSetName string, config Dat } } -func writeDataJSON(t *testing.T, data common.MapStr, path string) { +func writeDataJSON(t *testing.T, data mapstr.M, path string) { // Add hardcoded timestamp data.Put("@timestamp", "2019-03-01T08:05:34.853Z") output, err := json.MarshalIndent(&data, "", " ") @@ -321,7 +321,7 @@ func writeDataJSON(t *testing.T, data common.MapStr, path string) { } // checkDocumented checks that all fields which show up in the events are documented -func checkDocumented(data []common.MapStr, omitFields []string) error { +func checkDocumented(data []mapstr.M, omitFields []string) error { fieldsData, err := asset.GetFields("metricbeat") if err != nil { return err @@ -348,7 +348,7 @@ func checkDocumented(data []common.MapStr, omitFields []string) error { return nil } -func documentedFieldCheck(foundKeys common.MapStr, knownKeys map[string]interface{}, omitFields []string) error { +func documentedFieldCheck(foundKeys mapstr.M, knownKeys map[string]interface{}, omitFields []string) error { // Sort all found keys to guarantee consistent validation messages sortedFoundKeys := make([]string, 0, len(foundKeys)) for k := range foundKeys { diff --git a/metricbeat/mb/testing/testdata_test.go b/metricbeat/mb/testing/testdata_test.go index 442b97ba0f4..9d71ef19ba7 100644 --- a/metricbeat/mb/testing/testdata_test.go +++ b/metricbeat/mb/testing/testdata_test.go @@ -20,9 +20,9 @@ package testing import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/stretchr/testify/assert" + + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestOmitDocumentedField(t *testing.T) { @@ -44,7 +44,7 @@ func TestOmitDocumentedField(t *testing.T) { } func TestDocumentedFieldCheck(t *testing.T) { - foundKeys := common.MapStr{ + foundKeys := mapstr.M{ "hello": "hello", "elasticsearch.stats": "stats1", } @@ -58,7 +58,7 @@ func TestDocumentedFieldCheck(t *testing.T) { //error should be nil, as `hello` field is ignored and `elasticsearch.stats` field is defined assert.NoError(t, err) - foundKeys = common.MapStr{ + foundKeys = mapstr.M{ "elasticsearch.stats.cpu": "stats2", "elasticsearch.metrics.requests.count": "requests2", } @@ -71,7 +71,7 @@ func TestDocumentedFieldCheck(t *testing.T) { err = documentedFieldCheck(foundKeys, knownKeys, omitfields) assert.NoError(t, err) - foundKeys = common.MapStr{ + foundKeys = mapstr.M{ "elasticsearch.stats.cpu": "stats2", "elasticsearch.metrics.requests.count": "requests2", } diff --git a/metricbeat/module/aerospike/namespace/namespace.go b/metricbeat/module/aerospike/namespace/namespace.go index 2ac8a632626..1f421a1cc7b 100644 --- a/metricbeat/module/aerospike/namespace/namespace.go +++ b/metricbeat/module/aerospike/namespace/namespace.go @@ -23,9 +23,9 @@ import ( as "github.com/aerospike/aerospike-client-go" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/aerospike" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry. @@ -90,7 +90,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { data, _ := schema.Apply(aerospike.ParseInfo(info["namespace/"+namespace])) data["name"] = namespace - data["node"] = common.MapStr{ + data["node"] = mapstr.M{ "host": node.GetHost().String(), "name": node.GetName(), } diff --git a/metricbeat/module/apache/status/data.go b/metricbeat/module/apache/status/data.go index fdfab72e0b9..a1c8e0e6663 100644 --- a/metricbeat/module/apache/status/data.go +++ b/metricbeat/module/apache/status/data.go @@ -22,9 +22,9 @@ import ( "regexp" "strings" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -97,7 +97,7 @@ var ( } ) -func applySchema(event common.MapStr, fullEvent map[string]interface{}) error { +func applySchema(event mapstr.M, fullEvent map[string]interface{}) error { applicableSchema := schema if _, found := fullEvent["ServerUptimeSeconds"]; !found { applicableSchema = schemaOld @@ -107,7 +107,7 @@ func applySchema(event common.MapStr, fullEvent map[string]interface{}) error { } // Map body to MapStr -func eventMapping(scanner *bufio.Scanner, hostname string) (common.MapStr, error) { +func eventMapping(scanner *bufio.Scanner, hostname string) (mapstr.M, error) { var ( totalS int totalR int @@ -176,9 +176,9 @@ func eventMapping(scanner *bufio.Scanner, hostname string) (common.MapStr, error } } - event := common.MapStr{ + event := mapstr.M{ "hostname": hostname, - "scoreboard": common.MapStr{ + "scoreboard": mapstr.M{ "starting_up": totalS, "reading_request": totalR, "sending_reply": totalW, diff --git a/metricbeat/module/apache/status/status_integration_test.go b/metricbeat/module/apache/status/status_integration_test.go index 4a6ac7245ef..0c9c1451975 100644 --- a/metricbeat/module/apache/status/status_integration_test.go +++ b/metricbeat/module/apache/status/status_integration_test.go @@ -25,9 +25,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -70,14 +70,14 @@ func TestFetchFleetMode(t *testing.T) { } _, err := event.MetricSetFields.GetValue("total_kbytes") - assert.Equal(t, common.ErrKeyNotFound, err, "apache.status.total_kbytes shouldn't be present in the fleet mode") + assert.Equal(t, mapstr.ErrKeyNotFound, err, "apache.status.total_kbytes shouldn't be present in the fleet mode") totalBytes, err := event.MetricSetFields.GetValue("total_bytes") assert.NoError(t, err, "apache.status.total_bytes should be present in the fleet mode") assert.GreaterOrEqual(t, totalBytes.(int64), int64(0), "apache.status.total_bytes should be non-negative") _, err = event.MetricSetFields.GetValue("hostname") - assert.Equal(t, common.ErrKeyNotFound, err, "apache.status.hostname shouldn't be present in the fleet mode") + assert.Equal(t, mapstr.ErrKeyNotFound, err, "apache.status.hostname shouldn't be present in the fleet mode") } func getConfig(host string) map[string]interface{} { diff --git a/metricbeat/module/apache/status/status_test.go b/metricbeat/module/apache/status/status_test.go index 41eef7d2690..eedc176992f 100644 --- a/metricbeat/module/apache/status/status_test.go +++ b/metricbeat/module/apache/status/status_test.go @@ -32,8 +32,8 @@ import ( "testing" "time" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" @@ -102,18 +102,18 @@ func TestFetchEventContents(t *testing.T) { assert.Equal(t, 386.299, event["bytes_per_request"]) assert.Equal(t, .0628293, event["bytes_per_sec"]) - workers := event["workers"].(common.MapStr) + workers := event["workers"].(mapstr.M) assert.EqualValues(t, 1, workers["busy"]) assert.EqualValues(t, 99, workers["idle"]) - connections := event["connections"].(common.MapStr) - async := connections["async"].(common.MapStr) + connections := event["connections"].(mapstr.M) + async := connections["async"].(mapstr.M) assert.EqualValues(t, 3, async["closing"]) assert.EqualValues(t, 2, async["keep_alive"]) assert.EqualValues(t, 1, async["writing"]) assert.EqualValues(t, 6, connections["total"]) - cpu := event["cpu"].(common.MapStr) + cpu := event["cpu"].(mapstr.M) assert.Equal(t, 11.2, cpu["children_system"]) assert.Equal(t, 10.1, cpu["children_user"]) assert.Equal(t, 2.02841, cpu["load"]) @@ -122,14 +122,14 @@ func TestFetchEventContents(t *testing.T) { assert.Equal(t, server.URL[7:], event["hostname"]) - load := event["load"].(common.MapStr) + load := event["load"].(mapstr.M) assert.Equal(t, .02, load["1"]) assert.Equal(t, .05, load["15"]) assert.Equal(t, .01, load["5"]) assert.Equal(t, .000162644, event["requests_per_sec"]) - scoreboard := event["scoreboard"].(common.MapStr) + scoreboard := event["scoreboard"].(mapstr.M) assert.Equal(t, 0, scoreboard["closing_connection"]) assert.Equal(t, 0, scoreboard["dns_lookup"]) assert.Equal(t, 0, scoreboard["gracefully_finishing"]) @@ -145,7 +145,7 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, 167, event["total_accesses"]) assert.EqualValues(t, 63, event["total_kbytes"]) - uptime := event["uptime"].(common.MapStr) + uptime := event["uptime"].(mapstr.M) assert.EqualValues(t, 1026782, uptime["uptime"]) assert.EqualValues(t, 1026782, uptime["server_uptime"]) } diff --git a/metricbeat/module/beat/state/data.go b/metricbeat/module/beat/state/data.go index d22830e12ba..919da1b3f3e 100644 --- a/metricbeat/module/beat/state/data.go +++ b/metricbeat/module/beat/state/data.go @@ -21,8 +21,7 @@ import ( "encoding/json" "github.com/elastic/beats/v7/metricbeat/helper/elastic" - - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" @@ -69,8 +68,8 @@ var ( func eventMapping(r mb.ReporterV2, info beat.Info, content []byte, isXpack bool) error { event := mb.Event{ - RootFields: common.MapStr{}, - ModuleFields: common.MapStr{}, + RootFields: mapstr.M{}, + ModuleFields: mapstr.M{}, } var data map[string]interface{} @@ -99,7 +98,7 @@ func eventMapping(r mb.ReporterV2, info beat.Info, content []byte, isXpack bool) if event.MetricSetFields != nil { event.MetricSetFields.Put("cluster.uuid", clusterUUID) - event.MetricSetFields.Put("beat", common.MapStr{ + event.MetricSetFields.Put("beat", mapstr.M{ "name": info.Name, "host": info.Hostname, "type": info.Beat, @@ -111,7 +110,7 @@ func eventMapping(r mb.ReporterV2, info beat.Info, content []byte, isXpack bool) //Extract ECS fields from the host key host, ok := event.MetricSetFields["host"] if ok { - hostMap, ok := host.(common.MapStr) + hostMap, ok := host.(mapstr.M) if ok { arch, ok := hostMap["architecture"] if ok { diff --git a/metricbeat/module/beat/stats/data.go b/metricbeat/module/beat/stats/data.go index 096dc028ba7..24a430aff59 100644 --- a/metricbeat/module/beat/stats/data.go +++ b/metricbeat/module/beat/stats/data.go @@ -21,10 +21,10 @@ import ( "encoding/json" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -113,9 +113,9 @@ var ( func eventMapping(r mb.ReporterV2, info beat.Info, clusterUUID string, content []byte, isXpack bool) error { event := mb.Event{ - RootFields: common.MapStr{}, - ModuleFields: common.MapStr{}, - MetricSetFields: common.MapStr{}, + RootFields: mapstr.M{}, + ModuleFields: mapstr.M{}, + MetricSetFields: mapstr.M{}, } event.RootFields.Put("service.name", beat.ModuleName) @@ -133,7 +133,7 @@ func eventMapping(r mb.ReporterV2, info beat.Info, clusterUUID string, content [ } event.MetricSetFields, _ = schema.Apply(data) - event.MetricSetFields.Put("beat", common.MapStr{ + event.MetricSetFields.Put("beat", mapstr.M{ "name": info.Name, "host": info.Hostname, "type": info.Beat, diff --git a/metricbeat/module/ceph/cluster_disk/cluster_disk_test.go b/metricbeat/module/ceph/cluster_disk/cluster_disk_test.go index b6aa3b97bde..3848c2b7024 100644 --- a/metricbeat/module/ceph/cluster_disk/cluster_disk_test.go +++ b/metricbeat/module/ceph/cluster_disk/cluster_disk_test.go @@ -24,8 +24,8 @@ import ( "path/filepath" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -60,12 +60,12 @@ func TestFetchEventContents(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint()) - used := event["used"].(common.MapStr) + used := event["used"].(mapstr.M) assert.EqualValues(t, 1428520960, used["bytes"]) - total := event["total"].(common.MapStr) + total := event["total"].(mapstr.M) assert.EqualValues(t, uint64(6431965184), total["bytes"]) - available := event["available"].(common.MapStr) + available := event["available"].(mapstr.M) assert.EqualValues(t, uint64(5003444224), available["bytes"]) } diff --git a/metricbeat/module/ceph/cluster_disk/data.go b/metricbeat/module/ceph/cluster_disk/data.go index b75daa31b0e..f89d3017575 100644 --- a/metricbeat/module/ceph/cluster_disk/data.go +++ b/metricbeat/module/ceph/cluster_disk/data.go @@ -22,7 +22,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type StatsCluster struct { @@ -40,21 +40,21 @@ type DfRequest struct { Output Output `json:"output"` } -func eventMapping(content []byte) (common.MapStr, error) { +func eventMapping(content []byte) (mapstr.M, error) { var d DfRequest err := json.Unmarshal(content, &d) if err != nil { return nil, errors.Wrap(err, "could not get DFRequest data") } - return common.MapStr{ - "used": common.MapStr{ + return mapstr.M{ + "used": mapstr.M{ "bytes": d.Output.StatsCluster.TotalUsedBytes, }, - "total": common.MapStr{ + "total": mapstr.M{ "bytes": d.Output.StatsCluster.TotalBytes, }, - "available": common.MapStr{ + "available": mapstr.M{ "bytes": d.Output.StatsCluster.TotalAvailBytes, }, }, nil diff --git a/metricbeat/module/ceph/cluster_health/cluster_health_test.go b/metricbeat/module/ceph/cluster_health/cluster_health_test.go index e5514e94682..73f0dadc727 100644 --- a/metricbeat/module/ceph/cluster_health/cluster_health_test.go +++ b/metricbeat/module/ceph/cluster_health/cluster_health_test.go @@ -24,8 +24,8 @@ import ( "path/filepath" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -62,10 +62,10 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, "HEALTH_OK", event["overall_status"]) - timechecks := event["timechecks"].(common.MapStr) + timechecks := event["timechecks"].(mapstr.M) assert.EqualValues(t, 3, timechecks["epoch"]) - round := timechecks["round"].(common.MapStr) + round := timechecks["round"].(mapstr.M) assert.EqualValues(t, 0, round["value"]) assert.EqualValues(t, "finished", round["status"]) } diff --git a/metricbeat/module/ceph/cluster_health/data.go b/metricbeat/module/ceph/cluster_health/data.go index bd94c44be2e..6144b146825 100644 --- a/metricbeat/module/ceph/cluster_health/data.go +++ b/metricbeat/module/ceph/cluster_health/data.go @@ -22,7 +22,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Timecheck contains part of the response from a HealthRequest @@ -44,18 +44,18 @@ type HealthRequest struct { Output Output `json:"output"` } -func eventMapping(content []byte) (common.MapStr, error) { +func eventMapping(content []byte) (mapstr.M, error) { var d HealthRequest err := json.Unmarshal(content, &d) if err != nil { return nil, errors.Wrap(err, "error getting HealthRequest data") } - return common.MapStr{ + return mapstr.M{ "overall_status": d.Output.OverallStatus, - "timechecks": common.MapStr{ + "timechecks": mapstr.M{ "epoch": d.Output.Timechecks.Epoch, - "round": common.MapStr{ + "round": mapstr.M{ "value": d.Output.Timechecks.Round, "status": d.Output.Timechecks.RoundStatus, }, diff --git a/metricbeat/module/ceph/cluster_status/cluster_status_test.go b/metricbeat/module/ceph/cluster_status/cluster_status_test.go index c0c3858afd9..38b4968c498 100644 --- a/metricbeat/module/ceph/cluster_status/cluster_status_test.go +++ b/metricbeat/module/ceph/cluster_status/cluster_status_test.go @@ -24,8 +24,8 @@ import ( "path/filepath" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -64,7 +64,7 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, 813, event["version"]) //check osd info - osdmap := event["osd"].(common.MapStr) + osdmap := event["osd"].(mapstr.M) assert.EqualValues(t, false, osdmap["full"]) assert.EqualValues(t, false, osdmap["nearfull"]) assert.EqualValues(t, 6, osdmap["osd_count"]) @@ -74,48 +74,48 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, 264, osdmap["epoch"]) //check traffic info - trafficInfo := event["traffic"].(common.MapStr) + trafficInfo := event["traffic"].(mapstr.M) assert.EqualValues(t, 55667788, trafficInfo["read_bytes"]) assert.EqualValues(t, 1234, trafficInfo["read_op_per_sec"]) assert.EqualValues(t, 11996158, trafficInfo["write_bytes"]) assert.EqualValues(t, 10, trafficInfo["write_op_per_sec"]) //check misplace info - misplaceInfo := event["misplace"].(common.MapStr) + misplaceInfo := event["misplace"].(mapstr.M) assert.EqualValues(t, 768, misplaceInfo["total"]) assert.EqualValues(t, 88, misplaceInfo["objects"]) assert.EqualValues(t, 0.114583, misplaceInfo["pct"]) //check degraded info - degradedInfo := event["degraded"].(common.MapStr) + degradedInfo := event["degraded"].(mapstr.M) assert.EqualValues(t, 768, degradedInfo["total"]) assert.EqualValues(t, 294, degradedInfo["objects"]) assert.EqualValues(t, 0.382812, degradedInfo["pct"]) //check pg info - pgInfo := event["pg"].(common.MapStr) + pgInfo := event["pg"].(mapstr.M) assert.EqualValues(t, 1054023794, pgInfo["data_bytes"]) assert.EqualValues(t, int64(9965821952), pgInfo["avail_bytes"]) assert.EqualValues(t, int64(12838682624), pgInfo["total_bytes"]) assert.EqualValues(t, int64(2872860672), pgInfo["used_bytes"]) //check pg_state info - pgStateInfo := events[1].MetricSetFields["pg_state"].(common.MapStr) + pgStateInfo := events[1].MetricSetFields["pg_state"].(mapstr.M) assert.EqualValues(t, "active+undersized+degraded", pgStateInfo["state_name"]) assert.EqualValues(t, 109, pgStateInfo["count"]) assert.EqualValues(t, 813, pgStateInfo["version"]) - pgStateInfo = events[2].MetricSetFields["pg_state"].(common.MapStr) + pgStateInfo = events[2].MetricSetFields["pg_state"].(mapstr.M) assert.EqualValues(t, "undersized+degraded+peered", pgStateInfo["state_name"]) assert.EqualValues(t, 101, pgStateInfo["count"]) assert.EqualValues(t, 813, pgStateInfo["version"]) - pgStateInfo = events[3].MetricSetFields["pg_state"].(common.MapStr) + pgStateInfo = events[3].MetricSetFields["pg_state"].(mapstr.M) assert.EqualValues(t, "active+remapped", pgStateInfo["state_name"]) assert.EqualValues(t, 55, pgStateInfo["count"]) assert.EqualValues(t, 813, pgStateInfo["version"]) - pgStateInfo = events[4].MetricSetFields["pg_state"].(common.MapStr) + pgStateInfo = events[4].MetricSetFields["pg_state"].(mapstr.M) assert.EqualValues(t, "active+undersized+degraded+remapped", pgStateInfo["state_name"]) assert.EqualValues(t, 55, pgStateInfo["count"]) assert.EqualValues(t, 813, pgStateInfo["version"]) diff --git a/metricbeat/module/ceph/cluster_status/data.go b/metricbeat/module/ceph/cluster_status/data.go index c19a75b44d3..1b5f3757ea2 100644 --- a/metricbeat/module/ceph/cluster_status/data.go +++ b/metricbeat/module/ceph/cluster_status/data.go @@ -22,7 +22,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // PgState represents placement group state @@ -84,7 +84,7 @@ type HealthRequest struct { Output Output `json:"output"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var d HealthRequest err := json.Unmarshal(content, &d) if err != nil { @@ -94,7 +94,7 @@ func eventsMapping(content []byte) ([]common.MapStr, error) { //osd map info osdmap := d.Output.Osdmap.Osdmap - osdState := common.MapStr{} + osdState := mapstr.M{} osdState["epoch"] = osdmap.Epoch osdState["full"] = osdmap.Full osdState["nearfull"] = osdmap.Nearfull @@ -106,29 +106,29 @@ func eventsMapping(content []byte) ([]common.MapStr, error) { //pg map info pgmap := d.Output.Pgmap - traffic := common.MapStr{} + traffic := mapstr.M{} traffic["read_bytes"] = pgmap.ReadByteSec traffic["read_op_per_sec"] = pgmap.ReadOpSec traffic["write_bytes"] = pgmap.WriteByteSec traffic["write_op_per_sec"] = pgmap.WriteOpSec - misplace := common.MapStr{} + misplace := mapstr.M{} misplace["objects"] = pgmap.MisplacedObjs misplace["pct"] = pgmap.MisplacedRatio misplace["total"] = pgmap.MisplacedTotal - degraded := common.MapStr{} + degraded := mapstr.M{} degraded["objects"] = pgmap.DegradedObjs degraded["pct"] = pgmap.DegradedRatio degraded["total"] = pgmap.DegradedTotal - pg := common.MapStr{} + pg := mapstr.M{} pg["avail_bytes"] = pgmap.AvailByte pg["total_bytes"] = pgmap.TotalByte pg["used_bytes"] = pgmap.UsedByte pg["data_bytes"] = pgmap.DataByte - stateEvent := common.MapStr{} + stateEvent := mapstr.M{} stateEvent["osd"] = osdState stateEvent["traffic"] = traffic stateEvent["misplace"] = misplace @@ -136,17 +136,17 @@ func eventsMapping(content []byte) ([]common.MapStr, error) { stateEvent["pg"] = pg stateEvent["version"] = pgmap.Version - events := []common.MapStr{} + events := []mapstr.M{} events = append(events, stateEvent) //pg state info for _, state := range pgmap.PgStates { - stateEvn := common.MapStr{ + stateEvn := mapstr.M{ "count": state.Count, "state_name": state.StateName, "version": pgmap.Version, } - evt := common.MapStr{ + evt := mapstr.M{ "pg_state": stateEvn, } events = append(events, evt) diff --git a/metricbeat/module/ceph/mgr_cluster_disk/data.go b/metricbeat/module/ceph/mgr_cluster_disk/data.go index 8940f5030ed..e2e808cbc08 100644 --- a/metricbeat/module/ceph/mgr_cluster_disk/data.go +++ b/metricbeat/module/ceph/mgr_cluster_disk/data.go @@ -20,8 +20,8 @@ package mgr_cluster_disk import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) type DfResponse struct { @@ -32,21 +32,21 @@ type DfResponse struct { } `json:"stats"` } -func eventMapping(content []byte) (common.MapStr, error) { +func eventMapping(content []byte) (mapstr.M, error) { var response DfResponse err := mgr.UnmarshalResponse(content, &response) if err != nil { return nil, errors.Wrap(err, "could not get response data") } - return common.MapStr{ - "used": common.MapStr{ + return mapstr.M{ + "used": mapstr.M{ "bytes": response.Stats.TotalUsedBytes, }, - "total": common.MapStr{ + "total": mapstr.M{ "bytes": response.Stats.TotalBytes, }, - "available": common.MapStr{ + "available": mapstr.M{ "bytes": response.Stats.TotalAvailableBytes, }, }, nil diff --git a/metricbeat/module/ceph/mgr_cluster_disk/mgr_cluster_disk.go b/metricbeat/module/ceph/mgr_cluster_disk/mgr_cluster_disk.go index 14342f13621..b6b1fb1de3d 100644 --- a/metricbeat/module/ceph/mgr_cluster_disk/mgr_cluster_disk.go +++ b/metricbeat/module/ceph/mgr_cluster_disk/mgr_cluster_disk.go @@ -18,10 +18,10 @@ package mgr_cluster_disk import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -74,7 +74,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } reporter.Event(mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "cluster_disk": event, }}) return nil diff --git a/metricbeat/module/ceph/mgr_cluster_health/data.go b/metricbeat/module/ceph/mgr_cluster_health/data.go index b81fad378ca..ad4116a6296 100644 --- a/metricbeat/module/ceph/mgr_cluster_health/data.go +++ b/metricbeat/module/ceph/mgr_cluster_health/data.go @@ -20,8 +20,8 @@ package mgr_cluster_health import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) type StatusResponse struct { @@ -38,7 +38,7 @@ type TimeSyncStatusResponse struct { } `json:"timechecks"` } -func eventMapping(statusContent, timeSyncStatusContent []byte) (common.MapStr, error) { +func eventMapping(statusContent, timeSyncStatusContent []byte) (mapstr.M, error) { var statusResponse StatusResponse err := mgr.UnmarshalResponse(statusContent, &statusResponse) if err != nil { @@ -51,11 +51,11 @@ func eventMapping(statusContent, timeSyncStatusContent []byte) (common.MapStr, e return nil, errors.Wrap(err, "could not unmarshal response") } - return common.MapStr{ + return mapstr.M{ "overall_status": statusResponse.Health.Status, - "timechecks": common.MapStr{ + "timechecks": mapstr.M{ "epoch": timeSyncStatusResponse.Timechecks.Epoch, - "round": common.MapStr{ + "round": mapstr.M{ "value": timeSyncStatusResponse.Timechecks.Round, "status": timeSyncStatusResponse.Timechecks.RoundStatus, }, diff --git a/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health.go b/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health.go index fa2c1f387e5..72e1ca249d3 100644 --- a/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health.go +++ b/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health.go @@ -20,10 +20,10 @@ package mgr_cluster_health import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -83,7 +83,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } reporter.Event(mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "cluster_health": event, }}) return nil diff --git a/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health_test.go b/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health_test.go index 3aa12fe454c..9da0c6f276d 100644 --- a/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health_test.go +++ b/metricbeat/module/ceph/mgr_cluster_health/mgr_cluster_health_test.go @@ -29,8 +29,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) type clientRequest struct { @@ -75,16 +75,16 @@ func TestFetchEventContents(t *testing.T) { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) } assert.NotEmpty(t, events) - event := events[0].ModuleFields["cluster_health"].(common.MapStr) + event := events[0].ModuleFields["cluster_health"].(mapstr.M) t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint()) assert.EqualValues(t, "HEALTH_OK", event["overall_status"]) - timechecks := event["timechecks"].(common.MapStr) + timechecks := event["timechecks"].(mapstr.M) assert.EqualValues(t, 3, timechecks["epoch"]) - round := timechecks["round"].(common.MapStr) + round := timechecks["round"].(mapstr.M) assert.EqualValues(t, 0, round["value"]) assert.EqualValues(t, "finished", round["status"]) } diff --git a/metricbeat/module/ceph/mgr_osd_perf/data.go b/metricbeat/module/ceph/mgr_osd_perf/data.go index 562515e7b7c..20046a38df3 100644 --- a/metricbeat/module/ceph/mgr_osd_perf/data.go +++ b/metricbeat/module/ceph/mgr_osd_perf/data.go @@ -20,8 +20,8 @@ package mgr_osd_perf import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) type OsdPerfResponse struct { @@ -38,18 +38,18 @@ type OsdPerfResponse struct { } `json:"osdstats"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var response OsdPerfResponse err := mgr.UnmarshalResponse(content, &response) if err != nil { return nil, errors.Wrap(err, "could not get response data") } - var events []common.MapStr + var events []mapstr.M for _, OsdPerfInfo := range response.OsdStats.OsdPerfInfos { - event := common.MapStr{ + event := mapstr.M{ "id": OsdPerfInfo.ID, - "stats": common.MapStr{ + "stats": mapstr.M{ "commit_latency_ms": OsdPerfInfo.PerfStats.CommitLatencyMs, "apply_latency_ms": OsdPerfInfo.PerfStats.ApplyLatencyMs, "commit_latency_ns": OsdPerfInfo.PerfStats.CommitLatencyNs, diff --git a/metricbeat/module/ceph/mgr_osd_pool_stats/data.go b/metricbeat/module/ceph/mgr_osd_pool_stats/data.go index a4990384ef7..28c62d14789 100644 --- a/metricbeat/module/ceph/mgr_osd_pool_stats/data.go +++ b/metricbeat/module/ceph/mgr_osd_pool_stats/data.go @@ -20,8 +20,8 @@ package mgr_osd_pool_stats import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) type OsdPoolStat struct { @@ -35,19 +35,19 @@ type OsdPoolStat struct { } `json:"client_io_rate"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var response []OsdPoolStat err := mgr.UnmarshalResponse(content, &response) if err != nil { return nil, errors.Wrap(err, "could not get response data") } - var events []common.MapStr + var events []mapstr.M for _, stat := range response { - event := common.MapStr{ + event := mapstr.M{ "pool_id": stat.PoolID, "pool_name": stat.PoolName, - "client_io_rate": common.MapStr{ + "client_io_rate": mapstr.M{ "read_bytes_sec": stat.ClientIORate.ReadBytesSec, "write_bytes_sec": stat.ClientIORate.WriteBytesSec, "read_op_per_sec": stat.ClientIORate.ReadOpPerSec, diff --git a/metricbeat/module/ceph/mgr_osd_tree/data.go b/metricbeat/module/ceph/mgr_osd_tree/data.go index 3f3d1934c2e..fc1988e4092 100644 --- a/metricbeat/module/ceph/mgr_osd_tree/data.go +++ b/metricbeat/module/ceph/mgr_osd_tree/data.go @@ -23,8 +23,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) type OsdTreeResponse struct { @@ -44,7 +44,7 @@ type OsdTreeResponse struct { } `json:"nodes"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var response OsdTreeResponse err := mgr.UnmarshalResponse(content, &response) if err != nil { @@ -72,9 +72,9 @@ func eventsMapping(content []byte) ([]common.MapStr, error) { } // OSD node list - var events []common.MapStr + var events []mapstr.M for _, node := range nodeList { - nodeInfo := common.MapStr{} + nodeInfo := mapstr.M{} if node.ID < 0 { // bucket node nodeInfo["children"] = strings.Split(childrenMap[node.Name], ",") diff --git a/metricbeat/module/ceph/mgr_osd_tree/mgr_osd_tree.go b/metricbeat/module/ceph/mgr_osd_tree/mgr_osd_tree.go index aded13c1a7a..084879539e5 100644 --- a/metricbeat/module/ceph/mgr_osd_tree/mgr_osd_tree.go +++ b/metricbeat/module/ceph/mgr_osd_tree/mgr_osd_tree.go @@ -18,10 +18,10 @@ package mgr_osd_tree import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -75,7 +75,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { for _, event := range events { reported := reporter.Event(mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "osd_tree": event, }}) if !reported { diff --git a/metricbeat/module/ceph/mgr_pool_disk/data.go b/metricbeat/module/ceph/mgr_pool_disk/data.go index 5d586a0a134..79db8e01ae6 100644 --- a/metricbeat/module/ceph/mgr_pool_disk/data.go +++ b/metricbeat/module/ceph/mgr_pool_disk/data.go @@ -20,8 +20,8 @@ package mgr_pool_disk import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) type DfResponse struct { @@ -37,24 +37,24 @@ type DfResponse struct { } `json:"pools"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var response DfResponse err := mgr.UnmarshalResponse(content, &response) if err != nil { return nil, errors.Wrap(err, "could not get response data") } - var events []common.MapStr + var events []mapstr.M for _, Pool := range response.Pools { - event := common.MapStr{ + event := mapstr.M{ "name": Pool.Name, "id": Pool.ID, - "stats": common.MapStr{ - "used": common.MapStr{ + "stats": mapstr.M{ + "used": mapstr.M{ "bytes": Pool.Stats.BytesUsed, "kb": Pool.Stats.KbUsed, }, - "available": common.MapStr{ + "available": mapstr.M{ "bytes": Pool.Stats.MaxAvail, }, "objects": Pool.Stats.Objects, diff --git a/metricbeat/module/ceph/mgr_pool_disk/mgr_pool_disk.go b/metricbeat/module/ceph/mgr_pool_disk/mgr_pool_disk.go index a3b510de915..610282c9d8b 100644 --- a/metricbeat/module/ceph/mgr_pool_disk/mgr_pool_disk.go +++ b/metricbeat/module/ceph/mgr_pool_disk/mgr_pool_disk.go @@ -18,10 +18,10 @@ package mgr_pool_disk import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/ceph/mgr" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -75,7 +75,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { for _, event := range events { reported := reporter.Event(mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "pool_disk": event, }}) if !reported { diff --git a/metricbeat/module/ceph/monitor_health/data.go b/metricbeat/module/ceph/monitor_health/data.go index a40612e339d..6986c6bd15d 100644 --- a/metricbeat/module/ceph/monitor_health/data.go +++ b/metricbeat/module/ceph/monitor_health/data.go @@ -23,7 +23,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type Tick struct { @@ -86,42 +86,42 @@ type HealthRequest struct { Output Output `json:"output"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var d HealthRequest err := json.Unmarshal(content, &d) if err != nil { return nil, errors.Wrapf(err, "could not get HealthRequest data") } - events := []common.MapStr{} + events := []mapstr.M{} for _, HealthService := range d.Output.Health.HealthServices { for _, Mon := range HealthService.Mons { - event := common.MapStr{ + event := mapstr.M{ "last_updated": Mon.LastUpdated, "name": Mon.Name, - "available": common.MapStr{ + "available": mapstr.M{ "pct": Mon.AvailPercent, "kb": Mon.KbAvail, }, - "total": common.MapStr{ + "total": mapstr.M{ "kb": Mon.KbTotal, }, "health": Mon.Health, - "used": common.MapStr{ + "used": mapstr.M{ "kb": Mon.KbUsed, }, - "store_stats": common.MapStr{ - "log": common.MapStr{ + "store_stats": mapstr.M{ + "log": mapstr.M{ "bytes": Mon.StoreStats.BytesLog, }, - "misc": common.MapStr{ + "misc": mapstr.M{ "bytes": Mon.StoreStats.BytesMisc, }, - "sst": common.MapStr{ + "sst": mapstr.M{ "bytes": Mon.StoreStats.BytesSSt, }, - "total": common.MapStr{ + "total": mapstr.M{ "bytes": Mon.StoreStats.BytesTotal, }, "last_updated": Mon.StoreStats.LastUpdated, diff --git a/metricbeat/module/ceph/monitor_health/monitor_health_test.go b/metricbeat/module/ceph/monitor_health/monitor_health_test.go index e6a464b2543..b58ad8389a0 100644 --- a/metricbeat/module/ceph/monitor_health/monitor_health_test.go +++ b/metricbeat/module/ceph/monitor_health/monitor_health_test.go @@ -24,8 +24,8 @@ import ( "path/filepath" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" @@ -65,29 +65,29 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, "ceph", mon["name"]) assert.EqualValues(t, "2017-01-19 11:34:50.700723 +0000 UTC", mon["last_updated"].(Tick).Time.String()) - available := mon["available"].(common.MapStr) + available := mon["available"].(mapstr.M) assert.EqualValues(t, 4091244, available["kb"]) assert.EqualValues(t, 65, available["pct"]) - total := mon["total"].(common.MapStr) + total := mon["total"].(mapstr.M) assert.EqualValues(t, 6281216, total["kb"]) - used := mon["used"].(common.MapStr) + used := mon["used"].(mapstr.M) assert.EqualValues(t, 2189972, used["kb"]) - store_stats := mon["store_stats"].(common.MapStr) + store_stats := mon["store_stats"].(mapstr.M) assert.EqualValues(t, "0.000000", store_stats["last_updated"]) - misc := store_stats["misc"].(common.MapStr) + misc := store_stats["misc"].(mapstr.M) assert.EqualValues(t, 840, misc["bytes"]) - log := store_stats["log"].(common.MapStr) + log := store_stats["log"].(mapstr.M) assert.EqualValues(t, 8488103, log["bytes"]) - sst := store_stats["sst"].(common.MapStr) + sst := store_stats["sst"].(mapstr.M) assert.EqualValues(t, 0, sst["bytes"]) - total = store_stats["total"].(common.MapStr) + total = store_stats["total"].(mapstr.M) assert.EqualValues(t, 8488943, total["bytes"]) } diff --git a/metricbeat/module/ceph/osd_df/data.go b/metricbeat/module/ceph/osd_df/data.go index 9b48e4f89f3..3d82da0b552 100644 --- a/metricbeat/module/ceph/osd_df/data.go +++ b/metricbeat/module/ceph/osd_df/data.go @@ -22,7 +22,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Node represents a node object @@ -47,7 +47,7 @@ type OsdDfRequest struct { Output Output `json:"output"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var d OsdDfRequest err := json.Unmarshal(content, &d) if err != nil { @@ -57,9 +57,9 @@ func eventsMapping(content []byte) ([]common.MapStr, error) { nodeList := d.Output.Nodes //osd node list - events := []common.MapStr{} + events := []mapstr.M{} for _, node := range nodeList { - nodeInfo := common.MapStr{ + nodeInfo := mapstr.M{ "id": node.ID, "name": node.Name, "total.byte": node.Total, diff --git a/metricbeat/module/ceph/osd_tree/data.go b/metricbeat/module/ceph/osd_tree/data.go index f88426fa278..9d5e5b4f778 100644 --- a/metricbeat/module/ceph/osd_tree/data.go +++ b/metricbeat/module/ceph/osd_tree/data.go @@ -22,8 +22,8 @@ import ( "strconv" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Node represents a node object @@ -54,7 +54,7 @@ type OsdTreeRequest struct { Output Output `json:"output"` } -func eventsMapping(content []byte) ([]common.MapStr, error) { +func eventsMapping(content []byte) ([]mapstr.M, error) { var d OsdTreeRequest err := json.Unmarshal(content, &d) if err != nil { @@ -84,9 +84,9 @@ func eventsMapping(content []byte) ([]common.MapStr, error) { } //osd node list - events := []common.MapStr{} + events := []mapstr.M{} for _, node := range nodeList { - nodeInfo := common.MapStr{} + nodeInfo := mapstr.M{} if node.ID < 0 { //bucket node nodeInfo["children"] = strings.Split(childrenMap[node.Name], ",") diff --git a/metricbeat/module/ceph/pool_disk/data.go b/metricbeat/module/ceph/pool_disk/data.go index e3758c94816..85ab030f0b3 100644 --- a/metricbeat/module/ceph/pool_disk/data.go +++ b/metricbeat/module/ceph/pool_disk/data.go @@ -20,8 +20,8 @@ package pool_disk import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Stats represents the statistics for a pool @@ -50,25 +50,25 @@ type DfRequest struct { Output Output `json:"output"` } -func eventsMapping(content []byte) []common.MapStr { +func eventsMapping(content []byte) []mapstr.M { var d DfRequest err := json.Unmarshal(content, &d) if err != nil { logp.Err("Error: %+v", err) } - events := []common.MapStr{} + events := []mapstr.M{} for _, Pool := range d.Output.Pools { - event := common.MapStr{ + event := mapstr.M{ "name": Pool.Name, "id": Pool.ID, - "stats": common.MapStr{ - "used": common.MapStr{ + "stats": mapstr.M{ + "used": mapstr.M{ "bytes": Pool.Stats.BytesUsed, "kb": Pool.Stats.KbUsed, }, - "available": common.MapStr{ + "available": mapstr.M{ "bytes": Pool.Stats.MaxAvail, }, "objects": Pool.Stats.Objects, diff --git a/metricbeat/module/ceph/pool_disk/pool_disk_test.go b/metricbeat/module/ceph/pool_disk/pool_disk_test.go index 4f153dd9d5a..d28aadd54c3 100644 --- a/metricbeat/module/ceph/pool_disk/pool_disk_test.go +++ b/metricbeat/module/ceph/pool_disk/pool_disk_test.go @@ -24,8 +24,8 @@ import ( "path/filepath" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -61,12 +61,12 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, "rbd", event["name"]) assert.EqualValues(t, 0, event["id"]) - stats := event["stats"].(common.MapStr) + stats := event["stats"].(mapstr.M) - used := stats["used"].(common.MapStr) + used := stats["used"].(mapstr.M) assert.EqualValues(t, 0, used["bytes"]) assert.EqualValues(t, 0, used["kb"]) - available := stats["available"].(common.MapStr) + available := stats["available"].(mapstr.M) assert.EqualValues(t, uint64(5003444224), available["bytes"]) } diff --git a/metricbeat/module/consul/agent/agent_integration_test.go b/metricbeat/module/consul/agent/agent_integration_test.go index 82721f5df16..d363693d778 100644 --- a/metricbeat/module/consul/agent/agent_integration_test.go +++ b/metricbeat/module/consul/agent/agent_integration_test.go @@ -23,9 +23,9 @@ package agent import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" "github.com/elastic/beats/v7/metricbeat/module/consul" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/logp" @@ -54,7 +54,7 @@ func TestFetch(t *testing.T) { metricsetFields := event.MetricSetFields // Check runtime value - runtime, ok := metricsetFields["runtime"].(common.MapStr) + runtime, ok := metricsetFields["runtime"].(mapstr.M) assert.True(t, ok) //Check heapObjects diff --git a/metricbeat/module/consul/agent/agent_test.go b/metricbeat/module/consul/agent/agent_test.go index 10d7933d121..0b69a81e114 100644 --- a/metricbeat/module/consul/agent/agent_test.go +++ b/metricbeat/module/consul/agent/agent_test.go @@ -25,7 +25,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var jsonExample = `{ @@ -258,7 +258,7 @@ func TestEventMapping(t *testing.T) { runtimeI, ok := event["runtime"] assert.True(t, ok) - runtime, ok := runtimeI.(common.MapStr) + runtime, ok := runtimeI.(mapstr.M) assert.True(t, ok) //do not overwrite if heapObjectsFound has already been set to true diff --git a/metricbeat/module/consul/agent/data.go b/metricbeat/module/consul/agent/data.go index a8e45a06bab..6b8828e7fba 100644 --- a/metricbeat/module/consul/agent/data.go +++ b/metricbeat/module/consul/agent/data.go @@ -21,7 +21,7 @@ import ( "encoding/json" "fmt" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type valueConverter interface { @@ -86,14 +86,14 @@ var ( allowedDetailedValues = map[string]inputConverter{} ) -func eventMapping(content []byte) ([]common.MapStr, error) { +func eventMapping(content []byte) ([]mapstr.M, error) { var agent agent if err := json.Unmarshal(content, &agent); err != nil { return nil, err } - labels := map[string]common.MapStr{} + labels := map[string]mapstr.M{} for _, gauge := range agent.Gauges { metricApply(labels, gauge.consulMetric, gauge.Value) @@ -111,7 +111,7 @@ func eventMapping(content []byte) ([]common.MapStr, error) { metricApply(labels, sample.consulMetric, consulDetailedValue(sample)) } - data := make([]common.MapStr, 0) + data := make([]mapstr.M, 0) for _, v := range labels { data = append(data, v) } @@ -119,7 +119,7 @@ func eventMapping(content []byte) ([]common.MapStr, error) { return data, nil } -func metricApply(labels map[string]common.MapStr, m consulMetric, v interface{}) { +func metricApply(labels map[string]mapstr.M, m consulMetric, v interface{}) { prettyName := prettyName(m.Name) if prettyName == nil { //omitting unwanted metric @@ -128,7 +128,7 @@ func metricApply(labels map[string]common.MapStr, m consulMetric, v interface{}) labelsCombination := uniqueKeyForLabelMap(m.Labels) - temp := common.MapStr{} + temp := mapstr.M{} if len(m.Labels) != 0 { temp.Put("labels", m.Labels) } @@ -169,7 +169,7 @@ func prettyName(s string) inputConverter { // Create a simple unique value for a map of labels without using a hash function func uniqueKeyForLabelMap(m map[string]string) string { - mm := common.MapStr{} + mm := mapstr.M{} for k, v := range m { mm[k] = v } diff --git a/metricbeat/module/couchbase/bucket/bucket_test.go b/metricbeat/module/couchbase/bucket/bucket_test.go index b90dcb02bf0..c6a2373ad5f 100644 --- a/metricbeat/module/couchbase/bucket/bucket_test.go +++ b/metricbeat/module/couchbase/bucket/bucket_test.go @@ -27,8 +27,8 @@ import ( "path/filepath" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -67,25 +67,25 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, "membase", event["type"]) assert.EqualValues(t, "beer-sample", event["name"]) - data := event["data"].(common.MapStr) - data_used := data["used"].(common.MapStr) + data := event["data"].(mapstr.M) + data_used := data["used"].(mapstr.M) assert.EqualValues(t, 12597731, data_used["bytes"]) - disk := event["disk"].(common.MapStr) + disk := event["disk"].(mapstr.M) assert.EqualValues(t, 0, disk["fetches"]) - disk_used := disk["used"].(common.MapStr) + disk_used := disk["used"].(mapstr.M) assert.EqualValues(t, 16369008, disk_used["bytes"]) - memory := event["memory"].(common.MapStr) - memory_used := memory["used"].(common.MapStr) + memory := event["memory"].(mapstr.M) + memory_used := memory["used"].(mapstr.M) assert.EqualValues(t, 53962160, memory_used["bytes"]) - quota := event["quota"].(common.MapStr) - quota_ram := quota["ram"].(common.MapStr) + quota := event["quota"].(mapstr.M) + quota_ram := quota["ram"].(mapstr.M) assert.EqualValues(t, 104857600, quota_ram["bytes"]) - quota_use := quota["use"].(common.MapStr) + quota_use := quota["use"].(mapstr.M) assert.EqualValues(t, 51.46232604980469, quota_use["pct"]) assert.EqualValues(t, 7303, event["item_count"]) diff --git a/metricbeat/module/couchbase/bucket/data.go b/metricbeat/module/couchbase/bucket/data.go index ec8f4757430..2e44d3f7ff7 100644 --- a/metricbeat/module/couchbase/bucket/data.go +++ b/metricbeat/module/couchbase/bucket/data.go @@ -20,8 +20,8 @@ package bucket import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type BucketQuota struct { @@ -46,40 +46,40 @@ type Buckets []struct { BasicStats BucketBasicStats `json:"basicStats"` } -func eventsMapping(content []byte) []common.MapStr { +func eventsMapping(content []byte) []mapstr.M { var d Buckets err := json.Unmarshal(content, &d) if err != nil { logp.Err("Error: %+v", err) } - events := []common.MapStr{} + events := []mapstr.M{} for _, Bucket := range d { - event := common.MapStr{ + event := mapstr.M{ "name": Bucket.Name, "type": Bucket.BucketType, - "data": common.MapStr{ - "used": common.MapStr{ + "data": mapstr.M{ + "used": mapstr.M{ "bytes": Bucket.BasicStats.DataUsed, }, }, - "disk": common.MapStr{ + "disk": mapstr.M{ "fetches": Bucket.BasicStats.DiskFetches, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": Bucket.BasicStats.DiskUsed, }, }, - "memory": common.MapStr{ - "used": common.MapStr{ + "memory": mapstr.M{ + "used": mapstr.M{ "bytes": Bucket.BasicStats.MemUsed, }, }, - "quota": common.MapStr{ - "ram": common.MapStr{ + "quota": mapstr.M{ + "ram": mapstr.M{ "bytes": Bucket.Quota.RAM, }, - "use": common.MapStr{ + "use": mapstr.M{ "pct": Bucket.BasicStats.QuotaPercentUsed, }, }, diff --git a/metricbeat/module/couchbase/cluster/data.go b/metricbeat/module/couchbase/cluster/data.go index d106be8ff8f..1b104f750c7 100644 --- a/metricbeat/module/couchbase/cluster/data.go +++ b/metricbeat/module/couchbase/cluster/data.go @@ -20,8 +20,8 @@ package cluster import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type StorageTotals_Ram struct { @@ -57,7 +57,7 @@ type Data struct { MaxBucketCount int64 `json:"maxBucketCount"` } -func eventMapping(content []byte) common.MapStr { +func eventMapping(content []byte) mapstr.M { var d Data err := json.Unmarshal(content, &d) if err != nil { @@ -65,64 +65,64 @@ func eventMapping(content []byte) common.MapStr { } logp.Info("Printing Data:") - event := common.MapStr{ - "hdd": common.MapStr{ - "quota": common.MapStr{ - "total": common.MapStr{ + event := mapstr.M{ + "hdd": mapstr.M{ + "quota": mapstr.M{ + "total": mapstr.M{ "bytes": d.StorageTotals.Hdd.QuotaTotal, }, }, - "free": common.MapStr{ + "free": mapstr.M{ "bytes": d.StorageTotals.Hdd.Free, }, - "total": common.MapStr{ + "total": mapstr.M{ "bytes": d.StorageTotals.Hdd.Total, }, - "used": common.MapStr{ - "value": common.MapStr{ + "used": mapstr.M{ + "value": mapstr.M{ "bytes": d.StorageTotals.Hdd.Used, }, - "by_data": common.MapStr{ + "by_data": mapstr.M{ "bytes": d.StorageTotals.Hdd.UsedByData, }, }, }, "max_bucket_count": d.MaxBucketCount, - "quota": common.MapStr{ - "index_memory": common.MapStr{ + "quota": mapstr.M{ + "index_memory": mapstr.M{ "mb": d.IndexMemoryQuota, }, - "memory": common.MapStr{ + "memory": mapstr.M{ "mb": d.MemoryQuota, }, }, - "ram": common.MapStr{ - "quota": common.MapStr{ - "total": common.MapStr{ - "value": common.MapStr{ + "ram": mapstr.M{ + "quota": mapstr.M{ + "total": mapstr.M{ + "value": mapstr.M{ "bytes": d.StorageTotals.RAM.QuotaTotal, }, - "per_node": common.MapStr{ + "per_node": mapstr.M{ "bytes": d.StorageTotals.RAM.QuotaTotalPerNode, }, }, - "used": common.MapStr{ - "value": common.MapStr{ + "used": mapstr.M{ + "value": mapstr.M{ "bytes": d.StorageTotals.RAM.QuotaUsed, }, - "per_node": common.MapStr{ + "per_node": mapstr.M{ "bytes": d.StorageTotals.RAM.QuotaUsedPerNode, }, }, }, - "total": common.MapStr{ + "total": mapstr.M{ "bytes": d.StorageTotals.RAM.Total, }, - "used": common.MapStr{ - "value": common.MapStr{ + "used": mapstr.M{ + "value": mapstr.M{ "bytes": d.StorageTotals.RAM.Used, }, - "by_data": common.MapStr{ + "by_data": mapstr.M{ "bytes": d.StorageTotals.RAM.UsedByData, }, }, diff --git a/metricbeat/module/couchbase/node/data.go b/metricbeat/module/couchbase/node/data.go index 901729737f9..5cd64c7288c 100644 --- a/metricbeat/module/couchbase/node/data.go +++ b/metricbeat/module/couchbase/node/data.go @@ -22,8 +22,8 @@ import ( "strconv" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type NodeSystemStats struct { @@ -73,85 +73,85 @@ type Data struct { Nodes []Node `json:"nodes"` } -func eventsMapping(content []byte) []common.MapStr { +func eventsMapping(content []byte) []mapstr.M { var d Data err := json.Unmarshal(content, &d) if err != nil { logp.Err("Error: %+v", err) } - events := []common.MapStr{} + events := []mapstr.M{} for _, NodeItem := range d.Nodes { uptime, _ := strconv.ParseInt(NodeItem.Uptime, 10, 64) - event := common.MapStr{ + event := mapstr.M{ "cmd_get": NodeItem.InterestingStats.CmdGet, - "couch": common.MapStr{ - "docs": common.MapStr{ - "disk_size": common.MapStr{ + "couch": mapstr.M{ + "docs": mapstr.M{ + "disk_size": mapstr.M{ "bytes": NodeItem.InterestingStats.CouchDocsActualDiskSize, }, - "data_size": common.MapStr{ + "data_size": mapstr.M{ "bytes": NodeItem.InterestingStats.CouchDocsDataSize, }, }, - "spatial": common.MapStr{ - "data_size": common.MapStr{ + "spatial": mapstr.M{ + "data_size": mapstr.M{ "bytes": NodeItem.InterestingStats.CouchSpatialDataSize, }, - "disk_size": common.MapStr{ + "disk_size": mapstr.M{ "bytes": NodeItem.InterestingStats.CouchSpatialDiskSize, }, }, - "views": common.MapStr{ - "disk_size": common.MapStr{ + "views": mapstr.M{ + "disk_size": mapstr.M{ "bytes": NodeItem.InterestingStats.CouchViewsActualDiskSize, }, - "data_size": common.MapStr{ + "data_size": mapstr.M{ "bytes": NodeItem.InterestingStats.CouchViewsDataSize, }, }, }, - "cpu_utilization_rate": common.MapStr{ + "cpu_utilization_rate": mapstr.M{ "pct": NodeItem.SystemStats.CPUUtilizationRate, }, - "current_items": common.MapStr{ + "current_items": mapstr.M{ "value": NodeItem.InterestingStats.CurrItems, "total": NodeItem.InterestingStats.CurrItemsTot, }, "ep_bg_fetched": NodeItem.InterestingStats.EpBgFetched, "get_hits": NodeItem.InterestingStats.GetHits, "hostname": NodeItem.Hostname, - "mcd_memory": common.MapStr{ - "reserved": common.MapStr{ + "mcd_memory": mapstr.M{ + "reserved": mapstr.M{ "bytes": NodeItem.McdMemoryReserved, }, - "allocated": common.MapStr{ + "allocated": mapstr.M{ "bytes": NodeItem.McdMemoryAllocated, }, }, - "memory": common.MapStr{ - "total": common.MapStr{ + "memory": mapstr.M{ + "total": mapstr.M{ "bytes": NodeItem.SystemStats.MemTotal, }, - "free": common.MapStr{ + "free": mapstr.M{ "bytes": NodeItem.SystemStats.MemFree, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": NodeItem.InterestingStats.MemUsed, }, }, "ops": NodeItem.InterestingStats.Ops, - "swap": common.MapStr{ - "total": common.MapStr{ + "swap": mapstr.M{ + "total": mapstr.M{ "bytes": NodeItem.SystemStats.SwapTotal, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": NodeItem.SystemStats.SwapUsed, }, }, - "uptime": common.MapStr{ + "uptime": mapstr.M{ "sec": uptime, }, "vb_replica_curr_items": NodeItem.InterestingStats.VbReplicaCurrItems, diff --git a/metricbeat/module/couchdb/server/v1.go b/metricbeat/module/couchdb/server/v1.go index 88d9a05924b..2b3f43e3ce8 100644 --- a/metricbeat/module/couchdb/server/v1.go +++ b/metricbeat/module/couchdb/server/v1.go @@ -20,8 +20,8 @@ package server import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" ) @@ -35,15 +35,15 @@ func (v *V1) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { return mb.Event{}, errors.Wrap(err, "error parsing v1 server JSON") } - event := common.MapStr{ - "httpd": common.MapStr{ + event := mapstr.M{ + "httpd": mapstr.M{ "view_reads": data.Httpd.ViewReads.Current, "bulk_requests": data.Httpd.BulkRequests.Current, "clients_requesting_changes": data.Httpd.ClientsRequestingChanges.Current, "temporary_view_reads": data.Httpd.TemporaryViewReads.Current, "requests": data.Httpd.Requests.Current, }, - "httpd_request_methods": common.MapStr{ + "httpd_request_methods": mapstr.M{ "COPY": data.HttpdRequestMethods.Copy.Current, "HEAD": data.HttpdRequestMethods.Head.Current, "POST": data.HttpdRequestMethods.Post.Current, @@ -51,7 +51,7 @@ func (v *V1) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { "GET": data.HttpdRequestMethods.Get.Current, "PUT": data.HttpdRequestMethods.Put.Current, }, - "httpd_status_codes": common.MapStr{ + "httpd_status_codes": mapstr.M{ "200": data.HttpdStatusCodes.Num200.Current, "201": data.HttpdStatusCodes.Num201.Current, "202": data.HttpdStatusCodes.Num202.Current, @@ -66,7 +66,7 @@ func (v *V1) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { "412": data.HttpdStatusCodes.Num412.Current, "500": data.HttpdStatusCodes.Num500.Current, }, - "couchdb": common.MapStr{ + "couchdb": mapstr.M{ "database_writes": data.Couchdb.DatabaseWrites.Current, "open_databases": data.Couchdb.OpenDatabases.Current, "auth_cache_misses": data.Couchdb.AuthCacheMisses.Current, @@ -77,7 +77,7 @@ func (v *V1) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { }, } - ecs := common.MapStr{} + ecs := mapstr.M{} ecs.Put("service.id", info.UUID) ecs.Put("service.version", info.Version) diff --git a/metricbeat/module/couchdb/server/v2.go b/metricbeat/module/couchdb/server/v2.go index 8a575a01954..96e65edda9f 100644 --- a/metricbeat/module/couchdb/server/v2.go +++ b/metricbeat/module/couchdb/server/v2.go @@ -22,8 +22,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type V2 struct{} @@ -35,15 +35,15 @@ func (v *V2) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { return mb.Event{}, errors.Wrap(err, "error parsing v2 server JSON") } - event := common.MapStr{ - "httpd": common.MapStr{ + event := mapstr.M{ + "httpd": mapstr.M{ "view_reads": data.Couchdb.Httpd.ViewReads.Value, "bulk_requests": data.Couchdb.Httpd.BulkRequests.Value, "clients_requesting_changes": data.Couchdb.Httpd.ClientsRequestingChanges.Value, "temporary_view_reads": data.Couchdb.Httpd.TemporaryViewReads.Value, "requests": data.Couchdb.Httpd.Requests.Value, }, - "httpd_request_methods": common.MapStr{ + "httpd_request_methods": mapstr.M{ "COPY": data.Couchdb.HttpdRequestMethods.COPY.Value, "HEAD": data.Couchdb.HttpdRequestMethods.HEAD.Value, "POST": data.Couchdb.HttpdRequestMethods.POST.Value, @@ -51,7 +51,7 @@ func (v *V2) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { "GET": data.Couchdb.HttpdRequestMethods.GET.Value, "PUT": data.Couchdb.HttpdRequestMethods.PUT.Value, }, - "httpd_status_codes": common.MapStr{ + "httpd_status_codes": mapstr.M{ "200": data.Couchdb.HttpdStatusCodes.Num200.Value, "201": data.Couchdb.HttpdStatusCodes.Num201.Value, "202": data.Couchdb.HttpdStatusCodes.Num202.Value, @@ -66,7 +66,7 @@ func (v *V2) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { "412": data.Couchdb.HttpdStatusCodes.Num412.Value, "500": data.Couchdb.HttpdStatusCodes.Num500.Value, }, - "couchdb": common.MapStr{ + "couchdb": mapstr.M{ "database_writes": data.Couchdb.DatabaseWrites.Value, "open_databases": data.Couchdb.OpenDatabases.Value, "auth_cache_misses": data.Couchdb.AuthCacheMisses.Value, @@ -77,7 +77,7 @@ func (v *V2) MapEvent(info *CommonInfo, in []byte) (mb.Event, error) { }, } - ecs := common.MapStr{} + ecs := mapstr.M{} ecs.Put("service.id", info.UUID) ecs.Put("service.version", info.Version) diff --git a/metricbeat/module/docker/container/data.go b/metricbeat/module/docker/container/data.go index 6c32ecdb98a..6180d626574 100644 --- a/metricbeat/module/docker/container/data.go +++ b/metricbeat/module/docker/container/data.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) func eventsMapping(r mb.ReporterV2, containersList []types.Container, dedot bool, logger *logp.Logger) { @@ -36,21 +37,21 @@ func eventsMapping(r mb.ReporterV2, containersList []types.Container, dedot bool } func eventMapping(r mb.ReporterV2, cont *types.Container, dedot bool, logger *logp.Logger) { - event := common.MapStr{ - "container": common.MapStr{ + event := mapstr.M{ + "container": mapstr.M{ "id": cont.ID, - "image": common.MapStr{ + "image": mapstr.M{ "name": cont.Image, }, "name": docker.ExtractContainerName(cont.Names), "runtime": "docker", }, - "docker": common.MapStr{ - "container": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ "created": common.Time(time.Unix(cont.Created, 0)), "command": cont.Command, "ip_addresses": extractIPAddresses(cont.NetworkSettings), - "size": common.MapStr{ + "size": mapstr.M{ "root_fs": cont.SizeRootFs, "rw": cont.SizeRw, }, diff --git a/metricbeat/module/docker/cpu/cpu_test.go b/metricbeat/module/docker/cpu/cpu_test.go index d599abd19ed..16caf46e3ec 100644 --- a/metricbeat/module/docker/cpu/cpu_test.go +++ b/metricbeat/module/docker/cpu/cpu_test.go @@ -23,8 +23,8 @@ import ( "github.com/docker/docker/api/types" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) var cpuService CPUService @@ -49,25 +49,25 @@ func TestCPUService_PerCpuUsage(t *testing.T) { } testCase := []struct { given types.StatsJSON - expected common.MapStr + expected mapstr.M }{ - {statsList[0], common.MapStr{ - "0": common.MapStr{"pct": float64(0.40), "norm": common.MapStr{"pct": float64(0.40) / float64(onlineCPUS)}}, - "1": common.MapStr{"pct": float64(3.60), "norm": common.MapStr{"pct": float64(3.60) / float64(onlineCPUS)}}, - "2": common.MapStr{"pct": float64(3.60), "norm": common.MapStr{"pct": float64(3.60) / float64(onlineCPUS)}}, - "3": common.MapStr{"pct": float64(2.00), "norm": common.MapStr{"pct": float64(2.00) / float64(onlineCPUS)}}, + {statsList[0], mapstr.M{ + "0": mapstr.M{"pct": float64(0.40), "norm": mapstr.M{"pct": float64(0.40) / float64(onlineCPUS)}}, + "1": mapstr.M{"pct": float64(3.60), "norm": mapstr.M{"pct": float64(3.60) / float64(onlineCPUS)}}, + "2": mapstr.M{"pct": float64(3.60), "norm": mapstr.M{"pct": float64(3.60) / float64(onlineCPUS)}}, + "3": mapstr.M{"pct": float64(2.00), "norm": mapstr.M{"pct": float64(2.00) / float64(onlineCPUS)}}, }}, - {statsList[1], common.MapStr{ - "0": common.MapStr{"pct": float64(0.0000004), "norm": common.MapStr{"pct": float64(0.0000004) / float64(onlineCPUS)}}, - "1": common.MapStr{"pct": float64(0.0000008), "norm": common.MapStr{"pct": float64(0.0000008) / float64(onlineCPUS)}}, - "2": common.MapStr{"pct": float64(0.0000012), "norm": common.MapStr{"pct": float64(0.0000012) / float64(onlineCPUS)}}, - "3": common.MapStr{"pct": float64(0.0000016), "norm": common.MapStr{"pct": float64(0.0000016) / float64(onlineCPUS)}}, + {statsList[1], mapstr.M{ + "0": mapstr.M{"pct": float64(0.0000004), "norm": mapstr.M{"pct": float64(0.0000004) / float64(onlineCPUS)}}, + "1": mapstr.M{"pct": float64(0.0000008), "norm": mapstr.M{"pct": float64(0.0000008) / float64(onlineCPUS)}}, + "2": mapstr.M{"pct": float64(0.0000012), "norm": mapstr.M{"pct": float64(0.0000012) / float64(onlineCPUS)}}, + "3": mapstr.M{"pct": float64(0.0000016), "norm": mapstr.M{"pct": float64(0.0000016) / float64(onlineCPUS)}}, }}, - {statsList[2], common.MapStr{ - "0": common.MapStr{"pct": float64(0), "norm": common.MapStr{"pct": float64(0) / float64(onlineCPUS)}}, - "1": common.MapStr{"pct": float64(0), "norm": common.MapStr{"pct": float64(0) / float64(onlineCPUS)}}, - "2": common.MapStr{"pct": float64(0), "norm": common.MapStr{"pct": float64(0) / float64(onlineCPUS)}}, - "3": common.MapStr{"pct": float64(0), "norm": common.MapStr{"pct": float64(0) / float64(onlineCPUS)}}, + {statsList[2], mapstr.M{ + "0": mapstr.M{"pct": float64(0), "norm": mapstr.M{"pct": float64(0) / float64(onlineCPUS)}}, + "1": mapstr.M{"pct": float64(0), "norm": mapstr.M{"pct": float64(0) / float64(onlineCPUS)}}, + "2": mapstr.M{"pct": float64(0), "norm": mapstr.M{"pct": float64(0) / float64(onlineCPUS)}}, + "3": mapstr.M{"pct": float64(0), "norm": mapstr.M{"pct": float64(0) / float64(onlineCPUS)}}, }}, } for _, tt := range testCase { @@ -75,7 +75,7 @@ func TestCPUService_PerCpuUsage(t *testing.T) { out := usage.PerCPU() // Remove ticks for test for _, s := range out { - s.(common.MapStr).Delete("ticks") + s.(mapstr.M).Delete("ticks") } if !equalEvent(tt.expected, out) { t.Errorf("PerCPUUsage(%v) => %v, want %v", tt.given.CPUStats.CPUUsage.PercpuUsage, out, tt.expected) @@ -245,6 +245,6 @@ func TestCPUService_UsageInUsermodeNormalized(t *testing.T) { } } -func equalEvent(expectedEvent common.MapStr, event common.MapStr) bool { +func equalEvent(expectedEvent mapstr.M, event mapstr.M) bool { return reflect.DeepEqual(expectedEvent, event) } diff --git a/metricbeat/module/docker/cpu/data.go b/metricbeat/module/docker/cpu/data.go index a5c2ca309f1..fc77a7a30a8 100644 --- a/metricbeat/module/docker/cpu/data.go +++ b/metricbeat/module/docker/cpu/data.go @@ -18,8 +18,8 @@ package cpu import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func eventsMapping(r mb.ReporterV2, cpuStatsList []CPUStats) { @@ -29,32 +29,32 @@ func eventsMapping(r mb.ReporterV2, cpuStatsList []CPUStats) { } func eventMapping(r mb.ReporterV2, stats *CPUStats) { - fields := common.MapStr{ + fields := mapstr.M{ "core": stats.PerCPUUsage, - "total": common.MapStr{ + "total": mapstr.M{ "pct": stats.TotalUsage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.TotalUsageNormalized, }, }, - "kernel": common.MapStr{ + "kernel": mapstr.M{ "ticks": stats.UsageInKernelmode, "pct": stats.UsageInKernelmodePercentage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.UsageInKernelmodePercentageNormalized, }, }, - "user": common.MapStr{ + "user": mapstr.M{ "ticks": stats.UsageInUsermode, "pct": stats.UsageInUsermodePercentage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.UsageInUsermodePercentageNormalized, }, }, - "system": common.MapStr{ + "system": mapstr.M{ "ticks": stats.SystemUsage, "pct": stats.SystemUsagePercentage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.SystemUsagePercentageNormalized, }, }, diff --git a/metricbeat/module/docker/cpu/helper.go b/metricbeat/module/docker/cpu/helper.go index fcb8dc2de55..abf62ec02de 100644 --- a/metricbeat/module/docker/cpu/helper.go +++ b/metricbeat/module/docker/cpu/helper.go @@ -23,12 +23,13 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/module/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) type CPUStats struct { Time common.Time Container *docker.Container - PerCPUUsage common.MapStr + PerCPUUsage mapstr.M TotalUsage float64 TotalUsageNormalized float64 UsageInKernelmode uint64 @@ -127,17 +128,17 @@ func (u *CPUUsage) SystemDelta() uint64 { } // PerCPU calculates per CPU usage. -func (u *CPUUsage) PerCPU() common.MapStr { - var output common.MapStr +func (u *CPUUsage) PerCPU() mapstr.M { + var output mapstr.M if len(u.Stats.CPUStats.CPUUsage.PercpuUsage) == len(u.Stats.PreCPUStats.CPUUsage.PercpuUsage) { - output = common.MapStr{} + output = mapstr.M{} for index := range u.Stats.CPUStats.CPUUsage.PercpuUsage { - cpu := common.MapStr{} + cpu := mapstr.M{} cpu["pct"] = u.calculatePercentage( u.Stats.CPUStats.CPUUsage.PercpuUsage[index], u.Stats.PreCPUStats.CPUUsage.PercpuUsage[index], u.CPUs()) - cpu["norm"] = common.MapStr{ + cpu["norm"] = mapstr.M{ "pct": u.calculatePercentage( u.Stats.CPUStats.CPUUsage.PercpuUsage[index], u.Stats.PreCPUStats.CPUUsage.PercpuUsage[index], diff --git a/metricbeat/module/docker/diskio/data.go b/metricbeat/module/docker/diskio/data.go index f04582e18f3..27d4c9c8669 100644 --- a/metricbeat/module/docker/diskio/data.go +++ b/metricbeat/module/docker/diskio/data.go @@ -18,8 +18,8 @@ package diskio import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func eventsMapping(r mb.ReporterV2, blkioStatsList []BlkioStats) { @@ -29,8 +29,8 @@ func eventsMapping(r mb.ReporterV2, blkioStatsList []BlkioStats) { } func eventMapping(r mb.ReporterV2, stats *BlkioStats) { - fields := common.MapStr{ - "read": common.MapStr{ + fields := mapstr.M{ + "read": mapstr.M{ "ops": stats.serviced.reads, "bytes": stats.servicedBytes.reads, "rate": stats.reads, @@ -38,7 +38,7 @@ func eventMapping(r mb.ReporterV2, stats *BlkioStats) { "wait_time": stats.waitTime.reads, "queued": stats.queued.reads, }, - "write": common.MapStr{ + "write": mapstr.M{ "ops": stats.serviced.writes, "bytes": stats.servicedBytes.writes, "rate": stats.writes, @@ -46,7 +46,7 @@ func eventMapping(r mb.ReporterV2, stats *BlkioStats) { "wait_time": stats.waitTime.writes, "queued": stats.queued.writes, }, - "summary": common.MapStr{ + "summary": mapstr.M{ "ops": stats.serviced.totals, "bytes": stats.servicedBytes.totals, "rate": stats.totals, diff --git a/metricbeat/module/docker/event/event.go b/metricbeat/module/docker/event/event.go index b752666db9e..33091ddecbe 100644 --- a/metricbeat/module/docker/event/event.go +++ b/metricbeat/module/docker/event/event.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -131,13 +132,13 @@ func (m *MetricSet) reportEvent(reporter mb.ReporterV2, event events.Message) { reporter.Event(mb.Event{ Timestamp: time, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "id": event.ID, "type": event.Type, "action": event.Action, "status": event.Status, "from": event.From, - "actor": common.MapStr{ + "actor": mapstr.M{ "id": event.Actor.ID, "attributes": attributes, }, diff --git a/metricbeat/module/docker/healthcheck/data.go b/metricbeat/module/docker/healthcheck/data.go index 6ced9722328..3f42c270fbe 100644 --- a/metricbeat/module/docker/healthcheck/data.go +++ b/metricbeat/module/docker/healthcheck/data.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) func eventsMapping(r mb.ReporterV2, containers []types.Container, m *MetricSet) { @@ -58,10 +59,10 @@ func eventMapping(r mb.ReporterV2, cont *types.Container, m *MetricSet) { return } - fields := common.MapStr{ + fields := mapstr.M{ "status": container.State.Health.Status, "failingstreak": container.State.Health.FailingStreak, - "event": common.MapStr{ + "event": mapstr.M{ "start_date": common.Time(container.State.Health.Log[lastEvent].Start), "end_date": common.Time(container.State.Health.Log[lastEvent].End), "exit_code": container.State.Health.Log[lastEvent].ExitCode, diff --git a/metricbeat/module/docker/helper.go b/metricbeat/module/docker/helper.go index fd2343d6f25..2af4f7b7228 100644 --- a/metricbeat/module/docker/helper.go +++ b/metricbeat/module/docker/helper.go @@ -20,8 +20,8 @@ package docker import ( "github.com/docker/docker/api/types" - "github.com/elastic/beats/v7/libbeat/common" helpers "github.com/elastic/beats/v7/libbeat/common/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Container is a struct representation of a container @@ -29,16 +29,16 @@ type Container struct { ID string Name string Image string - Labels common.MapStr + Labels mapstr.M } // ToMapStr converts a container struct to a MapStrs -func (c *Container) ToMapStr() common.MapStr { - m := common.MapStr{ - "container": common.MapStr{ +func (c *Container) ToMapStr() mapstr.M { + m := mapstr.M{ + "container": mapstr.M{ "id": c.ID, "name": c.Name, - "image": common.MapStr{ + "image": mapstr.M{ "name": c.Image, }, "runtime": "docker", diff --git a/metricbeat/module/docker/helper_test.go b/metricbeat/module/docker/helper_test.go index 556c75e54f7..a7e750e3fae 100644 --- a/metricbeat/module/docker/helper_test.go +++ b/metricbeat/module/docker/helper_test.go @@ -20,11 +20,10 @@ package docker import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/stretchr/testify/assert" helpers "github.com/elastic/beats/v7/libbeat/common/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDeDotLabels(t *testing.T) { @@ -36,7 +35,7 @@ func TestDeDotLabels(t *testing.T) { t.Run("dedot enabled", func(t *testing.T) { result := helpers.DeDotLabels(labels, true) - assert.Equal(t, common.MapStr{ + assert.Equal(t, mapstr.M{ "com_docker_swarm_task": "", "com_docker_swarm_task_id": "1", "com_docker_swarm_task_name": "foobar", @@ -45,11 +44,11 @@ func TestDeDotLabels(t *testing.T) { t.Run("dedot disabled", func(t *testing.T) { result := helpers.DeDotLabels(labels, false) - assert.Equal(t, common.MapStr{ - "com": common.MapStr{ - "docker": common.MapStr{ - "swarm": common.MapStr{ - "task": common.MapStr{ + assert.Equal(t, mapstr.M{ + "com": mapstr.M{ + "docker": mapstr.M{ + "swarm": mapstr.M{ + "task": mapstr.M{ "value": "", "id": "1", "name": "foobar", diff --git a/metricbeat/module/docker/image/data.go b/metricbeat/module/docker/image/data.go index e809450f241..f2b59979aee 100644 --- a/metricbeat/module/docker/image/data.go +++ b/metricbeat/module/docker/image/data.go @@ -24,24 +24,25 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventsMapping(imagesList []types.ImageSummary, dedot bool) []common.MapStr { - events := []common.MapStr{} +func eventsMapping(imagesList []types.ImageSummary, dedot bool) []mapstr.M { + events := []mapstr.M{} for _, image := range imagesList { events = append(events, eventMapping(&image, dedot)) } return events } -func eventMapping(image *types.ImageSummary, dedot bool) common.MapStr { - event := common.MapStr{ - "id": common.MapStr{ +func eventMapping(image *types.ImageSummary, dedot bool) mapstr.M { + event := mapstr.M{ + "id": mapstr.M{ "current": image.ID, "parent": image.ParentID, }, "created": common.Time(time.Unix(image.Created, 0)), - "size": common.MapStr{ + "size": mapstr.M{ "regular": image.Size, "virtual": image.VirtualSize, }, diff --git a/metricbeat/module/docker/info/data.go b/metricbeat/module/docker/info/data.go index 6b3f2e628cf..d5c967ddfbf 100644 --- a/metricbeat/module/docker/info/data.go +++ b/metricbeat/module/docker/info/data.go @@ -20,13 +20,13 @@ package info import ( "github.com/docker/docker/api/types" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(info *types.Info) common.MapStr { - event := common.MapStr{ +func eventMapping(info *types.Info) mapstr.M { + event := mapstr.M{ "id": info.ID, - "containers": common.MapStr{ + "containers": mapstr.M{ "total": info.Containers, "running": info.ContainersRunning, "paused": info.ContainersPaused, diff --git a/metricbeat/module/docker/memory/data.go b/metricbeat/module/docker/memory/data.go index 735851f2131..31b943449a6 100644 --- a/metricbeat/module/docker/memory/data.go +++ b/metricbeat/module/docker/memory/data.go @@ -18,8 +18,8 @@ package memory import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func eventsMapping(r mb.ReporterV2, memoryDataList []MemoryData) { @@ -31,31 +31,31 @@ func eventsMapping(r mb.ReporterV2, memoryDataList []MemoryData) { func eventMapping(r mb.ReporterV2, memoryData *MemoryData) { //if we have windows memory data, just report windows stats - var fields common.MapStr + var fields mapstr.M rootFields := memoryData.Container.ToMapStr() if memoryData.Commit+memoryData.CommitPeak+memoryData.PrivateWorkingSet > 0 { - fields = common.MapStr{ - "commit": common.MapStr{ + fields = mapstr.M{ + "commit": mapstr.M{ "total": memoryData.Commit, "peak": memoryData.CommitPeak, }, - "private_working_set": common.MapStr{ + "private_working_set": mapstr.M{ "total": memoryData.PrivateWorkingSet, }, } } else { - fields = common.MapStr{ + fields = mapstr.M{ "stats": memoryData.Stats, - "fail": common.MapStr{ + "fail": mapstr.M{ "count": memoryData.Failcnt, }, "limit": memoryData.Limit, - "rss": common.MapStr{ + "rss": mapstr.M{ "total": memoryData.TotalRss, "pct": memoryData.TotalRssP, }, - "usage": common.MapStr{ + "usage": mapstr.M{ "total": memoryData.Usage, "pct": memoryData.UsageP, "max": memoryData.MaxUsage, diff --git a/metricbeat/module/docker/memory/memory_test.go b/metricbeat/module/docker/memory/memory_test.go index 213ef5036ac..93e11824e28 100644 --- a/metricbeat/module/docker/memory/memory_test.go +++ b/metricbeat/module/docker/memory/memory_test.go @@ -24,9 +24,9 @@ import ( "github.com/docker/docker/api/types" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestMemoryService_GetMemoryStats(t *testing.T) { @@ -56,23 +56,23 @@ func TestMemoryService_GetMemoryStats(t *testing.T) { memoryRawStats.Stats = memorystats totalRSS := memorystats.MemoryStats.Stats["total_rss"] - expectedRootFields := common.MapStr{ - "container": common.MapStr{ + expectedRootFields := mapstr.M{ + "container": mapstr.M{ "id": containerID, "name": "name1", - "image": common.MapStr{ + "image": mapstr.M{ "name": "image", }, "runtime": "docker", - "memory": common.MapStr{ + "memory": mapstr.M{ "usage": 0.5, }, }, - "docker": common.MapStr{ - "container": common.MapStr{ - "labels": common.MapStr{ + "docker": mapstr.M{ + "container": mapstr.M{ + "labels": mapstr.M{ "label1": "val1", - "label2": common.MapStr{ + "label2": mapstr.M{ "foo": "val3", "value": "val2", }, @@ -80,19 +80,19 @@ func TestMemoryService_GetMemoryStats(t *testing.T) { }, }, } - expectedFields := common.MapStr{ + expectedFields := mapstr.M{ "stats": map[string]uint64{ "total_rss": 5, }, - "fail": common.MapStr{ + "fail": mapstr.M{ "count": memorystats.MemoryStats.Failcnt, }, "limit": memorystats.MemoryStats.Limit, - "rss": common.MapStr{ + "rss": mapstr.M{ "total": totalRSS, "pct": float64(totalRSS) / float64(memorystats.MemoryStats.Limit), }, - "usage": common.MapStr{ + "usage": mapstr.M{ "total": memorystats.MemoryStats.Usage, "pct": float64(memorystats.MemoryStats.Usage) / float64(memorystats.MemoryStats.Limit), "max": memorystats.MemoryStats.MaxUsage, diff --git a/metricbeat/module/docker/network/data.go b/metricbeat/module/docker/network/data.go index fc8e03c6f14..823b226d0f9 100644 --- a/metricbeat/module/docker/network/data.go +++ b/metricbeat/module/docker/network/data.go @@ -18,8 +18,8 @@ package network import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func eventsMapping(r mb.ReporterV2, netsStatsList []NetStats) { @@ -35,15 +35,15 @@ func eventMapping(r mb.ReporterV2, stats *NetStats) { _, _ = rootFields.Put("container.network.egress.bytes", stats.Total.TxBytes) r.Event(mb.Event{ RootFields: rootFields, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "interface": stats.NameInterface, - "inbound": common.MapStr{ + "inbound": mapstr.M{ "bytes": stats.Total.RxBytes, "dropped": stats.Total.RxDropped, "errors": stats.Total.RxErrors, "packets": stats.Total.RxPackets, }, - "outbound": common.MapStr{ + "outbound": mapstr.M{ "bytes": stats.Total.TxBytes, "dropped": stats.Total.TxDropped, "errors": stats.Total.TxErrors, diff --git a/metricbeat/module/docker/network_summary/network_summary.go b/metricbeat/module/docker/network_summary/network_summary.go index c437b4e12f4..9ea83fcf392 100644 --- a/metricbeat/module/docker/network_summary/network_summary.go +++ b/metricbeat/module/docker/network_summary/network_summary.go @@ -27,11 +27,11 @@ import ( "github.com/docker/docker/client" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/network" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/docker" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -114,7 +114,7 @@ func (m *MetricSet) Fetch(ctx context.Context, report mb.ReporterV2) error { summary := network.MapProcNetCounters(networkStats) // attach metadata associated with the network counters - summary["namespace"] = common.MapStr{ + summary["namespace"] = mapstr.M{ "id": netNS, "pid": rootPID, } diff --git a/metricbeat/module/dropwizard/collector/collector_integration_test.go b/metricbeat/module/dropwizard/collector/collector_integration_test.go index 2526446b0eb..22e2234d63c 100644 --- a/metricbeat/module/dropwizard/collector/collector_integration_test.go +++ b/metricbeat/module/dropwizard/collector/collector_integration_test.go @@ -25,9 +25,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -59,9 +59,9 @@ func TestFetch(t *testing.T) { if err != nil { t.Fatal("write", err) } else { - tags, ok := tagsRaw.(common.MapStr) + tags, ok := tagsRaw.(mapstr.M) if !ok { - t.Fatal("write", "unable to cast tags to common.MapStr") + t.Fatal("write", "unable to cast tags to mapstr.M") } else { assert.Equal(t, len(tags), 1) hasTag = true diff --git a/metricbeat/module/dropwizard/collector/data.go b/metricbeat/module/dropwizard/collector/data.go index 59c8ad5a275..9f3bc17dfe7 100644 --- a/metricbeat/module/dropwizard/collector/data.go +++ b/metricbeat/module/dropwizard/collector/data.go @@ -21,26 +21,26 @@ import ( "encoding/json" "strings" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type DropWizardEvent struct { key string - value common.MapStr - tags common.MapStr + value mapstr.M + tags mapstr.M tagHash string } // NewPromEvent creates a prometheus event based on the given string -func eventMapping(metrics map[string]interface{}) map[string]common.MapStr { - eventList := map[string]common.MapStr{} +func eventMapping(metrics map[string]interface{}) map[string]mapstr.M { + eventList := map[string]mapstr.M{} for _, metricSet := range metrics { switch t := metricSet.(type) { case map[string]interface{}: for key, value := range t { name, tags := splitTagsFromMetricName(key) - valueMap := common.MapStr{} + valueMap := mapstr.M{} metric, _ := value.(map[string]interface{}) for k, v := range metric { @@ -67,7 +67,7 @@ func eventMapping(metrics map[string]interface{}) map[string]common.MapStr { } if _, ok := eventList[dropEvent.tagHash]; !ok { - eventList[dropEvent.tagHash] = common.MapStr{} + eventList[dropEvent.tagHash] = mapstr.M{} // Add labels if len(dropEvent.tags) > 0 { @@ -88,7 +88,7 @@ func eventMapping(metrics map[string]interface{}) map[string]common.MapStr { return eventList } -func splitTagsFromMetricName(metricName string) (string, common.MapStr) { +func splitTagsFromMetricName(metricName string) (string, mapstr.M) { if metricName == "" { return "", nil } @@ -103,7 +103,7 @@ func splitTagsFromMetricName(metricName string) (string, common.MapStr) { } key := metricName[:index] - tags := common.MapStr{} + tags := mapstr.M{} tagStr := metricName[index+1 : len(metricName)-1] diff --git a/metricbeat/module/dropwizard/collector/data_test.go b/metricbeat/module/dropwizard/collector/data_test.go index 823b81b3131..a20d78d562a 100644 --- a/metricbeat/module/dropwizard/collector/data_test.go +++ b/metricbeat/module/dropwizard/collector/data_test.go @@ -22,7 +22,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestSplitTagsFromMetricName(t *testing.T) { @@ -30,7 +30,7 @@ func TestSplitTagsFromMetricName(t *testing.T) { title string name string key string - tags common.MapStr + tags mapstr.M }{ { title: "no tags", @@ -45,12 +45,12 @@ func TestSplitTagsFromMetricName(t *testing.T) { title: "standard tags", name: "metric{key1=var1, key2=var2}", key: "metric", - tags: common.MapStr{"key1": "var1", "key2": "var2"}, + tags: mapstr.M{"key1": "var1", "key2": "var2"}, }, { title: "standard tags (no space)", name: "metric{key1=var1,key2=var2}", key: "metric", - tags: common.MapStr{"key1": "var1", "key2": "var2"}, + tags: mapstr.M{"key1": "var1", "key2": "var2"}, }, { title: "empty parameter", name: "metric/{}", @@ -58,22 +58,22 @@ func TestSplitTagsFromMetricName(t *testing.T) { title: "empty key or value", name: "metric{=var1, key2=}", key: "metric", - tags: common.MapStr{"": "var1", "key2": ""}, + tags: mapstr.M{"": "var1", "key2": ""}, }, { title: "empty key and value", name: "metric{=}", key: "metric", - tags: common.MapStr{"": ""}, + tags: mapstr.M{"": ""}, }, { title: "extra comma", name: "metric{a=b,}", key: "metric", - tags: common.MapStr{"a": "b"}, + tags: mapstr.M{"a": "b"}, }, { title: "extra comma and space", name: "metric{a=b, }", key: "metric", - tags: common.MapStr{"a": "b"}, + tags: mapstr.M{"a": "b"}, }, { title: "extra comma and space", name: "metric{,a=b}", diff --git a/metricbeat/module/elasticsearch/ccr/data.go b/metricbeat/module/elasticsearch/ccr/data.go index 71119599430..5d355ff45f0 100644 --- a/metricbeat/module/elasticsearch/ccr/data.go +++ b/metricbeat/module/elasticsearch/ccr/data.go @@ -21,8 +21,7 @@ import ( "encoding/json" "github.com/elastic/beats/v7/metricbeat/helper/elastic" - - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/joeshaw/multierror" "github.com/pkg/errors" @@ -146,8 +145,8 @@ func eventsMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isX for _, followerIndex := range data.FollowStats.Indices { for _, followerShard := range followerIndex.Shards { event := mb.Event{} - event.RootFields = common.MapStr{} - event.ModuleFields = common.MapStr{} + event.RootFields = mapstr.M{} + event.ModuleFields = mapstr.M{} event.RootFields.Put("service.name", elasticsearch.ModuleName) event.ModuleFields.Put("cluster.name", info.ClusterName) diff --git a/metricbeat/module/elasticsearch/cluster_stats/data.go b/metricbeat/module/elasticsearch/cluster_stats/data.go index a75f58f3a44..01eb5a52f46 100644 --- a/metricbeat/module/elasticsearch/cluster_stats/data.go +++ b/metricbeat/module/elasticsearch/cluster_stats/data.go @@ -26,13 +26,13 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/elasticsearch" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -97,7 +97,7 @@ var ( } ) -func clusterNeedsTLSEnabled(license *elasticsearch.License, stackStats common.MapStr) (bool, error) { +func clusterNeedsTLSEnabled(license *elasticsearch.License, stackStats mapstr.M) (bool, error) { // TLS does not need to be enabled if license type is something other than trial if !license.IsOneOf("trial") { return false, nil @@ -133,7 +133,7 @@ func clusterNeedsTLSEnabled(license *elasticsearch.License, stackStats common.Ma } // computeNodesHash computes a simple hash value that can be used to determine if the nodes listing has changed since the last report. -func computeNodesHash(clusterState common.MapStr) (int32, error) { +func computeNodesHash(clusterState mapstr.M) (int32, error) { value, err := clusterState.GetValue("nodes") if err != nil { return 0, elastic.MakeErrorForMissingField("nodes", elastic.Elasticsearch) @@ -176,7 +176,7 @@ func hash(s string) int32 { return int32(h.Sum32()) // This cast is needed because the ES mapping is for a 32-bit *signed* integer } -func apmIndicesExist(clusterState common.MapStr) (bool, error) { +func apmIndicesExist(clusterState mapstr.M) (bool, error) { value, err := clusterState.GetValue("routing_table.indices") if err != nil { return false, elastic.MakeErrorForMissingField("routing_table.indices", elastic.Elasticsearch) @@ -196,7 +196,7 @@ func apmIndicesExist(clusterState common.MapStr) (bool, error) { return false, nil } -func getClusterMetadataSettings(httpClient *helper.HTTP) (common.MapStr, error) { +func getClusterMetadataSettings(httpClient *helper.HTTP) (mapstr.M, error) { // For security reasons we only get the display_name setting filterPaths := []string{"*.cluster.metadata.display_name"} clusterSettings, err := elasticsearch.GetClusterSettingsWithDefaults(httpClient, httpClient.GetURI(), filterPaths) @@ -219,7 +219,7 @@ func eventMapping(r mb.ReporterV2, httpClient *helper.HTTP, info elasticsearch.I return errors.Wrap(err, "failure parsing Elasticsearch Cluster Stats API response") } - clusterStats := common.MapStr(data) + clusterStats := mapstr.M(data) clusterStats.Delete("_nodes") license, err := elasticsearch.GetLicense(httpClient, httpClient.GetURI()) @@ -234,7 +234,7 @@ func eventMapping(r mb.ReporterV2, httpClient *helper.HTTP, info elasticsearch.I } clusterState.Delete("cluster_name") - clusterStateReduced := common.MapStr{} + clusterStateReduced := mapstr.M{} if err = elasticsearch.PassThruField("status", clusterStats, clusterStateReduced); err != nil { return errors.Wrap(err, "failed to pass through status field") } @@ -286,8 +286,8 @@ func eventMapping(r mb.ReporterV2, httpClient *helper.HTTP, info elasticsearch.I stackData, _ := stackSchema.Apply(stackStats) event := mb.Event{ - ModuleFields: common.MapStr{}, - RootFields: common.MapStr{}, + ModuleFields: mapstr.M{}, + RootFields: mapstr.M{}, } event.ModuleFields.Put("cluster.name", info.ClusterName) event.ModuleFields.Put("cluster.id", info.ClusterID) diff --git a/metricbeat/module/elasticsearch/elasticsearch.go b/metricbeat/module/elasticsearch/elasticsearch.go index fa5f7eae958..9730a233ff7 100644 --- a/metricbeat/module/elasticsearch/elasticsearch.go +++ b/metricbeat/module/elasticsearch/elasticsearch.go @@ -33,6 +33,7 @@ import ( "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -281,7 +282,7 @@ func GetLicense(http *helper.HTTP, resetURI string) (*License, error) { } // GetClusterState returns cluster state information. -func GetClusterState(http *helper.HTTP, resetURI string, metrics []string) (common.MapStr, error) { +func GetClusterState(http *helper.HTTP, resetURI string, metrics []string) (mapstr.M, error) { clusterStateURI := "_cluster/state" if metrics != nil && len(metrics) > 0 { clusterStateURI += "/" + strings.Join(metrics, ",") @@ -298,12 +299,12 @@ func GetClusterState(http *helper.HTTP, resetURI string, metrics []string) (comm } // GetClusterSettingsWithDefaults returns cluster settings. -func GetClusterSettingsWithDefaults(http *helper.HTTP, resetURI string, filterPaths []string) (common.MapStr, error) { +func GetClusterSettingsWithDefaults(http *helper.HTTP, resetURI string, filterPaths []string) (mapstr.M, error) { return GetClusterSettings(http, resetURI, true, filterPaths) } // GetClusterSettings returns cluster settings -func GetClusterSettings(http *helper.HTTP, resetURI string, includeDefaults bool, filterPaths []string) (common.MapStr, error) { +func GetClusterSettings(http *helper.HTTP, resetURI string, includeDefaults bool, filterPaths []string) (mapstr.M, error) { clusterSettingsURI := "_cluster/settings" var queryParams []string if includeDefaults { @@ -460,7 +461,7 @@ func GetMasterNodeID(http *helper.HTTP, resetURI string) (string, error) { // PassThruField copies the field at the given path from the given source data object into // the same path in the given target data object. -func PassThruField(fieldPath string, sourceData, targetData common.MapStr) error { +func PassThruField(fieldPath string, sourceData, targetData mapstr.M) error { fieldValue, err := sourceData.GetValue(fieldPath) if err != nil { return elastic.MakeErrorForMissingField(fieldPath, elastic.Elasticsearch) @@ -471,7 +472,7 @@ func PassThruField(fieldPath string, sourceData, targetData common.MapStr) error } // MergeClusterSettings merges cluster settings in the correct precedence order -func MergeClusterSettings(clusterSettings common.MapStr) (common.MapStr, error) { +func MergeClusterSettings(clusterSettings mapstr.M) (mapstr.M, error) { transientSettings, err := getSettingGroup(clusterSettings, "transient") if err != nil { return nil, err @@ -564,12 +565,12 @@ func (l *License) IsOneOf(candidateLicenses ...string) bool { return false } -// ToMapStr converts the license to a common.MapStr. This is necessary +// ToMapStr converts the license to a mapstr.M. This is necessary // for proper marshaling of the data before it's sent over the wire. In // particular it ensures that ms-since-epoch values are marshaled as longs // and not floats in scientific notation as Elasticsearch does not like that. -func (l *License) ToMapStr() common.MapStr { - m := common.MapStr{ +func (l *License) ToMapStr() mapstr.M { + m := mapstr.M{ "status": l.Status, "uid": l.ID, "type": l.Type, @@ -600,7 +601,7 @@ func (l *License) ToMapStr() common.MapStr { return m } -func getSettingGroup(allSettings common.MapStr, groupKey string) (common.MapStr, error) { +func getSettingGroup(allSettings mapstr.M, groupKey string) (mapstr.M, error) { hasSettingGroup, err := allSettings.HasKey(groupKey) if err != nil { return nil, errors.Wrap(err, "failure to determine if "+groupKey+" settings exist") @@ -620,5 +621,5 @@ func getSettingGroup(allSettings common.MapStr, groupKey string) (common.MapStr, return nil, errors.Wrap(err, groupKey+" settings are not a map") } - return common.MapStr(v), nil + return mapstr.M(v), nil } diff --git a/metricbeat/module/elasticsearch/elasticsearch_integration_test.go b/metricbeat/module/elasticsearch/elasticsearch_integration_test.go index ddea977d7ce..b1789c93bb9 100644 --- a/metricbeat/module/elasticsearch/elasticsearch_integration_test.go +++ b/metricbeat/module/elasticsearch/elasticsearch_integration_test.go @@ -50,6 +50,7 @@ import ( _ "github.com/elastic/beats/v7/metricbeat/module/elasticsearch/node" _ "github.com/elastic/beats/v7/metricbeat/module/elasticsearch/node_stats" _ "github.com/elastic/beats/v7/metricbeat/module/elasticsearch/shard" + "github.com/elastic/elastic-agent-libs/mapstr" ) var metricSets = []string{ @@ -535,7 +536,7 @@ func countCatItems(elasticsearchHostPort, catObject, extraParams string) (int, e return 0, err } - var data []common.MapStr + var data []mapstr.M err = json.Unmarshal(body, &data) if err != nil { return 0, err @@ -572,7 +573,7 @@ func getElasticsearchVersion(elasticsearchHostPort string) (*common.Version, err return nil, err } - var data common.MapStr + var data mapstr.M err = json.Unmarshal(body, &data) if err != nil { return nil, err diff --git a/metricbeat/module/elasticsearch/enrich/data.go b/metricbeat/module/elasticsearch/enrich/data.go index dc105e29f00..e84ea0ee8da 100644 --- a/metricbeat/module/elasticsearch/enrich/data.go +++ b/metricbeat/module/elasticsearch/enrich/data.go @@ -21,11 +21,11 @@ import ( "encoding/json" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -81,7 +81,7 @@ func eventsMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isX event := mb.Event{} - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} event.ModuleFields.Put("cluster.name", info.ClusterName) event.ModuleFields.Put("cluster.id", info.ClusterID) @@ -110,10 +110,10 @@ func eventsMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isX for _, policy := range data.ExecutingPolicies { event := mb.Event{} - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} event.ModuleFields.Put("cluster.name", info.ClusterName) event.ModuleFields.Put("cluster.id", info.ClusterID) - event.MetricSetFields = common.MapStr{} + event.MetricSetFields = mapstr.M{} policyName, ok := policy["name"] if !ok { diff --git a/metricbeat/module/elasticsearch/index/data.go b/metricbeat/module/elasticsearch/index/data.go index d586832783f..1cf29c98b94 100644 --- a/metricbeat/module/elasticsearch/index/data.go +++ b/metricbeat/module/elasticsearch/index/data.go @@ -24,11 +24,11 @@ import ( "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/elasticsearch" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Based on https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java#L127-L203 @@ -156,7 +156,7 @@ func eventsMapping(r mb.ReporterV2, httpClient *helper.HTTP, info elasticsearch. var errs multierror.Errors for name, idx := range indicesStats.Indices { event := mb.Event{ - ModuleFields: common.MapStr{}, + ModuleFields: mapstr.M{}, } idx.Index = name @@ -181,7 +181,7 @@ func eventsMapping(r mb.ReporterV2, httpClient *helper.HTTP, info elasticsearch. errs = append(errs, errors.Wrap(err, "failure trying to convert metrics results to JSON")) continue } - var indexOutput common.MapStr + var indexOutput mapstr.M if err = json.Unmarshal(indexBytes, &indexOutput); err != nil { errs = append(errs, errors.Wrap(err, "failure trying to convert JSON metrics back to mapstr")) continue @@ -210,7 +210,7 @@ func parseAPIResponse(content []byte, indicesStats *stats) error { // Fields added here are based on same fields being added by internal collection in // https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java#L62-L124 -func addClusterStateFields(idx *Index, clusterState common.MapStr) error { +func addClusterStateFields(idx *Index, clusterState mapstr.M) error { indexRoutingTable, err := getClusterStateMetricForIndex(clusterState, idx.Index, "routing_table") if err != nil { return errors.Wrap(err, "failed to get index routing table from cluster state") @@ -238,7 +238,7 @@ func addClusterStateFields(idx *Index, clusterState common.MapStr) error { return nil } -func getClusterStateMetricForIndex(clusterState common.MapStr, index, metricKey string) (common.MapStr, error) { +func getClusterStateMetricForIndex(clusterState mapstr.M, index, metricKey string) (mapstr.M, error) { fieldKey := metricKey + ".indices." + index value, err := clusterState.GetValue(fieldKey) if err != nil { @@ -249,7 +249,7 @@ func getClusterStateMetricForIndex(clusterState common.MapStr, index, metricKey if !ok { return nil, elastic.MakeErrorForMissingField(fieldKey, elastic.Elasticsearch) } - return common.MapStr(metric), nil + return mapstr.M(metric), nil } func getIndexStatus(shards map[string]interface{}) (string, error) { @@ -273,7 +273,7 @@ func getIndexStatus(shards map[string]interface{}) (string, error) { return "", fmt.Errorf("%v.shards[%v] is not a map", indexName, shardIdx) } - shard := common.MapStr(s) + shard := mapstr.M(s) isPrimary := shard["primary"].(bool) state := shard["state"].(string) @@ -297,7 +297,7 @@ func getIndexStatus(shards map[string]interface{}) (string, error) { return "red", nil } -func getIndexShardStats(shards common.MapStr) (*shardStats, error) { +func getIndexShardStats(shards mapstr.M) (*shardStats, error) { primaries := 0 replicas := 0 @@ -322,7 +322,7 @@ func getIndexShardStats(shards common.MapStr) (*shardStats, error) { return nil, fmt.Errorf("%v.shards[%v] is not a map", indexName, shardIdx) } - shard := common.MapStr(s) + shard := mapstr.M(s) isPrimary := shard["primary"].(bool) state := shard["state"].(string) @@ -369,7 +369,7 @@ func getIndexShardStats(shards common.MapStr) (*shardStats, error) { }, nil } -func getShardsFromRoutingTable(indexRoutingTable common.MapStr) (map[string]interface{}, error) { +func getShardsFromRoutingTable(indexRoutingTable mapstr.M) (map[string]interface{}, error) { s, err := indexRoutingTable.GetValue("shards") if err != nil { return nil, err diff --git a/metricbeat/module/elasticsearch/index_recovery/data.go b/metricbeat/module/elasticsearch/index_recovery/data.go index 2d37bbae6b3..780d8ca6a9b 100644 --- a/metricbeat/module/elasticsearch/index_recovery/data.go +++ b/metricbeat/module/elasticsearch/index_recovery/data.go @@ -23,12 +23,12 @@ import ( "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/elasticsearch" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -102,10 +102,10 @@ func eventsMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isX for _, data := range shards { event := mb.Event{} - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("service.name", elasticsearch.ModuleName) - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} event.ModuleFields.Put("cluster.name", info.ClusterName) event.ModuleFields.Put("cluster.id", info.ClusterID) event.ModuleFields.Put("index.name", indexName) diff --git a/metricbeat/module/elasticsearch/index_summary/data.go b/metricbeat/module/elasticsearch/index_summary/data.go index e0c7a7de232..ff6fedb53c9 100644 --- a/metricbeat/module/elasticsearch/index_summary/data.go +++ b/metricbeat/module/elasticsearch/index_summary/data.go @@ -22,8 +22,8 @@ import ( "fmt" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -106,10 +106,10 @@ func eventMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isXp } var event mb.Event - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} _, _ = event.RootFields.Put("service.name", elasticsearch.ModuleName) - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} _, _ = event.ModuleFields.Put("cluster.name", info.ClusterName) _, _ = event.ModuleFields.Put("cluster.id", info.ClusterID) diff --git a/metricbeat/module/elasticsearch/ml_job/data.go b/metricbeat/module/elasticsearch/ml_job/data.go index a23c2677f1b..f70321606b9 100644 --- a/metricbeat/module/elasticsearch/ml_job/data.go +++ b/metricbeat/module/elasticsearch/ml_job/data.go @@ -24,8 +24,8 @@ import ( "github.com/pkg/errors" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -75,10 +75,10 @@ func eventsMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isX event := mb.Event{} - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("service.name", elasticsearch.ModuleName) - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} event.ModuleFields.Put("cluster.name", info.ClusterName) event.ModuleFields.Put("cluster.id", info.ClusterID) diff --git a/metricbeat/module/elasticsearch/node/data.go b/metricbeat/module/elasticsearch/node/data.go index 11bb39a01d2..742a2f056da 100644 --- a/metricbeat/module/elasticsearch/node/data.go +++ b/metricbeat/module/elasticsearch/node/data.go @@ -21,11 +21,11 @@ import ( "encoding/json" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -78,10 +78,10 @@ func eventsMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isX for id, node := range nodesStruct.Nodes { event := mb.Event{} - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("service.name", elasticsearch.ModuleName) - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} event.ModuleFields.Put("cluster.name", nodesStruct.ClusterName) event.ModuleFields.Put("cluster.id", info.ClusterID) diff --git a/metricbeat/module/elasticsearch/node_stats/data.go b/metricbeat/module/elasticsearch/node_stats/data.go index fb8c1092560..1e31b1db5bd 100644 --- a/metricbeat/module/elasticsearch/node_stats/data.go +++ b/metricbeat/module/elasticsearch/node_stats/data.go @@ -22,11 +22,11 @@ import ( "fmt" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -304,16 +304,16 @@ func eventsMapping(r mb.ReporterV2, m elasticsearch.MetricSetAPI, info elasticse event := mb.Event{} - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("service.name", elasticsearch.ModuleName) - event.ModuleFields = common.MapStr{ - "node": common.MapStr{ + event.ModuleFields = mapstr.M{ + "node": mapstr.M{ "id": nodeID, "mlockall": mlockall, "master": isMaster, }, - "cluster": common.MapStr{ + "cluster": mapstr.M{ "name": info.ClusterName, "id": info.ClusterID, }, diff --git a/metricbeat/module/elasticsearch/pending_tasks/data.go b/metricbeat/module/elasticsearch/pending_tasks/data.go index e3ab8d6909b..1e09dd169f6 100644 --- a/metricbeat/module/elasticsearch/pending_tasks/data.go +++ b/metricbeat/module/elasticsearch/pending_tasks/data.go @@ -23,12 +23,12 @@ import ( "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/elasticsearch" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -58,10 +58,10 @@ func eventsMapping(r mb.ReporterV2, info elasticsearch.Info, content []byte, isX for _, task := range tasksStruct.Tasks { event := mb.Event{} - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("service.name", elasticsearch.ModuleName) - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} event.ModuleFields.Put("cluster.name", info.ClusterName) event.ModuleFields.Put("cluster.id", info.ClusterID) diff --git a/metricbeat/module/elasticsearch/pending_tasks/data_test.go b/metricbeat/module/elasticsearch/pending_tasks/data_test.go index 3a14d7c6258..27c0645108e 100644 --- a/metricbeat/module/elasticsearch/pending_tasks/data_test.go +++ b/metricbeat/module/elasticsearch/pending_tasks/data_test.go @@ -28,10 +28,10 @@ import ( "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/elasticsearch" + "github.com/elastic/elastic-agent-libs/mapstr" ) var info = elasticsearch.Info{ @@ -113,18 +113,18 @@ func TestEventsMappedMatchToContentReceived(t *testing.T) { {"./_meta/test/empty.json", []mb.Event(nil)}, {"./_meta/test/task.622.json", []mb.Event{ { - RootFields: common.MapStr{ - "service": common.MapStr{ + RootFields: mapstr.M{ + "service": mapstr.M{ "name": "elasticsearch", }, }, - ModuleFields: common.MapStr{ - "cluster": common.MapStr{ + ModuleFields: mapstr.M{ + "cluster": mapstr.M{ "id": "1234", "name": "helloworld", }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "priority": "URGENT", "source": "create-index [foo_9], cause [api]", "time_in_queue.ms": int64(86), @@ -136,18 +136,18 @@ func TestEventsMappedMatchToContentReceived(t *testing.T) { }}, {"./_meta/test/tasks.622.json", []mb.Event{ { - RootFields: common.MapStr{ - "service": common.MapStr{ + RootFields: mapstr.M{ + "service": mapstr.M{ "name": "elasticsearch", }, }, - ModuleFields: common.MapStr{ - "cluster": common.MapStr{ + ModuleFields: mapstr.M{ + "cluster": mapstr.M{ "id": "1234", "name": "helloworld", }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "priority": "URGENT", "source": "create-index [foo_9], cause [api]", "time_in_queue.ms": int64(86), @@ -157,18 +157,18 @@ func TestEventsMappedMatchToContentReceived(t *testing.T) { Took: 0, }, { - RootFields: common.MapStr{ - "service": common.MapStr{ + RootFields: mapstr.M{ + "service": mapstr.M{ "name": "elasticsearch", }, }, - ModuleFields: common.MapStr{ - "cluster": common.MapStr{ + ModuleFields: mapstr.M{ + "cluster": mapstr.M{ "id": "1234", "name": "helloworld", }, }, - MetricSetFields: common.MapStr{"priority": "HIGH", + MetricSetFields: mapstr.M{"priority": "HIGH", "source": "shard-started ([foo_2][1], node[tMTocMvQQgGCkj7QDHl3OA], [P], s[INITIALIZING]), reason [after recovery from shard_store]", "time_in_queue.ms": int64(842), "insert_order": int64(46), @@ -176,18 +176,18 @@ func TestEventsMappedMatchToContentReceived(t *testing.T) { Timestamp: time.Time{}, Took: 0, }, { - RootFields: common.MapStr{ - "service": common.MapStr{ + RootFields: mapstr.M{ + "service": mapstr.M{ "name": "elasticsearch", }, }, - ModuleFields: common.MapStr{ - "cluster": common.MapStr{ + ModuleFields: mapstr.M{ + "cluster": mapstr.M{ "id": "1234", "name": "helloworld", }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "priority": "HIGH", "source": "shard-started ([foo_2][0], node[tMTocMvQQgGCkj7QDHl3OA], [P], s[INITIALIZING]), reason [after recovery from shard_store]", "time_in_queue.ms": int64(858), diff --git a/metricbeat/module/elasticsearch/shard/data.go b/metricbeat/module/elasticsearch/shard/data.go index 575be1d7184..b7bc6903075 100644 --- a/metricbeat/module/elasticsearch/shard/data.go +++ b/metricbeat/module/elasticsearch/shard/data.go @@ -22,11 +22,11 @@ import ( "strconv" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -69,7 +69,7 @@ func eventsMapping(r mb.ReporterV2, content []byte, isXpack bool) error { for _, shards := range index.Shards { for _, shard := range shards { event := mb.Event{ - ModuleFields: common.MapStr{}, + ModuleFields: mapstr.M{}, } event.ModuleFields.Put("cluster.state.id", stateData.StateID) @@ -148,19 +148,19 @@ func eventsMapping(r mb.ReporterV2, content []byte, isXpack bool) error { return errs.Err() } -func getSourceNode(nodeID string, stateData *stateStruct) (common.MapStr, error) { +func getSourceNode(nodeID string, stateData *stateStruct) (mapstr.M, error) { nodeInfo, ok := stateData.Nodes[nodeID] if !ok { return nil, elastic.MakeErrorForMissingField("nodes."+nodeID, elastic.Elasticsearch) } - return common.MapStr{ + return mapstr.M{ "uuid": nodeID, "name": nodeInfo.Name, }, nil } -func generateHashForEvent(stateID string, shard common.MapStr) (string, error) { +func generateHashForEvent(stateID string, shard mapstr.M) (string, error) { var nodeID string if shard["node"] == nil { nodeID = "_na" diff --git a/metricbeat/module/envoyproxy/server/data.go b/metricbeat/module/envoyproxy/server/data.go index 850d8c53914..ba54ba79f45 100644 --- a/metricbeat/module/envoyproxy/server/data.go +++ b/metricbeat/module/envoyproxy/server/data.go @@ -21,9 +21,9 @@ import ( "regexp" "strings" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -103,9 +103,9 @@ var ( ) var reStats *regexp.Regexp = regexp.MustCompile(`cluster_manager.*|filesystem.*|access_log_file.*|runtime.*|listener_manager.*|stats.*|server.*|http2\..*`) -func eventMapping(response []byte) (common.MapStr, error) { +func eventMapping(response []byte) (mapstr.M, error) { data := map[string]interface{}{} - var events common.MapStr + var events mapstr.M var err error data = findStats(data, response) @@ -116,7 +116,7 @@ func eventMapping(response []byte) (common.MapStr, error) { return events, nil } -func findStats(data common.MapStr, response []byte) common.MapStr { +func findStats(data mapstr.M, response []byte) mapstr.M { matches := reStats.FindAllString(string(response), -1) for i := 0; i < len(matches); i++ { entries := strings.Split(matches[i], ": ") diff --git a/metricbeat/module/envoyproxy/server/server_test.go b/metricbeat/module/envoyproxy/server/server_test.go index 50bed4bb107..6db24061360 100644 --- a/metricbeat/module/envoyproxy/server/server_test.go +++ b/metricbeat/module/envoyproxy/server/server_test.go @@ -26,8 +26,8 @@ import ( "testing" "time" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -43,7 +43,7 @@ func TestEventMapping(t *testing.T) { assert.Len(t, event, 7, "got wrong number of event") - clusterManager := event["cluster_manager"].(common.MapStr) + clusterManager := event["cluster_manager"].(mapstr.M) assert.Equal(t, int64(1), clusterManager["active_clusters"]) assert.Equal(t, int64(1), clusterManager["cluster_added"]) assert.Equal(t, int64(0), clusterManager["cluster_modified"]) @@ -54,7 +54,7 @@ func TestEventMapping(t *testing.T) { assert.Equal(t, int64(0), clusterManager["update_merge_cancelled"]) assert.Equal(t, int64(0), clusterManager["update_out_of_merge_window"]) - fileSystem := event["filesystem"].(common.MapStr) + fileSystem := event["filesystem"].(mapstr.M) assert.Equal(t, int64(389), fileSystem["flushed_by_timer"]) assert.Equal(t, int64(0), fileSystem["reopen_failed"]) assert.Equal(t, int64(44), fileSystem["write_buffered"]) @@ -63,7 +63,7 @@ func TestEventMapping(t *testing.T) { assert.Equal(t, int64(0), fileSystem["write_total_buffered"]) assert.Equal(t, int64(0), fileSystem["write_failed"]) - listenerManager := event["listener_manager"].(common.MapStr) + listenerManager := event["listener_manager"].(mapstr.M) assert.Equal(t, int64(1), listenerManager["listener_added"]) assert.Equal(t, int64(0), listenerManager["listener_create_failure"]) assert.Equal(t, int64(4), listenerManager["listener_create_success"]) @@ -74,7 +74,7 @@ func TestEventMapping(t *testing.T) { assert.Equal(t, int64(0), listenerManager["total_listeners_warming"]) assert.Equal(t, int64(0), listenerManager["listener_stopped"]) - runtime := event["runtime"].(common.MapStr) + runtime := event["runtime"].(mapstr.M) assert.Equal(t, int64(0), runtime["admin_overrides_active"]) assert.Equal(t, int64(0), runtime["load_error"]) assert.Equal(t, int64(0), runtime["load_success"]) @@ -84,7 +84,7 @@ func TestEventMapping(t *testing.T) { assert.Equal(t, int64(0), runtime["deprecated_feature_use"]) assert.Equal(t, int64(2), runtime["num_layers"]) - server := event["server"].(common.MapStr) + server := event["server"].(mapstr.M) assert.Equal(t, int64(2147483647), server["days_until_first_cert_expiring"]) assert.Equal(t, int64(1), server["live"]) assert.Equal(t, int64(3120760), server["memory_allocated"]) @@ -103,10 +103,10 @@ func TestEventMapping(t *testing.T) { assert.Equal(t, int64(0), server["static_unknown_fields"]) assert.Equal(t, int64(0), server["stats_recent_lookups"]) - stats := event["stats"].(common.MapStr) + stats := event["stats"].(mapstr.M) assert.Equal(t, int64(0), stats["overflow"]) - http2 := event["http2"].(common.MapStr) + http2 := event["http2"].(mapstr.M) assert.Equal(t, int64(0), http2["header_overflow"]) assert.Equal(t, int64(0), http2["headers_cb_no_stream"]) assert.Equal(t, int64(0), http2["rx_reset"]) @@ -145,7 +145,7 @@ func TestFetchEventContent(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) } -func testValue(t *testing.T, event common.MapStr, field string, value interface{}) { +func testValue(t *testing.T, event mapstr.M, field string, value interface{}) { data, err := event.GetValue(field) assert.NoError(t, err, "Could not read field "+field) assert.EqualValues(t, data, value, "Wrong value for field "+field) diff --git a/metricbeat/module/etcd/leader/data.go b/metricbeat/module/etcd/leader/data.go index 15322921b37..d39b1b6b89e 100644 --- a/metricbeat/module/etcd/leader/data.go +++ b/metricbeat/module/etcd/leader/data.go @@ -20,7 +20,7 @@ package leader import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type Counts struct { @@ -46,10 +46,10 @@ type Leader struct { Leader string `json:"leader"` } -func eventMapping(content []byte) common.MapStr { +func eventMapping(content []byte) mapstr.M { var data Leader json.Unmarshal(content, &data) - event := common.MapStr{ + event := mapstr.M{ "followers": data.Followers, "leader": data.Leader, } diff --git a/metricbeat/module/etcd/leader/leader.go b/metricbeat/module/etcd/leader/leader.go index 940be4a0f79..b823b56fca3 100644 --- a/metricbeat/module/etcd/leader/leader.go +++ b/metricbeat/module/etcd/leader/leader.go @@ -25,8 +25,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" @@ -104,7 +104,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { if res.StatusCode == http.StatusOK { reporter.Event(mb.Event{ MetricSetFields: eventMapping(content), - ModuleFields: common.MapStr{"api_version": apiVersion}, + ModuleFields: mapstr.M{"api_version": apiVersion}, }) return nil } diff --git a/metricbeat/module/etcd/self/data.go b/metricbeat/module/etcd/self/data.go index fd3bfe23e05..fb318d90257 100644 --- a/metricbeat/module/etcd/self/data.go +++ b/metricbeat/module/etcd/self/data.go @@ -20,7 +20,7 @@ package self import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type LeaderInfo struct { @@ -59,26 +59,26 @@ type Self struct { State string `json:"state"` } -func eventMapping(content []byte) common.MapStr { +func eventMapping(content []byte) mapstr.M { var data Self json.Unmarshal(content, &data) - event := common.MapStr{ + event := mapstr.M{ "id": data.ID, - "leaderinfo": common.MapStr{ + "leaderinfo": mapstr.M{ "leader": data.LeaderInfo.Leader, "starttime": data.LeaderInfo.StartTime, "uptime": data.LeaderInfo.Uptime, }, "name": data.Name, - "recv": common.MapStr{ - "appendrequest": common.MapStr{ + "recv": mapstr.M{ + "appendrequest": mapstr.M{ "count": data.Recv.Appendrequest.Count, }, "bandwidthrate": data.Recv.Bandwidthrate, "pkgrate": data.Recv.Pkgrate, }, - "send": common.MapStr{ - "appendrequest": common.MapStr{ + "send": mapstr.M{ + "appendrequest": mapstr.M{ "count": data.Send.AppendRequest.Cnt, }, "bandwidthrate": data.Send.BandwidthRate, diff --git a/metricbeat/module/etcd/self/self.go b/metricbeat/module/etcd/self/self.go index 30f649db4c8..066f4048079 100644 --- a/metricbeat/module/etcd/self/self.go +++ b/metricbeat/module/etcd/self/self.go @@ -20,10 +20,10 @@ package self import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -79,7 +79,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { reporter.Event(mb.Event{ MetricSetFields: eventMapping(content), - ModuleFields: common.MapStr{"api_version": apiVersion}, + ModuleFields: mapstr.M{"api_version": apiVersion}, }) return nil diff --git a/metricbeat/module/etcd/store/data.go b/metricbeat/module/etcd/store/data.go index de8271d6f20..8c0a2249dc4 100644 --- a/metricbeat/module/etcd/store/data.go +++ b/metricbeat/module/etcd/store/data.go @@ -20,10 +20,9 @@ package store import ( "encoding/json" - "github.com/elastic/beats/v7/libbeat/common" - s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -63,7 +62,7 @@ var ( } ) -func eventMapping(content []byte) common.MapStr { +func eventMapping(content []byte) mapstr.M { var data map[string]interface{} json.Unmarshal(content, &data) event, _ := schema.Apply(data) diff --git a/metricbeat/module/etcd/store/store.go b/metricbeat/module/etcd/store/store.go index 44baaf873fd..00662ccbd00 100644 --- a/metricbeat/module/etcd/store/store.go +++ b/metricbeat/module/etcd/store/store.go @@ -20,10 +20,10 @@ package store import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -78,7 +78,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { reporter.Event(mb.Event{ MetricSetFields: eventMapping(content), - ModuleFields: common.MapStr{"api_version": apiVersion}, + ModuleFields: mapstr.M{"api_version": apiVersion}, }) return nil diff --git a/metricbeat/module/etcd/store/store_test.go b/metricbeat/module/etcd/store/store_test.go index cb1173e3eb4..a3a72ec6a13 100644 --- a/metricbeat/module/etcd/store/store_test.go +++ b/metricbeat/module/etcd/store/store_test.go @@ -25,8 +25,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "testing" ) @@ -37,7 +37,7 @@ func TestEventMapping(t *testing.T) { event := eventMapping(content) - assert.Equal(t, event["gets"].(common.MapStr)["fail"], int64(3)) + assert.Equal(t, event["gets"].(mapstr.M)["fail"], int64(3)) } func TestFetchEventContent(t *testing.T) { diff --git a/metricbeat/module/golang/heap/data.go b/metricbeat/module/golang/heap/data.go index ad052e4348e..56c59a6bee8 100644 --- a/metricbeat/module/golang/heap/data.go +++ b/metricbeat/module/golang/heap/data.go @@ -20,8 +20,8 @@ package heap import ( "runtime" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/module/golang" + "github.com/elastic/elastic-agent-libs/mapstr" ) //Stats contains the memory info that we get from the fetch request @@ -30,15 +30,15 @@ type Stats struct { Cmdline []interface{} } -func eventMapping(stats Stats, m *MetricSet) common.MapStr { - var event = common.MapStr{ +func eventMapping(stats Stats, m *MetricSet) mapstr.M { + var event = mapstr.M{ "cmdline": golang.GetCmdStr(stats.Cmdline), } //currentNumGC ms := &stats.MemStats // add heap summary - event["allocations"] = common.MapStr{ + event["allocations"] = mapstr.M{ "mallocs": ms.Mallocs, "frees": ms.Frees, "objects": ms.HeapObjects, @@ -50,7 +50,7 @@ func eventMapping(stats Stats, m *MetricSet) common.MapStr { "active": ms.HeapInuse, } - event["system"] = common.MapStr{ + event["system"] = mapstr.M{ "total": ms.Sys, "obtained": ms.HeapSys, "stack": ms.StackSys, @@ -84,22 +84,22 @@ func eventMapping(stats Stats, m *MetricSet) common.MapStr { m.lastNumGC = ms.NumGC } - event["gc"] = common.MapStr{ + event["gc"] = mapstr.M{ "next_gc_limit": ms.NextGC, "total_count": ms.NumGC, "cpu_fraction": ms.GCCPUFraction, - "total_pause": common.MapStr{ + "total_pause": mapstr.M{ "ns": ms.PauseTotalNs, }, - "pause": common.MapStr{ + "pause": mapstr.M{ "count": count, - "sum": common.MapStr{ + "sum": mapstr.M{ "ns": duration, }, - "avg": common.MapStr{ + "avg": mapstr.M{ "ns": avgDuration, }, - "max": common.MapStr{ + "max": mapstr.M{ "ns": maxDuration, }, }, diff --git a/metricbeat/module/graphite/server/data.go b/metricbeat/module/graphite/server/data.go index df6fb7ee515..202cb11c0e7 100644 --- a/metricbeat/module/graphite/server/data.go +++ b/metricbeat/module/graphite/server/data.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type template struct { @@ -76,7 +77,7 @@ func (m *metricProcessor) RemoveTemplate(template TemplateConfig) { m.Unlock() } -func (m *metricProcessor) Process(message string) (common.MapStr, error) { +func (m *metricProcessor) Process(message string) (mapstr.M, error) { metric, timestamp, value, err := m.splitMetric(message) if err != nil { return nil, err @@ -86,7 +87,7 @@ func (m *metricProcessor) Process(message string) (common.MapStr, error) { t := m.FindTemplate(parts) var name, namespace string - var tags common.MapStr + var tags mapstr.M if t == nil { name, tags = m.defaultTemplate.Apply(parts) namespace = m.defaultTemplate.Namespace @@ -95,7 +96,7 @@ func (m *metricProcessor) Process(message string) (common.MapStr, error) { namespace = t.Namespace } - event := common.MapStr{ + event := mapstr.M{ "@timestamp": timestamp, name: value, mb.NamespaceKey: namespace, @@ -151,8 +152,8 @@ func (m *metricProcessor) splitMetric(metric string) (string, common.Time, float return metricName, timestamp, value, nil } -func (t *template) Apply(parts []string) (string, common.MapStr) { - tags := make(common.MapStr) +func (t *template) Apply(parts []string) (string, mapstr.M) { + tags := make(mapstr.M) metric := make([]string, 0) for tagKey, tagVal := range t.Tags { diff --git a/metricbeat/module/graphite/server/data_test.go b/metricbeat/module/graphite/server/data_test.go index 208d7d30d2c..dbc9dc6bc6c 100644 --- a/metricbeat/module/graphite/server/data_test.go +++ b/metricbeat/module/graphite/server/data_test.go @@ -28,6 +28,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func GetMetricProcessor() *metricProcessor { @@ -88,7 +89,7 @@ func TestMetricProcessorProcess(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, event) - tag := event["tag"].(common.MapStr) + tag := event["tag"].(mapstr.M) assert.Equal(t, len(tag), 2) assert.Equal(t, tag["host"], "localhost") assert.Equal(t, tag["shell"], "bash") diff --git a/metricbeat/module/haproxy/info/data.go b/metricbeat/module/haproxy/info/data.go index 94837022789..b52c3fe9c48 100644 --- a/metricbeat/module/haproxy/info/data.go +++ b/metricbeat/module/haproxy/info/data.go @@ -20,11 +20,11 @@ package info import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/haproxy" + "github.com/elastic/elastic-agent-libs/mapstr" "reflect" "strconv" @@ -196,7 +196,7 @@ func eventMapping(info *haproxy.Info, r mb.ReporterV2) (mb.Event, error) { } event := mb.Event{ - RootFields: common.MapStr{}, + RootFields: mapstr.M{}, } fields, err := schema.Apply(source) diff --git a/metricbeat/module/haproxy/stat/data.go b/metricbeat/module/haproxy/stat/data.go index f0657a9b433..964e029c81a 100644 --- a/metricbeat/module/haproxy/stat/data.go +++ b/metricbeat/module/haproxy/stat/data.go @@ -20,11 +20,11 @@ package stat import ( "reflect" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/haproxy" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -187,7 +187,7 @@ func eventMapping(info []*haproxy.Stat, r mb.ReporterV2) { fields, _ := schema.Apply(source) event := mb.Event{ - RootFields: common.MapStr{}, + RootFields: mapstr.M{}, } if processID, err := fields.GetValue("process_id"); err == nil { diff --git a/metricbeat/module/http/json/data.go b/metricbeat/module/http/json/data.go index feabe8657f5..341fa4b310f 100644 --- a/metricbeat/module/http/json/data.go +++ b/metricbeat/module/http/json/data.go @@ -24,23 +24,24 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func (m *MetricSet) processBody(response *http.Response, jsonBody interface{}) mb.Event { - var event common.MapStr + var event mapstr.M if m.deDotEnabled { - event = common.DeDotJSON(jsonBody).(common.MapStr) + event = common.DeDotJSON(jsonBody).(mapstr.M) } else { - event = jsonBody.(common.MapStr) + event = jsonBody.(mapstr.M) } if m.requestEnabled { - event[mb.ModuleDataKey] = common.MapStr{ - "request": common.MapStr{ + event[mb.ModuleDataKey] = mapstr.M{ + "request": mapstr.M{ "headers": m.getHeaders(response.Request.Header), "method": response.Request.Method, - "body": common.MapStr{ + "body": mapstr.M{ "content": m.body, }, }, @@ -49,8 +50,8 @@ func (m *MetricSet) processBody(response *http.Response, jsonBody interface{}) m if m.responseEnabled { phrase := strings.TrimPrefix(response.Status, strconv.Itoa(response.StatusCode)+" ") - event[mb.ModuleDataKey] = common.MapStr{ - "response": common.MapStr{ + event[mb.ModuleDataKey] = mapstr.M{ + "response": mapstr.M{ "code": response.StatusCode, "phrase": phrase, "headers": m.getHeaders(response.Header), diff --git a/metricbeat/module/http/json/json.go b/metricbeat/module/http/json/json.go index 8a0f549c876..0f2e0f2ff1a 100644 --- a/metricbeat/module/http/json/json.go +++ b/metricbeat/module/http/json/json.go @@ -23,10 +23,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry. @@ -135,7 +135,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } if m.jsonIsArray { - var jsonBodyArr []common.MapStr + var jsonBodyArr []mapstr.M if err = json.Unmarshal(body, &jsonBodyArr); err != nil { return err } @@ -149,7 +149,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } } } else { - var jsonBody common.MapStr + var jsonBody mapstr.M if err = json.Unmarshal(body, &jsonBody); err != nil { return err } diff --git a/metricbeat/module/http/server/config.go b/metricbeat/module/http/server/config.go index 065001b06df..12172ab47ba 100644 --- a/metricbeat/module/http/server/config.go +++ b/metricbeat/module/http/server/config.go @@ -20,7 +20,7 @@ package server import ( "errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type HttpServerConfig struct { @@ -29,9 +29,9 @@ type HttpServerConfig struct { } type PathConfig struct { - Path string `config:"path"` - Fields common.MapStr `config:"fields"` - Namespace string `config:"namespace"` + Path string `config:"path"` + Fields mapstr.M `config:"fields"` + Namespace string `config:"namespace"` } func defaultHttpServerConfig() HttpServerConfig { diff --git a/metricbeat/module/http/server/data.go b/metricbeat/module/http/server/data.go index a65c95e3cae..f202a2fe32a 100644 --- a/metricbeat/module/http/server/data.go +++ b/metricbeat/module/http/server/data.go @@ -24,9 +24,9 @@ import ( "strings" "sync" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/server" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type metricProcessor struct { @@ -59,7 +59,7 @@ func (m *metricProcessor) RemovePath(path PathConfig) { m.Unlock() } -func (p *metricProcessor) Process(event server.Event) (common.MapStr, error) { +func (p *metricProcessor) Process(event server.Event) (mapstr.M, error) { urlRaw, ok := event.GetMeta()["path"] if !ok { return nil, errors.New("Malformed HTTP event. Path missing.") @@ -83,7 +83,7 @@ func (p *metricProcessor) Process(event server.Event) (common.MapStr, error) { return nil, errors.New("Request has no data") } - out := common.MapStr{} + out := mapstr.M{} switch contentType { case "application/json": err := json.Unmarshal(bytes, &out) @@ -97,7 +97,7 @@ func (p *metricProcessor) Process(event server.Event) (common.MapStr, error) { out[mb.NamespaceKey] = pathConf.Namespace if len(pathConf.Fields) != 0 { // Overwrite any keys that are present in the incoming payload - common.MergeFields(out, pathConf.Fields, true) + mapstr.MergeFields(out, pathConf.Fields, true) } return out, nil } diff --git a/metricbeat/module/http/server/data_test.go b/metricbeat/module/http/server/data_test.go index b4ef1be5e1a..79f7e34e8a3 100644 --- a/metricbeat/module/http/server/data_test.go +++ b/metricbeat/module/http/server/data_test.go @@ -23,7 +23,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func GetMetricProcessor() *metricProcessor { @@ -31,7 +31,7 @@ func GetMetricProcessor() *metricProcessor { { Namespace: "foo", Path: "/foo", - Fields: common.MapStr{ + Fields: mapstr.M{ "a": "b", }, }, diff --git a/metricbeat/module/jolokia/jmx/config.go b/metricbeat/module/jolokia/jmx/config.go index f0949a9fb7d..20532b440c3 100644 --- a/metricbeat/module/jolokia/jmx/config.go +++ b/metricbeat/module/jolokia/jmx/config.go @@ -26,8 +26,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type JMXMapping struct { @@ -245,8 +245,8 @@ type JolokiaHTTPRequestFetcher interface { // BuildRequestsAndMappings builds the request information and mappings needed to fetch information from Jolokia server BuildRequestsAndMappings(configMappings []JMXMapping) ([]*JolokiaHTTPRequest, AttributeMapping, error) // Fetches the information from Jolokia server regarding MBeans - Fetch(m *MetricSet) ([]common.MapStr, error) - EventMapping(content []byte, mapping AttributeMapping) ([]common.MapStr, error) + Fetch(m *MetricSet) ([]mapstr.M, error) + EventMapping(content []byte, mapping AttributeMapping) ([]mapstr.M, error) } // JolokiaHTTPGetFetcher constructs and executes an HTTP GET request @@ -341,9 +341,9 @@ func (pc *JolokiaHTTPGetFetcher) buildGetRequestURIs(mappings []JMXMapping) ([]s } // Fetch perfrorms one or more GET requests to Jolokia server and gets information about MBeans. -func (pc *JolokiaHTTPGetFetcher) Fetch(m *MetricSet) ([]common.MapStr, error) { +func (pc *JolokiaHTTPGetFetcher) Fetch(m *MetricSet) ([]mapstr.M, error) { - var allEvents []common.MapStr + var allEvents []mapstr.M // Prepare Http request objects and attribute mappings according to selected Http method httpReqs, mapping, err := pc.BuildRequestsAndMappings(m.mapping) @@ -387,7 +387,7 @@ func (pc *JolokiaHTTPGetFetcher) Fetch(m *MetricSet) ([]common.MapStr, error) { } // EventMapping maps a Jolokia response from a GET request is to one or more Metricbeat events -func (pc *JolokiaHTTPGetFetcher) EventMapping(content []byte, mapping AttributeMapping) ([]common.MapStr, error) { +func (pc *JolokiaHTTPGetFetcher) EventMapping(content []byte, mapping AttributeMapping) ([]mapstr.M, error) { var singleEntry Entry @@ -470,7 +470,7 @@ func (pc *JolokiaHTTPPostFetcher) buildRequestBodyAndMapping(mappings []JMXMappi } // Fetch perfrorms a POST request to Jolokia server and gets information about MBeans. -func (pc *JolokiaHTTPPostFetcher) Fetch(m *MetricSet) ([]common.MapStr, error) { +func (pc *JolokiaHTTPPostFetcher) Fetch(m *MetricSet) ([]mapstr.M, error) { // Prepare Http POST request object and attribute mappings according to selected Http method httpReqs, mapping, err := pc.BuildRequestsAndMappings(m.mapping) @@ -509,7 +509,7 @@ func (pc *JolokiaHTTPPostFetcher) Fetch(m *MetricSet) ([]common.MapStr, error) { } // EventMapping maps a Jolokia response from a POST request is to one or more Metricbeat events -func (pc *JolokiaHTTPPostFetcher) EventMapping(content []byte, mapping AttributeMapping) ([]common.MapStr, error) { +func (pc *JolokiaHTTPPostFetcher) EventMapping(content []byte, mapping AttributeMapping) ([]mapstr.M, error) { var entries []Entry diff --git a/metricbeat/module/jolokia/jmx/data.go b/metricbeat/module/jolokia/jmx/data.go index b7e0913fec9..7dd1162bf14 100644 --- a/metricbeat/module/jolokia/jmx/data.go +++ b/metricbeat/module/jolokia/jmx/data.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -39,7 +40,7 @@ type Entry struct { Value interface{} } -// Map responseBody to common.MapStr +// Map responseBody to mapstr.M // // A response has the following structure // [ @@ -111,11 +112,11 @@ type eventKey struct { mbean, event string } -func eventMapping(entries []Entry, mapping AttributeMapping) ([]common.MapStr, error) { +func eventMapping(entries []Entry, mapping AttributeMapping) ([]mapstr.M, error) { // Generate a different event for each wildcard mbean, and and additional one // for non-wildcard requested mbeans, group them by event name if defined - mbeanEvents := make(map[eventKey]common.MapStr) + mbeanEvents := make(map[eventKey]mapstr.M) var errs multierror.Errors for _, v := range entries { @@ -140,7 +141,7 @@ func eventMapping(entries []Entry, mapping AttributeMapping) ([]common.MapStr, e } } - var events []common.MapStr + var events []mapstr.M for _, event := range mbeanEvents { events = append(events, event) } @@ -148,7 +149,7 @@ func eventMapping(entries []Entry, mapping AttributeMapping) ([]common.MapStr, e return events, errs.Err() } -func constructEvents(entryValues map[string]interface{}, v Entry, mbeanEvents map[eventKey]common.MapStr, mapping AttributeMapping, errs multierror.Errors) { +func constructEvents(entryValues map[string]interface{}, v Entry, mbeanEvents map[eventKey]mapstr.M, mapping AttributeMapping, errs multierror.Errors) { hasWildcard := strings.Contains(v.Request.Mbean, "*") for attribute, value := range entryValues { if !hasWildcard { @@ -178,10 +179,10 @@ func constructEvents(entryValues map[string]interface{}, v Entry, mbeanEvents ma } } -func selectEvent(events map[eventKey]common.MapStr, key eventKey) common.MapStr { +func selectEvent(events map[eventKey]mapstr.M, key eventKey) mapstr.M { event, found := events[key] if !found { - event = common.MapStr{} + event = mapstr.M{} if key.mbean != "" { event.Put(mbeanEventKey, key.mbean) } @@ -195,7 +196,7 @@ func parseResponseEntry( responseMbeanName string, attributeName string, attributeValue interface{}, - events map[eventKey]common.MapStr, + events map[eventKey]mapstr.M, mapping AttributeMapping, ) error { field, exists := mapping.Get(requestMbeanName, attributeName) diff --git a/metricbeat/module/jolokia/jmx/data_test.go b/metricbeat/module/jolokia/jmx/data_test.go index b9b3c9d5275..8357bb5d455 100644 --- a/metricbeat/module/jolokia/jmx/data_test.go +++ b/metricbeat/module/jolokia/jmx/data_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEventMapper(t *testing.T) { @@ -62,14 +62,14 @@ func TestEventMapper(t *testing.T) { require.NoError(t, err) - expected := []common.MapStr{ + expected := []mapstr.M{ { "uptime": float64(47283), - "gc": common.MapStr{ + "gc": mapstr.M{ "cms_collection_time": float64(53), "cms_collection_count": float64(1), }, - "memory": common.MapStr{ + "memory": mapstr.M{ "heap_usage": map[string]interface{}{ "init": float64(1073741824), "committed": float64(1037959168), @@ -133,7 +133,7 @@ func TestEventGroupingMapper(t *testing.T) { require.NoError(t, err) - expected := []common.MapStr{ + expected := []mapstr.M{ { "uptime": float64(47283), "metrics": map[string]interface{}{ @@ -145,13 +145,13 @@ func TestEventGroupingMapper(t *testing.T) { "server_info": "Apache Tomcat/9.0.7", }, { - "gc": common.MapStr{ + "gc": mapstr.M{ "cms_collection_time": float64(53), "cms_collection_count": float64(1), }, }, { - "memory": common.MapStr{ + "memory": mapstr.M{ "heap_usage": map[string]interface{}{ "init": float64(1073741824), "committed": float64(1037959168), @@ -200,9 +200,9 @@ func TestEventGroupingMapperGetRequest(t *testing.T) { require.NoError(t, err) - expected := []common.MapStr{ + expected := []mapstr.M{ { - "memory": common.MapStr{ + "memory": mapstr.M{ "heap_usage": map[string]interface{}{ "init": float64(1073741824), "committed": float64(1037959168), @@ -247,9 +247,9 @@ func TestEventGroupingMapperGetRequestUptime(t *testing.T) { require.NoError(t, err) - expected := []common.MapStr{ + expected := []mapstr.M{ { - "runtime": common.MapStr{ + "runtime": mapstr.M{ "uptime": float64(88622), }, }, @@ -283,7 +283,7 @@ func TestEventMapperWithWildcard(t *testing.T) { require.NoError(t, err) require.Equal(t, 2, len(events)) - expected := []common.MapStr{ + expected := []mapstr.M{ { "mbean": "Catalina:name=\"http-bio-8080\",type=ThreadPool", "max_connections": float64(200), @@ -324,7 +324,7 @@ func TestEventGroupingMapperWithWildcard(t *testing.T) { require.NoError(t, err) require.Equal(t, 4, len(events)) - expected := []common.MapStr{ + expected := []mapstr.M{ { "mbean": "Catalina:name=\"http-bio-8080\",type=ThreadPool", "port": float64(8080), diff --git a/metricbeat/module/jolokia/jmx/jmx.go b/metricbeat/module/jolokia/jmx/jmx.go index f4500b2c1b2..7fc5e8e0669 100644 --- a/metricbeat/module/jolokia/jmx/jmx.go +++ b/metricbeat/module/jolokia/jmx/jmx.go @@ -19,8 +19,8 @@ package jmx import ( "github.com/elastic/beats/v7/metricbeat/helper" + "github.com/elastic/elastic-agent-libs/mapstr" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" @@ -95,7 +95,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // format. It publishes the event which is then forwarded to the output. In case // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { - var allEvents []common.MapStr + var allEvents []mapstr.M allEvents, err := m.jolokia.Fetch(m) if err != nil { diff --git a/metricbeat/module/kafka/consumergroup/consumergroup.go b/metricbeat/module/kafka/consumergroup/consumergroup.go index 875d0f38cc5..82853e6f653 100644 --- a/metricbeat/module/kafka/consumergroup/consumergroup.go +++ b/metricbeat/module/kafka/consumergroup/consumergroup.go @@ -22,10 +22,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kafka" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry. @@ -85,21 +85,21 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } defer broker.Close() - brokerInfo := common.MapStr{ + brokerInfo := mapstr.M{ "id": broker.ID(), "address": broker.AdvertisedAddr(), } - emitEvent := func(event common.MapStr) { + emitEvent := func(event mapstr.M) { // Helpful IDs to avoid scripts on queries partitionTopicID := fmt.Sprintf("%d-%s", event["partition"], event["topic"]) - moduleFields := common.MapStr{ + moduleFields := mapstr.M{ "broker": brokerInfo, - "topic": common.MapStr{ + "topic": mapstr.M{ "name": event["topic"], }, - "partition": common.MapStr{ + "partition": mapstr.M{ "id": event["partition"], "topic_id": partitionTopicID, }, diff --git a/metricbeat/module/kafka/consumergroup/query.go b/metricbeat/module/kafka/consumergroup/query.go index cb200db83b5..bd4ff07bb78 100644 --- a/metricbeat/module/kafka/consumergroup/query.go +++ b/metricbeat/module/kafka/consumergroup/query.go @@ -20,9 +20,9 @@ package consumergroup import ( "github.com/Shopify/sarama" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/module/kafka" + "github.com/elastic/elastic-agent-libs/mapstr" ) type client interface { @@ -33,7 +33,7 @@ type client interface { } func fetchGroupInfo( - emit func(common.MapStr), + emit func(mapstr.M), b client, groupsFilter, topicsFilter func(string) bool, ) error { @@ -120,21 +120,21 @@ func fetchGroupInfo( continue } consumerLag := partitionOffset - info.Offset - event := common.MapStr{ + event := mapstr.M{ "id": ret.group, "topic": topic, "partition": partition, "offset": info.Offset, "meta": info.Metadata, "consumer_lag": consumerLag, - "error": common.MapStr{ + "error": mapstr.M{ "code": info.Err, }, } if asgnTopic, ok := ret.assign[topic]; ok { if assignment, found := asgnTopic[partition]; found { - event["client"] = common.MapStr{ + event["client"] = mapstr.M{ "id": assignment.clientID, "host": assignment.clientHost, "member_id": assignment.memberID, diff --git a/metricbeat/module/kafka/consumergroup/query_test.go b/metricbeat/module/kafka/consumergroup/query_test.go index 6c913fb9428..79e26da275b 100644 --- a/metricbeat/module/kafka/consumergroup/query_test.go +++ b/metricbeat/module/kafka/consumergroup/query_test.go @@ -25,11 +25,11 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetchGroupInfo(t *testing.T) { - noEvents := func(events []common.MapStr) { + noEvents := func(events []mapstr.M) { assert.Len(t, events, 0) } @@ -39,8 +39,8 @@ func TestFetchGroupInfo(t *testing.T) { groups []string topics []string err error - expected []common.MapStr - validate func([]common.MapStr) + expected []mapstr.M + validate func([]mapstr.M) }{ { name: "Test all groups", @@ -65,43 +65,43 @@ func TestFetchGroupInfo(t *testing.T) { }, }, }), - expected: []common.MapStr{ - testEvent("group1", "topic1", 0, common.MapStr{ + expected: []mapstr.M{ + testEvent("group1", "topic1", 0, mapstr.M{ "client": clientMeta(0), "offset": int64(10), "consumer_lag": int64(42) - int64(10), }), - testEvent("group1", "topic1", 1, common.MapStr{ + testEvent("group1", "topic1", 1, mapstr.M{ "client": clientMeta(1), "offset": int64(11), "consumer_lag": int64(42) - int64(11), }), - testEvent("group1", "topic1", 2, common.MapStr{ + testEvent("group1", "topic1", 2, mapstr.M{ "client": clientMeta(0), "offset": int64(12), "consumer_lag": int64(42) - int64(12), }), - testEvent("group1", "topic3", 0, common.MapStr{ + testEvent("group1", "topic3", 0, mapstr.M{ "client": clientMeta(1), "offset": int64(6), "consumer_lag": int64(42) - int64(6), }), - testEvent("group1", "topic3", 1, common.MapStr{ + testEvent("group1", "topic3", 1, mapstr.M{ "client": clientMeta(0), "offset": int64(7), "consumer_lag": int64(42) - int64(7), }), - testEvent("group2", "topic2", 0, common.MapStr{ + testEvent("group2", "topic2", 0, mapstr.M{ "client": clientMeta(0), "offset": int64(3), "consumer_lag": int64(42) - int64(3), }), - testEvent("group2", "topic3", 0, common.MapStr{ + testEvent("group2", "topic3", 0, mapstr.M{ "client": clientMeta(0), "offset": int64(9), "consumer_lag": int64(42) - int64(9), }), - testEvent("group2", "topic3", 1, common.MapStr{ + testEvent("group2", "topic3", 1, mapstr.M{ "client": clientMeta(0), "offset": int64(10), "consumer_lag": int64(42) - int64(10), @@ -133,13 +133,13 @@ func TestFetchGroupInfo(t *testing.T) { }), groups: []string{"group1"}, topics: []string{"topic1"}, - expected: []common.MapStr{ - testEvent("group1", "topic1", 0, common.MapStr{ + expected: []mapstr.M{ + testEvent("group1", "topic1", 0, mapstr.M{ "client": clientMeta(0), "offset": int64(1), "consumer_lag": int64(42) - int64(1), }), - testEvent("group1", "topic1", 1, common.MapStr{ + testEvent("group1", "topic1", 1, mapstr.M{ "client": clientMeta(0), "offset": int64(2), "consumer_lag": int64(42) - int64(2), @@ -200,14 +200,14 @@ func TestFetchGroupInfo(t *testing.T) { for i, test := range tests { t.Logf("run test (%v): %v", i, test.name) - var events []common.MapStr - collectEvents := func(event common.MapStr) { + var events []mapstr.M + collectEvents := func(event mapstr.M) { t.Logf("new event: %v", event) events = append(events, event) } - indexEvents := func(events []common.MapStr) map[string]common.MapStr { - index := map[string]common.MapStr{} + indexEvents := func(events []mapstr.M) map[string]mapstr.M { + index := map[string]mapstr.M{} for _, e := range events { key := fmt.Sprintf("%v::%v::%v", e["id"], e["topic"], e["partition"], @@ -246,7 +246,7 @@ func TestFetchGroupInfo(t *testing.T) { } } -func assertEvent(t *testing.T, expected, event common.MapStr) { +func assertEvent(t *testing.T, expected, event mapstr.M) { for field, exp := range expected { val, found := event[field] if !found { @@ -254,8 +254,8 @@ func assertEvent(t *testing.T, expected, event common.MapStr) { continue } - if sub, ok := exp.(common.MapStr); ok { - assertEvent(t, sub, val.(common.MapStr)) + if sub, ok := exp.(mapstr.M); ok { + assertEvent(t, sub, val.(mapstr.M)) } else { if !assert.Equal(t, exp, val) { t.Logf("failed in field: %v", field) @@ -270,9 +270,9 @@ func assertEvent(t *testing.T, expected, event common.MapStr) { func testEvent( group, topic string, partition int, - fields ...common.MapStr, -) common.MapStr { - event := common.MapStr{ + fields ...mapstr.M, +) mapstr.M { + event := mapstr.M{ "id": group, "topic": topic, "partition": int32(partition), @@ -286,8 +286,8 @@ func testEvent( return event } -func clientMeta(id int) common.MapStr { - return common.MapStr{ +func clientMeta(id int) mapstr.M { + return mapstr.M{ "id": fmt.Sprintf("consumer-%v", id), } } diff --git a/metricbeat/module/kafka/partition/partition.go b/metricbeat/module/kafka/partition/partition.go index 1fd58cdbbd2..bebcc45b2bd 100644 --- a/metricbeat/module/kafka/partition/partition.go +++ b/metricbeat/module/kafka/partition/partition.go @@ -24,11 +24,11 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/kafka" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the partition MetricSet with the central registry. @@ -91,19 +91,19 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { return nil } - evtBroker := common.MapStr{ + evtBroker := mapstr.M{ "id": broker.ID(), "address": broker.AdvertisedAddr(), } for _, topic := range topics { debugf("fetch events for topic: ", topic.Name) - evtTopic := common.MapStr{ + evtTopic := mapstr.M{ "name": topic.Name, } if topic.Err != 0 { - evtTopic["error"] = common.MapStr{ + evtTopic["error"] = mapstr.M{ "code": topic.Err, } } @@ -133,7 +133,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { continue } - partitionEvent := common.MapStr{ + partitionEvent := mapstr.M{ "leader": partition.Leader, "replica": id, "is_leader": partition.Leader == id, @@ -141,7 +141,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } if partition.Err != 0 { - partitionEvent["error"] = common.MapStr{ + partitionEvent["error"] = mapstr.M{ "code": partition.Err, } } @@ -151,21 +151,21 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { partitionTopicBrokerID := fmt.Sprintf("%s-%d", partitionTopicID, id) // create event - event := common.MapStr{ + event := mapstr.M{ // Common `kafka.partition` fields "id": partition.ID, "topic_id": partitionTopicID, "topic_broker_id": partitionTopicBrokerID, "partition": partitionEvent, - "offset": common.MapStr{ + "offset": mapstr.M{ "newest": offNewest, "oldest": offOldest, }, } sent := r.Event(mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "broker": evtBroker, "topic": evtTopic, }, diff --git a/metricbeat/module/kafka/partition/partition_integration_test.go b/metricbeat/module/kafka/partition/partition_integration_test.go index 976e5416f00..77673d1f5dd 100644 --- a/metricbeat/module/kafka/partition/partition_integration_test.go +++ b/metricbeat/module/kafka/partition/partition_integration_test.go @@ -30,10 +30,10 @@ import ( "github.com/Shopify/sarama" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -107,14 +107,14 @@ func TestTopic(t *testing.T) { // Its possible that other topics exists -> select the right data for _, data := range dataBefore { - if data.ModuleFields["topic"].(common.MapStr)["name"] == testTopic { - offsetBefore = data.MetricSetFields["offset"].(common.MapStr)["newest"].(int64) + if data.ModuleFields["topic"].(mapstr.M)["name"] == testTopic { + offsetBefore = data.MetricSetFields["offset"].(mapstr.M)["newest"].(int64) } } for _, data := range dataAfter { - if data.ModuleFields["topic"].(common.MapStr)["name"] == testTopic { - offsetAfter = data.MetricSetFields["offset"].(common.MapStr)["newest"].(int64) + if data.ModuleFields["topic"].(mapstr.M)["name"] == testTopic { + offsetAfter = data.MetricSetFields["offset"].(mapstr.M)["newest"].(int64) } } diff --git a/metricbeat/module/kibana/settings/data.go b/metricbeat/module/kibana/settings/data.go index 88258c9b1cc..55c6860e9f6 100644 --- a/metricbeat/module/kibana/settings/data.go +++ b/metricbeat/module/kibana/settings/data.go @@ -22,8 +22,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" @@ -54,7 +54,7 @@ func eventMapping(r mb.ReporterV2, content []byte) error { event := mb.Event{ ModuleFields: res, MetricSetFields: nil, - RootFields: make(common.MapStr), + RootFields: make(mapstr.M), } // Set service address diff --git a/metricbeat/module/kibana/stats/data.go b/metricbeat/module/kibana/stats/data.go index a2148f64e84..cceec2a00be 100644 --- a/metricbeat/module/kibana/stats/data.go +++ b/metricbeat/module/kibana/stats/data.go @@ -23,8 +23,8 @@ import ( "github.com/pkg/errors" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" @@ -115,7 +115,7 @@ func eventMapping(r mb.ReporterV2, content []byte, isXpack bool) error { return errors.Wrap(err, "failure to apply stats schema") } - event := mb.Event{ModuleFields: common.MapStr{}, RootFields: common.MapStr{}} + event := mb.Event{ModuleFields: mapstr.M{}, RootFields: mapstr.M{}} // Set elasticsearch cluster id elasticsearchClusterID, ok := data["cluster_uuid"] diff --git a/metricbeat/module/kibana/stats/stats_integration_test.go b/metricbeat/module/kibana/stats/stats_integration_test.go index e9ac99b3170..56a910e7979 100644 --- a/metricbeat/module/kibana/stats/stats_integration_test.go +++ b/metricbeat/module/kibana/stats/stats_integration_test.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" + "github.com/elastic/elastic-agent-libs/mapstr" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/kibana" @@ -93,7 +94,7 @@ func getKibanaVersion(t *testing.T, kibanaHostPort string) (*common.Version, err return nil, err } - var data common.MapStr + var data mapstr.M err = json.Unmarshal(body, &data) if err != nil { return nil, err diff --git a/metricbeat/module/kibana/status/data.go b/metricbeat/module/kibana/status/data.go index ee842b8fcd5..f02b2812d0b 100644 --- a/metricbeat/module/kibana/status/data.go +++ b/metricbeat/module/kibana/status/data.go @@ -22,12 +22,12 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kibana" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -54,7 +54,7 @@ var ( func eventMapping(r mb.ReporterV2, content []byte, isXpack bool) error { var event mb.Event - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("service.name", kibana.ModuleName) var data map[string]interface{} diff --git a/metricbeat/module/kubernetes/container/container.go b/metricbeat/module/kubernetes/container/container.go index 9dd431797f1..70cbd52647b 100644 --- a/metricbeat/module/kubernetes/container/container.go +++ b/metricbeat/module/kubernetes/container/container.go @@ -20,12 +20,12 @@ package container import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" k8smod "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -112,11 +112,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) { containerEcsFields := ecsfields(event, m.Logger()) if len(containerEcsFields) != 0 { if e.RootFields != nil { - e.RootFields.DeepUpdate(common.MapStr{ + e.RootFields.DeepUpdate(mapstr.M{ "container": containerEcsFields, }) } else { - e.RootFields = common.MapStr{ + e.RootFields = mapstr.M{ "container": containerEcsFields, } } diff --git a/metricbeat/module/kubernetes/container/container_test.go b/metricbeat/module/kubernetes/container/container_test.go index 57775c96214..a576777c7ef 100644 --- a/metricbeat/module/kubernetes/container/container_test.go +++ b/metricbeat/module/kubernetes/container/container_test.go @@ -27,9 +27,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) const testFile = "../_meta/test/stats_summary.json" @@ -101,7 +101,7 @@ func TestEventMapping(t *testing.T) { } } -func testValue(t *testing.T, event common.MapStr, field string, value interface{}) { +func testValue(t *testing.T, event mapstr.M, field string, value interface{}) { data, err := event.GetValue(field) assert.NoError(t, err, "Could not read field "+field) assert.EqualValues(t, data, value, "Wrong value for field "+field) diff --git a/metricbeat/module/kubernetes/container/data.go b/metricbeat/module/kubernetes/container/data.go index bbe8bbc6fa1..84980d1559c 100644 --- a/metricbeat/module/kubernetes/container/data.go +++ b/metricbeat/module/kubernetes/container/data.go @@ -21,15 +21,15 @@ import ( "encoding/json" "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *logp.Logger) ([]common.MapStr, error) { - events := []common.MapStr{} +func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *logp.Logger) ([]mapstr.M, error) { + events := []mapstr.M{} var summary kubernetes.Summary err := json.Unmarshal(content, &summary) @@ -42,71 +42,71 @@ func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *lo nodeMem := perfMetrics.NodeMemAllocatable.Get(node.NodeName) for _, pod := range summary.Pods { for _, container := range pod.Containers { - containerEvent := common.MapStr{ - mb.ModuleDataKey: common.MapStr{ + containerEvent := mapstr.M{ + mb.ModuleDataKey: mapstr.M{ "namespace": pod.PodRef.Namespace, - "node": common.MapStr{ + "node": mapstr.M{ "name": node.NodeName, }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": pod.PodRef.Name, }, }, "name": container.Name, - "cpu": common.MapStr{ - "usage": common.MapStr{ + "cpu": mapstr.M{ + "usage": mapstr.M{ "nanocores": container.CPU.UsageNanoCores, - "core": common.MapStr{ + "core": mapstr.M{ "ns": container.CPU.UsageCoreNanoSeconds, }, }, }, - "memory": common.MapStr{ - "available": common.MapStr{ + "memory": mapstr.M{ + "available": mapstr.M{ "bytes": container.Memory.AvailableBytes, }, - "usage": common.MapStr{ + "usage": mapstr.M{ "bytes": container.Memory.UsageBytes, }, - "workingset": common.MapStr{ + "workingset": mapstr.M{ "bytes": container.Memory.WorkingSetBytes, }, - "rss": common.MapStr{ + "rss": mapstr.M{ "bytes": container.Memory.RssBytes, }, "pagefaults": container.Memory.PageFaults, "majorpagefaults": container.Memory.MajorPageFaults, }, - "rootfs": common.MapStr{ - "available": common.MapStr{ + "rootfs": mapstr.M{ + "available": mapstr.M{ "bytes": container.Rootfs.AvailableBytes, }, - "capacity": common.MapStr{ + "capacity": mapstr.M{ "bytes": container.Rootfs.CapacityBytes, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": container.Rootfs.UsedBytes, }, - "inodes": common.MapStr{ + "inodes": mapstr.M{ "used": container.Rootfs.InodesUsed, }, }, - "logs": common.MapStr{ - "available": common.MapStr{ + "logs": mapstr.M{ + "available": mapstr.M{ "bytes": container.Logs.AvailableBytes, }, - "capacity": common.MapStr{ + "capacity": mapstr.M{ "bytes": container.Logs.CapacityBytes, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": container.Logs.UsedBytes, }, - "inodes": common.MapStr{ + "inodes": mapstr.M{ "used": container.Logs.InodesUsed, "free": container.Logs.InodesFree, "count": container.Logs.Inodes, @@ -148,8 +148,8 @@ func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *lo } // ecsfields maps container events fields to container ecs fields -func ecsfields(containerEvent common.MapStr, logger *logp.Logger) common.MapStr { - ecsfields := common.MapStr{} +func ecsfields(containerEvent mapstr.M, logger *logp.Logger) mapstr.M { + ecsfields := mapstr.M{} name, err := containerEvent.GetValue("name") if err == nil { diff --git a/metricbeat/module/kubernetes/event/event.go b/metricbeat/module/kubernetes/event/event.go index 559e2151a5d..c48cb950591 100644 --- a/metricbeat/module/kubernetes/event/event.go +++ b/metricbeat/module/kubernetes/event/event.go @@ -26,10 +26,11 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) // init registers the MetricSet with the central registry. @@ -47,7 +48,7 @@ type MetricSet struct { watchOptions kubernetes.WatchOptions dedotConfig dedotConfig skipOlder bool - clusterMeta common.MapStr + clusterMeta mapstr.M } // dedotConfig defines LabelsDedot and AnnotationsDedot. @@ -110,12 +111,12 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { return ms, nil } -func getClusterECSMeta(cfg *common.Config, client k8sclient.Interface, logger *logp.Logger) (common.MapStr, error) { +func getClusterECSMeta(cfg *common.Config, client k8sclient.Interface, logger *logp.Logger) (mapstr.M, error) { clusterInfo, err := metadata.GetKubernetesClusterIdentifier(cfg, client) if err != nil { return nil, fmt.Errorf("fail to get kubernetes cluster metadata: %w", err) } - ecsClusterMeta := common.MapStr{} + ecsClusterMeta := mapstr.M{} if clusterInfo.Url != "" { util.ShouldPut(ecsClusterMeta, "orchestrator.cluster.url", clusterInfo.Url, logger) } @@ -178,9 +179,9 @@ func (m *MetricSet) reportEvent(obj interface{}, reporter mb.PushReporterV2) { reporter.Event(event) } -func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig, logger *logp.Logger) common.MapStr { - eventMeta := common.MapStr{ - "timestamp": common.MapStr{ +func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig, logger *logp.Logger) mapstr.M { + eventMeta := mapstr.M{ + "timestamp": mapstr.M{ "created": kubernetes.Time(&eve.ObjectMeta.CreationTimestamp).UTC(), }, "name": eve.ObjectMeta.GetName(), @@ -192,7 +193,7 @@ func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig, log } if len(eve.ObjectMeta.Labels) != 0 { - labels := make(common.MapStr, len(eve.ObjectMeta.Labels)) + labels := make(mapstr.M, len(eve.ObjectMeta.Labels)) for k, v := range eve.ObjectMeta.Labels { if dedotConfig.LabelsDedot { label := common.DeDot(k) @@ -210,7 +211,7 @@ func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig, log } if len(eve.ObjectMeta.Annotations) != 0 { - annotations := make(common.MapStr, len(eve.ObjectMeta.Annotations)) + annotations := make(mapstr.M, len(eve.ObjectMeta.Annotations)) for k, v := range eve.ObjectMeta.Annotations { if dedotConfig.AnnotationsDedot { annotation := common.DeDot(k) @@ -226,16 +227,16 @@ func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig, log eventMeta["annotations"] = annotations } - output := common.MapStr{ + output := mapstr.M{ "message": eve.Message, "reason": eve.Reason, "type": eve.Type, "count": eve.Count, - "source": common.MapStr{ + "source": mapstr.M{ "host": eve.Source.Host, "component": eve.Source.Component, }, - "involved_object": common.MapStr{ + "involved_object": mapstr.M{ "api_version": eve.InvolvedObject.APIVersion, "resource_version": eve.InvolvedObject.ResourceVersion, "name": eve.InvolvedObject.Name, @@ -245,7 +246,7 @@ func generateMapStrFromEvent(eve *kubernetes.Event, dedotConfig dedotConfig, log "metadata": eventMeta, } - tsMap := make(common.MapStr) + tsMap := make(mapstr.M) tsMap["first_occurrence"] = kubernetes.Time(&eve.FirstTimestamp).UTC() tsMap["last_occurrence"] = kubernetes.Time(&eve.LastTimestamp).UTC() diff --git a/metricbeat/module/kubernetes/event/event_test.go b/metricbeat/module/kubernetes/event/event_test.go index e9bf8542517..e0d0f8619b3 100644 --- a/metricbeat/module/kubernetes/event/event_test.go +++ b/metricbeat/module/kubernetes/event/event_test.go @@ -24,8 +24,8 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateMapStrFromEvent(t *testing.T) { @@ -44,9 +44,9 @@ func TestGenerateMapStrFromEvent(t *testing.T) { "prometheus.io/scrape": "false", } - expectedLabelsMapStrWithDot := common.MapStr{ - "app": common.MapStr{ - "kubernetes": common.MapStr{ + expectedLabelsMapStrWithDot := mapstr.M{ + "app": mapstr.M{ + "kubernetes": mapstr.M{ "io/version": "5.7.21", "io/component": "database", "io/name": "mysql", @@ -54,14 +54,14 @@ func TestGenerateMapStrFromEvent(t *testing.T) { }, } - expectedLabelsMapStrWithDeDot := common.MapStr{ + expectedLabelsMapStrWithDeDot := mapstr.M{ "app_kubernetes_io/name": "mysql", "app_kubernetes_io/version": "5.7.21", "app_kubernetes_io/component": "database", } - expectedAnnotationsMapStrWithDot := common.MapStr{ - "prometheus": common.MapStr{ + expectedAnnotationsMapStrWithDot := mapstr.M{ + "prometheus": mapstr.M{ "io/path": "/metrics", "io/port": "9102", "io/scheme": "http", @@ -69,7 +69,7 @@ func TestGenerateMapStrFromEvent(t *testing.T) { }, } - expectedAnnotationsMapStrWithDeDot := common.MapStr{ + expectedAnnotationsMapStrWithDeDot := mapstr.M{ "prometheus_io/path": "/metrics", "prometheus_io/port": "9102", "prometheus_io/scheme": "http", @@ -83,7 +83,7 @@ func TestGenerateMapStrFromEvent(t *testing.T) { testCases := map[string]struct { mockEvent v1.Event - expectedMetadata common.MapStr + expectedMetadata mapstr.M dedotConfig dedotConfig }{ "no dedots": { @@ -94,7 +94,7 @@ func TestGenerateMapStrFromEvent(t *testing.T) { }, Source: source, }, - expectedMetadata: common.MapStr{ + expectedMetadata: mapstr.M{ "labels": expectedLabelsMapStrWithDot, "annotations": expectedAnnotationsMapStrWithDot, }, @@ -111,7 +111,7 @@ func TestGenerateMapStrFromEvent(t *testing.T) { }, Source: source, }, - expectedMetadata: common.MapStr{ + expectedMetadata: mapstr.M{ "labels": expectedLabelsMapStrWithDeDot, "annotations": expectedAnnotationsMapStrWithDot, }, @@ -128,7 +128,7 @@ func TestGenerateMapStrFromEvent(t *testing.T) { }, Source: source, }, - expectedMetadata: common.MapStr{ + expectedMetadata: mapstr.M{ "labels": expectedLabelsMapStrWithDot, "annotations": expectedAnnotationsMapStrWithDeDot, }, @@ -145,7 +145,7 @@ func TestGenerateMapStrFromEvent(t *testing.T) { }, Source: source, }, - expectedMetadata: common.MapStr{ + expectedMetadata: mapstr.M{ "labels": expectedLabelsMapStrWithDeDot, "annotations": expectedAnnotationsMapStrWithDeDot, }, @@ -159,10 +159,10 @@ func TestGenerateMapStrFromEvent(t *testing.T) { for name, test := range testCases { t.Run(name, func(t *testing.T) { mapStrOutput := generateMapStrFromEvent(&test.mockEvent, test.dedotConfig, logger) - assert.Equal(t, test.expectedMetadata["labels"], mapStrOutput["metadata"].(common.MapStr)["labels"]) - assert.Equal(t, test.expectedMetadata["annotations"], mapStrOutput["metadata"].(common.MapStr)["annotations"]) - assert.Equal(t, source.Host, mapStrOutput["source"].(common.MapStr)["host"]) - assert.Equal(t, source.Component, mapStrOutput["source"].(common.MapStr)["component"]) + assert.Equal(t, test.expectedMetadata["labels"], mapStrOutput["metadata"].(mapstr.M)["labels"]) + assert.Equal(t, test.expectedMetadata["annotations"], mapStrOutput["metadata"].(mapstr.M)["annotations"]) + assert.Equal(t, source.Host, mapStrOutput["source"].(mapstr.M)["host"]) + assert.Equal(t, source.Component, mapStrOutput["source"].(mapstr.M)["component"]) }) } } diff --git a/metricbeat/module/kubernetes/node/data.go b/metricbeat/module/kubernetes/node/data.go index 47e144db0a3..5c03290154e 100644 --- a/metricbeat/module/kubernetes/node/data.go +++ b/metricbeat/module/kubernetes/node/data.go @@ -21,13 +21,13 @@ import ( "encoding/json" "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(content []byte, logger *logp.Logger) (common.MapStr, error) { +func eventMapping(content []byte, logger *logp.Logger) (mapstr.M, error) { var summary kubernetes.Summary err := json.Unmarshal(content, &summary) if err != nil { @@ -35,72 +35,72 @@ func eventMapping(content []byte, logger *logp.Logger) (common.MapStr, error) { } node := summary.Node - nodeEvent := common.MapStr{ + nodeEvent := mapstr.M{ "name": node.NodeName, - "cpu": common.MapStr{ - "usage": common.MapStr{ + "cpu": mapstr.M{ + "usage": mapstr.M{ "nanocores": node.CPU.UsageNanoCores, - "core": common.MapStr{ + "core": mapstr.M{ "ns": node.CPU.UsageCoreNanoSeconds, }, }, }, - "memory": common.MapStr{ - "available": common.MapStr{ + "memory": mapstr.M{ + "available": mapstr.M{ "bytes": node.Memory.AvailableBytes, }, - "usage": common.MapStr{ + "usage": mapstr.M{ "bytes": node.Memory.UsageBytes, }, - "workingset": common.MapStr{ + "workingset": mapstr.M{ "bytes": node.Memory.WorkingSetBytes, }, - "rss": common.MapStr{ + "rss": mapstr.M{ "bytes": node.Memory.RssBytes, }, "pagefaults": node.Memory.PageFaults, "majorpagefaults": node.Memory.MajorPageFaults, }, - "network": common.MapStr{ - "rx": common.MapStr{ + "network": mapstr.M{ + "rx": mapstr.M{ "bytes": node.Network.RxBytes, "errors": node.Network.RxErrors, }, - "tx": common.MapStr{ + "tx": mapstr.M{ "bytes": node.Network.TxBytes, "errors": node.Network.TxErrors, }, }, - "fs": common.MapStr{ - "available": common.MapStr{ + "fs": mapstr.M{ + "available": mapstr.M{ "bytes": node.Fs.AvailableBytes, }, - "capacity": common.MapStr{ + "capacity": mapstr.M{ "bytes": node.Fs.CapacityBytes, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": node.Fs.UsedBytes, }, - "inodes": common.MapStr{ + "inodes": mapstr.M{ "used": node.Fs.InodesUsed, "free": node.Fs.InodesFree, "count": node.Fs.Inodes, }, }, - "runtime": common.MapStr{ - "imagefs": common.MapStr{ - "available": common.MapStr{ + "runtime": mapstr.M{ + "imagefs": mapstr.M{ + "available": mapstr.M{ "bytes": node.Runtime.ImageFs.AvailableBytes, }, - "capacity": common.MapStr{ + "capacity": mapstr.M{ "bytes": node.Runtime.ImageFs.CapacityBytes, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": node.Runtime.ImageFs.UsedBytes, }, }, diff --git a/metricbeat/module/kubernetes/node/node.go b/metricbeat/module/kubernetes/node/node.go index 31d3f6e408f..80bd60d67fa 100644 --- a/metricbeat/module/kubernetes/node/node.go +++ b/metricbeat/module/kubernetes/node/node.go @@ -20,13 +20,13 @@ package node import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" k8smod "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -101,7 +101,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) { return } - m.enricher.Enrich([]common.MapStr{event}) + m.enricher.Enrich([]mapstr.M{event}) e, err := util.CreateEvent(event, "kubernetes.node") if err != nil { diff --git a/metricbeat/module/kubernetes/node/node_test.go b/metricbeat/module/kubernetes/node/node_test.go index 8c4bf582f03..b8faba68ba9 100644 --- a/metricbeat/module/kubernetes/node/node_test.go +++ b/metricbeat/module/kubernetes/node/node_test.go @@ -27,8 +27,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const testFile = "../_meta/test/stats_summary.json" @@ -80,7 +80,7 @@ func TestEventMapping(t *testing.T) { } } -func testValue(t *testing.T, event common.MapStr, field string, value interface{}) { +func testValue(t *testing.T, event mapstr.M, field string, value interface{}) { data, err := event.GetValue(field) assert.NoError(t, err, "Could not read field "+field) assert.EqualValues(t, data, value, "Wrong value for field "+field) diff --git a/metricbeat/module/kubernetes/pod/data.go b/metricbeat/module/kubernetes/pod/data.go index 8a53ef02fa8..f6c47b20371 100644 --- a/metricbeat/module/kubernetes/pod/data.go +++ b/metricbeat/module/kubernetes/pod/data.go @@ -21,15 +21,15 @@ import ( "encoding/json" "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *logp.Logger) ([]common.MapStr, error) { - events := []common.MapStr{} +func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *logp.Logger) ([]mapstr.M, error) { + events := []mapstr.M{} var summary kubernetes.Summary err := json.Unmarshal(content, &summary) @@ -58,45 +58,45 @@ func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *lo memLimit += perfMetrics.ContainerMemLimit.GetWithDefault(cuid, nodeMem) } - podEvent := common.MapStr{ - mb.ModuleDataKey: common.MapStr{ + podEvent := mapstr.M{ + mb.ModuleDataKey: mapstr.M{ "namespace": pod.PodRef.Namespace, - "node": common.MapStr{ + "node": mapstr.M{ "name": node.NodeName, }, }, "name": pod.PodRef.Name, "uid": pod.PodRef.UID, - "cpu": common.MapStr{ - "usage": common.MapStr{ + "cpu": mapstr.M{ + "usage": mapstr.M{ "nanocores": usageNanoCores, }, }, - "memory": common.MapStr{ - "usage": common.MapStr{ + "memory": mapstr.M{ + "usage": mapstr.M{ "bytes": usageMem, }, - "available": common.MapStr{ + "available": mapstr.M{ "bytes": availMem, }, - "working_set": common.MapStr{ + "working_set": mapstr.M{ "bytes": workingSet, }, - "rss": common.MapStr{ + "rss": mapstr.M{ "bytes": rss, }, "page_faults": pageFaults, "major_page_faults": majorPageFaults, }, - "network": common.MapStr{ - "rx": common.MapStr{ + "network": mapstr.M{ + "rx": mapstr.M{ "bytes": pod.Network.RxBytes, "errors": pod.Network.RxErrors, }, - "tx": common.MapStr{ + "tx": mapstr.M{ "bytes": pod.Network.TxBytes, "errors": pod.Network.TxErrors, }, @@ -150,8 +150,8 @@ func eventMapping(content []byte, perfMetrics *util.PerfMetricsCache, logger *lo } // ecsfields maps pod events fields to container ecs fields -func ecsfields(podEvent common.MapStr, logger *logp.Logger) common.MapStr { - ecsfields := common.MapStr{} +func ecsfields(podEvent mapstr.M, logger *logp.Logger) mapstr.M { + ecsfields := mapstr.M{} egressBytes, err := podEvent.GetValue("network.tx.bytes") if err == nil { diff --git a/metricbeat/module/kubernetes/pod/pod.go b/metricbeat/module/kubernetes/pod/pod.go index 68817d5a034..eae281feb9d 100644 --- a/metricbeat/module/kubernetes/pod/pod.go +++ b/metricbeat/module/kubernetes/pod/pod.go @@ -20,13 +20,13 @@ package pod import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" k8smod "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -115,11 +115,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) { containerEcsFields := ecsfields(event, m.Logger()) if len(containerEcsFields) != 0 { if e.RootFields != nil { - e.RootFields.DeepUpdate(common.MapStr{ + e.RootFields.DeepUpdate(mapstr.M{ "container": containerEcsFields, }) } else { - e.RootFields = common.MapStr{ + e.RootFields = mapstr.M{ "container": containerEcsFields, } } diff --git a/metricbeat/module/kubernetes/pod/pod_test.go b/metricbeat/module/kubernetes/pod/pod_test.go index 46a1dca047f..4d437515147 100644 --- a/metricbeat/module/kubernetes/pod/pod_test.go +++ b/metricbeat/module/kubernetes/pod/pod_test.go @@ -27,9 +27,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) const testFile = "../_meta/test/stats_summary.json" @@ -78,7 +78,7 @@ func TestEventMapping(t *testing.T) { } } -func testValue(t *testing.T, event common.MapStr, field string, expected interface{}) { +func testValue(t *testing.T, event mapstr.M, field string, expected interface{}) { data, err := event.GetValue(field) assert.NoError(t, err, "Could not read field "+field) assert.EqualValues(t, expected, data, "Wrong value for field "+field) diff --git a/metricbeat/module/kubernetes/state_container/state_container.go b/metricbeat/module/kubernetes/state_container/state_container.go index 29be5ed4a0c..a33dce02f85 100644 --- a/metricbeat/module/kubernetes/state_container/state_container.go +++ b/metricbeat/module/kubernetes/state_container/state_container.go @@ -21,12 +21,12 @@ import ( "fmt" "strings" - "github.com/elastic/beats/v7/libbeat/common" p "github.com/elastic/beats/v7/metricbeat/helper/prometheus" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" k8smod "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -143,7 +143,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { for _, event := range events { // applying ECS to kubernetes.container.id in the form :// // copy to ECS fields the kubernetes.container.image, kubernetes.container.name - containerFields := common.MapStr{} + containerFields := mapstr.M{} if containerID, ok := event["id"]; ok { // we don't expect errors here, but if any we would obtain an // empty string @@ -176,11 +176,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { if len(containerFields) > 0 { if e.RootFields != nil { - e.RootFields.DeepUpdate(common.MapStr{ + e.RootFields.DeepUpdate(mapstr.M{ "container": containerFields, }) } else { - e.RootFields = common.MapStr{ + e.RootFields = mapstr.M{ "container": containerFields, } } diff --git a/metricbeat/module/kubernetes/system/data.go b/metricbeat/module/kubernetes/system/data.go index 874764731af..ae2e78694bf 100644 --- a/metricbeat/module/kubernetes/system/data.go +++ b/metricbeat/module/kubernetes/system/data.go @@ -21,15 +21,15 @@ import ( "encoding/json" "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(content []byte, logger *logp.Logger) ([]common.MapStr, error) { - events := []common.MapStr{} +func eventMapping(content []byte, logger *logp.Logger) ([]mapstr.M, error) { + events := []mapstr.M{} var summary kubernetes.Summary err := json.Unmarshal(content, &summary) @@ -40,29 +40,29 @@ func eventMapping(content []byte, logger *logp.Logger) ([]common.MapStr, error) node := summary.Node for _, syscontainer := range node.SystemContainers { - containerEvent := common.MapStr{ - mb.ModuleDataKey: common.MapStr{ - "node": common.MapStr{ + containerEvent := mapstr.M{ + mb.ModuleDataKey: mapstr.M{ + "node": mapstr.M{ "name": node.NodeName, }, }, "container": syscontainer.Name, - "cpu": common.MapStr{ - "usage": common.MapStr{ + "cpu": mapstr.M{ + "usage": mapstr.M{ "nanocores": syscontainer.CPU.UsageNanoCores, - "core": common.MapStr{ + "core": mapstr.M{ "ns": syscontainer.CPU.UsageCoreNanoSeconds, }, }, }, - "memory": common.MapStr{ - "usage": common.MapStr{ + "memory": mapstr.M{ + "usage": mapstr.M{ "bytes": syscontainer.Memory.UsageBytes, }, - "workingset": common.MapStr{ + "workingset": mapstr.M{ "bytes": syscontainer.Memory.WorkingSetBytes, }, - "rss": common.MapStr{ + "rss": mapstr.M{ "bytes": syscontainer.Memory.RssBytes, }, "pagefaults": syscontainer.Memory.PageFaults, diff --git a/metricbeat/module/kubernetes/system/system_test.go b/metricbeat/module/kubernetes/system/system_test.go index b374df5e030..e2e4dfc6e21 100644 --- a/metricbeat/module/kubernetes/system/system_test.go +++ b/metricbeat/module/kubernetes/system/system_test.go @@ -27,8 +27,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const testFile = "../_meta/test/stats_summary.json" @@ -65,7 +65,7 @@ func TestEventMapping(t *testing.T) { } } -func testValue(t *testing.T, event common.MapStr, field string, value interface{}) { +func testValue(t *testing.T, event mapstr.M, field string, value interface{}) { data, err := event.GetValue(field) assert.NoError(t, err, "Could not read field "+field) assert.EqualValues(t, data, value, "Wrong value for field "+field) diff --git a/metricbeat/module/kubernetes/util/kubernetes.go b/metricbeat/module/kubernetes/util/kubernetes.go index 2658a1d9ef1..2e7b64c5c20 100644 --- a/metricbeat/module/kubernetes/util/kubernetes.go +++ b/metricbeat/module/kubernetes/util/kubernetes.go @@ -24,6 +24,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common/kubernetes/metadata" + "github.com/elastic/elastic-agent-libs/mapstr" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/resource" @@ -44,7 +45,7 @@ type Enricher interface { Stop() // Enrich the given list of events - Enrich([]common.MapStr) + Enrich([]mapstr.M) } type kubernetesConfig struct { @@ -62,8 +63,8 @@ type kubernetesConfig struct { type enricher struct { sync.RWMutex - metadata map[string]common.MapStr - index func(common.MapStr) string + metadata map[string]mapstr.M + index func(mapstr.M) string watcher kubernetes.Watcher watchersStarted bool watchersStartedLock sync.Mutex @@ -107,7 +108,7 @@ func NewResourceMetadataEnricher( serviceMetaGen := metadata.NewServiceMetadataGenerator(cfg, watcher.Store(), namespaceMeta, watcher.Client()) enricher := buildMetadataEnricher(watcher, nodeWatcher, namespaceWatcher, // update - func(m map[string]common.MapStr, r kubernetes.Resource) { + func(m map[string]mapstr.M, r kubernetes.Resource) { accessor, _ := meta.Accessor(r) id := join(accessor.GetNamespace(), accessor.GetName()) @@ -150,13 +151,13 @@ func NewResourceMetadataEnricher( } }, // delete - func(m map[string]common.MapStr, r kubernetes.Resource) { + func(m map[string]mapstr.M, r kubernetes.Resource) { accessor, _ := meta.Accessor(r) id := join(accessor.GetNamespace(), accessor.GetName()) delete(m, id) }, // index - func(e common.MapStr) string { + func(e mapstr.M) string { return join(getString(e, mb.ModuleDataKey+".namespace"), getString(e, "name")) }, ) @@ -197,7 +198,7 @@ func NewContainerMetadataEnricher( enricher := buildMetadataEnricher(watcher, nodeWatcher, namespaceWatcher, // update - func(m map[string]common.MapStr, r kubernetes.Resource) { + func(m map[string]mapstr.M, r kubernetes.Resource) { pod, ok := r.(*kubernetes.Pod) if !ok { base.Logger().Debugf("Error while casting event: %s", ok) @@ -242,7 +243,7 @@ func NewContainerMetadataEnricher( } }, // delete - func(m map[string]common.MapStr, r kubernetes.Resource) { + func(m map[string]mapstr.M, r kubernetes.Resource) { pod, ok := r.(*kubernetes.Pod) if !ok { base.Logger().Debugf("Error while casting event: %s", ok) @@ -253,7 +254,7 @@ func NewContainerMetadataEnricher( } }, // index - func(e common.MapStr) string { + func(e mapstr.M) string { return join(getString(e, mb.ModuleDataKey+".namespace"), getString(e, mb.ModuleDataKey+".pod.name"), getString(e, "name")) }, ) @@ -338,7 +339,7 @@ func validatedConfig(base mb.BaseMetricSet) *kubernetesConfig { return &config } -func getString(m common.MapStr, key string) string { +func getString(m mapstr.M, key string) string { val, err := m.GetValue(key) if err != nil { return "" @@ -356,12 +357,12 @@ func buildMetadataEnricher( watcher kubernetes.Watcher, nodeWatcher kubernetes.Watcher, namespaceWatcher kubernetes.Watcher, - update func(map[string]common.MapStr, kubernetes.Resource), - delete func(map[string]common.MapStr, kubernetes.Resource), - index func(e common.MapStr) string) *enricher { + update func(map[string]mapstr.M, kubernetes.Resource), + delete func(map[string]mapstr.M, kubernetes.Resource), + index func(e mapstr.M) string) *enricher { enricher := enricher{ - metadata: map[string]common.MapStr{}, + metadata: map[string]mapstr.M{}, index: index, watcher: watcher, nodeWatcher: nodeWatcher, @@ -431,7 +432,7 @@ func (m *enricher) Stop() { } } -func (m *enricher) Enrich(events []common.MapStr) { +func (m *enricher) Enrich(events []mapstr.M) { m.RLock() defer m.RUnlock() for _, event := range events { @@ -440,14 +441,14 @@ func (m *enricher) Enrich(events []common.MapStr) { if err != nil { continue } - k8sMeta, ok := k8s.(common.MapStr) + k8sMeta, ok := k8s.(mapstr.M) if !ok { continue } if m.isPod { // apply pod meta at metricset level - if podMeta, ok := k8sMeta["pod"].(common.MapStr); ok { + if podMeta, ok := k8sMeta["pod"].(mapstr.M); ok { event.DeepUpdate(podMeta) } @@ -461,7 +462,7 @@ func (m *enricher) Enrich(events []common.MapStr) { logp.Debug("kubernetes", "Failed to delete field '%s': %s", "kubernetes", err) } - event.DeepUpdate(common.MapStr{ + event.DeepUpdate(mapstr.M{ mb.ModuleDataKey: k8sMeta, "meta": ecsMeta, }) @@ -471,18 +472,18 @@ func (m *enricher) Enrich(events []common.MapStr) { type nilEnricher struct{} -func (*nilEnricher) Start() {} -func (*nilEnricher) Stop() {} -func (*nilEnricher) Enrich([]common.MapStr) {} +func (*nilEnricher) Start() {} +func (*nilEnricher) Stop() {} +func (*nilEnricher) Enrich([]mapstr.M) {} -func CreateEvent(event common.MapStr, namespace string) (mb.Event, error) { - var moduleFieldsMapStr common.MapStr +func CreateEvent(event mapstr.M, namespace string) (mb.Event, error) { + var moduleFieldsMapStr mapstr.M moduleFields, ok := event[mb.ModuleDataKey] var err error if ok { - moduleFieldsMapStr, ok = moduleFields.(common.MapStr) + moduleFieldsMapStr, ok = moduleFields.(mapstr.M) if !ok { - err = fmt.Errorf("error trying to convert '%s' from event to common.MapStr", mb.ModuleDataKey) + err = fmt.Errorf("error trying to convert '%s' from event to mapstr.M", mb.ModuleDataKey) } } delete(event, mb.ModuleDataKey) @@ -494,12 +495,12 @@ func CreateEvent(event common.MapStr, namespace string) (mb.Event, error) { } // add root-level fields like ECS fields - var metaFieldsMapStr common.MapStr + var metaFieldsMapStr mapstr.M metaFields, ok := event["meta"] if ok { - metaFieldsMapStr, ok = metaFields.(common.MapStr) + metaFieldsMapStr, ok = metaFields.(mapstr.M) if !ok { - err = fmt.Errorf("error trying to convert '%s' from event to common.MapStr", "meta") + err = fmt.Errorf("error trying to convert '%s' from event to mapstr.M", "meta") } delete(event, "meta") if len(metaFieldsMapStr) > 0 { @@ -509,14 +510,14 @@ func CreateEvent(event common.MapStr, namespace string) (mb.Event, error) { return e, err } -func ShouldPut(event common.MapStr, field string, value interface{}, logger *logp.Logger) { +func ShouldPut(event mapstr.M, field string, value interface{}, logger *logp.Logger) { _, err := event.Put(field, value) if err != nil { logger.Debugf("Failed to put field '%s' with value '%s': %s", field, value, err) } } -func ShouldDelete(event common.MapStr, field string, logger *logp.Logger) { +func ShouldDelete(event mapstr.M, field string, logger *logp.Logger) { err := event.Delete(field) if err != nil { logger.Debugf("Failed to delete field '%s': %s", field, err) diff --git a/metricbeat/module/kubernetes/util/kubernetes_test.go b/metricbeat/module/kubernetes/util/kubernetes_test.go index 239f1551d1e..752d6fa1500 100644 --- a/metricbeat/module/kubernetes/util/kubernetes_test.go +++ b/metricbeat/module/kubernetes/util/kubernetes_test.go @@ -30,9 +30,9 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/kubernetes" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -66,36 +66,36 @@ func TestBuildMetadataEnricher(t *testing.T) { assert.Equal(t, resource, funcs.updated) // Test enricher - events := []common.MapStr{ + events := []mapstr.M{ {"name": "unknown"}, {"name": "enrich"}, } enricher.Enrich(events) - assert.Equal(t, []common.MapStr{ + assert.Equal(t, []mapstr.M{ {"name": "unknown"}, { "name": "enrich", - "_module": common.MapStr{"label": "value", "pod": common.MapStr{"name": "enrich", "uid": "mockuid"}}, - "meta": common.MapStr{"orchestrator": common.MapStr{"cluster": common.MapStr{"name": "gke-4242"}}}, + "_module": mapstr.M{"label": "value", "pod": mapstr.M{"name": "enrich", "uid": "mockuid"}}, + "meta": mapstr.M{"orchestrator": mapstr.M{"cluster": mapstr.M{"name": "gke-4242"}}}, }, }, events) // Enrich a pod (metadata goes in root level) - events = []common.MapStr{ + events = []mapstr.M{ {"name": "unknown"}, {"name": "enrich"}, } enricher.isPod = true enricher.Enrich(events) - assert.Equal(t, []common.MapStr{ + assert.Equal(t, []mapstr.M{ {"name": "unknown"}, { "name": "enrich", "uid": "mockuid", - "_module": common.MapStr{"label": "value"}, - "meta": common.MapStr{"orchestrator": common.MapStr{"cluster": common.MapStr{"name": "gke-4242"}}}, + "_module": mapstr.M{"label": "value"}, + "meta": mapstr.M{"orchestrator": mapstr.M{"cluster": mapstr.M{"name": "gke-4242"}}}, }, }, events) @@ -103,13 +103,13 @@ func TestBuildMetadataEnricher(t *testing.T) { watcher.handler.OnDelete(resource) assert.Equal(t, resource, funcs.deleted) - events = []common.MapStr{ + events = []mapstr.M{ {"name": "unknown"}, {"name": "enrich"}, } enricher.Enrich(events) - assert.Equal(t, []common.MapStr{ + assert.Equal(t, []mapstr.M{ {"name": "unknown"}, {"name": "enrich"}, }, events) @@ -118,15 +118,15 @@ func TestBuildMetadataEnricher(t *testing.T) { type mockFuncs struct { updated kubernetes.Resource deleted kubernetes.Resource - indexed common.MapStr + indexed mapstr.M } -func (f *mockFuncs) update(m map[string]common.MapStr, obj kubernetes.Resource) { +func (f *mockFuncs) update(m map[string]mapstr.M, obj kubernetes.Resource) { accessor, _ := meta.Accessor(obj) f.updated = obj - meta := common.MapStr{ - "kubernetes": common.MapStr{ - "pod": common.MapStr{ + meta := mapstr.M{ + "kubernetes": mapstr.M{ + "pod": mapstr.M{ "name": accessor.GetName(), "uid": string(accessor.GetUID()), }, @@ -139,13 +139,13 @@ func (f *mockFuncs) update(m map[string]common.MapStr, obj kubernetes.Resource) m[accessor.GetName()] = meta } -func (f *mockFuncs) delete(m map[string]common.MapStr, obj kubernetes.Resource) { +func (f *mockFuncs) delete(m map[string]mapstr.M, obj kubernetes.Resource) { accessor, _ := meta.Accessor(obj) f.deleted = obj delete(m, accessor.GetName()) } -func (f *mockFuncs) index(m common.MapStr) string { +func (f *mockFuncs) index(m mapstr.M) string { f.indexed = m return m["name"].(string) } diff --git a/metricbeat/module/kubernetes/volume/data.go b/metricbeat/module/kubernetes/volume/data.go index bea4fec0d34..063546696d6 100644 --- a/metricbeat/module/kubernetes/volume/data.go +++ b/metricbeat/module/kubernetes/volume/data.go @@ -21,15 +21,15 @@ import ( "encoding/json" "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/kubernetes" "github.com/elastic/beats/v7/metricbeat/module/kubernetes/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(content []byte, logger *logp.Logger) ([]common.MapStr, error) { - events := []common.MapStr{} +func eventMapping(content []byte, logger *logp.Logger) ([]mapstr.M, error) { + events := []mapstr.M{} var summary kubernetes.Summary err := json.Unmarshal(content, &summary) @@ -40,29 +40,29 @@ func eventMapping(content []byte, logger *logp.Logger) ([]common.MapStr, error) node := summary.Node for _, pod := range summary.Pods { for _, volume := range pod.Volume { - volumeEvent := common.MapStr{ - mb.ModuleDataKey: common.MapStr{ + volumeEvent := mapstr.M{ + mb.ModuleDataKey: mapstr.M{ "namespace": pod.PodRef.Namespace, - "node": common.MapStr{ + "node": mapstr.M{ "name": node.NodeName, }, - "pod": common.MapStr{ + "pod": mapstr.M{ "name": pod.PodRef.Name, }, }, "name": volume.Name, - "fs": common.MapStr{ - "available": common.MapStr{ + "fs": mapstr.M{ + "available": mapstr.M{ "bytes": volume.AvailableBytes, }, - "capacity": common.MapStr{ + "capacity": mapstr.M{ "bytes": volume.CapacityBytes, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": volume.UsedBytes, }, - "inodes": common.MapStr{ + "inodes": mapstr.M{ "used": volume.InodesUsed, "free": volume.InodesFree, "count": volume.Inodes, diff --git a/metricbeat/module/kubernetes/volume/volume_test.go b/metricbeat/module/kubernetes/volume/volume_test.go index e7d7811bce5..269794f561a 100644 --- a/metricbeat/module/kubernetes/volume/volume_test.go +++ b/metricbeat/module/kubernetes/volume/volume_test.go @@ -27,8 +27,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const testFile = "../_meta/test/stats_summary.json" @@ -65,7 +65,7 @@ func TestEventMapping(t *testing.T) { } } -func testValue(t *testing.T, event common.MapStr, field string, value interface{}) { +func testValue(t *testing.T, event mapstr.M, field string, value interface{}) { data, err := event.GetValue(field) assert.NoError(t, err, "Could not read field "+field) assert.EqualValues(t, data, value, "Wrong value for field "+field) diff --git a/metricbeat/module/kvm/dommemstat/dommemstat.go b/metricbeat/module/kvm/dommemstat/dommemstat.go index 8beb22d433c..2714204d0af 100644 --- a/metricbeat/module/kvm/dommemstat/dommemstat.go +++ b/metricbeat/module/kvm/dommemstat/dommemstat.go @@ -23,13 +23,13 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" "github.com/digitalocean/go-libvirt" "github.com/digitalocean/go-libvirt/libvirttest" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" ) @@ -142,10 +142,10 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { for i := range gotDomainMemoryStats { reported := report.Event(mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "id": d.ID, "name": d.Name, - "stat": common.MapStr{ + "stat": mapstr.M{ "name": getDomainMemoryStatName(gotDomainMemoryStats[i].Tag), "value": gotDomainMemoryStats[i].Val, }, diff --git a/metricbeat/module/kvm/status/status.go b/metricbeat/module/kvm/status/status.go index ddff67f91ba..fdbb7a22b7a 100644 --- a/metricbeat/module/kvm/status/status.go +++ b/metricbeat/module/kvm/status/status.go @@ -27,9 +27,9 @@ import ( "github.com/digitalocean/go-libvirt" "github.com/digitalocean/go-libvirt/libvirttest" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -119,11 +119,11 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { continue } reported := report.Event(mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "id": d.ID, "name": d.Name, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "state": getDomainStateName(state), }, }) diff --git a/metricbeat/module/linux/conntrack/conntrack.go b/metricbeat/module/linux/conntrack/conntrack.go index 4d896a20928..713d1585d16 100644 --- a/metricbeat/module/linux/conntrack/conntrack.go +++ b/metricbeat/module/linux/conntrack/conntrack.go @@ -21,10 +21,10 @@ import ( "github.com/pkg/errors" "github.com/prometheus/procfs" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -83,8 +83,8 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { } report.Event(mb.Event{ - MetricSetFields: common.MapStr{ - "summary": common.MapStr{ + MetricSetFields: mapstr.M{ + "summary": mapstr.M{ "entries": summedEvents.Entries, "found": summedEvents.Found, "invalid": summedEvents.Invalid, diff --git a/metricbeat/module/linux/conntrack/conntrack_test.go b/metricbeat/module/linux/conntrack/conntrack_test.go index e09d9439cc1..cc3b2a05230 100644 --- a/metricbeat/module/linux/conntrack/conntrack_test.go +++ b/metricbeat/module/linux/conntrack/conntrack_test.go @@ -22,9 +22,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/linux" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { @@ -44,7 +44,7 @@ func TestFetch(t *testing.T) { t.FailNow() } - testConn := common.MapStr{ + testConn := mapstr.M{ "drop": uint64(0), "early_drop": uint64(0), "entries": uint64(16), @@ -55,7 +55,7 @@ func TestFetch(t *testing.T) { "search_restart": uint64(3), } - rawEvent := events[0].BeatEvent("linux", "conntrack").Fields["linux"].(common.MapStr)["conntrack"].(common.MapStr)["summary"] + rawEvent := events[0].BeatEvent("linux", "conntrack").Fields["linux"].(mapstr.M)["conntrack"].(mapstr.M)["summary"] assert.Equal(t, testConn, rawEvent) } diff --git a/metricbeat/module/linux/iostat/data.go b/metricbeat/module/linux/iostat/data.go index 4e546deaa34..7dbf08df45d 100644 --- a/metricbeat/module/linux/iostat/data.go +++ b/metricbeat/module/linux/iostat/data.go @@ -18,37 +18,37 @@ package iostat import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/diskio" + "github.com/elastic/elastic-agent-libs/mapstr" ) // AddLinuxIOStat adds the linux iostat data to the provided map -func AddLinuxIOStat(extraMetrics diskio.IOMetric) common.MapStr { - return common.MapStr{ - "read": common.MapStr{ - "request": common.MapStr{ +func AddLinuxIOStat(extraMetrics diskio.IOMetric) mapstr.M { + return mapstr.M{ + "read": mapstr.M{ + "request": mapstr.M{ "merges_per_sec": extraMetrics.ReadRequestMergeCountPerSec, "per_sec": extraMetrics.ReadRequestCountPerSec, }, - "per_sec": common.MapStr{ + "per_sec": mapstr.M{ "bytes": extraMetrics.ReadBytesPerSec, }, "await": extraMetrics.AvgReadAwaitTime, }, - "write": common.MapStr{ - "request": common.MapStr{ + "write": mapstr.M{ + "request": mapstr.M{ "merges_per_sec": extraMetrics.WriteRequestMergeCountPerSec, "per_sec": extraMetrics.WriteRequestCountPerSec, }, - "per_sec": common.MapStr{ + "per_sec": mapstr.M{ "bytes": extraMetrics.WriteBytesPerSec, }, "await": extraMetrics.AvgWriteAwaitTime, }, - "queue": common.MapStr{ + "queue": mapstr.M{ "avg_size": extraMetrics.AvgQueueSize, }, - "request": common.MapStr{ + "request": mapstr.M{ "avg_size": extraMetrics.AvgRequestSize, }, "await": extraMetrics.AvgAwaitTime, diff --git a/metricbeat/module/linux/iostat/iostat.go b/metricbeat/module/linux/iostat/iostat.go index 389424ff2fe..a8172a8ed3f 100644 --- a/metricbeat/module/linux/iostat/iostat.go +++ b/metricbeat/module/linux/iostat/iostat.go @@ -23,10 +23,10 @@ package iostat import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/diskio" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -82,7 +82,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { defer m.stats.CloseSampling() for _, counters := range IOstats { - event := common.MapStr{ + event := mapstr.M{ "name": counters.Name, } if counters.SerialNumber != "" { diff --git a/metricbeat/module/linux/ksm/ksm.go b/metricbeat/module/linux/ksm/ksm.go index 204b9364f54..a538168faed 100644 --- a/metricbeat/module/linux/ksm/ksm.go +++ b/metricbeat/module/linux/ksm/ksm.go @@ -18,13 +18,12 @@ package ksm import ( - "github.com/elastic/beats/v7/libbeat/common" - "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -67,7 +66,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { } report.Event(mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "stats": ksmData, }, }) diff --git a/metricbeat/module/linux/ksm/ksm_test.go b/metricbeat/module/linux/ksm/ksm_test.go index f4ee324d3d8..5a6b9562e94 100644 --- a/metricbeat/module/linux/ksm/ksm_test.go +++ b/metricbeat/module/linux/ksm/ksm_test.go @@ -22,9 +22,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/linux" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { @@ -54,7 +54,7 @@ func TestFetch(t *testing.T) { StableNodeDups: 0, } - rawEvent := events[0].BeatEvent("linux", "ksm").Fields["linux"].(common.MapStr)["ksm"].(common.MapStr)["stats"] + rawEvent := events[0].BeatEvent("linux", "ksm").Fields["linux"].(mapstr.M)["ksm"].(mapstr.M)["stats"] assert.Equal(t, testKSM, rawEvent) } diff --git a/metricbeat/module/linux/memory/data.go b/metricbeat/module/linux/memory/data.go index e825d6b565b..c83b355acfa 100644 --- a/metricbeat/module/linux/memory/data.go +++ b/metricbeat/module/linux/memory/data.go @@ -30,16 +30,17 @@ import ( "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/internal/metrics/memory" metrics "github.com/elastic/beats/v7/metricbeat/internal/metrics/memory" + "github.com/elastic/elastic-agent-libs/mapstr" ) // FetchLinuxMemStats gets page_stat and huge pages data for linux -func FetchLinuxMemStats(baseMap common.MapStr, hostfs resolve.Resolver) error { +func FetchLinuxMemStats(baseMap mapstr.M, hostfs resolve.Resolver) error { vmstat, err := GetVMStat(hostfs) if err != nil { return errors.Wrap(err, "error fetching VMStats") } - pageStats := common.MapStr{} + pageStats := mapstr.M{} insertPagesChild("pgscan_kswapd", vmstat, pageStats) insertPagesChild("pgscan_direct", vmstat, pageStats) @@ -72,7 +73,7 @@ func FetchLinuxMemStats(baseMap common.MapStr, hostfs resolve.Resolver) error { if err != nil { return errors.Wrap(err, "error fetching memory metrics") } - swap := common.MapStr{} + swap := mapstr.M{} err = typeconv.Convert(&swap, &eventRaw.Swap) if err != nil { return errors.Wrap(err, "error converting raw event") @@ -91,7 +92,7 @@ func FetchLinuxMemStats(baseMap common.MapStr, hostfs resolve.Resolver) error { return nil } -func map2evt(inName string, outName string, rawEvt map[string]uint64, outEvt common.MapStr) { +func map2evt(inName string, outName string, rawEvt map[string]uint64, outEvt mapstr.M) { if selected, ok := rawEvt[inName]; ok { outEvt.Put(outName, selected) } @@ -99,31 +100,31 @@ func map2evt(inName string, outName string, rawEvt map[string]uint64, outEvt com // insertPagesChild inserts a "child" MapStr into given events. This is mostly so we don't break mapping for fields that have been around. // most of the fields in vmstat are fairly esoteric and (somewhat) self-documenting, so use of this shouldn't expand beyond what's needed for backwards compat. -func insertPagesChild(field string, raw map[string]uint64, evt common.MapStr) { +func insertPagesChild(field string, raw map[string]uint64, evt mapstr.M) { stat, ok := raw[field] if ok { evt.Put(fmt.Sprintf("%s.pages", field), stat) } } -func computeEfficiency(scanName string, stealName string, fieldName string, raw map[string]uint64, inMap common.MapStr) { +func computeEfficiency(scanName string, stealName string, fieldName string, raw map[string]uint64, inMap mapstr.M) { scanVal, _ := raw[scanName] stealVal, stealOk := raw[stealName] if scanVal != 0 && stealOk { - inMap[fieldName] = common.MapStr{ + inMap[fieldName] = mapstr.M{ "pct": common.Round(float64(stealVal)/float64(scanVal), common.DefaultDecimalPlacesCount), } } } -func getHugePages(hostfs resolve.Resolver) (common.MapStr, error) { +func getHugePages(hostfs resolve.Resolver) (mapstr.M, error) { // see https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt table, err := memory.ParseMeminfo(hostfs) if err != nil { return nil, errors.Wrap(err, "error parsing meminfo") } - thp := common.MapStr{} + thp := mapstr.M{} total, okTotal := table["HugePages_Total"] free, okFree := table["HugePages_Free"] diff --git a/metricbeat/module/linux/memory/memory.go b/metricbeat/module/linux/memory/memory.go index bd71cadc690..b70349b0267 100644 --- a/metricbeat/module/linux/memory/memory.go +++ b/metricbeat/module/linux/memory/memory.go @@ -20,10 +20,10 @@ package memory import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -58,7 +58,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // format. It publishes the event which is then forwarded to the output. In case // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(report mb.ReporterV2) error { - rootEvent := common.MapStr{} + rootEvent := mapstr.M{} err := FetchLinuxMemStats(rootEvent, m.mod) if err != nil { return errors.Wrap(err, "error fetching memory stats") diff --git a/metricbeat/module/linux/memory/memory_linux_test.go b/metricbeat/module/linux/memory/memory_linux_test.go index 9c2cb409550..638e5be20de 100644 --- a/metricbeat/module/linux/memory/memory_linux_test.go +++ b/metricbeat/module/linux/memory/memory_linux_test.go @@ -22,31 +22,31 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/linux" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestPercents(t *testing.T) { res := resolve.NewTestResolver("./_meta/testdata/") - data := common.MapStr{} + data := mapstr.M{} err := FetchLinuxMemStats(data, res) assert.NoError(t, err, "FetchLinuxMemStats") - assert.Equal(t, float64(1), data["page_stats"].(common.MapStr)["kswapd_efficiency"].(common.MapStr)["pct"].(float64)) - assert.Equal(t, float64(0.7143), data["page_stats"].(common.MapStr)["direct_efficiency"].(common.MapStr)["pct"].(float64)) + assert.Equal(t, float64(1), data["page_stats"].(mapstr.M)["kswapd_efficiency"].(mapstr.M)["pct"].(float64)) + assert.Equal(t, float64(0.7143), data["page_stats"].(mapstr.M)["direct_efficiency"].(mapstr.M)["pct"].(float64)) } func TestPagesFields(t *testing.T) { res := resolve.NewTestResolver("./_meta/testdata/") - data := common.MapStr{} + data := mapstr.M{} err := FetchLinuxMemStats(data, res) assert.NoError(t, err, "FetchLinuxMemStats") - assert.Equal(t, uint64(2077939388), data["page_stats"].(common.MapStr)["pgfree"].(common.MapStr)["pages"].(uint64)) - assert.Equal(t, uint64(7), data["page_stats"].(common.MapStr)["pgscan_direct"].(common.MapStr)["pages"].(uint64)) - assert.Equal(t, uint64(5), data["page_stats"].(common.MapStr)["pgsteal_direct"].(common.MapStr)["pages"].(uint64)) + assert.Equal(t, uint64(2077939388), data["page_stats"].(mapstr.M)["pgfree"].(mapstr.M)["pages"].(uint64)) + assert.Equal(t, uint64(7), data["page_stats"].(mapstr.M)["pgscan_direct"].(mapstr.M)["pages"].(uint64)) + assert.Equal(t, uint64(5), data["page_stats"].(mapstr.M)["pgsteal_direct"].(mapstr.M)["pages"].(uint64)) } func TestFetch(t *testing.T) { diff --git a/metricbeat/module/linux/pageinfo/pageinfo.go b/metricbeat/module/linux/pageinfo/pageinfo.go index 2ffad602d99..016294cc22a 100644 --- a/metricbeat/module/linux/pageinfo/pageinfo.go +++ b/metricbeat/module/linux/pageinfo/pageinfo.go @@ -23,10 +23,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -79,7 +79,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { } report.Event(mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "nodes": zones.Zones, "buddy_info": zones.BuddyInfo, }, diff --git a/metricbeat/module/linux/pageinfo/pageinfo_test.go b/metricbeat/module/linux/pageinfo/pageinfo_test.go index 272e79cb0a7..329d2ad6f92 100644 --- a/metricbeat/module/linux/pageinfo/pageinfo_test.go +++ b/metricbeat/module/linux/pageinfo/pageinfo_test.go @@ -22,9 +22,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/linux" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { @@ -50,7 +50,7 @@ func TestFetch(t *testing.T) { Normal: map[int]int64{0: 3409, 1: 4916, 2: 4010, 3: 4853, 4: 3613, 5: 2472, 6: 793, 7: 552, 8: 189, 9: 148, 10: 292}, } - rawEvent := events[0].BeatEvent("linux", "pageinfo").Fields["linux"].(common.MapStr)["pageinfo"].(common.MapStr)["buddy_info"] + rawEvent := events[0].BeatEvent("linux", "pageinfo").Fields["linux"].(mapstr.M)["pageinfo"].(mapstr.M)["buddy_info"] assert.Equal(t, testBuddyInfo, rawEvent) diff --git a/metricbeat/module/linux/pressure/pressure.go b/metricbeat/module/linux/pressure/pressure.go index f352cc04843..e856673e22a 100644 --- a/metricbeat/module/linux/pressure/pressure.go +++ b/metricbeat/module/linux/pressure/pressure.go @@ -24,10 +24,10 @@ import ( "github.com/pkg/errors" "github.com/prometheus/procfs" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -86,9 +86,9 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { return nil } -func fetchLinuxPSIStats(m *MetricSet) ([]common.MapStr, error) { +func fetchLinuxPSIStats(m *MetricSet) ([]mapstr.M, error) { resources := []string{"cpu", "memory", "io"} - events := []common.MapStr{} + events := []mapstr.M{} procfs, err := procfs.NewFS(m.mod.ResolveHostFS("/proc")) if err != nil { @@ -101,20 +101,20 @@ func fetchLinuxPSIStats(m *MetricSet) ([]common.MapStr, error) { return nil, errors.Wrap(err, "check that /proc/pressure is available, and/or enabled") } - event := common.MapStr{ - resource: common.MapStr{ - "some": common.MapStr{ - "10": common.MapStr{ + event := mapstr.M{ + resource: mapstr.M{ + "some": mapstr.M{ + "10": mapstr.M{ "pct": psiMetric.Some.Avg10, }, - "60": common.MapStr{ + "60": mapstr.M{ "pct": psiMetric.Some.Avg60, }, - "300": common.MapStr{ + "300": mapstr.M{ "pct": psiMetric.Some.Avg300, }, - "total": common.MapStr{ - "time": common.MapStr{ + "total": mapstr.M{ + "time": mapstr.M{ "us": psiMetric.Some.Total, }, }, diff --git a/metricbeat/module/linux/pressure/pressure_test.go b/metricbeat/module/linux/pressure/pressure_test.go index d6b737dc1ef..9abaea577e4 100644 --- a/metricbeat/module/linux/pressure/pressure_test.go +++ b/metricbeat/module/linux/pressure/pressure_test.go @@ -25,10 +25,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" - mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/linux" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -47,20 +46,20 @@ func TestFetch(t *testing.T) { for i := range events { resource := resources[i] - testEvent := common.MapStr{ - resource: common.MapStr{ - "some": common.MapStr{ - "10": common.MapStr{ + testEvent := mapstr.M{ + resource: mapstr.M{ + "some": mapstr.M{ + "10": mapstr.M{ "pct": 5.86, }, - "60": common.MapStr{ + "60": mapstr.M{ "pct": 1.10, }, - "300": common.MapStr{ + "300": mapstr.M{ "pct": 0.23, }, - "total": common.MapStr{ - "time": common.MapStr{ + "total": mapstr.M{ + "time": mapstr.M{ "us": uint64(9895236), }, }, @@ -75,7 +74,7 @@ func TestFetch(t *testing.T) { testEvent.Put(resource+".full.total.time.us", uint64(10895236)) } - rawEvent := events[i].BeatEvent("linux", "pressure").Fields["linux"].(common.MapStr)["pressure"] + rawEvent := events[i].BeatEvent("linux", "pressure").Fields["linux"].(mapstr.M)["pressure"] assert.Equal(t, testEvent, rawEvent) } } diff --git a/metricbeat/module/linux/rapl/rapl.go b/metricbeat/module/linux/rapl/rapl.go index 40a627f53f8..9c85c6fbd4f 100644 --- a/metricbeat/module/linux/rapl/rapl.go +++ b/metricbeat/module/linux/rapl/rapl.go @@ -39,6 +39,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -143,11 +144,11 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { watts := m.updatePower() for cpu, metric := range watts { - evt := common.MapStr{ + evt := mapstr.M{ "core": cpu, } for domain, power := range metric { - evt[strings.ToLower(domain.Name)] = common.MapStr{ + evt[strings.ToLower(domain.Name)] = mapstr.M{ "watts": common.Round(power.watts, common.DefaultDecimalPlacesCount), "joules": common.Round(power.joules, common.DefaultDecimalPlacesCount), } diff --git a/metricbeat/module/logstash/node/data.go b/metricbeat/module/logstash/node/data.go index 1d5725ed969..42fcbfe3f48 100644 --- a/metricbeat/module/logstash/node/data.go +++ b/metricbeat/module/logstash/node/data.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/logstash" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -42,8 +43,8 @@ var ( } ) -func commonFieldsMapping(event *mb.Event, fields common.MapStr) error { - event.RootFields = common.MapStr{} +func commonFieldsMapping(event *mb.Event, fields mapstr.M) error { + event.RootFields = mapstr.M{} event.RootFields.Put("service.name", logstash.ModuleName) // Set service ID @@ -109,10 +110,10 @@ func eventMapping(r mb.ReporterV2, content []byte, pipelines []logstash.Pipeline } event := mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "state": logstashState, }, - ModuleFields: common.MapStr{}, + ModuleFields: mapstr.M{}, } event.MetricSetFields.Update(fields) diff --git a/metricbeat/module/logstash/node_stats/data.go b/metricbeat/module/logstash/node_stats/data.go index d91e2724548..5eb89db9de7 100644 --- a/metricbeat/module/logstash/node_stats/data.go +++ b/metricbeat/module/logstash/node_stats/data.go @@ -22,6 +22,7 @@ import ( "time" "github.com/elastic/beats/v7/metricbeat/helper/elastic" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/metricbeat/module/logstash" @@ -192,10 +193,10 @@ func eventMapping(r mb.ReporterV2, content []byte, isXpack bool) error { } event := mb.Event{ - RootFields: common.MapStr{ - "service": common.MapStr{"name": logstash.ModuleName}, + RootFields: mapstr.M{ + "service": mapstr.M{"name": logstash.ModuleName}, }, - ModuleFields: common.MapStr{}, + ModuleFields: mapstr.M{}, } event.ModuleFields.Put("node.stats", logstashStats) diff --git a/metricbeat/module/mongodb/collstats/collstats.go b/metricbeat/module/mongodb/collstats/collstats.go index ebf7db992ce..51233f254a9 100644 --- a/metricbeat/module/mongodb/collstats/collstats.go +++ b/metricbeat/module/mongodb/collstats/collstats.go @@ -20,9 +20,9 @@ package collstats import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/mongodb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -62,7 +62,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } defer mongoSession.Close() - result := common.MapStr{} + result := mapstr.M{} err = mongoSession.Run("top", &result) if err != nil { @@ -73,7 +73,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { return errors.New("Error accessing collection totals in returned data") } - totals, ok := result["totals"].(common.MapStr) + totals, ok := result["totals"].(mapstr.M) if !ok { return errors.New("Collection totals are not a map") } @@ -83,7 +83,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { continue } - infoMap, ok := info.(common.MapStr) + infoMap, ok := info.(mapstr.M) if !ok { err = errors.New("Unexpected data returned by mongodb") reporter.Error(err) diff --git a/metricbeat/module/mongodb/collstats/data.go b/metricbeat/module/mongodb/collstats/data.go index 5d70c68f7f3..f896722345a 100644 --- a/metricbeat/module/mongodb/collstats/data.go +++ b/metricbeat/module/mongodb/collstats/data.go @@ -21,72 +21,72 @@ import ( "errors" "strings" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(key string, data common.MapStr) (common.MapStr, error) { +func eventMapping(key string, data mapstr.M) (mapstr.M, error) { names := strings.SplitN(key, ".", 2) if len(names) < 2 { return nil, errors.New("Collection name invalid") } - event := common.MapStr{ + event := mapstr.M{ "db": names[0], "collection": names[1], "name": key, - "total": common.MapStr{ - "time": common.MapStr{ + "total": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "total.time"), }, "count": mustGetMapStrValue(data, "total.count"), }, - "lock": common.MapStr{ - "read": common.MapStr{ - "time": common.MapStr{ + "lock": mapstr.M{ + "read": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "readLock.time"), }, "count": mustGetMapStrValue(data, "readLock.count"), }, - "write": common.MapStr{ - "time": common.MapStr{ + "write": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "writeLock.time"), }, "count": mustGetMapStrValue(data, "writeLock.count"), }, }, - "queries": common.MapStr{ - "time": common.MapStr{ + "queries": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "queries.time"), }, "count": mustGetMapStrValue(data, "queries.count"), }, - "getmore": common.MapStr{ - "time": common.MapStr{ + "getmore": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "getmore.time"), }, "count": mustGetMapStrValue(data, "getmore.count"), }, - "insert": common.MapStr{ - "time": common.MapStr{ + "insert": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "insert.time"), }, "count": mustGetMapStrValue(data, "insert.count"), }, - "update": common.MapStr{ - "time": common.MapStr{ + "update": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "update.time"), }, "count": mustGetMapStrValue(data, "update.count"), }, - "remove": common.MapStr{ - "time": common.MapStr{ + "remove": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "remove.time"), }, "count": mustGetMapStrValue(data, "remove.count"), }, - "commands": common.MapStr{ - "time": common.MapStr{ + "commands": mapstr.M{ + "time": mapstr.M{ "us": mustGetMapStrValue(data, "commands.time"), }, "count": mustGetMapStrValue(data, "commands.count"), @@ -96,7 +96,7 @@ func eventMapping(key string, data common.MapStr) (common.MapStr, error) { return event, nil } -func mustGetMapStrValue(m common.MapStr, key string) interface{} { +func mustGetMapStrValue(m mapstr.M, key string) interface{} { v, _ := m.GetValue(key) return v } diff --git a/metricbeat/module/mongodb/collstats/data_test.go b/metricbeat/module/mongodb/collstats/data_test.go index 4797c44d5c1..dde72277405 100644 --- a/metricbeat/module/mongodb/collstats/data_test.go +++ b/metricbeat/module/mongodb/collstats/data_test.go @@ -25,9 +25,9 @@ import ( "io/ioutil" "testing" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/stretchr/testify/assert" + + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEventMapping(t *testing.T) { @@ -35,10 +35,10 @@ func TestEventMapping(t *testing.T) { content, err := ioutil.ReadFile("./_meta/test/input.json") assert.NoError(t, err) - data := common.MapStr{} + data := mapstr.M{} json.Unmarshal(content, &data) event, _ := eventMapping("unit.test", data) - assert.Equal(t, event["total"].(common.MapStr)["count"], float64(1)) + assert.Equal(t, event["total"].(mapstr.M)["count"], float64(1)) } diff --git a/metricbeat/module/mongodb/dbstats/dbstats.go b/metricbeat/module/mongodb/dbstats/dbstats.go index 4153793ed26..2a01b2759b0 100644 --- a/metricbeat/module/mongodb/dbstats/dbstats.go +++ b/metricbeat/module/mongodb/dbstats/dbstats.go @@ -20,10 +20,10 @@ package dbstats import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/mongodb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var logger = logp.NewLogger("mongodb.dbstats") @@ -78,7 +78,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { for _, dbName := range dbNames { db := mongoSession.DB(dbName) - result := common.MapStr{} + result := mapstr.M{} err := db.Run("dbStats", &result) if err != nil { diff --git a/metricbeat/module/mongodb/replstatus/data.go b/metricbeat/module/mongodb/replstatus/data.go index fdeeb2b5381..ed5e8cfb0bd 100644 --- a/metricbeat/module/mongodb/replstatus/data.go +++ b/metricbeat/module/mongodb/replstatus/data.go @@ -17,29 +17,27 @@ package replstatus -import ( - "github.com/elastic/beats/v7/libbeat/common" -) +import "github.com/elastic/elastic-agent-libs/mapstr" -func eventMapping(oplogInfo oplogInfo, replStatus MongoReplStatus) common.MapStr { - var result common.MapStr = make(common.MapStr) +func eventMapping(oplogInfo oplogInfo, replStatus MongoReplStatus) mapstr.M { + var result mapstr.M = make(mapstr.M) - result["oplog"] = common.MapStr{ - "size": common.MapStr{ + result["oplog"] = mapstr.M{ + "size": mapstr.M{ "allocated": oplogInfo.allocated, "used": oplogInfo.used, }, - "first": common.MapStr{ + "first": mapstr.M{ "timestamp": oplogInfo.firstTs, }, - "last": common.MapStr{ + "last": mapstr.M{ "timestamp": oplogInfo.lastTs, }, "window": oplogInfo.diff, } result["set_name"] = replStatus.Set result["server_date"] = replStatus.Date - result["optimes"] = common.MapStr{ + result["optimes"] = mapstr.M{ "last_committed": replStatus.OpTimes.LastCommitted.getTimeStamp(), "applied": replStatus.OpTimes.Applied.getTimeStamp(), "durable": replStatus.OpTimes.Durable.getTimeStamp(), @@ -48,22 +46,22 @@ func eventMapping(oplogInfo oplogInfo, replStatus MongoReplStatus) common.MapStr // find lag and headroom minLag, maxLag, lagIsOk := findLag(replStatus.Members) if lagIsOk { - result["lag"] = common.MapStr{ + result["lag"] = mapstr.M{ "max": maxLag, "min": minLag, } - result["headroom"] = common.MapStr{ + result["headroom"] = mapstr.M{ "max": oplogInfo.diff - minLag, "min": oplogInfo.diff - maxLag, } } else { - result["lag"] = common.MapStr{ + result["lag"] = mapstr.M{ "max": nil, "min": nil, } - result["headroom"] = common.MapStr{ + result["headroom"] = mapstr.M{ "max": nil, "min": nil, } @@ -80,41 +78,41 @@ func eventMapping(oplogInfo oplogInfo, replStatus MongoReplStatus) common.MapStr unhealthyHosts = findUnhealthyHosts(replStatus.Members) ) - result["members"] = common.MapStr{ - "primary": common.MapStr{ + result["members"] = mapstr.M{ + "primary": mapstr.M{ "host": findHostsByState(replStatus.Members, PRIMARY)[0], "optime": findOptimesByState(replStatus.Members, PRIMARY)[0], }, - "secondary": common.MapStr{ + "secondary": mapstr.M{ "hosts": secondaryHosts, "count": len(secondaryHosts), "optimes": findOptimesByState(replStatus.Members, SECONDARY), }, - "recovering": common.MapStr{ + "recovering": mapstr.M{ "hosts": recoveringHosts, "count": len(recoveringHosts), }, - "unknown": common.MapStr{ + "unknown": mapstr.M{ "hosts": unknownHosts, "count": len(unknownHosts), }, - "startup2": common.MapStr{ + "startup2": mapstr.M{ "hosts": startup2Hosts, "count": len(startup2Hosts), }, - "arbiter": common.MapStr{ + "arbiter": mapstr.M{ "hosts": arbiterHosts, "count": len(arbiterHosts), }, - "down": common.MapStr{ + "down": mapstr.M{ "hosts": downHosts, "count": len(downHosts), }, - "rollback": common.MapStr{ + "rollback": mapstr.M{ "hosts": rollbackHosts, "count": len(rollbackHosts), }, - "unhealthy": common.MapStr{ + "unhealthy": mapstr.M{ "hosts": unhealthyHosts, "count": len(unhealthyHosts), }, diff --git a/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go b/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go index 0dbaa484e17..31bef3dd34b 100644 --- a/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go +++ b/metricbeat/module/mongodb/replstatus/replstatus_integration_test.go @@ -28,10 +28,10 @@ import ( mgo "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/mongodb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -54,21 +54,21 @@ func TestFetch(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events[0]) // Check event fields - oplog := event["oplog"].(common.MapStr) - allocated := oplog["size"].(common.MapStr)["allocated"].(int64) + oplog := event["oplog"].(mapstr.M) + allocated := oplog["size"].(mapstr.M)["allocated"].(int64) assert.True(t, allocated >= 0) - used := oplog["size"].(common.MapStr)["used"].(float64) + used := oplog["size"].(mapstr.M)["used"].(float64) assert.True(t, used > 0) - firstTs := oplog["first"].(common.MapStr)["timestamp"].(int64) + firstTs := oplog["first"].(mapstr.M)["timestamp"].(int64) assert.True(t, firstTs >= 0) window := oplog["window"].(int64) assert.True(t, window >= 0) - members := event["members"].(common.MapStr) - primary := members["primary"].(common.MapStr) + members := event["members"].(mapstr.M) + primary := members["primary"].(mapstr.M) assert.NotEmpty(t, primary["host"].(string)) assert.True(t, primary["optime"].(int64) > 0) diff --git a/metricbeat/module/mongodb/status/status.go b/metricbeat/module/mongodb/status/status.go index 8615d84e358..79d0842e573 100644 --- a/metricbeat/module/mongodb/status/status.go +++ b/metricbeat/module/mongodb/status/status.go @@ -20,9 +20,9 @@ package status import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/mongodb" + "github.com/elastic/elastic-agent-libs/mapstr" "gopkg.in/mgo.v2/bson" ) @@ -71,7 +71,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } event := mb.Event{ - RootFields: common.MapStr{}, + RootFields: mapstr.M{}, } event.MetricSetFields, _ = schema.Apply(result) diff --git a/metricbeat/module/munin/munin.go b/metricbeat/module/munin/munin.go index 39837a42bcd..9f4f3aa3420 100644 --- a/metricbeat/module/munin/munin.go +++ b/metricbeat/module/munin/munin.go @@ -28,8 +28,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -85,13 +85,13 @@ func (n *Node) List() ([]string, error) { } // Fetch metrics from munin node -func (n *Node) Fetch(plugin string, sanitize bool) (common.MapStr, error) { +func (n *Node) Fetch(plugin string, sanitize bool) (mapstr.M, error) { _, err := io.WriteString(n.writer, "fetch "+plugin+"\n") if err != nil { return nil, errors.Wrapf(err, "failed to fetch metrics for plugin '%s'", plugin) } - event := common.MapStr{} + event := mapstr.M{} scanner := bufio.NewScanner(n.reader) scanner.Split(bufio.ScanWords) for scanner.Scan() { diff --git a/metricbeat/module/munin/munin_test.go b/metricbeat/module/munin/munin_test.go index b6f0f544b9d..80df5a5c5b0 100644 --- a/metricbeat/module/munin/munin_test.go +++ b/metricbeat/module/munin/munin_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func dummyNode(response string) *Node { @@ -75,12 +75,12 @@ func TestFetch(t *testing.T) { cases := []struct { title string response string - expected common.MapStr + expected mapstr.M }{ { "normal case", responseCPU, - common.MapStr{ + mapstr.M{ "user": float64(4679836), "nice": float64(59278), "system": float64(1979168), @@ -95,14 +95,14 @@ func TestFetch(t *testing.T) { { "unknown values", responseUnknown, - common.MapStr{ + mapstr.M{ "other": float64(42), }, }, { "wrong field names", responseWithWrongFields, - common.MapStr{ + mapstr.M{ "user": float64(4679836), "nice": float64(59278), "system": float64(1979168), diff --git a/metricbeat/module/munin/node/node.go b/metricbeat/module/munin/node/node.go index 529031a500c..a0668a98681 100644 --- a/metricbeat/module/munin/node/node.go +++ b/metricbeat/module/munin/node/node.go @@ -22,9 +22,9 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/munin" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -97,9 +97,9 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } event := mb.Event{ Service: plugin, - RootFields: common.MapStr{ - "munin": common.MapStr{ - "plugin": common.MapStr{ + RootFields: mapstr.M{ + "munin": mapstr.M{ + "plugin": mapstr.M{ "name": plugin, }, "metrics": metrics, diff --git a/metricbeat/module/mysql/galera_status/data.go b/metricbeat/module/mysql/galera_status/data.go index 82fa64c44b7..643576d3d75 100644 --- a/metricbeat/module/mysql/galera_status/data.go +++ b/metricbeat/module/mysql/galera_status/data.go @@ -18,9 +18,9 @@ package galera_status import ( - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -94,7 +94,7 @@ var ( // Map data to MapStr of server stats variables: http://galeracluster.com/documentation-webpages/galerastatusvariables.html // queryMode specifies, which subset of the available Variables is used. -func eventMapping(status map[string]string) common.MapStr { +func eventMapping(status map[string]string) mapstr.M { source := map[string]interface{}{} for key, val := range status { source[key] = val @@ -105,8 +105,8 @@ func eventMapping(status map[string]string) common.MapStr { } // Maps all variables from the status fetch which are not in the predefined schema -func rawEventMapping(status map[string]string) common.MapStr { - source := common.MapStr{} +func rawEventMapping(status map[string]string) mapstr.M { + source := mapstr.M{} for key, val := range status { // Only adds events which are not in the mapping if schema.HasKey(key) { diff --git a/metricbeat/module/mysql/query/query.go b/metricbeat/module/mysql/query/query.go index d7664cc8635..1af5d3644cd 100644 --- a/metricbeat/module/mysql/query/query.go +++ b/metricbeat/module/mysql/query/query.go @@ -28,11 +28,11 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/metricbeat/helper/sql" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/mysql" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -120,8 +120,8 @@ func (m *MetricSet) fetchQuery(ctx context.Context, query query, reporter mb.Rep return nil } -func (m *MetricSet) transformMapStrToEvent(query query, ms common.MapStr) mb.Event { - event := mb.Event{ModuleFields: common.MapStr{m.Config.Namespace: common.MapStr{}}} +func (m *MetricSet) transformMapStrToEvent(query query, ms mapstr.M) mb.Event { + event := mb.Event{ModuleFields: mapstr.M{m.Config.Namespace: mapstr.M{}}} data := ms if query.ReplaceUnderscores { @@ -129,7 +129,7 @@ func (m *MetricSet) transformMapStrToEvent(query query, ms common.MapStr) mb.Eve } if query.Namespace != "" { - event.ModuleFields[m.Config.Namespace] = common.MapStr{query.Namespace: data} + event.ModuleFields[m.Config.Namespace] = mapstr.M{query.Namespace: data} } else { event.ModuleFields[m.Config.Namespace] = data } diff --git a/metricbeat/module/mysql/status/data.go b/metricbeat/module/mysql/status/data.go index ab55a47e450..52e1709bf53 100644 --- a/metricbeat/module/mysql/status/data.go +++ b/metricbeat/module/mysql/status/data.go @@ -18,9 +18,9 @@ package status import ( - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -161,7 +161,7 @@ var ( // Map data to MapStr of server stats variables: http://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html // This is only a subset of the available values -func eventMapping(status map[string]string) common.MapStr { +func eventMapping(status map[string]string) mapstr.M { source := map[string]interface{}{} for key, val := range status { source[key] = val @@ -170,8 +170,8 @@ func eventMapping(status map[string]string) common.MapStr { return data } -func rawEventMapping(status map[string]string) common.MapStr { - source := common.MapStr{} +func rawEventMapping(status map[string]string) mapstr.M { + source := mapstr.M{} for key, val := range status { // Only adds events which are not in the mapping if schema.HasKey(key) { diff --git a/metricbeat/module/mysql/status/status_integration_test.go b/metricbeat/module/mysql/status/status_integration_test.go index 506f7955c7a..3b2c7b2639b 100644 --- a/metricbeat/module/mysql/status/status_integration_test.go +++ b/metricbeat/module/mysql/status/status_integration_test.go @@ -23,10 +23,10 @@ package status import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/mysql" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -45,7 +45,7 @@ func TestFetch(t *testing.T) { // Check event fields connections := event["connections"].(int64) - open := event["open"].(common.MapStr) + open := event["open"].(mapstr.M) openTables := open["tables"].(int64) openFiles := open["files"].(int64) openStreams := open["streams"].(int64) @@ -69,10 +69,10 @@ func TestFetchRaw(t *testing.T) { t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check event fields - cachedThreads := event["threads"].(common.MapStr)["cached"].(int64) + cachedThreads := event["threads"].(mapstr.M)["cached"].(int64) assert.True(t, cachedThreads >= 0) - rawData := event["raw"].(common.MapStr) + rawData := event["raw"].(mapstr.M) // Make sure field was removed from raw fields as in schema _, exists := rawData["Threads_cached"] diff --git a/metricbeat/module/nats/stats/data.go b/metricbeat/module/nats/stats/data.go index fb014eea466..7bc72177a54 100644 --- a/metricbeat/module/nats/stats/data.go +++ b/metricbeat/module/nats/stats/data.go @@ -22,11 +22,11 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/nats/util" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -66,7 +66,7 @@ var ( ) func eventMapping(r mb.ReporterV2, content []byte) error { - var metricsetMetrics common.MapStr + var metricsetMetrics mapstr.M var inInterface map[string]interface{} err := json.Unmarshal(content, &inInterface) @@ -87,7 +87,7 @@ func eventMapping(r mb.ReporterV2, content []byte) error { if err != nil { return errors.Wrap(err, "failure retrieving http_req_stats key") } - httpStats, ok := d.(common.MapStr) + httpStats, ok := d.(mapstr.M) if !ok { return errors.Wrap(err, "failure casting http_req_stats to common.Mapstr") @@ -97,9 +97,9 @@ func eventMapping(r mb.ReporterV2, content []byte) error { return errors.Wrap(err, "failure deleting http_req_stats key") } - metricsetMetrics["http"] = common.MapStr{ - "req_stats": common.MapStr{ - "uri": common.MapStr{ + metricsetMetrics["http"] = mapstr.M{ + "req_stats": mapstr.M{ + "uri": mapstr.M{ "root": httpStats["root_uri"], "connz": httpStats["connz_uri"], "routez": httpStats["routez_uri"], diff --git a/metricbeat/module/nats/util/util.go b/metricbeat/module/nats/util/util.go index da95c12391a..1decbf0bb9f 100644 --- a/metricbeat/module/nats/util/util.go +++ b/metricbeat/module/nats/util/util.go @@ -25,7 +25,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // convertUptimeToSeconds converts uptime from formatted string to seconds @@ -91,8 +91,8 @@ func convertUptimeToSeconds(uptime string) (seconds int64, err error) { return } -// UpdateDuration updates a duration in a common.MapStr from formatted string to seconds -func UpdateDuration(event common.MapStr, key string) error { +// UpdateDuration updates a duration in a mapstr.M from formatted string to seconds +func UpdateDuration(event mapstr.M, key string) error { item, err := event.GetValue(key) if err != nil { return nil @@ -109,7 +109,7 @@ func UpdateDuration(event common.MapStr, key string) error { } // GetNatsTimestamp gets the timestamp of base level metrics NATS server returns -func GetNatsTimestamp(event common.MapStr) (time.Time, error) { +func GetNatsTimestamp(event mapstr.M) (time.Time, error) { var timeStamp time.Time timestamp, _ := event.GetValue("server.time") timestampString := timestamp.(string) diff --git a/metricbeat/module/nginx/stubstatus/data.go b/metricbeat/module/nginx/stubstatus/data.go index c58cabe43da..402e2c2d4cd 100644 --- a/metricbeat/module/nginx/stubstatus/data.go +++ b/metricbeat/module/nginx/stubstatus/data.go @@ -23,7 +23,7 @@ import ( "regexp" "strconv" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -33,7 +33,7 @@ var ( ) // Map body to MapStr -func eventMapping(scanner *bufio.Scanner, m *MetricSet) (common.MapStr, error) { +func eventMapping(scanner *bufio.Scanner, m *MetricSet) (mapstr.M, error) { // Nginx stub status sample: // Active connections: 1 // server accepts handled requests @@ -92,7 +92,7 @@ func eventMapping(scanner *bufio.Scanner, m *MetricSet) (common.MapStr, error) { writing, _ = strconv.Atoi(matches[2]) waiting, _ = strconv.Atoi(matches[3]) - event := common.MapStr{ + event := mapstr.M{ "hostname": m.Host(), "active": active, "accepts": accepts, diff --git a/metricbeat/module/openmetrics/collector/collector.go b/metricbeat/module/openmetrics/collector/collector.go index ab54dca1cfa..2739f8cf310 100644 --- a/metricbeat/module/openmetrics/collector/collector.go +++ b/metricbeat/module/openmetrics/collector/collector.go @@ -24,10 +24,10 @@ import ( "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/textparse" - "github.com/elastic/beats/v7/libbeat/common" p "github.com/elastic/beats/v7/metricbeat/helper/openmetrics" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -137,7 +137,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } families, err := m.openmetrics.GetFamilies() - eventList := map[textparse.MetricType]map[string]common.MapStr{} + eventList := map[textparse.MetricType]map[string]mapstr.M{} if err != nil { // send up event only families = append(families, m.upMetricFamily(0.0)) @@ -163,10 +163,10 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { labelsHash = openMetricEvent.LabelsHash() } if _, ok := eventList[openMetricEvent.Type]; !ok { - eventList[openMetricEvent.Type] = make(map[string]common.MapStr) + eventList[openMetricEvent.Type] = make(map[string]mapstr.M) } if _, ok := eventList[openMetricEvent.Type][labelsHash]; !ok { - eventList[openMetricEvent.Type][labelsHash] = common.MapStr{} + eventList[openMetricEvent.Type][labelsHash] = mapstr.M{} // Add default instance label if not already there if exists, _ := openMetricEvent.Labels.HasKey(upMetricInstanceLabel); !exists { @@ -206,7 +206,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { for _, e := range eventList { for _, ev := range e { isOpen := reporter.Event(mb.Event{ - RootFields: common.MapStr{m.namespace: ev}, + RootFields: mapstr.M{m.namespace: ev}, }) if !isOpen { break diff --git a/metricbeat/module/openmetrics/collector/collector_test.go b/metricbeat/module/openmetrics/collector/collector_test.go index dd798676612..069bd36f8c2 100644 --- a/metricbeat/module/openmetrics/collector/collector_test.go +++ b/metricbeat/module/openmetrics/collector/collector_test.go @@ -28,9 +28,9 @@ import ( "github.com/prometheus/prometheus/pkg/textparse" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/openmetrics" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" @@ -46,7 +46,7 @@ func TestSameLabels(t *testing.T) { mbtest.TestDataFilesWithConfig(t, "openmetrics", "collector", dataConfig) } func TestGetOpenMetricsEventsFromMetricFamily(t *testing.T) { - labels := common.MapStr{ + labels := mapstr.M{ "handler": "query", } tests := []struct { @@ -75,15 +75,15 @@ func TestGetOpenMetricsEventsFromMetricFamily(t *testing.T) { }, Event: []OpenMetricEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds_total": float64(10), }, }, Help: "foo", Type: textparse.MetricTypeCounter, Labels: labels, - Exemplars: common.MapStr{}, + Exemplars: mapstr.M{}, }, }, }, @@ -102,14 +102,14 @@ func TestGetOpenMetricsEventsFromMetricFamily(t *testing.T) { }, Event: []OpenMetricEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds": float64(10), }, }, Help: "foo", Type: textparse.MetricTypeGauge, - Labels: common.MapStr{}, + Labels: mapstr.M{}, }, }, }, @@ -135,23 +135,23 @@ func TestGetOpenMetricsEventsFromMetricFamily(t *testing.T) { }, Event: []OpenMetricEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds_count": uint64(10), "http_request_duration_microseconds_sum": float64(10), }, }, Help: "foo", Type: textparse.MetricTypeSummary, - Labels: common.MapStr{}, + Labels: mapstr.M{}, }, { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds": float64(10), }, }, - Labels: common.MapStr{ + Labels: mapstr.M{ "quantile": "0.99", }, }, @@ -179,24 +179,24 @@ func TestGetOpenMetricsEventsFromMetricFamily(t *testing.T) { }, Event: []OpenMetricEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds_count": uint64(10), "http_request_duration_microseconds_sum": float64(10), }, }, Help: "foo", Type: textparse.MetricTypeHistogram, - Labels: common.MapStr{}, + Labels: mapstr.M{}, }, { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds_bucket": uint64(10), }, }, - Labels: common.MapStr{"le": "0.99"}, - Exemplars: common.MapStr{}, + Labels: mapstr.M{"le": "0.99"}, + Exemplars: mapstr.M{}, }, }, }, @@ -221,8 +221,8 @@ func TestGetOpenMetricsEventsFromMetricFamily(t *testing.T) { }, Event: []OpenMetricEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds": float64(10), }, }, diff --git a/metricbeat/module/openmetrics/collector/data.go b/metricbeat/module/openmetrics/collector/data.go index a3b83ccd818..836b6107c88 100644 --- a/metricbeat/module/openmetrics/collector/data.go +++ b/metricbeat/module/openmetrics/collector/data.go @@ -24,20 +24,20 @@ import ( "github.com/prometheus/prometheus/pkg/textparse" p "github.com/elastic/beats/v7/metricbeat/helper/openmetrics" + "github.com/elastic/elastic-agent-libs/mapstr" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/labelhash" "github.com/elastic/beats/v7/metricbeat/mb" ) // OpenMetricEvent stores a set of one or more metrics with the same labels type OpenMetricEvent struct { - Data common.MapStr - Labels common.MapStr + Data mapstr.M + Labels mapstr.M Help string Type textparse.MetricType Unit string - Exemplars common.MapStr + Exemplars mapstr.M } // LabelsHash returns a repeatable string that is unique for the set of labels in this event @@ -45,7 +45,7 @@ func (p *OpenMetricEvent) LabelsHash() string { return labelhash.LabelHash(p.Labels) } func (p *OpenMetricEvent) MetaDataHash() string { - m := common.MapStr{} + m := mapstr.M{} m.DeepUpdate(p.Labels) if len(p.Help) > 0 { m["help"] = p.Help @@ -86,7 +86,7 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam } for _, metric := range metrics { - labels := common.MapStr{} + labels := mapstr.M{} mn := metric.GetName() if len(metric.Label) != 0 { @@ -97,9 +97,9 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam } } - exemplars := common.MapStr{} + exemplars := mapstr.M{} if metric.Exemplar != nil { - exemplars = common.MapStr{*mn: metric.Exemplar.Value} + exemplars = mapstr.M{*mn: metric.Exemplar.Value} if metric.Exemplar.HasTs { exemplars.Put("timestamp", metric.Exemplar.Ts) } @@ -117,8 +117,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam Type: textparse.MetricTypeCounter, Help: help, Unit: unit, - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ *mn: counter.GetValue(), }, }, @@ -135,8 +135,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam Type: textparse.MetricTypeGauge, Help: help, Unit: unit, - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: gauge.GetValue(), }, }, @@ -150,8 +150,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam if info.HasValidValue() { events = append(events, OpenMetricEvent{ Type: textparse.MetricTypeInfo, - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: info.GetValue(), }, }, @@ -165,8 +165,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam if stateset.HasValidValue() { events = append(events, OpenMetricEvent{ Type: textparse.MetricTypeStateset, - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: stateset.GetValue(), }, }, @@ -182,8 +182,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam Type: textparse.MetricTypeSummary, Help: help, Unit: unit, - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name + "_sum": summary.GetSampleSum(), name + "_count": summary.GetSampleCount(), }, @@ -200,8 +200,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam quantileLabels := labels.Clone() quantileLabels["quantile"] = strconv.FormatFloat(quantile.GetQuantile(), 'f', -1, 64) events = append(events, OpenMetricEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: quantile.GetValue(), }, }, @@ -226,8 +226,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam Type: typ, Help: help, Unit: unit, - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name + sum: histogram.GetSampleSum(), name + count: histogram.GetSampleCount(), }, @@ -242,7 +242,7 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam } if bucket.Exemplar != nil { - exemplars = common.MapStr{name: bucket.Exemplar.Value} + exemplars = mapstr.M{name: bucket.Exemplar.Value} if bucket.Exemplar.HasTs { exemplars.Put("timestamp", bucket.Exemplar.Ts) } @@ -257,8 +257,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam bucketLabels["le"] = strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64) events = append(events, OpenMetricEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name + "_bucket": bucket.GetCumulativeCount(), }, }, @@ -275,8 +275,8 @@ func (p *openmetricEventGenerator) GenerateOpenMetricsEvents(mf *p.OpenMetricFam Type: textparse.MetricTypeUnknown, Help: help, Unit: unit, - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: unknown.GetValue(), }, }, diff --git a/metricbeat/module/php_fpm/process/data.go b/metricbeat/module/php_fpm/process/data.go index 8a5d142ed05..ef09cc74b2f 100644 --- a/metricbeat/module/php_fpm/process/data.go +++ b/metricbeat/module/php_fpm/process/data.go @@ -22,8 +22,7 @@ import ( "strings" "github.com/elastic/beats/v7/metricbeat/mb" - - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type phpFpmStatus struct { @@ -56,28 +55,28 @@ func eventsMapping(r mb.ReporterV2, content []byte) error { //remapping process details to match the naming format for _, process := range status.Processes { event := mb.Event{ - RootFields: common.MapStr{ - "http": common.MapStr{ - "request": common.MapStr{ + RootFields: mapstr.M{ + "http": mapstr.M{ + "request": mapstr.M{ "method": strings.ToLower(process.RequestMethod), }, - "response": common.MapStr{ - "body": common.MapStr{ + "response": mapstr.M{ + "body": mapstr.M{ "bytes": process.ContentLength, }, }, }, - "user": common.MapStr{ + "user": mapstr.M{ "name": process.User, }, - "process": common.MapStr{ + "process": mapstr.M{ "pid": process.PID, }, - "url": common.MapStr{ + "url": mapstr.M{ "original": process.RequestURI, }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "state": process.State, "start_time": process.StartTime, "start_since": process.StartSince, @@ -89,7 +88,7 @@ func eventsMapping(r mb.ReporterV2, content []byte) error { }, } - event.ModuleFields = common.MapStr{} + event.ModuleFields = mapstr.M{} event.ModuleFields.Put("pool.name", status.Name) r.Event(event) } diff --git a/metricbeat/module/postgresql/activity/activity.go b/metricbeat/module/postgresql/activity/activity.go index 38558f18071..ac2748cd582 100644 --- a/metricbeat/module/postgresql/activity/activity.go +++ b/metricbeat/module/postgresql/activity/activity.go @@ -22,9 +22,9 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/postgresql" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry. @@ -64,7 +64,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } for _, result := range results { - var data common.MapStr + var data mapstr.M // If the activity is not connected to any database, it is from a backend service. This // can be distingished by checking if the record has a database identifier (`datid`). // Activity records on these cases have different sets of fields. diff --git a/metricbeat/module/postgresql/activity/activity_integration_test.go b/metricbeat/module/postgresql/activity/activity_integration_test.go index c4fd5cbd28b..a4c75eceb13 100644 --- a/metricbeat/module/postgresql/activity/activity_integration_test.go +++ b/metricbeat/module/postgresql/activity/activity_integration_test.go @@ -25,10 +25,10 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/postgresql" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -49,12 +49,12 @@ func TestFetch(t *testing.T) { // Check event fields if _, isQuery := event["database"]; isQuery { - db_oid := event["database"].(common.MapStr)["oid"].(int64) + db_oid := event["database"].(mapstr.M)["oid"].(int64) assert.True(t, db_oid > 0) assert.Contains(t, event, "user") - assert.Contains(t, event["user"].(common.MapStr), "name") - assert.Contains(t, event["user"].(common.MapStr), "id") + assert.Contains(t, event["user"].(mapstr.M), "name") + assert.Contains(t, event["user"].(mapstr.M), "id") } else { assert.Contains(t, event, "backend_type") assert.Contains(t, event, "wait_event") @@ -68,11 +68,11 @@ func TestData(t *testing.T) { f := mbtest.NewFetcher(t, getConfig(service.Host())) dbNameKey := "postgresql.activity.database.name" - f.WriteEventsCond(t, "", func(event common.MapStr) bool { + f.WriteEventsCond(t, "", func(event mapstr.M) bool { _, err := event.GetValue(dbNameKey) return err == nil }) - f.WriteEventsCond(t, "./_meta/data_backend.json", func(event common.MapStr) bool { + f.WriteEventsCond(t, "./_meta/data_backend.json", func(event mapstr.M) bool { _, err := event.GetValue(dbNameKey) return err != nil }) diff --git a/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go b/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go index 077ac2de9d6..eb5ffd3d053 100644 --- a/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go +++ b/metricbeat/module/postgresql/bgwriter/bgwriter_integration_test.go @@ -23,10 +23,10 @@ package bgwriter import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/postgresql" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -48,12 +48,12 @@ func TestFetch(t *testing.T) { assert.Contains(t, event, "buffers") assert.Contains(t, event, "stats_reset") - checkpoints := event["checkpoints"].(common.MapStr) + checkpoints := event["checkpoints"].(mapstr.M) assert.Contains(t, checkpoints, "scheduled") assert.Contains(t, checkpoints, "requested") assert.Contains(t, checkpoints, "times") - buffers := event["buffers"].(common.MapStr) + buffers := event["buffers"].(mapstr.M) assert.Contains(t, buffers, "checkpoints") assert.Contains(t, buffers, "clean") assert.Contains(t, buffers, "clean_full") diff --git a/metricbeat/module/postgresql/database/database_integration_test.go b/metricbeat/module/postgresql/database/database_integration_test.go index 648104de5d5..d19bb10b68b 100644 --- a/metricbeat/module/postgresql/database/database_integration_test.go +++ b/metricbeat/module/postgresql/database/database_integration_test.go @@ -23,10 +23,10 @@ package database import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/postgresql" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -52,7 +52,7 @@ func TestFetch(t *testing.T) { _, ok := event["name"].(string) assert.True(t, ok) - rows := event["rows"].(common.MapStr) + rows := event["rows"].(mapstr.M) assert.Contains(t, rows, "returned") assert.Contains(t, rows, "fetched") assert.Contains(t, rows, "inserted") @@ -63,7 +63,7 @@ func TestFetch(t *testing.T) { func TestData(t *testing.T) { service := compose.EnsureUp(t, "postgresql") - getOid := func(event common.MapStr) int { + getOid := func(event mapstr.M) int { oid, err := event.GetValue("postgresql.database.oid") require.NoError(t, err) @@ -79,10 +79,10 @@ func TestData(t *testing.T) { } f := mbtest.NewFetcher(t, getConfig(service.Host())) - f.WriteEventsCond(t, "", func(event common.MapStr) bool { + f.WriteEventsCond(t, "", func(event mapstr.M) bool { return getOid(event) != 0 }) - f.WriteEventsCond(t, "./_meta/data_shared.json", func(event common.MapStr) bool { + f.WriteEventsCond(t, "./_meta/data_shared.json", func(event mapstr.M) bool { return getOid(event) == 0 }) } diff --git a/metricbeat/module/postgresql/statement/statement_integration_test.go b/metricbeat/module/postgresql/statement/statement_integration_test.go index e8180a627ba..f81e0bd69d3 100644 --- a/metricbeat/module/postgresql/statement/statement_integration_test.go +++ b/metricbeat/module/postgresql/statement/statement_integration_test.go @@ -23,10 +23,10 @@ package statement import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/postgresql" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -46,21 +46,21 @@ func TestFetch(t *testing.T) { // Check event fields assert.Contains(t, event, "user") - assert.Contains(t, event["user"].(common.MapStr), "id") + assert.Contains(t, event["user"].(mapstr.M), "id") assert.Contains(t, event, "database") - db_oid := event["database"].(common.MapStr)["oid"].(int64) + db_oid := event["database"].(mapstr.M)["oid"].(int64) assert.True(t, db_oid > 0) assert.Contains(t, event, "query") - query := event["query"].(common.MapStr) + query := event["query"].(mapstr.M) assert.Contains(t, query, "id") assert.Contains(t, query, "text") assert.Contains(t, query, "calls") assert.Contains(t, query, "rows") assert.Contains(t, query, "time") - time := query["time"].(common.MapStr) + time := query["time"].(mapstr.M) assert.Contains(t, time, "total") assert.Contains(t, time, "min") assert.Contains(t, time, "max") @@ -68,24 +68,24 @@ func TestFetch(t *testing.T) { assert.Contains(t, time, "stddev") assert.Contains(t, query["memory"], "shared") - memory := query["memory"].(common.MapStr) + memory := query["memory"].(mapstr.M) assert.Contains(t, memory, "shared") - shared := memory["shared"].(common.MapStr) + shared := memory["shared"].(mapstr.M) assert.Contains(t, shared, "hit") assert.Contains(t, shared, "read") assert.Contains(t, shared, "dirtied") assert.Contains(t, shared, "written") assert.Contains(t, memory, "local") - local := memory["local"].(common.MapStr) + local := memory["local"].(mapstr.M) assert.Contains(t, local, "hit") assert.Contains(t, local, "read") assert.Contains(t, local, "dirtied") assert.Contains(t, local, "written") assert.Contains(t, memory, "temp") - temp := memory["temp"].(common.MapStr) + temp := memory["temp"].(mapstr.M) assert.Contains(t, temp, "read") assert.Contains(t, temp, "written") } diff --git a/metricbeat/module/prometheus/collector/collector.go b/metricbeat/module/prometheus/collector/collector.go index 74b61b829f0..69a91a7618a 100644 --- a/metricbeat/module/prometheus/collector/collector.go +++ b/metricbeat/module/prometheus/collector/collector.go @@ -23,10 +23,10 @@ import ( "github.com/pkg/errors" dto "github.com/prometheus/client_model/go" - "github.com/elastic/beats/v7/libbeat/common" p "github.com/elastic/beats/v7/metricbeat/helper/prometheus" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -132,7 +132,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } families, err := m.prometheus.GetFamilies() - eventList := map[string]common.MapStr{} + eventList := map[string]mapstr.M{} if err != nil { // send up event only families = append(families, m.upMetricFamily(0.0)) @@ -153,7 +153,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { for _, promEvent := range promEvents { labelsHash := promEvent.LabelsHash() if _, ok := eventList[labelsHash]; !ok { - eventList[labelsHash] = common.MapStr{} + eventList[labelsHash] = mapstr.M{} // Add default instance label if not already there if exists, _ := promEvent.Labels.HasKey(upMetricInstanceLabel); !exists { @@ -177,7 +177,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { // Report events for _, e := range eventList { isOpen := reporter.Event(mb.Event{ - RootFields: common.MapStr{m.namespace: e}, + RootFields: mapstr.M{m.namespace: e}, }) if !isOpen { break diff --git a/metricbeat/module/prometheus/collector/collector_test.go b/metricbeat/module/prometheus/collector/collector_test.go index 6432a686ec3..4ccea353559 100644 --- a/metricbeat/module/prometheus/collector/collector_test.go +++ b/metricbeat/module/prometheus/collector/collector_test.go @@ -24,12 +24,12 @@ import ( "testing" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/golang/protobuf/proto" dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" p "github.com/elastic/beats/v7/metricbeat/helper/prometheus" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" @@ -37,7 +37,7 @@ import ( ) func TestGetPromEventsFromMetricFamily(t *testing.T) { - labels := common.MapStr{ + labels := mapstr.M{ "handler": "query", } tests := []struct { @@ -65,8 +65,8 @@ func TestGetPromEventsFromMetricFamily(t *testing.T) { }, Event: []PromEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds": float64(10), }, }, @@ -89,12 +89,12 @@ func TestGetPromEventsFromMetricFamily(t *testing.T) { }, Event: []PromEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds": float64(10), }, }, - Labels: common.MapStr{}, + Labels: mapstr.M{}, }, }, }, @@ -120,21 +120,21 @@ func TestGetPromEventsFromMetricFamily(t *testing.T) { }, Event: []PromEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds_count": uint64(10), "http_request_duration_microseconds_sum": float64(10), }, }, - Labels: common.MapStr{}, + Labels: mapstr.M{}, }, { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds": float64(10), }, }, - Labels: common.MapStr{ + Labels: mapstr.M{ "quantile": "0.99", }, }, @@ -162,21 +162,21 @@ func TestGetPromEventsFromMetricFamily(t *testing.T) { }, Event: []PromEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds_count": uint64(10), "http_request_duration_microseconds_sum": float64(10), }, }, - Labels: common.MapStr{}, + Labels: mapstr.M{}, }, { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds_bucket": uint64(10), }, }, - Labels: common.MapStr{"le": "0.99"}, + Labels: mapstr.M{"le": "0.99"}, }, }, }, @@ -201,8 +201,8 @@ func TestGetPromEventsFromMetricFamily(t *testing.T) { }, Event: []PromEvent{ { - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ "http_request_duration_microseconds": float64(10), }, }, diff --git a/metricbeat/module/prometheus/collector/data.go b/metricbeat/module/prometheus/collector/data.go index 4bc6fe503af..aed18a21f5f 100644 --- a/metricbeat/module/prometheus/collector/data.go +++ b/metricbeat/module/prometheus/collector/data.go @@ -21,17 +21,17 @@ import ( "math" "strconv" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/labelhash" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" dto "github.com/prometheus/client_model/go" ) // PromEvent stores a set of one or more metrics with the same labels type PromEvent struct { - Data common.MapStr - Labels common.MapStr + Data mapstr.M + Labels mapstr.M } // LabelsHash returns a repeatable string that is unique for the set of labels in this event @@ -57,7 +57,7 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven name := *mf.Name metrics := mf.Metric for _, metric := range metrics { - labels := common.MapStr{} + labels := mapstr.M{} if len(metric.Label) != 0 { for _, label := range metric.Label { @@ -71,8 +71,8 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven if counter != nil { if !math.IsNaN(counter.GetValue()) && !math.IsInf(counter.GetValue(), 0) { events = append(events, PromEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: counter.GetValue(), }, }, @@ -85,8 +85,8 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven if gauge != nil { if !math.IsNaN(gauge.GetValue()) && !math.IsInf(gauge.GetValue(), 0) { events = append(events, PromEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: gauge.GetValue(), }, }, @@ -99,8 +99,8 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven if summary != nil { if !math.IsNaN(summary.GetSampleSum()) && !math.IsInf(summary.GetSampleSum(), 0) { events = append(events, PromEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name + "_sum": summary.GetSampleSum(), name + "_count": summary.GetSampleCount(), }, @@ -117,8 +117,8 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven quantileLabels := labels.Clone() quantileLabels["quantile"] = strconv.FormatFloat(quantile.GetQuantile(), 'f', -1, 64) events = append(events, PromEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: quantile.GetValue(), }, }, @@ -131,8 +131,8 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven if histogram != nil { if !math.IsNaN(histogram.GetSampleSum()) && !math.IsInf(histogram.GetSampleSum(), 0) { events = append(events, PromEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name + "_sum": histogram.GetSampleSum(), name + "_count": histogram.GetSampleCount(), }, @@ -150,8 +150,8 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven bucketLabels["le"] = strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64) events = append(events, PromEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name + "_bucket": bucket.GetCumulativeCount(), }, }, @@ -164,8 +164,8 @@ func (p *promEventGenerator) GeneratePromEvents(mf *dto.MetricFamily) []PromEven if untyped != nil { if !math.IsNaN(untyped.GetValue()) && !math.IsInf(untyped.GetValue(), 0) { events = append(events, PromEvent{ - Data: common.MapStr{ - "metrics": common.MapStr{ + Data: mapstr.M{ + "metrics": mapstr.M{ name: untyped.GetValue(), }, }, diff --git a/metricbeat/module/prometheus/query/config.go b/metricbeat/module/prometheus/query/config.go index afb30f1c80d..1a96dc7e410 100644 --- a/metricbeat/module/prometheus/query/config.go +++ b/metricbeat/module/prometheus/query/config.go @@ -20,7 +20,7 @@ package query import ( "errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Config defines the "query" metricset's configuration @@ -31,9 +31,9 @@ type Config struct { // QueryConfig is used to make an API request. type QueryConfig struct { - Path string `config:"path"` - Params common.MapStr `config:"params"` - Name string `config:"name"` + Path string `config:"path"` + Params mapstr.M `config:"params"` + Name string `config:"name"` } func defaultConfig() Config { diff --git a/metricbeat/module/prometheus/query/data.go b/metricbeat/module/prometheus/query/data.go index e8a625a3012..1a660984007 100644 --- a/metricbeat/module/prometheus/query/data.go +++ b/metricbeat/module/prometheus/query/data.go @@ -26,8 +26,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Response stores the very basic response information to only keep the Status and the ResultType. @@ -147,8 +147,8 @@ func getEventsFromMatrix(body []byte, queryName string) ([]mb.Event, error) { } events = append(events, mb.Event{ Timestamp: getTimestamp(timestamp), - ModuleFields: common.MapStr{"labels": result.Metric}, - MetricSetFields: common.MapStr{ + ModuleFields: mapstr.M{"labels": result.Metric}, + MetricSetFields: mapstr.M{ queryName: val, }, }) @@ -182,8 +182,8 @@ func getEventsFromVector(body []byte, queryName string) ([]mb.Event, error) { } events = append(events, mb.Event{ Timestamp: getTimestamp(timestamp), - ModuleFields: common.MapStr{"labels": result.Metric}, - MetricSetFields: common.MapStr{ + ModuleFields: mapstr.M{"labels": result.Metric}, + MetricSetFields: mapstr.M{ queryName: val, }, }) @@ -214,7 +214,7 @@ func getEventFromScalarOrString(body []byte, resultType string, queryName string } return mb.Event{ Timestamp: getTimestamp(timestamp), - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ queryName: val, }, }, nil @@ -226,12 +226,12 @@ func getEventFromScalarOrString(body []byte, resultType string, queryName string } return mb.Event{ Timestamp: getTimestamp(timestamp), - ModuleFields: common.MapStr{ - "labels": common.MapStr{ + ModuleFields: mapstr.M{ + "labels": mapstr.M{ queryName: value, }, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ queryName: 1, }, }, nil diff --git a/metricbeat/module/prometheus/query/query.go b/metricbeat/module/prometheus/query/query.go index 3f419753a72..72013c66553 100644 --- a/metricbeat/module/prometheus/query/query.go +++ b/metricbeat/module/prometheus/query/query.go @@ -22,10 +22,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -108,7 +108,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { return nil } -func (m *MetricSet) getURL(path string, queryMap common.MapStr) string { +func (m *MetricSet) getURL(path string, queryMap mapstr.M) string { queryStr := mb.QueryParams(queryMap).String() return m.baseURL + path + "?" + queryStr } diff --git a/metricbeat/module/prometheus/query/query_integration_test.go b/metricbeat/module/prometheus/query/query_integration_test.go index d3bdb0f5706..143e2296cd7 100644 --- a/metricbeat/module/prometheus/query/query_integration_test.go +++ b/metricbeat/module/prometheus/query/query_integration_test.go @@ -26,10 +26,10 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" "github.com/elastic/beats/v7/metricbeat/mb" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { @@ -39,11 +39,11 @@ func TestData(t *testing.T) { "module": "prometheus", "metricsets": []string{"query"}, "hosts": []string{service.Host()}, - "queries": []common.MapStr{ - common.MapStr{ + "queries": []mapstr.M{ + mapstr.M{ "name": "go_threads", "path": "/api/v1/query", - "params": common.MapStr{ + "params": mapstr.M{ "query": "go_threads", }, }, @@ -68,11 +68,11 @@ func TestQueryFetch(t *testing.T) { "module": "prometheus", "metricsets": []string{"query"}, "hosts": []string{service.Host()}, - "queries": []common.MapStr{ - common.MapStr{ + "queries": []mapstr.M{ + mapstr.M{ "name": "go_info", "path": "/api/v1/query", - "params": common.MapStr{ + "params": mapstr.M{ "query": "go_info", }, }, diff --git a/metricbeat/module/prometheus/query/query_test.go b/metricbeat/module/prometheus/query/query_test.go index 73a7b885f50..581063d4029 100644 --- a/metricbeat/module/prometheus/query/query_test.go +++ b/metricbeat/module/prometheus/query/query_test.go @@ -24,8 +24,8 @@ import ( "path/filepath" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestQueryFetchEventContentInstantVector(t *testing.T) { @@ -52,11 +52,11 @@ func TestQueryFetchEventContentInstantVector(t *testing.T) { "metricsets": []string{"query"}, "hosts": []string{server.URL}, // queries do not have an actual role here since all http responses are mocked - "queries": []common.MapStr{ - common.MapStr{ + "queries": []mapstr.M{ + mapstr.M{ "name": "up", "path": "/api/v1/query", - "params": common.MapStr{ + "params": mapstr.M{ "query": "up", }, }, @@ -101,11 +101,11 @@ func TestQueryFetchEventContentRangeVector(t *testing.T) { "metricsets": []string{"query"}, "hosts": []string{server.URL}, // queries do not have an actual role here since all http responses are mocked - "queries": []common.MapStr{ - common.MapStr{ + "queries": []mapstr.M{ + mapstr.M{ "name": "up_range", "path": "/api/v1/query", - "params": common.MapStr{ + "params": mapstr.M{ "query": "up", "start": "2019-12-20T23:30:30.000Z", "end": "2019-12-21T23:31:00.000Z", @@ -147,11 +147,11 @@ func TestQueryFetchEventContentScalar(t *testing.T) { "metricsets": []string{"query"}, "hosts": []string{server.URL}, // queries do not have an actual role here since all http responses are mocked - "queries": []common.MapStr{ - common.MapStr{ + "queries": []mapstr.M{ + mapstr.M{ "name": "scalar", "path": "/api/v1/query", - "params": common.MapStr{ + "params": mapstr.M{ "query": "100", }, }, @@ -190,11 +190,11 @@ func TestQueryFetchEventContentString(t *testing.T) { "metricsets": []string{"query"}, "hosts": []string{server.URL}, // queries do not have an actual role here since all http responses are mocked - "queries": []common.MapStr{ - common.MapStr{ + "queries": []mapstr.M{ + mapstr.M{ "name": "string", "path": "/api/v1/query", - "params": common.MapStr{ + "params": mapstr.M{ "query": "some", }, }, diff --git a/metricbeat/module/prometheus/remote_write/data.go b/metricbeat/module/prometheus/remote_write/data.go index 3afaa7e9529..2063c881aa7 100644 --- a/metricbeat/module/prometheus/remote_write/data.go +++ b/metricbeat/module/prometheus/remote_write/data.go @@ -22,8 +22,8 @@ import ( "github.com/prometheus/common/model" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // DefaultRemoteWriteEventsGeneratorFactory returns the default prometheus events generator @@ -40,7 +40,7 @@ func (p *remoteWriteEventGenerator) GenerateEvents(metrics model.Samples) map[st eventList := map[string]mb.Event{} for _, metric := range metrics { - labels := common.MapStr{} + labels := mapstr.M{} if metric == nil { continue @@ -61,8 +61,8 @@ func (p *remoteWriteEventGenerator) GenerateEvents(metrics model.Samples) map[st labelsHash := labels.String() + metric.Timestamp.Time().String() if _, ok := eventList[labelsHash]; !ok { eventList[labelsHash] = mb.Event{ - ModuleFields: common.MapStr{ - "metrics": common.MapStr{}, + ModuleFields: mapstr.M{ + "metrics": mapstr.M{}, }, Timestamp: metric.Timestamp.Time(), } @@ -75,10 +75,10 @@ func (p *remoteWriteEventGenerator) GenerateEvents(metrics model.Samples) map[st // Not checking anything here because we create these maps some lines before e := eventList[labelsHash] - data := common.MapStr{ + data := mapstr.M{ name: val, } - e.ModuleFields["metrics"].(common.MapStr).Update(data) + e.ModuleFields["metrics"].(mapstr.M).Update(data) } return eventList diff --git a/metricbeat/module/prometheus/remote_write/remote_write_test.go b/metricbeat/module/prometheus/remote_write/remote_write_test.go index f0e533f49bc..183ba7e7220 100644 --- a/metricbeat/module/prometheus/remote_write/remote_write_test.go +++ b/metricbeat/module/prometheus/remote_write/remote_write_test.go @@ -23,7 +23,7 @@ import ( "github.com/prometheus/common/model" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TestGenerateEventsCounter tests counter simple cases @@ -32,7 +32,7 @@ func TestGenerateEventsCounter(t *testing.T) { timestamp := model.Time(424242) timestamp1 := model.Time(424243) - labels := common.MapStr{ + labels := mapstr.M{ "listener_name": model.LabelValue("http"), } @@ -57,14 +57,14 @@ func TestGenerateEventsCounter(t *testing.T) { } events := g.GenerateEvents(metrics) - expected := common.MapStr{ - "metrics": common.MapStr{ + expected := mapstr.M{ + "metrics": mapstr.M{ "net_conntrack_listener_conn_closed_total": float64(42), }, "labels": labels, } - expected1 := common.MapStr{ - "metrics": common.MapStr{ + expected1 := mapstr.M{ + "metrics": mapstr.M{ "net_conntrack_listener_conn_closed_total": float64(43), }, "labels": labels, diff --git a/metricbeat/module/rabbitmq/connection/connection_test.go b/metricbeat/module/rabbitmq/connection/connection_test.go index d5acf1abbb6..ed2c2678b0e 100644 --- a/metricbeat/module/rabbitmq/connection/connection_test.go +++ b/metricbeat/module/rabbitmq/connection/connection_test.go @@ -20,9 +20,9 @@ package connection import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/rabbitmq/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" @@ -42,7 +42,7 @@ func TestFetchEventContents(t *testing.T) { t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) ee, _ := e.Fields.GetValue("rabbitmq.connection") - event := ee.(common.MapStr) + event := ee.(mapstr.M) assert.EqualValues(t, "[::1]:60938 -> [::1]:5672", event["name"]) assert.EqualValues(t, 8, event["channels"]) @@ -50,19 +50,19 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, 131072, event["frame_max"]) assert.EqualValues(t, "network", event["type"]) - packetCount := event["packet_count"].(common.MapStr) + packetCount := event["packet_count"].(mapstr.M) assert.EqualValues(t, 376, packetCount["sent"]) assert.EqualValues(t, 376, packetCount["received"]) assert.EqualValues(t, 0, packetCount["pending"]) - octetCount := event["octet_count"].(common.MapStr) + octetCount := event["octet_count"].(mapstr.M) assert.EqualValues(t, 3840, octetCount["sent"]) assert.EqualValues(t, 3764, octetCount["received"]) assert.EqualValues(t, "::1", event["host"]) assert.EqualValues(t, 5672, event["port"]) - peer := event["peer"].(common.MapStr) + peer := event["peer"].(mapstr.M) assert.EqualValues(t, "::1", peer["host"]) assert.EqualValues(t, 60938, peer["port"]) } diff --git a/metricbeat/module/rabbitmq/connection/data.go b/metricbeat/module/rabbitmq/connection/data.go index 4179fe80407..3d92544431c 100644 --- a/metricbeat/module/rabbitmq/connection/data.go +++ b/metricbeat/module/rabbitmq/connection/data.go @@ -22,10 +22,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -77,13 +77,13 @@ func eventsMapping(content []byte, r mb.ReporterV2) error { func eventMapping(connection map[string]interface{}) mb.Event { fields, _ := schema.Apply(connection, s.FailOnRequired) - rootFields := common.MapStr{} + rootFields := mapstr.M{} if v, err := fields.GetValue("user"); err == nil { rootFields.Put("user.name", v) fields.Delete("user") } - moduleFields := common.MapStr{} + moduleFields := mapstr.M{} if v, err := fields.GetValue("vhost"); err == nil { moduleFields.Put("vhost", v) fields.Delete("vhost") diff --git a/metricbeat/module/rabbitmq/exchange/data.go b/metricbeat/module/rabbitmq/exchange/data.go index 3e93508f1ac..e181c76a3a2 100644 --- a/metricbeat/module/rabbitmq/exchange/data.go +++ b/metricbeat/module/rabbitmq/exchange/data.go @@ -22,10 +22,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -72,13 +72,13 @@ func eventsMapping(content []byte, r mb.ReporterV2) error { func eventMapping(exchange map[string]interface{}) mb.Event { fields, _ := schema.Apply(exchange) - rootFields := common.MapStr{} + rootFields := mapstr.M{} if v, err := fields.GetValue("user"); err == nil { rootFields.Put("user.name", v) fields.Delete("user") } - moduleFields := common.MapStr{} + moduleFields := mapstr.M{} if v, err := fields.GetValue("vhost"); err == nil { moduleFields.Put("vhost", v) fields.Delete("vhost") diff --git a/metricbeat/module/rabbitmq/exchange/exchange_test.go b/metricbeat/module/rabbitmq/exchange/exchange_test.go index 6bb9136a634..6a5718499f0 100644 --- a/metricbeat/module/rabbitmq/exchange/exchange_test.go +++ b/metricbeat/module/rabbitmq/exchange/exchange_test.go @@ -20,9 +20,9 @@ package exchange import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/rabbitmq/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -41,16 +41,16 @@ func TestFetchEventContents(t *testing.T) { t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) ee, _ := e.Fields.GetValue("rabbitmq.exchange") - event := ee.(common.MapStr) + event := ee.(mapstr.M) - messagesExpected := common.MapStr{ - "publish_in": common.MapStr{ + messagesExpected := mapstr.M{ + "publish_in": mapstr.M{ "count": int64(100), - "details": common.MapStr{"rate": float64(0.5)}, + "details": mapstr.M{"rate": float64(0.5)}, }, - "publish_out": common.MapStr{ + "publish_out": mapstr.M{ "count": int64(99), - "details": common.MapStr{"rate": float64(0.9)}, + "details": mapstr.M{"rate": float64(0.9)}, }, } @@ -66,7 +66,7 @@ func TestData(t *testing.T) { defer server.Close() ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(server.URL)) - err := mbtest.WriteEventsReporterV2ErrorCond(ms, t, "", func(e common.MapStr) bool { + err := mbtest.WriteEventsReporterV2ErrorCond(ms, t, "", func(e mapstr.M) bool { hasIn, _ := e.HasKey("rabbitmq.exchange.messages.publish_in") hasOut, _ := e.HasKey("rabbitmq.exchange.messages.publish_out") return hasIn && hasOut diff --git a/metricbeat/module/rabbitmq/node/node_test.go b/metricbeat/module/rabbitmq/node/node_test.go index 9f11c05de84..f84c9fead28 100644 --- a/metricbeat/module/rabbitmq/node/node_test.go +++ b/metricbeat/module/rabbitmq/node/node_test.go @@ -20,9 +20,9 @@ package node import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/rabbitmq/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -58,96 +58,96 @@ func testFetch(t *testing.T, collect string) { t.Logf("%s/%s event: %+v", ms.Module().Name(), ms.Name(), event.StringToPrint()) - disk := event["disk"].(common.MapStr) - free := disk["free"].(common.MapStr) + disk := event["disk"].(mapstr.M) + free := disk["free"].(mapstr.M) assert.EqualValues(t, int64(98317942784), free["bytes"]) - limit := free["limit"].(common.MapStr) + limit := free["limit"].(mapstr.M) assert.EqualValues(t, 50000000, limit["bytes"]) - fd := event["fd"].(common.MapStr) + fd := event["fd"].(mapstr.M) assert.EqualValues(t, 1048576, fd["total"]) assert.EqualValues(t, 31, fd["used"]) - gc := event["gc"].(common.MapStr) - num := gc["num"].(common.MapStr) + gc := event["gc"].(mapstr.M) + num := gc["num"].(mapstr.M) assert.EqualValues(t, 1049055, num["count"]) - reclaimed := gc["reclaimed"].(common.MapStr) + reclaimed := gc["reclaimed"].(mapstr.M) assert.EqualValues(t, int64(27352751800), reclaimed["bytes"]) - io := event["io"].(common.MapStr) - fileHandle := io["file_handle"].(common.MapStr) - openAttempt := fileHandle["open_attempt"].(common.MapStr) - avg := openAttempt["avg"].(common.MapStr) + io := event["io"].(mapstr.M) + fileHandle := io["file_handle"].(mapstr.M) + openAttempt := fileHandle["open_attempt"].(mapstr.M) + avg := openAttempt["avg"].(mapstr.M) assert.EqualValues(t, 0, avg["ms"]) assert.EqualValues(t, 597670, openAttempt["count"]) - read := io["read"].(common.MapStr) - avg = read["avg"].(common.MapStr) + read := io["read"].(mapstr.M) + avg = read["avg"].(mapstr.M) assert.EqualValues(t, 0, avg["ms"]) assert.EqualValues(t, 1, read["bytes"]) assert.EqualValues(t, 3, read["count"]) - reopen := io["reopen"].(common.MapStr) + reopen := io["reopen"].(mapstr.M) assert.EqualValues(t, 3, reopen["count"]) - seek := io["seek"].(common.MapStr) - avg = seek["avg"].(common.MapStr) + seek := io["seek"].(mapstr.M) + avg = seek["avg"].(mapstr.M) assert.EqualValues(t, 0, avg["ms"]) assert.EqualValues(t, 23, seek["count"]) - sync := io["sync"].(common.MapStr) - avg = sync["avg"].(common.MapStr) + sync := io["sync"].(mapstr.M) + avg = sync["avg"].(mapstr.M) assert.EqualValues(t, 2, avg["ms"]) assert.EqualValues(t, 149402, sync["count"]) - write := io["write"].(common.MapStr) - avg = write["avg"].(common.MapStr) + write := io["write"].(mapstr.M) + avg = write["avg"].(mapstr.M) assert.EqualValues(t, 0, avg["ms"]) assert.EqualValues(t, 36305460, write["bytes"]) assert.EqualValues(t, 149402, write["count"]) - mem := event["mem"].(common.MapStr) - limit = mem["limit"].(common.MapStr) + mem := event["mem"].(mapstr.M) + limit = mem["limit"].(mapstr.M) assert.EqualValues(t, int64(6628692787), limit["bytes"]) - used := mem["used"].(common.MapStr) + used := mem["used"].(mapstr.M) assert.EqualValues(t, 105504768, used["bytes"]) - mnesia := event["mnesia"].(common.MapStr) - disk = mnesia["disk"].(common.MapStr) - tx := disk["tx"].(common.MapStr) + mnesia := event["mnesia"].(mapstr.M) + disk = mnesia["disk"].(mapstr.M) + tx := disk["tx"].(mapstr.M) assert.EqualValues(t, 1, tx["count"]) - ram := mnesia["ram"].(common.MapStr) - tx = ram["tx"].(common.MapStr) + ram := mnesia["ram"].(mapstr.M) + tx = ram["tx"].(mapstr.M) assert.EqualValues(t, 92, tx["count"]) - msg := event["msg"].(common.MapStr) - storeRead := msg["store_read"].(common.MapStr) + msg := event["msg"].(mapstr.M) + storeRead := msg["store_read"].(mapstr.M) assert.EqualValues(t, 0, storeRead["count"]) - storeWrite := msg["store_write"].(common.MapStr) + storeWrite := msg["store_write"].(mapstr.M) assert.EqualValues(t, 0, storeWrite["count"]) assert.EqualValues(t, "rabbit@e2b1ae6390fd", event["name"]) - proc := event["proc"].(common.MapStr) + proc := event["proc"].(mapstr.M) assert.EqualValues(t, 1048576, proc["total"]) assert.EqualValues(t, 403, proc["used"]) assert.EqualValues(t, 4, event["processors"]) - queue := event["queue"].(common.MapStr) - index := queue["index"].(common.MapStr) - journalWrite := index["journal_write"].(common.MapStr) + queue := event["queue"].(mapstr.M) + index := queue["index"].(mapstr.M) + journalWrite := index["journal_write"].(mapstr.M) assert.EqualValues(t, 448230, journalWrite["count"]) - read = index["read"].(common.MapStr) + read = index["read"].(mapstr.M) assert.EqualValues(t, 0, read["count"]) - write = index["write"].(common.MapStr) + write = index["write"].(mapstr.M) assert.EqualValues(t, 2, write["count"]) - run := event["run"].(common.MapStr) + run := event["run"].(mapstr.M) assert.EqualValues(t, 0, run["queue"]) - socket := event["socket"].(common.MapStr) + socket := event["socket"].(mapstr.M) assert.EqualValues(t, 943626, socket["total"]) assert.EqualValues(t, 3, socket["used"]) diff --git a/metricbeat/module/rabbitmq/queue/data.go b/metricbeat/module/rabbitmq/queue/data.go index 896bf119381..70529b5f219 100644 --- a/metricbeat/module/rabbitmq/queue/data.go +++ b/metricbeat/module/rabbitmq/queue/data.go @@ -23,8 +23,8 @@ import ( "github.com/pkg/errors" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" ) @@ -102,7 +102,7 @@ func eventsMapping(content []byte, r mb.ReporterV2) error { func eventMapping(queue map[string]interface{}) mb.Event { fields, _ := schema.Apply(queue) - moduleFields := common.MapStr{} + moduleFields := mapstr.M{} if v, err := fields.GetValue("vhost"); err == nil { moduleFields.Put("vhost", v) fields.Delete("vhost") diff --git a/metricbeat/module/rabbitmq/queue/queue_test.go b/metricbeat/module/rabbitmq/queue/queue_test.go index 5c9438c2a64..4e0d08aba6f 100644 --- a/metricbeat/module/rabbitmq/queue/queue_test.go +++ b/metricbeat/module/rabbitmq/queue/queue_test.go @@ -20,9 +20,9 @@ package queue import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/metricbeat/module/rabbitmq/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -41,7 +41,7 @@ func TestFetchEventContents(t *testing.T) { t.Logf("%s/%s event: %+v", metricSet.Module().Name(), metricSet.Name(), e.Fields.StringToPrint()) ee, _ := e.Fields.GetValue("rabbitmq.queue") - event := ee.(common.MapStr) + event := ee.(mapstr.M) assert.EqualValues(t, "queuenamehere", event["name"]) assert.EqualValues(t, true, event["durable"]) @@ -49,39 +49,39 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, false, event["exclusive"]) assert.EqualValues(t, "running", event["state"]) - arguments := event["arguments"].(common.MapStr) + arguments := event["arguments"].(mapstr.M) assert.EqualValues(t, 9, arguments["max_priority"]) - consumers := event["consumers"].(common.MapStr) - utilisation := consumers["utilisation"].(common.MapStr) + consumers := event["consumers"].(mapstr.M) + utilisation := consumers["utilisation"].(mapstr.M) assert.EqualValues(t, 3, consumers["count"]) assert.EqualValues(t, 0.7, utilisation["pct"]) - memory := event["memory"].(common.MapStr) + memory := event["memory"].(mapstr.M) assert.EqualValues(t, 232720, memory["bytes"]) - messages := event["messages"].(common.MapStr) - total := messages["total"].(common.MapStr) - ready := messages["ready"].(common.MapStr) - unacknowledged := messages["unacknowledged"].(common.MapStr) - persistent := messages["persistent"].(common.MapStr) + messages := event["messages"].(mapstr.M) + total := messages["total"].(mapstr.M) + ready := messages["ready"].(mapstr.M) + unacknowledged := messages["unacknowledged"].(mapstr.M) + persistent := messages["persistent"].(mapstr.M) assert.EqualValues(t, 74, total["count"]) assert.EqualValues(t, 71, ready["count"]) assert.EqualValues(t, 3, unacknowledged["count"]) assert.EqualValues(t, 73, persistent["count"]) - totalDetails := total["details"].(common.MapStr) + totalDetails := total["details"].(mapstr.M) assert.EqualValues(t, 2.2, totalDetails["rate"]) - readyDetails := ready["details"].(common.MapStr) + readyDetails := ready["details"].(mapstr.M) assert.EqualValues(t, 0, readyDetails["rate"]) - unacknowledgedDetails := unacknowledged["details"].(common.MapStr) + unacknowledgedDetails := unacknowledged["details"].(mapstr.M) assert.EqualValues(t, 0.5, unacknowledgedDetails["rate"]) - disk := event["disk"].(common.MapStr) - reads := disk["reads"].(common.MapStr) - writes := disk["writes"].(common.MapStr) + disk := event["disk"].(mapstr.M) + reads := disk["reads"].(mapstr.M) + writes := disk["writes"].(mapstr.M) assert.EqualValues(t, 212, reads["count"]) assert.EqualValues(t, 121, writes["count"]) } @@ -91,7 +91,7 @@ func TestData(t *testing.T) { defer server.Close() ms := mbtest.NewReportingMetricSetV2Error(t, getConfig(server.URL)) - err := mbtest.WriteEventsReporterV2ErrorCond(ms, t, "", func(e common.MapStr) bool { + err := mbtest.WriteEventsReporterV2ErrorCond(ms, t, "", func(e mapstr.M) bool { hasTotal, _ := e.HasKey("rabbitmq.queue.messages.total") return hasTotal }) diff --git a/metricbeat/module/redis/info/data.go b/metricbeat/module/redis/info/data.go index 48c2a0c95f8..a4f79e33e72 100644 --- a/metricbeat/module/redis/info/data.go +++ b/metricbeat/module/redis/info/data.go @@ -20,10 +20,10 @@ package info import ( "strings" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -265,7 +265,7 @@ func eventMapping(r mb.ReporterV2, info map[string]string) { commandstatsData, _ := commandstatsSchema.Apply(source) data["commandstats"] = commandstatsData - rootFields := common.MapStr{} + rootFields := mapstr.M{} if v, err := data.GetValue("server.version"); err == nil { rootFields.Put("service.version", v) data.Delete("server.version") diff --git a/metricbeat/module/redis/info/info_integration_test.go b/metricbeat/module/redis/info/info_integration_test.go index 99a6bf4d3f0..5eba0a42f5b 100644 --- a/metricbeat/module/redis/info/info_integration_test.go +++ b/metricbeat/module/redis/info/info_integration_test.go @@ -23,9 +23,9 @@ package info import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" ) @@ -47,7 +47,7 @@ func TestFetch(t *testing.T) { // Check fields assert.Equal(t, 10, len(event)) - server := event["server"].(common.MapStr) + server := event["server"].(mapstr.M) assert.Equal(t, "standalone", server["mode"]) } diff --git a/metricbeat/module/redis/key/data.go b/metricbeat/module/redis/key/data.go index d6fd8134d87..30707687be1 100644 --- a/metricbeat/module/redis/key/data.go +++ b/metricbeat/module/redis/key/data.go @@ -20,16 +20,16 @@ package key import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func eventMapping(keyspace uint, info map[string]interface{}) mb.Event { info["id"] = fmt.Sprintf("%d:%s", keyspace, info["name"]) return mb.Event{ MetricSetFields: info, - ModuleFields: common.MapStr{ - "keyspace": common.MapStr{ + ModuleFields: mapstr.M{ + "keyspace": mapstr.M{ "id": fmt.Sprintf("db%d", keyspace), }, }, diff --git a/metricbeat/module/redis/key/key_integration_test.go b/metricbeat/module/redis/key/key_integration_test.go index 850b19510be..797a00f09c2 100644 --- a/metricbeat/module/redis/key/key_integration_test.go +++ b/metricbeat/module/redis/key/key_integration_test.go @@ -27,9 +27,9 @@ import ( rd "github.com/gomodule/redigo/redis" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -100,7 +100,7 @@ func TestFetchMultipleKeyspaces(t *testing.T) { } id := event.MetricSetFields["id"].(string) assert.Equal(t, fmt.Sprintf("%d:%s", expectedKeyspace, name), id) - keyspace := event.ModuleFields["keyspace"].(common.MapStr) + keyspace := event.ModuleFields["keyspace"].(mapstr.M) keyspaceID := keyspace["id"].(string) assert.Equal(t, fmt.Sprintf("db%d", expectedKeyspace), keyspaceID) } diff --git a/metricbeat/module/redis/keyspace/data.go b/metricbeat/module/redis/keyspace/data.go index 91f181305a9..0f7be47e1f6 100644 --- a/metricbeat/module/redis/keyspace/data.go +++ b/metricbeat/module/redis/keyspace/data.go @@ -20,11 +20,11 @@ package keyspace import ( "strings" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/redis" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Map data to MapStr @@ -38,7 +38,7 @@ func eventsMapping(r mb.ReporterV2, info map[string]string) { } } -func getKeyspaceStats(info map[string]string) map[string]common.MapStr { +func getKeyspaceStats(info map[string]string) map[string]mapstr.M { keyspaceMap := findKeyspaceStats(info) return parseKeyspaceStats(keyspaceMap) } @@ -62,8 +62,8 @@ var schema = s.Schema{ } // parseKeyspaceStats resolves the overloaded value string that Redis returns for keyspace -func parseKeyspaceStats(keyspaceMap map[string]string) map[string]common.MapStr { - keyspace := map[string]common.MapStr{} +func parseKeyspaceStats(keyspaceMap map[string]string) map[string]mapstr.M { + keyspace := map[string]mapstr.M{} for k, v := range keyspaceMap { // Extract out the overloaded values for db keyspace diff --git a/metricbeat/module/system/cpu/cpu.go b/metricbeat/module/system/cpu/cpu.go index 704cc73b16c..ed6028cd69d 100644 --- a/metricbeat/module/system/cpu/cpu.go +++ b/metricbeat/module/system/cpu/cpu.go @@ -23,11 +23,11 @@ package cpu import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" metrics "github.com/elastic/beats/v7/metricbeat/internal/metrics/cpu" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -85,7 +85,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { if err != nil { return errors.Wrap(err, "error creating host fields") } - hostFields := common.MapStr{} + hostFields := mapstr.M{} err = copyFieldsOrDefault(hostEvent, hostFields, "total.norm.pct", "host.cpu.usage", 0) if err != nil { return errors.Wrap(err, "error fetching normalized CPU percent") @@ -102,9 +102,9 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { // copyFieldsOrDefault copies the field specified by key to the given map. It will // overwrite the key if it exists. It will update the map with a default value if // the key does not exist in the source map. -func copyFieldsOrDefault(from, to common.MapStr, key, newkey string, value interface{}) error { +func copyFieldsOrDefault(from, to mapstr.M, key, newkey string, value interface{}) error { v, err := from.GetValue(key) - if errors.Is(err, common.ErrKeyNotFound) { + if errors.Is(err, mapstr.ErrKeyNotFound) { _, err = to.Put(newkey, value) return err } diff --git a/metricbeat/module/system/diskio/diskio.go b/metricbeat/module/system/diskio/diskio.go index 20ec9e3f213..b9296f399a9 100644 --- a/metricbeat/module/system/diskio/diskio.go +++ b/metricbeat/module/system/diskio/diskio.go @@ -23,10 +23,10 @@ package diskio import ( "runtime" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/diskio" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" ) @@ -84,14 +84,14 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { var diskReadBytes, diskWriteBytes uint64 for _, counters := range stats { - event := common.MapStr{ + event := mapstr.M{ "name": counters.Name, - "read": common.MapStr{ + "read": mapstr.M{ "count": counters.ReadCount, "time": counters.ReadTime, "bytes": counters.ReadBytes, }, - "write": common.MapStr{ + "write": mapstr.M{ "count": counters.WriteCount, "time": counters.WriteTime, "bytes": counters.WriteBytes, @@ -126,9 +126,9 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { if m.prevCounters != (diskCounter{}) { // convert network metrics from counters to gauges r.Event(mb.Event{ - RootFields: common.MapStr{ - "host": common.MapStr{ - "disk": common.MapStr{ + RootFields: mapstr.M{ + "host": mapstr.M{ + "disk": mapstr.M{ "read.bytes": diskReadBytes - m.prevCounters.prevDiskReadBytes, "write.bytes": diskWriteBytes - m.prevCounters.prevDiskWriteBytes, }, diff --git a/metricbeat/module/system/entropy/entropy.go b/metricbeat/module/system/entropy/entropy.go index e3ced9ea307..207c65dfcee 100644 --- a/metricbeat/module/system/entropy/entropy.go +++ b/metricbeat/module/system/entropy/entropy.go @@ -27,10 +27,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -76,7 +76,7 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) error { return errors.Wrap(err, "error getting poolsize") } report.Event(mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "available_bits": entropy, "pct": float64(entropy) / float64(poolsize), }, diff --git a/metricbeat/module/system/filesystem/helper.go b/metricbeat/module/system/filesystem/helper.go index 32a2bd98cb1..da71738bd7f 100644 --- a/metricbeat/module/system/filesystem/helper.go +++ b/metricbeat/module/system/filesystem/helper.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" + "github.com/elastic/elastic-agent-libs/mapstr" sigar "github.com/elastic/gosigar" ) @@ -132,8 +133,8 @@ func AddFileSystemUsedPercentage(f *FSStat) { } // GetFilesystemEvent turns a stat struct into a MapStr -func GetFilesystemEvent(fsStat *FSStat, addStats bool) common.MapStr { - evt := common.MapStr{ +func GetFilesystemEvent(fsStat *FSStat, addStats bool) mapstr.M { + evt := mapstr.M{ "type": fsStat.SysTypeName, "device_name": fsStat.DevName, "mount_point": fsStat.Mount, @@ -142,7 +143,7 @@ func GetFilesystemEvent(fsStat *FSStat, addStats bool) common.MapStr { evt.Put("total", fsStat.Total) evt.Put("available", fsStat.Avail) evt.Put("free", fsStat.Free) - evt.Put("used", common.MapStr{ + evt.Put("used", mapstr.M{ "pct": fsStat.UsedPercent, "bytes": fsStat.Used, }) diff --git a/metricbeat/module/system/fsstat/fsstat.go b/metricbeat/module/system/fsstat/fsstat.go index ff404cdccee..03078d8d2af 100644 --- a/metricbeat/module/system/fsstat/fsstat.go +++ b/metricbeat/module/system/fsstat/fsstat.go @@ -24,11 +24,11 @@ import ( "runtime" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/system/filesystem" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" ) @@ -94,8 +94,8 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { totalSizeUsed += stat.Used } - event := common.MapStr{ - "total_size": common.MapStr{ + event := mapstr.M{ + "total_size": mapstr.M{ "free": totalSizeFree, "used": totalSizeUsed, "total": totalSize, diff --git a/metricbeat/module/system/load/load.go b/metricbeat/module/system/load/load.go index a671f41636a..c1d60ca161a 100644 --- a/metricbeat/module/system/load/load.go +++ b/metricbeat/module/system/load/load.go @@ -23,11 +23,11 @@ package load import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/cpu" "github.com/elastic/beats/v7/libbeat/metric/system/numcpu" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -59,12 +59,12 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { avgs := load.Averages() normAvgs := load.NormalizedAverages() - event := common.MapStr{ + event := mapstr.M{ "cores": numcpu.NumCPU(), "1": avgs.OneMinute, "5": avgs.FiveMinute, "15": avgs.FifteenMinute, - "norm": common.MapStr{ + "norm": mapstr.M{ "1": normAvgs.OneMinute, "5": normAvgs.FiveMinute, "15": normAvgs.FifteenMinute, diff --git a/metricbeat/module/system/memory/memory.go b/metricbeat/module/system/memory/memory.go index 4efb08be981..e3504b59be6 100644 --- a/metricbeat/module/system/memory/memory.go +++ b/metricbeat/module/system/memory/memory.go @@ -23,12 +23,12 @@ package memory import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transform/typeconv" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" metrics "github.com/elastic/beats/v7/metricbeat/internal/metrics/memory" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -58,7 +58,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { return errors.Wrap(err, "error fetching memory metrics") } - memory := common.MapStr{} + memory := mapstr.M{} err = typeconv.Convert(&memory, &eventRaw) if err != nil { return err diff --git a/metricbeat/module/system/network/network.go b/metricbeat/module/system/network/network.go index da3448f5b07..c3fa9becf80 100644 --- a/metricbeat/module/system/network/network.go +++ b/metricbeat/module/system/network/network.go @@ -23,10 +23,10 @@ package network import ( "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" "github.com/shirou/gopsutil/v3/net" @@ -119,14 +119,14 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { if m.prevCounters != (networkCounter{}) { // convert network metrics from counters to gauges r.Event(mb.Event{ - RootFields: common.MapStr{ - "host": common.MapStr{ - "network": common.MapStr{ - "ingress": common.MapStr{ + RootFields: mapstr.M{ + "host": mapstr.M{ + "network": mapstr.M{ + "ingress": mapstr.M{ "bytes": networkInBytes - m.prevCounters.prevNetworkInBytes, "packets": networkInPackets - m.prevCounters.prevNetworkInPackets, }, - "egress": common.MapStr{ + "egress": mapstr.M{ "bytes": networkOutBytes - m.prevCounters.prevNetworkOutBytes, "packets": networkOutPackets - m.prevCounters.prevNetworkOutPackets, }, @@ -145,16 +145,16 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { return nil } -func ioCountersToMapStr(counters net.IOCountersStat) common.MapStr { - return common.MapStr{ +func ioCountersToMapStr(counters net.IOCountersStat) mapstr.M { + return mapstr.M{ "name": counters.Name, - "in": common.MapStr{ + "in": mapstr.M{ "errors": counters.Errin, "dropped": counters.Dropin, "bytes": counters.BytesRecv, "packets": counters.PacketsRecv, }, - "out": common.MapStr{ + "out": mapstr.M{ "errors": counters.Errout, "dropped": counters.Dropout, "packets": counters.PacketsSent, diff --git a/metricbeat/module/system/network_summary/network_summary_test.go b/metricbeat/module/system/network_summary/network_summary_test.go index cb0d44ce344..2e50589dc29 100644 --- a/metricbeat/module/system/network_summary/network_summary_test.go +++ b/metricbeat/module/system/network_summary/network_summary_test.go @@ -22,10 +22,10 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/network" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/system" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo/types" ) @@ -45,7 +45,7 @@ func TestMapping(t *testing.T) { }, } - exampleOut := common.MapStr{ + exampleOut := mapstr.M{ "icmp": map[string]interface{}{"InAddrMaskReps": uint64(5), "InType3": uint64(835)}, "ip": map[string]interface{}{"DefaultTTL": uint64(64), "InBcastOctets": uint64(431773621), "InBcastPkts": uint64(1686995), "InCEPkts": uint64(0)}, "tcp": map[string]interface{}{"DelayedACKLocked": uint64(111), "DelayedACKLost": uint64(1587), "DelayedACKs": uint64(516004), "MaxConn": int64(-1)}, diff --git a/metricbeat/module/system/process_summary/process_summary.go b/metricbeat/module/system/process_summary/process_summary.go index e21f7da7c3a..3d056ca31c4 100644 --- a/metricbeat/module/system/process_summary/process_summary.go +++ b/metricbeat/module/system/process_summary/process_summary.go @@ -23,12 +23,12 @@ package process_summary import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transform/typeconv" "github.com/elastic/beats/v7/libbeat/metric/system/process" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry. @@ -79,7 +79,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } } - outMap := common.MapStr{} + outMap := mapstr.M{} typeconv.Convert(&outMap, procStates) outMap["total"] = len(procList) r.Event(mb.Event{ diff --git a/metricbeat/module/system/process_summary/process_summary_test.go b/metricbeat/module/system/process_summary/process_summary_test.go index 46e71b41928..7fbec2d4b8a 100644 --- a/metricbeat/module/system/process_summary/process_summary_test.go +++ b/metricbeat/module/system/process_summary/process_summary_test.go @@ -26,11 +26,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/metric/system/process" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/system" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { @@ -69,7 +69,7 @@ func TestStateNames(t *testing.T) { summary, err := event.GetValue("system.process.summary") require.NoError(t, err) - event, ok := summary.(common.MapStr) + event, ok := summary.(mapstr.M) require.True(t, ok) // if there's nothing marked as sleeping or idle, something weird is happening diff --git a/metricbeat/module/system/raid/blockinfo/blockinfo.go b/metricbeat/module/system/raid/blockinfo/blockinfo.go index f0dbd982238..8794109bbfd 100644 --- a/metricbeat/module/system/raid/blockinfo/blockinfo.go +++ b/metricbeat/module/system/raid/blockinfo/blockinfo.go @@ -17,7 +17,7 @@ package blockinfo -import "github.com/elastic/beats/v7/libbeat/common" +import "github.com/elastic/elastic-agent-libs/mapstr" // SyncStatus represents the status of a sync action as Complete/Total. Will be 0/0 if no sync action is going on type SyncStatus struct { @@ -42,5 +42,5 @@ type DiskStates struct { Total int Failed int Spare int - States common.MapStr + States mapstr.M } diff --git a/metricbeat/module/system/raid/blockinfo/parser.go b/metricbeat/module/system/raid/blockinfo/parser.go index 7d0eb7561db..211dad92656 100644 --- a/metricbeat/module/system/raid/blockinfo/parser.go +++ b/metricbeat/module/system/raid/blockinfo/parser.go @@ -26,8 +26,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var debugf = logp.MakeDebug("system.raid") @@ -155,7 +155,7 @@ func getDisks(path string) (DiskStates, error) { } var disks DiskStates - disks.States = common.MapStr{} + disks.States = mapstr.M{} //This is meant to provide a 'common status' for disks in the array //see https://www.kernel.org/doc/html/v4.15/admin-guide/md.html#md-devices-in-sysfs for _, disk := range devices { diff --git a/metricbeat/module/system/raid/raid.go b/metricbeat/module/system/raid/raid.go index 4f03d2e9db6..4bdad8d3af4 100644 --- a/metricbeat/module/system/raid/raid.go +++ b/metricbeat/module/system/raid/raid.go @@ -20,11 +20,11 @@ package raid import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/system/raid/blockinfo" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -67,11 +67,11 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { for _, blockDev := range devices { - event := common.MapStr{ + event := mapstr.M{ "name": blockDev.Name, "status": blockDev.ArrayState, "level": blockDev.Level, - "disks": common.MapStr{ + "disks": mapstr.M{ "active": blockDev.DiskStates.Active, "total": blockDev.DiskStates.Total, "spare": blockDev.DiskStates.Spare, @@ -81,12 +81,12 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } //emulate the behavior of the previous mdstat parser by using the size when no sync data is available if blockDev.SyncStatus.Total == 0 { - event["blocks"] = common.MapStr{ + event["blocks"] = mapstr.M{ "synced": blockto1024(blockDev.Size), "total": blockto1024(blockDev.Size), } } else { - event["blocks"] = common.MapStr{ + event["blocks"] = mapstr.M{ "synced": blockto1024(blockDev.SyncStatus.Complete), "total": blockto1024(blockDev.SyncStatus.Total), } diff --git a/metricbeat/module/system/service/data.go b/metricbeat/module/system/service/data.go index bbb753b1271..24e7866df86 100644 --- a/metricbeat/module/system/service/data.go +++ b/metricbeat/module/system/service/data.go @@ -26,8 +26,8 @@ import ( "github.com/coreos/go-systemd/v22/dbus" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Properties is a struct representation of the dbus returns from GetAllProperties @@ -68,14 +68,14 @@ func formProperties(unit dbus.UnitStatus, props Properties) (mb.Event, error) { } event := mb.Event{ - RootFields: common.MapStr{}, + RootFields: mapstr.M{}, } - msData := common.MapStr{ + msData := mapstr.M{ "name": unit.Name, "load_state": unit.LoadState, "state": unit.ActiveState, "sub_state": unit.SubState, - "unit_file": common.MapStr{ + "unit_file": mapstr.M{ "state": props.UnitFileState, "vendor_preset": props.UnitFilePreset, }, @@ -90,7 +90,7 @@ func formProperties(unit dbus.UnitStatus, props Properties) (mb.Event, error) { msData["resources"] = getMetricsFromServivce(props) } - var childProc = common.MapStr{} + var childProc = mapstr.M{} childData := false //anything less than 1 isn't a valid SIGCHLD code if props.ExecMainCode > 0 { @@ -114,12 +114,12 @@ func formProperties(unit dbus.UnitStatus, props Properties) (mb.Event, error) { event.RootFields["process"] = childProc } if props.IPAccounting { - event.RootFields["network"] = common.MapStr{ + event.RootFields["network"] = mapstr.M{ "packets": props.IPIngressPackets + props.IPEgressPackets, "bytes": props.IPIngressBytes + props.IPEgressBytes, } } - event.RootFields["systemd"] = common.MapStr{ + event.RootFields["systemd"] = mapstr.M{ "unit": unit.Name, "fragment_path": props.FragmentPath, } @@ -130,38 +130,38 @@ func formProperties(unit dbus.UnitStatus, props Properties) (mb.Event, error) { } // getMetricsFromServivce checks what accounting we have enabled and uses that to determine what metrics we can send back to the user -func getMetricsFromServivce(props Properties) common.MapStr { - metrics := common.MapStr{} +func getMetricsFromServivce(props Properties) mapstr.M { + metrics := mapstr.M{} if props.CPUAccounting { - metrics["cpu"] = common.MapStr{ - "usage": common.MapStr{ + metrics["cpu"] = mapstr.M{ + "usage": mapstr.M{ "nsec": props.CPUUsageNSec, }, } } if props.MemoryAccounting { - metrics["memory"] = common.MapStr{ - "usage": common.MapStr{ + metrics["memory"] = mapstr.M{ + "usage": mapstr.M{ "bytes": props.MemoryCurrent, }, } } if props.TasksAccounting { - metrics["tasks"] = common.MapStr{ + metrics["tasks"] = mapstr.M{ "count": props.TasksCurrent, } } if props.IPAccounting { - metrics["network"] = common.MapStr{ - "in": common.MapStr{ + metrics["network"] = mapstr.M{ + "in": mapstr.M{ "packets": props.IPIngressPackets, "bytes": props.IPIngressBytes, }, - "out": common.MapStr{ + "out": mapstr.M{ "packets": props.IPEgressPackets, "bytes": props.IPEgressBytes, }, diff --git a/metricbeat/module/system/service/service_test.go b/metricbeat/module/system/service/service_test.go index 3aeba519bc9..3ee8d990eba 100644 --- a/metricbeat/module/system/service/service_test.go +++ b/metricbeat/module/system/service/service_test.go @@ -27,8 +27,8 @@ import ( "github.com/coreos/go-systemd/v22/dbus" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" _ "github.com/elastic/beats/v7/metricbeat/module/system" + "github.com/elastic/elastic-agent-libs/mapstr" ) var exampleUnits = []dbus.UnitStatus{ @@ -66,18 +66,18 @@ func TestFormProps(t *testing.T) { event, err := formProperties(testUnit, testprops) assert.NoError(t, err) - testEvent := common.MapStr{ + testEvent := mapstr.M{ "state": "active", "exec_code": "exited", "load_state": "loaded", "name": "test.service", "state_since": time.Unix(0, 1571850129000000*1000), "sub_state": "running", - "resources": common.MapStr{"network": common.MapStr{ - "in": common.MapStr{ + "resources": mapstr.M{"network": mapstr.M{ + "in": mapstr.M{ "bytes": 50, "packets": 50}, - "out": common.MapStr{ + "out": mapstr.M{ "bytes": 100, "packets": 100}, }, diff --git a/metricbeat/module/system/socket/socket.go b/metricbeat/module/system/socket/socket.go index 560e4aa7803..a3f837f813d 100644 --- a/metricbeat/module/system/socket/socket.go +++ b/metricbeat/module/system/socket/socket.go @@ -30,12 +30,12 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" sock "github.com/elastic/beats/v7/metricbeat/helper/socket" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/gosigar/sys/linux" ) @@ -290,31 +290,31 @@ var ( } ) -func (c *connection) ToMapStr() (fields common.MapStr, metricSetFields common.MapStr) { +func (c *connection) ToMapStr() (fields mapstr.M, metricSetFields mapstr.M) { localGroup := "server" if g, ok := localHostInfoGroup[c.Direction.String()]; ok { localGroup = g } - fields = common.MapStr{ - "network": common.MapStr{ + fields = mapstr.M{ + "network": mapstr.M{ "type": c.Family.String(), "iana_number": ianaNumbersMap[c.Family.String()], "direction": c.Direction.String(), }, - "user": common.MapStr{ + "user": mapstr.M{ "id": strconv.Itoa(int(c.UID)), }, // Aliases for this are not going to be possible, keeping // duplicated fields by now for backwards comatibility - localGroup: common.MapStr{ + localGroup: mapstr.M{ "ip": c.LocalIP.String(), "port": c.LocalPort, }, } - metricSetFields = common.MapStr{ - "local": common.MapStr{ + metricSetFields = mapstr.M{ + "local": mapstr.M{ "ip": c.LocalIP.String(), "port": c.LocalPort, }, @@ -331,7 +331,7 @@ func (c *connection) ToMapStr() (fields common.MapStr, metricSetFields common.Ma if c.ProcessError != nil { fields.Put("error.code", c.ProcessError.Error()) } else { - process := common.MapStr{"pid": c.PID} + process := mapstr.M{"pid": c.PID} if c.PID > 0 { addOptionalString(process, "executable", c.Exe) @@ -339,7 +339,7 @@ func (c *connection) ToMapStr() (fields common.MapStr, metricSetFields common.Ma if len(c.Args) >= 0 { process["args"] = c.Args - metricSetFields["process"] = common.MapStr{ + metricSetFields["process"] = mapstr.M{ "cmdline": c.CmdLine, } } @@ -355,7 +355,7 @@ func (c *connection) ToMapStr() (fields common.MapStr, metricSetFields common.Ma if c.RemotePort != 0 { // Aliases for this are not going to be possible, keeping // duplicated fields by now for backwards comatibility - remote := common.MapStr{ + remote := mapstr.M{ "ip": c.RemoteIP.String(), "port": c.RemotePort, } @@ -369,7 +369,7 @@ func (c *connection) ToMapStr() (fields common.MapStr, metricSetFields common.Ma remoteGroup, ok := remoteHostInfoGroup[c.Direction.String()] if ok { - fields[remoteGroup] = common.MapStr{ + fields[remoteGroup] = mapstr.M{ "ip": c.RemoteIP.String(), "port": c.RemotePort, } @@ -379,7 +379,7 @@ func (c *connection) ToMapStr() (fields common.MapStr, metricSetFields common.Ma return fields, metricSetFields } -func addOptionalString(m common.MapStr, key, value string) { +func addOptionalString(m mapstr.M, key, value string) { if value == "" { return } diff --git a/metricbeat/module/system/socket/socket_test.go b/metricbeat/module/system/socket/socket_test.go index bd5c485806a..dcc881fd874 100644 --- a/metricbeat/module/system/socket/socket_test.go +++ b/metricbeat/module/system/socket/socket_test.go @@ -32,15 +32,15 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" sock "github.com/elastic/beats/v7/metricbeat/helper/socket" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" _ "github.com/elastic/beats/v7/metricbeat/module/system" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { - directionIs := func(direction string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + directionIs := func(direction string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue("network.direction") return err == nil && v == direction } @@ -110,7 +110,7 @@ func TestFetch(t *testing.T) { s, err := root.GetValue("system.socket") require.NoError(t, err) - fields, ok := s.(common.MapStr) + fields, ok := s.(mapstr.M) require.True(t, ok) port, ok := getRequiredValue(t, "local.port", fields).(int) @@ -151,7 +151,7 @@ func TestFetch(t *testing.T) { assert.True(t, found, "listener not found") } -func getRequiredValue(t testing.TB, key string, m common.MapStr) interface{} { +func getRequiredValue(t testing.TB, key string, m mapstr.M) interface{} { v, err := m.GetValue(key) if err != nil { t.Fatal(errors.Wrapf(err, "failed to get value for key '%s'", key)) diff --git a/metricbeat/module/system/socket_summary/socket_summary.go b/metricbeat/module/system/socket_summary/socket_summary.go index 03b88cea513..3a7569f3f3b 100644 --- a/metricbeat/module/system/socket_summary/socket_summary.go +++ b/metricbeat/module/system/socket_summary/socket_summary.go @@ -23,9 +23,9 @@ import ( "github.com/pkg/errors" "github.com/shirou/gopsutil/v3/net" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -59,7 +59,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { }, nil } -func calculateConnStats(conns []net.ConnectionStat) common.MapStr { +func calculateConnStats(conns []net.ConnectionStat) mapstr.M { var ( allConns = len(conns) allListening = 0 @@ -119,13 +119,13 @@ func calculateConnStats(conns []net.ConnectionStat) common.MapStr { } } - return common.MapStr{ - "all": common.MapStr{ + return mapstr.M{ + "all": mapstr.M{ "count": allConns, "listening": allListening, }, - "tcp": common.MapStr{ - "all": common.MapStr{ + "tcp": mapstr.M{ + "all": mapstr.M{ "count": tcpConns, "listening": tcpListening, "established": tcpEstablished, @@ -139,8 +139,8 @@ func calculateConnStats(conns []net.ConnectionStat) common.MapStr { "closing": tcpClosing, }, }, - "udp": common.MapStr{ - "all": common.MapStr{ + "udp": mapstr.M{ + "all": mapstr.M{ "count": udpConns, }, }, diff --git a/metricbeat/module/system/socket_summary/sockstat_linux.go b/metricbeat/module/system/socket_summary/sockstat_linux.go index 8546127e6dc..56d1c7ee01c 100644 --- a/metricbeat/module/system/socket_summary/sockstat_linux.go +++ b/metricbeat/module/system/socket_summary/sockstat_linux.go @@ -28,8 +28,8 @@ import ( "github.com/pkg/errors" "github.com/shirou/gopsutil/v3/net" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" + "github.com/elastic/elastic-agent-libs/mapstr" ) // SockStat contains data from /proc/net/sockstat @@ -61,7 +61,7 @@ type SockStat struct { } // applyEnhancements gets a list of platform-specific enhancements and apply them to our mapStr object. -func applyEnhancements(data common.MapStr, sys resolve.Resolver) (common.MapStr, error) { +func applyEnhancements(data mapstr.M, sys resolve.Resolver) (mapstr.M, error) { dir := sys.ResolveHostFS("/proc/net/sockstat") pageSize := os.Getpagesize() diff --git a/metricbeat/module/system/socket_summary/sockstat_other.go b/metricbeat/module/system/socket_summary/sockstat_other.go index f316f44c28d..a4c42eaa23d 100644 --- a/metricbeat/module/system/socket_summary/sockstat_other.go +++ b/metricbeat/module/system/socket_summary/sockstat_other.go @@ -23,13 +23,13 @@ package socket_summary import ( "github.com/shirou/gopsutil/v3/net" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/metric/system/resolve" + "github.com/elastic/elastic-agent-libs/mapstr" ) //a stub function for non-linux systems //get a list of platform-specific enhancements and apply them to our mapStr object. -func applyEnhancements(data common.MapStr, sys resolve.Resolver) (common.MapStr, error) { +func applyEnhancements(data mapstr.M, sys resolve.Resolver) (mapstr.M, error) { return data, nil } diff --git a/metricbeat/module/system/uptime/uptime.go b/metricbeat/module/system/uptime/uptime.go index 12e4ec7ac4c..784a9b466ea 100644 --- a/metricbeat/module/system/uptime/uptime.go +++ b/metricbeat/module/system/uptime/uptime.go @@ -23,9 +23,9 @@ package uptime import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" sigar "github.com/elastic/gosigar" ) @@ -54,8 +54,8 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } r.Event(mb.Event{ - MetricSetFields: common.MapStr{ - "duration": common.MapStr{ + MetricSetFields: mapstr.M{ + "duration": mapstr.M{ "ms": int64(uptime.Length * 1000), }, }, diff --git a/metricbeat/module/system/users/users.go b/metricbeat/module/system/users/users.go index 84311b83725..49f21ddc74a 100644 --- a/metricbeat/module/system/users/users.go +++ b/metricbeat/module/system/users/users.go @@ -27,9 +27,9 @@ import ( "github.com/godbus/dbus" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry as soon as the program @@ -91,7 +91,7 @@ func eventMapping(conn *dbus.Conn, sessions []loginSession, report mb.ReporterV2 return errors.Wrap(err, "error getting properties") } - event := common.MapStr{ + event := mapstr.M{ "id": session.ID, "seat": session.Seat, "path": session.Path, @@ -103,11 +103,11 @@ func eventMapping(conn *dbus.Conn, sessions []loginSession, report mb.ReporterV2 "leader": props.Leader, } - rootEvents := common.MapStr{ - "process": common.MapStr{ + rootEvents := mapstr.M{ + "process": mapstr.M{ "pid": props.Leader, }, - "user": common.MapStr{ + "user": mapstr.M{ "name": session.User, "id": strconv.Itoa(int(session.UID)), }, @@ -116,7 +116,7 @@ func eventMapping(conn *dbus.Conn, sessions []loginSession, report mb.ReporterV2 if props.Remote { event["remote_host"] = props.RemoteHost if ipAddr := net.ParseIP(props.RemoteHost); ipAddr != nil { - rootEvents["source"] = common.MapStr{ + rootEvents["source"] = mapstr.M{ "ip": ipAddr, } } diff --git a/metricbeat/module/traefik/health/data.go b/metricbeat/module/traefik/health/data.go index 5d00a02399d..7c267904de6 100644 --- a/metricbeat/module/traefik/health/data.go +++ b/metricbeat/module/traefik/health/data.go @@ -18,9 +18,9 @@ package health import ( - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -37,7 +37,7 @@ var ( } ) -func eventMapping(health map[string]interface{}) (common.MapStr, error) { +func eventMapping(health map[string]interface{}) (mapstr.M, error) { if averageResponseTimeSec, ok := health["average_response_time_sec"]; ok { if averageResponseTimeSec, ok := averageResponseTimeSec.(float64); ok { health["average_response_time_us"] = averageResponseTimeSec * 1000 * 1000 diff --git a/metricbeat/module/traefik/health/data_test.go b/metricbeat/module/traefik/health/data_test.go index 26b5a840f02..3d8dfa2fe4d 100644 --- a/metricbeat/module/traefik/health/data_test.go +++ b/metricbeat/module/traefik/health/data_test.go @@ -23,9 +23,9 @@ package health import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/stretchr/testify/assert" + + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEventMapping(t *testing.T) { @@ -52,16 +52,16 @@ func TestEventMapping(t *testing.T) { event, errors := eventMapping(input) assert.Nil(t, errors, "Errors while mapping input to event") - uptime := event["uptime"].(common.MapStr) + uptime := event["uptime"].(mapstr.M) assert.EqualValues(t, 60231, uptime["sec"]) - response := event["response"].(common.MapStr) + response := event["response"].(mapstr.M) assert.EqualValues(t, 18, response["count"]) - avgTime := response["avg_time"].(common.MapStr) + avgTime := response["avg_time"].(mapstr.M) assert.EqualValues(t, 15, avgTime["us"]) - statusCodes := response["status_codes"].(common.MapStr) + statusCodes := response["status_codes"].(mapstr.M) assert.EqualValues(t, 17, statusCodes["200"]) assert.EqualValues(t, 1, statusCodes["404"]) } diff --git a/metricbeat/module/traefik/health/health.go b/metricbeat/module/traefik/health/health.go index 23e94e9619c..ec528d83a98 100644 --- a/metricbeat/module/traefik/health/health.go +++ b/metricbeat/module/traefik/health/health.go @@ -20,10 +20,10 @@ package health import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry. @@ -72,7 +72,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { metricSetFields, _ := eventMapping(data) event := mb.Event{ MetricSetFields: metricSetFields, - RootFields: common.MapStr{}, + RootFields: mapstr.M{}, } event.RootFields.Put("service.name", "traefik") diff --git a/metricbeat/module/traefik/health/health_test.go b/metricbeat/module/traefik/health/health_test.go index a95adc7fcbf..a68df530c33 100644 --- a/metricbeat/module/traefik/health/health_test.go +++ b/metricbeat/module/traefik/health/health_test.go @@ -29,8 +29,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" _ "github.com/elastic/beats/v7/metricbeat/module/traefik" ) @@ -62,16 +62,16 @@ func TestFetchEventContents(t *testing.T) { fmt.Println(event.MetricSetFields) metricSetFields := event.MetricSetFields - uptime := metricSetFields["uptime"].(common.MapStr) + uptime := metricSetFields["uptime"].(mapstr.M) assert.EqualValues(t, 64283, uptime["sec"]) - response := metricSetFields["response"].(common.MapStr) + response := metricSetFields["response"].(mapstr.M) assert.EqualValues(t, 18, response["count"]) - avgTime := response["avg_time"].(common.MapStr) + avgTime := response["avg_time"].(mapstr.M) assert.EqualValues(t, 15, avgTime["us"]) - statusCodes := response["status_codes"].(common.MapStr) + statusCodes := response["status_codes"].(mapstr.M) assert.EqualValues(t, 17, statusCodes["200"]) assert.EqualValues(t, 1, statusCodes["404"]) } diff --git a/metricbeat/module/uwsgi/status/data.go b/metricbeat/module/uwsgi/status/data.go index 7b371a44b78..6c690db50f9 100644 --- a/metricbeat/module/uwsgi/status/data.go +++ b/metricbeat/module/uwsgi/status/data.go @@ -22,8 +22,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type uwsgiCore struct { @@ -92,8 +92,8 @@ func eventsMapping(content []byte, reporter mb.ReporterV2) error { // worker cores info for _, worker := range stats.Workers { - workerEvent := common.MapStr{ - "worker": common.MapStr{ + workerEvent := mapstr.M{ + "worker": mapstr.M{ "id": worker.ID, "pid": worker.PID, "accepting": worker.Accepting, @@ -119,11 +119,11 @@ func eventsMapping(content []byte, reporter mb.ReporterV2) error { totalWriteErrors += core.WriteErrors totalReadErrors += core.ReadErrors - coreEvent := common.MapStr{ - "core": common.MapStr{ + coreEvent := mapstr.M{ + "core": mapstr.M{ "id": coreID, "worker_pid": worker.PID, - "requests": common.MapStr{ + "requests": mapstr.M{ "total": core.Requests, "static": core.StaticRequests, "routed": core.RoutedRequests, @@ -141,8 +141,8 @@ func eventsMapping(content []byte, reporter mb.ReporterV2) error { } // overall - baseEvent := common.MapStr{ - "total": common.MapStr{ + baseEvent := mapstr.M{ + "total": mapstr.M{ "requests": totalRequests, "exceptions": totalExceptions, "write_errors": totalWriteErrors, diff --git a/metricbeat/module/uwsgi/status/status.go b/metricbeat/module/uwsgi/status/status.go index 334662d8979..e343ada60c6 100644 --- a/metricbeat/module/uwsgi/status/status.go +++ b/metricbeat/module/uwsgi/status/status.go @@ -28,9 +28,9 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/uwsgi" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -104,7 +104,7 @@ func fetchStatData(URL string) ([]byte, error) { func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { content, err := fetchStatData(m.HostData().URI) if err != nil { - reporter.Event(mb.Event{MetricSetFields: common.MapStr{ + reporter.Event(mb.Event{MetricSetFields: mapstr.M{ "error": err.Error(), }}, ) diff --git a/metricbeat/module/uwsgi/status/status_integration_test.go b/metricbeat/module/uwsgi/status/status_integration_test.go index 0cf0cbc8620..165dfe11403 100644 --- a/metricbeat/module/uwsgi/status/status_integration_test.go +++ b/metricbeat/module/uwsgi/status/status_integration_test.go @@ -25,9 +25,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetchTCP(t *testing.T) { @@ -49,15 +49,15 @@ func TestData(t *testing.T) { service := compose.EnsureUp(t, "uwsgi_http") f := mbtest.NewFetcher(t, getConfig("http", service.Host())) - f.WriteEventsCond(t, "", func(event common.MapStr) bool { + f.WriteEventsCond(t, "", func(event mapstr.M) bool { isOverall, _ := event.HasKey("uwsgi.status.total") return isOverall }) - f.WriteEventsCond(t, "_meta/data_core.json", func(event common.MapStr) bool { + f.WriteEventsCond(t, "_meta/data_core.json", func(event mapstr.M) bool { isCore, _ := event.HasKey("uwsgi.status.core") return isCore }) - f.WriteEventsCond(t, "_meta/data_worker.json", func(event common.MapStr) bool { + f.WriteEventsCond(t, "_meta/data_worker.json", func(event mapstr.M) bool { isWorker, _ := event.HasKey("uwsgi.status.worker") return isWorker }) diff --git a/metricbeat/module/uwsgi/status/status_test.go b/metricbeat/module/uwsgi/status/status_test.go index 30fc0867a47..f8ab2133e86 100644 --- a/metricbeat/module/uwsgi/status/status_test.go +++ b/metricbeat/module/uwsgi/status/status_test.go @@ -28,9 +28,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func testData(t *testing.T) (data []byte) { @@ -48,11 +48,11 @@ func testData(t *testing.T) (data []byte) { return } -func findItems(mp []mb.Event, key string) []common.MapStr { - result := make([]common.MapStr, 0, 1) +func findItems(mp []mb.Event, key string) []mapstr.M { + result := make([]mapstr.M, 0, 1) for _, v := range mp { if el, ok := v.MetricSetFields[key]; ok { - result = append(result, el.(common.MapStr)) + result = append(result, el.(mapstr.M)) } } diff --git a/metricbeat/module/vsphere/datastore/datastore.go b/metricbeat/module/vsphere/datastore/datastore.go index f37171c65a3..0afba2c7ca8 100644 --- a/metricbeat/module/vsphere/datastore/datastore.go +++ b/metricbeat/module/vsphere/datastore/datastore.go @@ -22,9 +22,9 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/vsphere" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/vmware/govmomi" "github.com/vmware/govmomi/view" @@ -99,17 +99,17 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error { } usedSpaceBytes := ds.Summary.Capacity - ds.Summary.FreeSpace - event := common.MapStr{ + event := mapstr.M{ "name": ds.Summary.Name, "fstype": ds.Summary.Type, - "capacity": common.MapStr{ - "total": common.MapStr{ + "capacity": mapstr.M{ + "total": mapstr.M{ "bytes": ds.Summary.Capacity, }, - "free": common.MapStr{ + "free": mapstr.M{ "bytes": ds.Summary.FreeSpace, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": usedSpaceBytes, "pct": usedSpacePercent, }, diff --git a/metricbeat/module/vsphere/host/data.go b/metricbeat/module/vsphere/host/data.go index 81ceba8c43a..4be828fd448 100644 --- a/metricbeat/module/vsphere/host/data.go +++ b/metricbeat/module/vsphere/host/data.go @@ -18,38 +18,38 @@ package host import ( - "github.com/elastic/beats/v7/libbeat/common" - "github.com/vmware/govmomi/vim25/mo" + + "github.com/elastic/elastic-agent-libs/mapstr" ) -func eventMapping(hs mo.HostSystem) common.MapStr { +func eventMapping(hs mo.HostSystem) mapstr.M { totalCPU := int64(hs.Summary.Hardware.CpuMhz) * int64(hs.Summary.Hardware.NumCpuCores) freeCPU := int64(totalCPU) - int64(hs.Summary.QuickStats.OverallCpuUsage) usedMemory := int64(hs.Summary.QuickStats.OverallMemoryUsage) * 1024 * 1024 freeMemory := int64(hs.Summary.Hardware.MemorySize) - usedMemory - event := common.MapStr{ + event := mapstr.M{ "name": hs.Summary.Config.Name, - "cpu": common.MapStr{ - "used": common.MapStr{ + "cpu": mapstr.M{ + "used": mapstr.M{ "mhz": hs.Summary.QuickStats.OverallCpuUsage, }, - "total": common.MapStr{ + "total": mapstr.M{ "mhz": totalCPU, }, - "free": common.MapStr{ + "free": mapstr.M{ "mhz": freeCPU, }, }, - "memory": common.MapStr{ - "used": common.MapStr{ + "memory": mapstr.M{ + "used": mapstr.M{ "bytes": usedMemory, }, - "total": common.MapStr{ + "total": mapstr.M{ "bytes": hs.Summary.Hardware.MemorySize, }, - "free": common.MapStr{ + "free": mapstr.M{ "bytes": freeMemory, }, }, diff --git a/metricbeat/module/vsphere/host/host.go b/metricbeat/module/vsphere/host/host.go index 6f7d19b5d57..b299d6b83b0 100644 --- a/metricbeat/module/vsphere/host/host.go +++ b/metricbeat/module/vsphere/host/host.go @@ -24,9 +24,9 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/vsphere" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/vmware/govmomi" "github.com/vmware/govmomi/property" @@ -100,7 +100,7 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error { for _, hs := range hst { - event := common.MapStr{} + event := mapstr.M{} event["name"] = hs.Summary.Config.Name event.Put("cpu.used.mhz", hs.Summary.QuickStats.OverallCpuUsage) diff --git a/metricbeat/module/vsphere/host/host_test.go b/metricbeat/module/vsphere/host/host_test.go index ce9ee1ba80f..17bd2a09dd1 100644 --- a/metricbeat/module/vsphere/host/host_test.go +++ b/metricbeat/module/vsphere/host/host_test.go @@ -20,8 +20,8 @@ package host import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" "github.com/vmware/govmomi/simulator" @@ -50,26 +50,26 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, "localhost.localdomain", event["name"]) - cpu := event["cpu"].(common.MapStr) + cpu := event["cpu"].(mapstr.M) - cpuUsed := cpu["used"].(common.MapStr) + cpuUsed := cpu["used"].(mapstr.M) assert.EqualValues(t, 67, cpuUsed["mhz"]) - cpuTotal := cpu["total"].(common.MapStr) + cpuTotal := cpu["total"].(mapstr.M) assert.EqualValues(t, 4588, cpuTotal["mhz"]) - cpuFree := cpu["free"].(common.MapStr) + cpuFree := cpu["free"].(mapstr.M) assert.EqualValues(t, 4521, cpuFree["mhz"]) - memory := event["memory"].(common.MapStr) + memory := event["memory"].(mapstr.M) - memoryUsed := memory["used"].(common.MapStr) + memoryUsed := memory["used"].(mapstr.M) assert.EqualValues(t, uint64(1472200704), memoryUsed["bytes"]) - memoryTotal := memory["total"].(common.MapStr) + memoryTotal := memory["total"].(mapstr.M) assert.EqualValues(t, uint64(4294430720), memoryTotal["bytes"]) - memoryFree := memory["free"].(common.MapStr) + memoryFree := memory["free"].(mapstr.M) assert.EqualValues(t, uint64(2822230016), memoryFree["bytes"]) } diff --git a/metricbeat/module/vsphere/virtualmachine/virtualmachine.go b/metricbeat/module/vsphere/virtualmachine/virtualmachine.go index 13af0168765..546a40fdbfb 100644 --- a/metricbeat/module/vsphere/virtualmachine/virtualmachine.go +++ b/metricbeat/module/vsphere/virtualmachine/virtualmachine.go @@ -22,9 +22,9 @@ import ( "fmt" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/vsphere" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" "github.com/vmware/govmomi" @@ -125,20 +125,20 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error { for _, vm := range vmt { usedMemory := int64(vm.Summary.QuickStats.GuestMemoryUsage) * 1024 * 1024 usedCPU := vm.Summary.QuickStats.OverallCpuUsage - event := common.MapStr{ + event := mapstr.M{ "name": vm.Summary.Config.Name, "os": vm.Summary.Config.GuestFullName, - "cpu": common.MapStr{ - "used": common.MapStr{ + "cpu": mapstr.M{ + "used": mapstr.M{ "mhz": usedCPU, }, }, - "memory": common.MapStr{ - "used": common.MapStr{ - "guest": common.MapStr{ + "memory": mapstr.M{ + "used": mapstr.M{ + "guest": mapstr.M{ "bytes": usedMemory, }, - "host": common.MapStr{ + "host": mapstr.M{ "bytes": int64(vm.Summary.QuickStats.HostMemoryUsage) * 1024 * 1024, }, }, @@ -213,8 +213,8 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error { return nil } -func getCustomFields(customFields []types.BaseCustomFieldValue, customFieldsMap map[int32]string) common.MapStr { - outputFields := common.MapStr{} +func getCustomFields(customFields []types.BaseCustomFieldValue, customFieldsMap map[int32]string) mapstr.M { + outputFields := mapstr.M{} for _, v := range customFields { customFieldString := v.(*types.CustomFieldStringValue) key, ok := customFieldsMap[v.GetCustomFieldValue().Key] diff --git a/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go b/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go index 83a2db898dd..e43345e5e53 100644 --- a/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go +++ b/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go @@ -24,8 +24,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/vmware/govmomi/simulator" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetchEventContents(t *testing.T) { @@ -52,25 +52,25 @@ func TestFetchEventContents(t *testing.T) { assert.EqualValues(t, "localhost.localdomain", event["host.hostname"]) assert.True(t, strings.Contains(event["name"].(string), "ha-host_VM")) - cpu := event["cpu"].(common.MapStr) + cpu := event["cpu"].(mapstr.M) - cpuUsed := cpu["used"].(common.MapStr) + cpuUsed := cpu["used"].(mapstr.M) assert.EqualValues(t, 0, cpuUsed["mhz"]) - memory := event["memory"].(common.MapStr) + memory := event["memory"].(mapstr.M) - memoryUsed := memory["used"].(common.MapStr) - memoryUsedHost := memoryUsed["host"].(common.MapStr) - memoryUsedGuest := memoryUsed["guest"].(common.MapStr) + memoryUsed := memory["used"].(mapstr.M) + memoryUsedHost := memoryUsed["host"].(mapstr.M) + memoryUsedGuest := memoryUsed["guest"].(mapstr.M) assert.EqualValues(t, 0, memoryUsedGuest["bytes"]) assert.EqualValues(t, 0, memoryUsedHost["bytes"]) - memoryTotal := memory["total"].(common.MapStr) - memoryTotalGuest := memoryTotal["guest"].(common.MapStr) + memoryTotal := memory["total"].(mapstr.M) + memoryTotalGuest := memoryTotal["guest"].(mapstr.M) assert.EqualValues(t, uint64(33554432), memoryTotalGuest["bytes"]) - memoryFree := memory["free"].(common.MapStr) - memoryFreeGuest := memoryFree["guest"].(common.MapStr) + memoryFree := memory["free"].(mapstr.M) + memoryFreeGuest := memoryFree["guest"].(mapstr.M) assert.EqualValues(t, uint64(33554432), memoryFreeGuest["bytes"]) } diff --git a/metricbeat/module/windows/perfmon/config_test.go b/metricbeat/module/windows/perfmon/config_test.go index 51330b4e60e..4ae6bc8a7cb 100644 --- a/metricbeat/module/windows/perfmon/config_test.go +++ b/metricbeat/module/windows/perfmon/config_test.go @@ -23,14 +23,14 @@ package perfmon import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" "github.com/stretchr/testify/assert" ) func TestValidate(t *testing.T) { - conf := common.MapStr{ + conf := mapstr.M{ "module": "windows", "period": "10s", "metricsets": []string{"perfmon"}, @@ -41,7 +41,7 @@ func TestValidate(t *testing.T) { var config Config err = c.Unpack(&config) assert.Error(t, err, "no perfmon counters or queries have been configured") - conf["perfmon.queries"] = []common.MapStr{ + conf["perfmon.queries"] = []mapstr.M{ { "object": "Process", }, @@ -51,10 +51,10 @@ func TestValidate(t *testing.T) { err = c.Unpack(&config) assert.Error(t, err, "missing required field accessing 'perfmon.queries.0.counters'") - conf["perfmon.queries"] = []common.MapStr{ + conf["perfmon.queries"] = []mapstr.M{ { "object": "Process", - "counters": []common.MapStr{ + "counters": []mapstr.M{ { "name": "Thread Count", }, diff --git a/metricbeat/module/windows/perfmon/data.go b/metricbeat/module/windows/perfmon/data.go index 691e0b7041c..9eddf8a7060 100644 --- a/metricbeat/module/windows/perfmon/data.go +++ b/metricbeat/module/windows/perfmon/data.go @@ -28,10 +28,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper/windows/pdh" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var processRegexp = regexp.MustCompile(`(.+?[^\s])(?:#\d+|$)`) @@ -71,7 +71,7 @@ func (re *Reader) groupToEvents(counters map[string][]pdh.CounterValue) []mb.Eve // Create a new event if the key doesn't exist in the map if _, ok := eventMap[eventKey]; !ok { eventMap[eventKey] = &mb.Event{ - MetricSetFields: common.MapStr{}, + MetricSetFields: mapstr.M{}, Error: errors.Wrapf(val.Err.Error, "failed on query=%v", counterPath), } if val.Instance != "" { @@ -105,7 +105,7 @@ func (re *Reader) groupToEvents(counters map[string][]pdh.CounterValue) []mb.Eve func (re *Reader) groupToSingleEvent(counters map[string][]pdh.CounterValue) mb.Event { event := mb.Event{ - MetricSetFields: common.MapStr{}, + MetricSetFields: mapstr.M{}, } measurements := make(map[string]float64, 0) for counterPath, values := range counters { diff --git a/metricbeat/module/windows/perfmon/data_test.go b/metricbeat/module/windows/perfmon/data_test.go index 67ec8b58470..d9db331c88f 100644 --- a/metricbeat/module/windows/perfmon/data_test.go +++ b/metricbeat/module/windows/perfmon/data_test.go @@ -25,8 +25,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/windows/pdh" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGroupToEvents(t *testing.T) { @@ -217,7 +217,7 @@ func TestGroupToSingleEvent(t *testing.T) { assert.Equal(t, val, float64(44)) val, err = event.MetricSetFields.GetValue("%_processor_time:count") assert.NoError(t, err) - assert.Equal(t, val, common.MapStr{"processor_count": float64(2)}) + assert.Equal(t, val, mapstr.M{"processor_count": float64(2)}) ok, err = event.MetricSetFields.HasKey("%_user_time") assert.NoError(t, err) assert.True(t, ok) @@ -229,7 +229,7 @@ func TestGroupToSingleEvent(t *testing.T) { assert.Equal(t, val, float64(21)) val, err = event.MetricSetFields.GetValue("%_user_time:count") assert.NoError(t, err) - assert.Equal(t, val, common.MapStr{"processor_count": float64(2)}) + assert.Equal(t, val, mapstr.M{"processor_count": float64(2)}) } func TestMatchesParentProcess(t *testing.T) { diff --git a/metricbeat/module/windows/service/reader.go b/metricbeat/module/windows/service/reader.go index e172ea67418..72fa47b8579 100644 --- a/metricbeat/module/windows/service/reader.go +++ b/metricbeat/module/windows/service/reader.go @@ -29,7 +29,7 @@ import ( "github.com/pkg/errors" "golang.org/x/sys/windows/registry" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -73,16 +73,16 @@ func NewReader() (*Reader, error) { return r, nil } -func (reader *Reader) Read() ([]common.MapStr, error) { +func (reader *Reader) Read() ([]mapstr.M, error) { services, err := GetServiceStates(reader.handle, reader.state, reader.protectedServices) if err != nil { return nil, err } - result := make([]common.MapStr, 0, len(services)) + result := make([]mapstr.M, 0, len(services)) for _, service := range services { - ev := common.MapStr{ + ev := mapstr.M{ "id": reader.getServiceID(service.ServiceName), "display_name": service.DisplayName, "name": service.ServiceName, diff --git a/metricbeat/module/windows/service/service_integration_test.go b/metricbeat/module/windows/service/service_integration_test.go index 65f4cb9544b..c5127d8fdcd 100644 --- a/metricbeat/module/windows/service/service_integration_test.go +++ b/metricbeat/module/windows/service/service_integration_test.go @@ -23,12 +23,11 @@ package service import ( "testing" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/StackExchange/wmi" "github.com/stretchr/testify/assert" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) type Win32Service struct { @@ -74,7 +73,7 @@ func TestReadService(t *testing.T) { t.Fatal(err) } - var stateChangedServices []common.MapStr + var stateChangedServices []mapstr.M // Compare our module's data against WMI. for _, s := range services { diff --git a/metricbeat/module/zookeeper/connection/connection_test.go b/metricbeat/module/zookeeper/connection/connection_test.go index 7576183addc..2dec3b7b626 100644 --- a/metricbeat/module/zookeeper/connection/connection_test.go +++ b/metricbeat/module/zookeeper/connection/connection_test.go @@ -21,9 +21,9 @@ import ( "bytes" "testing" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/stretchr/testify/assert" + + "github.com/elastic/elastic-agent-libs/mapstr" ) var srvrTestInput = `/172.17.0.1:55218[0](queued=0,recved=1,sent=0) @@ -43,19 +43,19 @@ func TestParser(t *testing.T) { firstLineClient, ok := firstLine.RootFields["client"] assert.True(t, ok) - firstLineClientMap, ok := firstLineClient.(common.MapStr) + firstLineClientMap, ok := firstLineClient.(mapstr.M) assert.True(t, ok) secondLineClient, ok := secondLine.RootFields["client"] assert.True(t, ok) - secondLineClientMap, ok := secondLineClient.(common.MapStr) + secondLineClientMap, ok := secondLineClient.(mapstr.M) assert.True(t, ok) thirdLineClient, ok := thirdLine.RootFields["client"] assert.True(t, ok) - thirdLineClientMap, ok := thirdLineClient.(common.MapStr) + thirdLineClientMap, ok := thirdLineClient.(mapstr.M) assert.True(t, ok) assert.Equal(t, "172.17.0.1", firstLineClientMap["ip"]) diff --git a/metricbeat/module/zookeeper/connection/data.go b/metricbeat/module/zookeeper/connection/data.go index 5e1842dcc7b..715cb8bc87c 100644 --- a/metricbeat/module/zookeeper/connection/data.go +++ b/metricbeat/module/zookeeper/connection/data.go @@ -25,8 +25,7 @@ import ( "strconv" "github.com/elastic/beats/v7/metricbeat/mb" - - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var capturer = regexp.MustCompile(`/(?P.*):(?P\d+)\[(?P\d*)]\(queued=(?P\d*),recved=(?P\d*),sent=(?P\d*)\)`) @@ -37,8 +36,8 @@ func (m *MetricSet) parseCons(i io.Reader) []mb.Event { result := make([]mb.Event, 0) for scanner.Scan() { - metricsetFields := common.MapStr{} - rootFields := common.MapStr{} + metricsetFields := mapstr.M{} + rootFields := mapstr.M{} line := scanner.Text() oneParsingIsCorrect := false @@ -89,7 +88,7 @@ func lineToMap(line string) (map[string]string, error) { return keyMap, nil } -func (m *MetricSet) checkRegexAndSetInt(output common.MapStr, capturedData string, key string, correct *bool) { +func (m *MetricSet) checkRegexAndSetInt(output mapstr.M, capturedData string, key string, correct *bool) { if capturedData != "" { n, err := strconv.ParseInt(capturedData, 10, 64) if err != nil { diff --git a/metricbeat/module/zookeeper/mntr/data.go b/metricbeat/module/zookeeper/mntr/data.go index 49eb1726d17..d40acac71df 100644 --- a/metricbeat/module/zookeeper/mntr/data.go +++ b/metricbeat/module/zookeeper/mntr/data.go @@ -22,12 +22,11 @@ import ( "io" "regexp" - "github.com/elastic/beats/v7/libbeat/common" - s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstrstr" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -78,7 +77,7 @@ func eventMapping(serverId string, response io.Reader, r mb.ReporterV2, logger * } event, _ := schema.Apply(fullEvent) - e := mb.Event{RootFields: common.MapStr{}} + e := mb.Event{RootFields: mapstr.M{}} e.RootFields.Put("service.node.name", serverId) if version, ok := event["version"]; ok { diff --git a/metricbeat/module/zookeeper/mntr/mntr_integration_test.go b/metricbeat/module/zookeeper/mntr/mntr_integration_test.go index 394215af5ec..9670369e6aa 100644 --- a/metricbeat/module/zookeeper/mntr/mntr_integration_test.go +++ b/metricbeat/module/zookeeper/mntr/mntr_integration_test.go @@ -25,9 +25,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -44,10 +44,10 @@ func TestFetch(t *testing.T) { events[0].BeatEvent("zookeeper", "mntr").Fields.StringToPrint()) e, _ := events[0].BeatEvent("zookeeper", "mntr").Fields.GetValue("zookeeper.mntr") - event := e.(common.MapStr) + event := e.(mapstr.M) // Check values - avgLatency := event["latency"].(common.MapStr)["avg"].(float64) - maxLatency := event["latency"].(common.MapStr)["max"].(float64) + avgLatency := event["latency"].(mapstr.M)["avg"].(float64) + maxLatency := event["latency"].(mapstr.M)["max"].(float64) numAliveConnections := event["num_alive_connections"].(int64) assert.True(t, avgLatency >= 0) diff --git a/metricbeat/module/zookeeper/mntr/mntr_test.go b/metricbeat/module/zookeeper/mntr/mntr_test.go index 0eb4d9593cd..bbae2153c5c 100644 --- a/metricbeat/module/zookeeper/mntr/mntr_test.go +++ b/metricbeat/module/zookeeper/mntr/mntr_test.go @@ -25,19 +25,19 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func assertExpectations(t *testing.T, expectations common.MapStr, report common.MapStr, message ...string) { +func assertExpectations(t *testing.T, expectations mapstr.M, report mapstr.M, message ...string) { for key, expectation := range expectations { assert.Contains(t, report, key, message) switch expectation := expectation.(type) { - case common.MapStr: + case mapstr.M: nestedReport, _ := report.GetValue(key) assert.IsType(t, nestedReport, report, message) - assertExpectations(t, expectation, nestedReport.(common.MapStr), message...) + assertExpectations(t, expectation, nestedReport.(mapstr.M), message...) default: reportValue, _ := report.GetValue(key) assert.Equal(t, expectation, reportValue, message) @@ -56,17 +56,17 @@ func TestEventMapping(t *testing.T) { type TestCase struct { Version string MntrSample string - ExpectedValues common.MapStr + ExpectedValues mapstr.M } mntrSamples := []TestCase{ { "3.5.3", mntrTestInputZooKeeper35, - common.MapStr{ + mapstr.M{ "learners": int64(1), "followers": int64(1), - "latency": common.MapStr{ + "latency": mapstr.M{ "max": float64(29), "avg": float64(0), "min": float64(0), @@ -76,10 +76,10 @@ func TestEventMapping(t *testing.T) { { "3.7.0", mntrTestInputZooKeeper37, - common.MapStr{ + mapstr.M{ "learners": int64(1), "followers": int64(1), - "latency": common.MapStr{ + "latency": mapstr.M{ "max": float64(8), "avg": float64(0.5714), "min": float64(0), diff --git a/metricbeat/module/zookeeper/server/data.go b/metricbeat/module/zookeeper/server/data.go index 9f185e3a218..24018f99d3f 100644 --- a/metricbeat/module/zookeeper/server/data.go +++ b/metricbeat/module/zookeeper/server/data.go @@ -28,8 +28,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var latencyCapturer = regexp.MustCompile(`(\d+)/(\d+)/(\d+)`) @@ -41,7 +41,7 @@ var fieldsCapturer = regexp.MustCompile(`^([a-zA-Z\s]+):\s(\d+)`) var versionCapturer = regexp.MustCompile(`:\s(.*),`) var dateCapturer = regexp.MustCompile(`built on (.*)`) -func parseSrvr(i io.Reader, logger *logp.Logger) (common.MapStr, string, error) { +func parseSrvr(i io.Reader, logger *logp.Logger) (mapstr.M, string, error) { scanner := bufio.NewScanner(i) //Get version @@ -51,7 +51,7 @@ func parseSrvr(i io.Reader, logger *logp.Logger) (common.MapStr, string, error) return nil, "", errors.New("no initial successful text scan, aborting") } - output := common.MapStr{} + output := mapstr.M{} version := versionCapturer.FindStringSubmatch(scanner.Text())[1] dateString := dateCapturer.FindStringSubmatch(scanner.Text())[1] @@ -150,8 +150,8 @@ func parseSrvr(i io.Reader, logger *logp.Logger) (common.MapStr, string, error) return output, version, nil } -func parseZxid(line string) (common.MapStr, error) { - output := common.MapStr{} +func parseZxid(line string) (mapstr.M, error) { + output := mapstr.M{} zxidSplit := strings.Split(line, " ") if len(zxidSplit) < 2 { @@ -180,8 +180,8 @@ func parseZxid(line string) (common.MapStr, error) { return output, nil } -func parseProposalSizes(line string) (common.MapStr, error) { - output := common.MapStr{} +func parseProposalSizes(line string) (mapstr.M, error) { + output := mapstr.M{} initialSplit := strings.Split(line, " ") if len(initialSplit) < 4 { @@ -213,8 +213,8 @@ func parseProposalSizes(line string) (common.MapStr, error) { return output, nil } -func parseLatencyLine(line string) (common.MapStr, error) { - output := common.MapStr{} +func parseLatencyLine(line string) (mapstr.M, error) { + output := mapstr.M{} values := latencyCapturer.FindStringSubmatch(line) if len(values) < 4 { diff --git a/metricbeat/module/zookeeper/server/server.go b/metricbeat/module/zookeeper/server/server.go index 6b4c7d31011..d4b81ddc391 100644 --- a/metricbeat/module/zookeeper/server/server.go +++ b/metricbeat/module/zookeeper/server/server.go @@ -42,11 +42,10 @@ package server import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" "github.com/elastic/beats/v7/metricbeat/module/zookeeper" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -89,9 +88,9 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { event := mb.Event{ MetricSetFields: metricsetFields, - RootFields: common.MapStr{ - "service": common.MapStr{ - "node": common.MapStr{ + RootFields: mapstr.M{ + "service": mapstr.M{ + "node": mapstr.M{ "name": serverID, }, "version": version, diff --git a/metricbeat/module/zookeeper/server/server_test.go b/metricbeat/module/zookeeper/server/server_test.go index 442770ce613..91e737cf1c1 100644 --- a/metricbeat/module/zookeeper/server/server_test.go +++ b/metricbeat/module/zookeeper/server/server_test.go @@ -23,8 +23,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var srvrTestInput = `Zookeeper version: 3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653, built on 05/03/2019 12:07 GMT @@ -49,7 +49,7 @@ func TestParser(t *testing.T) { assert.Equal(t, "2019-05-03T12:07:00Z", mapStr["version_date"]) assert.Equal(t, "3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653", versionID) - latency := mapStr["latency"].(common.MapStr) + latency := mapStr["latency"].(mapstr.M) assert.Equal(t, int64(1), latency["min"]) assert.Equal(t, int64(2), latency["avg"]) assert.Equal(t, int64(3), latency["max"]) @@ -61,7 +61,7 @@ func TestParser(t *testing.T) { assert.Equal(t, "standalone", mapStr["mode"]) assert.Equal(t, int64(4), mapStr["node_count"]) - proposalSizes := mapStr["proposal_sizes"].(common.MapStr) + proposalSizes := mapStr["proposal_sizes"].(mapstr.M) assert.Equal(t, int64(-3), proposalSizes["last"]) assert.Equal(t, int64(-999), proposalSizes["min"]) assert.Equal(t, int64(-1), proposalSizes["max"]) diff --git a/metricbeat/scripts/module/metricset/metricset.go.tmpl b/metricbeat/scripts/module/metricset/metricset.go.tmpl index a156abd916f..b79ff2f19f2 100644 --- a/metricbeat/scripts/module/metricset/metricset.go.tmpl +++ b/metricbeat/scripts/module/metricset/metricset.go.tmpl @@ -1,7 +1,7 @@ package {metricset} import ( - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/metricbeat/mb" ) @@ -44,7 +44,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(report mb.ReporterV2) error { report.Event(mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "counter": m.counter, }, }) diff --git a/packetbeat/beater/setup.go b/packetbeat/beater/setup.go index 5664e281bc1..bc4a450a6cc 100644 --- a/packetbeat/beater/setup.go +++ b/packetbeat/beater/setup.go @@ -19,13 +19,13 @@ package beater import ( "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/packetbeat/config" "github.com/elastic/beats/v7/packetbeat/flows" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" "github.com/elastic/beats/v7/packetbeat/sniffer" + "github.com/elastic/elastic-agent-libs/mapstr" ) func setupSniffer(cfg config.Config, protocols *protos.ProtocolsStruct, workerFactory sniffer.WorkerFactory) (*sniffer.Sniffer, error) { @@ -60,7 +60,7 @@ func setupFlows(pipeline beat.Pipeline, watcher procs.ProcessesWatcher, cfg conf }, } if cfg.Flows.Index != "" { - clientConfig.Processing.Meta = common.MapStr{"raw_index": cfg.Flows.Index} + clientConfig.Processing.Meta = mapstr.M{"raw_index": cfg.Flows.Index} } client, err := pipeline.ConnectWith(clientConfig) diff --git a/packetbeat/cmd/root.go b/packetbeat/cmd/root.go index 6032089c9fc..845129266fa 100644 --- a/packetbeat/cmd/root.go +++ b/packetbeat/cmd/root.go @@ -24,10 +24,10 @@ import ( cmd "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/publisher/processing" "github.com/elastic/beats/v7/packetbeat/beater" + "github.com/elastic/elastic-agent-libs/mapstr" // Register fields and protocol modules. _ "github.com/elastic/beats/v7/packetbeat/include" @@ -39,8 +39,8 @@ const ( ) // withECSVersion is a modifier that adds ecs.version to events. -var withECSVersion = processing.WithFields(common.MapStr{ - "ecs": common.MapStr{ +var withECSVersion = processing.WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) diff --git a/packetbeat/config/agent.go b/packetbeat/config/agent.go index 34df1721597..39dcda2ad40 100644 --- a/packetbeat/config/agent.go +++ b/packetbeat/config/agent.go @@ -24,6 +24,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/packetbeat/procs" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" ) @@ -36,7 +37,7 @@ type datastream struct { type agentInput struct { Type string `config:"type"` Datastream datastream `config:"data_stream"` - Processors []common.MapStr `config:"processors"` + Processors []mapstr.M `config:"processors"` Streams []map[string]interface{} `config:"streams"` } @@ -63,13 +64,13 @@ func (i agentInput) addProcessorsAndIndex(cfg *common.Config) (*common.Config, e if err := cfg.Unpack(&datastreamConfig); err != nil { return nil, err } - mergeConfig, err := common.NewConfigFrom(common.MapStr{ + mergeConfig, err := common.NewConfigFrom(mapstr.M{ "index": datastreamConfig.Datastream.Type + "-" + datastreamConfig.Datastream.Dataset + "-" + namespace, - "processors": append([]common.MapStr{ + "processors": append([]mapstr.M{ { - "add_fields": common.MapStr{ + "add_fields": mapstr.M{ "target": "data_stream", - "fields": common.MapStr{ + "fields": mapstr.M{ "type": datastreamConfig.Datastream.Type, "dataset": datastreamConfig.Datastream.Dataset, "namespace": namespace, @@ -77,9 +78,9 @@ func (i agentInput) addProcessorsAndIndex(cfg *common.Config) (*common.Config, e }, }, { - "add_fields": common.MapStr{ + "add_fields": mapstr.M{ "target": "event", - "fields": common.MapStr{ + "fields": mapstr.M{ "dataset": datastreamConfig.Datastream.Dataset, }, }, diff --git a/packetbeat/config/config.go b/packetbeat/config/config.go index 173e507fc40..2ec7e2fa8d3 100644 --- a/packetbeat/config/config.go +++ b/packetbeat/config/config.go @@ -24,6 +24,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/packetbeat/procs" + "github.com/elastic/elastic-agent-libs/mapstr" ) type Config struct { @@ -94,7 +95,7 @@ type Flows struct { Enabled *bool `config:"enabled"` Timeout string `config:"timeout"` Period string `config:"period"` - EventMetadata common.EventMetadata `config:",inline"` + EventMetadata mapstr.EventMetadata `config:",inline"` Processors processors.PluginConfig `config:"processors"` KeepNull bool `config:"keep_null"` // Index is used to overwrite the index where flows are published diff --git a/packetbeat/flows/flows_test.go b/packetbeat/flows/flows_test.go index 9cee1c59e2d..6052a01c803 100644 --- a/packetbeat/flows/flows_test.go +++ b/packetbeat/flows/flows_test.go @@ -28,10 +28,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/packetbeat/config" "github.com/elastic/beats/v7/packetbeat/procs" + "github.com/elastic/elastic-agent-libs/mapstr" ) type flowsChan struct { @@ -136,9 +136,9 @@ func TestFlowsCounting(t *testing.T) { event := events[0].Fields t.Logf("event: %v", event) - source := event["source"].(common.MapStr) - dest := event["destination"].(common.MapStr) - network := event["network"].(common.MapStr) + source := event["source"].(mapstr.M) + dest := event["destination"].(mapstr.M) + network := event["network"].(mapstr.M) // validate generated event assert.Equal(t, net.HardwareAddr(mac1).String(), source["mac"]) diff --git a/packetbeat/flows/worker.go b/packetbeat/flows/worker.go index fac8371afbd..7ac9f7125ca 100644 --- a/packetbeat/flows/worker.go +++ b/packetbeat/flows/worker.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/flowhash" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos/applayer" + "github.com/elastic/elastic-agent-libs/mapstr" ) type flowsProcessor struct { @@ -211,7 +212,7 @@ func createEvent( ) beat.Event { timestamp := ts - event := common.MapStr{ + event := mapstr.M{ "start": common.Time(f.createTS), "end": common.Time(f.ts), "duration": f.ts.Sub(f.createTS), @@ -226,18 +227,18 @@ func createEvent( } event["type"] = eventType - flow := common.MapStr{ + flow := mapstr.M{ "id": common.NetString(f.id.Serialize()), "final": isOver, } - fields := common.MapStr{ + fields := mapstr.M{ "event": event, "flow": flow, "type": "flow", } - network := common.MapStr{} - source := common.MapStr{} - dest := common.MapStr{} + network := mapstr.M{} + source := mapstr.M{} + dest := mapstr.M{} tuple := common.IPPortTuple{} var communityID flowhash.Flow var proto applayer.Transport @@ -398,7 +399,7 @@ func createEvent( if tuple.IPLength != 0 && tuple.SrcPort != 0 { if proc := watcher.FindProcessesTuple(&tuple, proto); proc != nil { if proc.Src.PID > 0 { - p := common.MapStr{ + p := mapstr.M{ "pid": proc.Src.PID, "name": proc.Src.Name, "args": proc.Src.Args, @@ -414,7 +415,7 @@ func createEvent( fields["process"] = p } if proc.Dst.PID > 0 { - p := common.MapStr{ + p := mapstr.M{ "pid": proc.Dst.PID, "name": proc.Dst.Name, "args": proc.Dst.Args, @@ -480,7 +481,7 @@ func encodeStats( return report } -func putOrAppendString(m common.MapStr, key, value string) { +func putOrAppendString(m mapstr.M, key, value string) { old, found := m[key] if !found { m[key] = value @@ -497,7 +498,7 @@ func putOrAppendString(m common.MapStr, key, value string) { } } -func putOrAppendUint64(m common.MapStr, key string, value uint64) { +func putOrAppendUint64(m mapstr.M, key string, value uint64) { old, found := m[key] if !found { m[key] = value diff --git a/packetbeat/pb/event.go b/packetbeat/pb/event.go index 4ba7e4acf15..3c38f98729b 100644 --- a/packetbeat/pb/event.go +++ b/packetbeat/pb/event.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/flowhash" "github.com/elastic/beats/v7/libbeat/conditions" "github.com/elastic/beats/v7/libbeat/ecs" + "github.com/elastic/elastic-agent-libs/mapstr" ) // FieldsKey is the key under which a *pb.Fields value may be stored in a @@ -95,7 +96,7 @@ func NewBeatEvent(timestamp time.Time) (beat.Event, *Fields) { pbf := NewFields() return beat.Event{ Timestamp: timestamp, - Fields: common.MapStr{ + Fields: mapstr.M{ FieldsKey: pbf, }, }, pbf @@ -103,7 +104,7 @@ func NewBeatEvent(timestamp time.Time) (beat.Event, *Fields) { // GetFields returns a pointer to a Fields object if one is stored within the // given MapStr. It returns nil and no error if no Fields value is present. -func GetFields(m common.MapStr) (*Fields, error) { +func GetFields(m mapstr.M) (*Fields, error) { v, found := m[FieldsKey] if !found { return nil, nil @@ -365,7 +366,7 @@ func perimeterBasedDirection(source, destination net.IP, internalNetworks []stri // MarshalMapStr marshals the fields into MapStr. It returns an error if there // is a problem writing the keys to the given map (like if an intermediate key // exists and is not a map). -func (f *Fields) MarshalMapStr(m common.MapStr) error { +func (f *Fields) MarshalMapStr(m mapstr.M) error { typ := reflect.TypeOf(*f) val := reflect.ValueOf(*f) @@ -397,11 +398,11 @@ func (f *Fields) MarshalMapStr(m common.MapStr) error { // MarshalStruct marshals any struct containing ecs or packetbeat tags into the // given MapStr. Zero-value and nil fields are always omitted. -func MarshalStruct(m common.MapStr, key string, val interface{}) error { +func MarshalStruct(m mapstr.M, key string, val interface{}) error { return marshalStruct(m, key, reflect.ValueOf(val)) } -func marshalStruct(m common.MapStr, key string, val reflect.Value) error { +func marshalStruct(m mapstr.M, key string, val reflect.Value) error { // Dereference pointers. if val.Type().Kind() == reflect.Ptr { if val.IsNil() { diff --git a/packetbeat/pb/event_test.go b/packetbeat/pb/event_test.go index c4a6fadb91e..9765cc194bb 100644 --- a/packetbeat/pb/event_test.go +++ b/packetbeat/pb/event_test.go @@ -25,26 +25,26 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestMarshalMapStr(t *testing.T) { f := NewFields() f.Source = &ecs.Source{IP: "127.0.0.1"} - m := common.MapStr{} + m := mapstr.M{} if err := f.MarshalMapStr(m); err != nil { t.Fatal(err) } - assert.Equal(t, common.MapStr{ - "event": common.MapStr{ + assert.Equal(t, mapstr.M{ + "event": mapstr.M{ "kind": "event", "category": []string{"network"}, "type": []string{"connection", "protocol"}, }, - "source": common.MapStr{"ip": "127.0.0.1"}, + "source": mapstr.M{"ip": "127.0.0.1"}, }, m) } @@ -76,7 +76,7 @@ func TestIsEmptyValue(t *testing.T) { } func TestSkipFields(t *testing.T) { - m := common.MapStr{} + m := mapstr.M{} if err := MarshalStruct(m, "test", &struct { Field1 string `ecs:"field1"` Field2 string @@ -88,8 +88,8 @@ func TestSkipFields(t *testing.T) { }); err != nil { t.Fatal(err) } - assert.Equal(t, common.MapStr{ - "test": common.MapStr{ + assert.Equal(t, mapstr.M{ + "test": mapstr.M{ "field1": "field1", "field3": "field3", }, diff --git a/packetbeat/protos/amqp/amqp.go b/packetbeat/protos/amqp/amqp.go index 859e3bf84eb..a538f1f5698 100644 --- a/packetbeat/protos/amqp/amqp.go +++ b/packetbeat/protos/amqp/amqp.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" @@ -289,7 +290,7 @@ func (amqp *amqpPlugin) handleAmqpRequest(msg *amqpMessage) { if msg.fields != nil { trans.amqp = msg.fields } else { - trans.amqp = common.MapStr{} + trans.amqp = mapstr.M{} } // if error or exception, publish it now. sometimes client or server never send @@ -556,7 +557,7 @@ func isCloseError(t *amqpTransaction) bool { getReplyCode(t.amqp) >= 300 } -func getReplyCode(m common.MapStr) uint16 { +func getReplyCode(m mapstr.M) uint16 { code, _ := m["reply-code"].(uint16) return code } diff --git a/packetbeat/protos/amqp/amqp_fields.go b/packetbeat/protos/amqp/amqp_fields.go index d7536572978..70a7bae3164 100644 --- a/packetbeat/protos/amqp/amqp_fields.go +++ b/packetbeat/protos/amqp/amqp_fields.go @@ -24,13 +24,13 @@ import ( "strings" "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // getTable updates fields with the table data at the given offset. // fields must be non_nil on entry. -func getTable(fields common.MapStr, data []byte, offset uint32) (next uint32, err bool, exists bool) { +func getTable(fields mapstr.M, data []byte, offset uint32) (next uint32, err bool, exists bool) { length := binary.BigEndian.Uint32(data[offset : offset+4]) // size declared too big @@ -39,7 +39,7 @@ func getTable(fields common.MapStr, data []byte, offset uint32) (next uint32, er } if length > 0 { exists = true - table := common.MapStr{} + table := mapstr.M{} err := fieldUnmarshal(table, data[offset+4:offset+4+length], 0, length, -1) if err { logp.Warn("Error while parsing a field table") @@ -52,7 +52,7 @@ func getTable(fields common.MapStr, data []byte, offset uint32) (next uint32, er // getTable updates fields with the array data at the given offset. // fields must be non_nil on entry. -func getArray(fields common.MapStr, data []byte, offset uint32) (next uint32, err bool, exists bool) { +func getArray(fields mapstr.M, data []byte, offset uint32) (next uint32, err bool, exists bool) { length := binary.BigEndian.Uint32(data[offset : offset+4]) // size declared too big @@ -61,7 +61,7 @@ func getArray(fields common.MapStr, data []byte, offset uint32) (next uint32, er } if length > 0 { exists = true - array := common.MapStr{} + array := mapstr.M{} err := fieldUnmarshal(array, data[offset+4:offset+4+length], 0, length, 0) if err { logp.Warn("Error while parsing a field array") @@ -74,7 +74,7 @@ func getArray(fields common.MapStr, data []byte, offset uint32) (next uint32, er // The index parameter, when set at -1, indicates that the entry is a field table. // If it's set at 0, it is an array. -func fieldUnmarshal(table common.MapStr, data []byte, offset uint32, length uint32, index int) (err bool) { +func fieldUnmarshal(table mapstr.M, data []byte, offset uint32, length uint32, index int) (err bool) { var name string if offset >= length { @@ -166,7 +166,7 @@ func fieldUnmarshal(table common.MapStr, data []byte, offset uint32, length uint table[name] = s offset = next case fieldArray: - newMap := common.MapStr{} + newMap := mapstr.M{} next, err, _ := getArray(newMap, data, offset+1) if err { return true @@ -178,7 +178,7 @@ func fieldUnmarshal(table common.MapStr, data []byte, offset uint32, length uint table[name] = t.Format(amqpTimeLayout) offset += 9 case fieldTable: - newMap := common.MapStr{} + newMap := mapstr.M{} next, err, _ := getTable(newMap, data, offset+1) if err { return true diff --git a/packetbeat/protos/amqp/amqp_methods.go b/packetbeat/protos/amqp/amqp_methods.go index d74148026c5..867f7b9fcab 100644 --- a/packetbeat/protos/amqp/amqp_methods.go +++ b/packetbeat/protos/amqp/amqp_methods.go @@ -22,14 +22,14 @@ import ( "strconv" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func connectionStartMethod(m *amqpMessage, args []byte) (bool, bool) { major := args[0] minor := args[1] - properties := make(common.MapStr) + properties := make(mapstr.M) next, err, exists := getTable(properties, args, 2) if err { // failed to get de peer-properties, size may be wrong, let's quit @@ -48,7 +48,7 @@ func connectionStartMethod(m *amqpMessage, args []byte) (bool, bool) { } m.method = "connection.start" m.isRequest = true - m.fields = common.MapStr{ + m.fields = mapstr.M{ "version-major": major, "version-minor": minor, "mechanisms": mechanisms, @@ -62,7 +62,7 @@ func connectionStartMethod(m *amqpMessage, args []byte) (bool, bool) { } func connectionStartOkMethod(m *amqpMessage, args []byte) (bool, bool) { - properties := make(common.MapStr) + properties := make(mapstr.M) next, err, exists := getTable(properties, args, 0) if err { // failed to get de peer-properties, size may be wrong, let's quit @@ -85,7 +85,7 @@ func connectionStartOkMethod(m *amqpMessage, args []byte) (bool, bool) { return false, false } m.isRequest = false - m.fields = common.MapStr{ + m.fields = mapstr.M{ "mechanism": mechanism, "locale": locale, } @@ -105,7 +105,7 @@ func connectionTuneMethod(m *amqpMessage, args []byte) (bool, bool) { } func connectionTuneOkMethod(m *amqpMessage, args []byte) (bool, bool) { - m.fields = common.MapStr{ + m.fields = mapstr.M{ "channel-max": binary.BigEndian.Uint16(args[0:2]), "frame-max": binary.BigEndian.Uint32(args[2:6]), "heartbeat": binary.BigEndian.Uint16(args[6:8]), @@ -121,7 +121,7 @@ func connectionOpenMethod(m *amqpMessage, args []byte) (bool, bool) { logp.Warn("Failed to get virtual host from client") return false, false } - m.fields = common.MapStr{"virtual-host": host} + m.fields = mapstr.M{"virtual-host": host} return true, true } @@ -149,7 +149,7 @@ func channelFlowMethod(m *amqpMessage, args []byte) (bool, bool) { func channelFlowOkMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[0]) - m.fields = common.MapStr{"active": params[0]} + m.fields = mapstr.M{"active": params[0]} return true, true } @@ -172,7 +172,7 @@ func getCloseInfo(args []byte, m *amqpMessage) bool { logp.Warn("Failed to get error reply text") return true } - m.fields = common.MapStr{ + m.fields = mapstr.M{ "reply-code": code, "reply-text": replyText, "class-id": binary.BigEndian.Uint16(args[nextOffset : nextOffset+2]), @@ -191,7 +191,7 @@ func queueDeclareMethod(m *amqpMessage, args []byte) (bool, bool) { m.method = "queue.declare" params := getBitParams(args[offset]) m.request = name - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": name, "passive": params[0], "durable": params[1], @@ -200,7 +200,7 @@ func queueDeclareMethod(m *amqpMessage, args []byte) (bool, bool) { "no-wait": params[4], } if args[offset+1] != frameEndOctet && m.parseArguments { - arguments := make(common.MapStr) + arguments := make(mapstr.M) _, err, exists := getTable(arguments, args, offset+1) if !err && exists { m.fields["arguments"] = arguments @@ -218,7 +218,7 @@ func queueDeclareOkMethod(m *amqpMessage, args []byte) (bool, bool) { return false, false } m.method = "queue.declare-ok" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": name, "consumer-count": binary.BigEndian.Uint32(args[nextOffset+4:]), "message-count": binary.BigEndian.Uint32(args[nextOffset : nextOffset+4]), @@ -246,7 +246,7 @@ func queueBindMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[offset]) m.method = "queue.bind" m.request = strings.Join([]string{queue, exchange}, " ") - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": queue, "routing-key": routingKey, "no-wait": params[0], @@ -255,7 +255,7 @@ func queueBindMethod(m *amqpMessage, args []byte) (bool, bool) { m.fields["exchange"] = exchange } if args[offset+1] != frameEndOctet && m.parseArguments { - arguments := make(common.MapStr) + arguments := make(mapstr.M) _, err, exists := getTable(arguments, args, offset+1) if !err && exists { m.fields["arguments"] = arguments @@ -285,7 +285,7 @@ func queueUnbindMethod(m *amqpMessage, args []byte) (bool, bool) { m.isRequest = true m.method = "queue.unbind" m.request = strings.Join([]string{queue, exchange}, " ") - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": queue, "routing-key": routingKey, } @@ -293,7 +293,7 @@ func queueUnbindMethod(m *amqpMessage, args []byte) (bool, bool) { m.fields["exchange"] = exchange } if args[offset+1] != frameEndOctet && m.parseArguments { - arguments := make(common.MapStr) + arguments := make(mapstr.M) _, err, exists := getTable(arguments, args, offset+1) if !err && exists { m.fields["arguments"] = arguments @@ -314,7 +314,7 @@ func queuePurgeMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[nextOffset]) m.method = "queue.purge" m.request = queue - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": queue, "no-wait": params[0], } @@ -323,7 +323,7 @@ func queuePurgeMethod(m *amqpMessage, args []byte) (bool, bool) { func queuePurgeOkMethod(m *amqpMessage, args []byte) (bool, bool) { m.method = "queue.purge-ok" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "message-count": binary.BigEndian.Uint32(args[0:4]), } return true, true @@ -339,7 +339,7 @@ func queueDeleteMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[nextOffset]) m.method = "queue.delete" m.request = queue - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": queue, "if-unused": params[0], "if-empty": params[1], @@ -350,7 +350,7 @@ func queueDeleteMethod(m *amqpMessage, args []byte) (bool, bool) { func queueDeleteOkMethod(m *amqpMessage, args []byte) (bool, bool) { m.method = "queue.delete-ok" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "message-count": binary.BigEndian.Uint32(args[0:4]), } return true, true @@ -374,7 +374,7 @@ func exchangeDeclareMethod(m *amqpMessage, args []byte) (bool, bool) { if exchangeType == "" { exchangeType = "direct" } - m.fields = common.MapStr{ + m.fields = mapstr.M{ "exchange": exchange, "exchange-type": exchangeType, "passive": params[0], @@ -382,7 +382,7 @@ func exchangeDeclareMethod(m *amqpMessage, args []byte) (bool, bool) { "no-wait": params[4], } if args[offset+1] != frameEndOctet && m.parseArguments { - arguments := make(common.MapStr) + arguments := make(mapstr.M) _, err, exists := getTable(arguments, args, offset+1) if !err && exists { m.fields["arguments"] = arguments @@ -403,7 +403,7 @@ func exchangeDeleteMethod(m *amqpMessage, args []byte) (bool, bool) { m.isRequest = true params := getBitParams(args[nextOffset]) m.request = exchange - m.fields = common.MapStr{ + m.fields = mapstr.M{ "exchange": exchange, "if-unused": params[0], "no-wait": params[1], @@ -450,14 +450,14 @@ func exchangeBindUnbindInfo(m *amqpMessage, args []byte) bool { m.isRequest = true params := getBitParams(args[offset]) m.request = strings.Join([]string{source, destination}, " ") - m.fields = common.MapStr{ + m.fields = mapstr.M{ "destination": destination, "source": source, "routing-key": routingKey, "no-wait": params[0], } if args[offset+1] != frameEndOctet && m.parseArguments { - arguments := make(common.MapStr) + arguments := make(mapstr.M) _, err, exists := getTable(arguments, args, offset+1) if !err && exists { m.fields["arguments"] = arguments @@ -474,7 +474,7 @@ func basicQosMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[6]) m.isRequest = true m.method = "basic.qos" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "prefetch-size": prefetchSize, "prefetch-count": prefetchCount, "global": params[0], @@ -497,7 +497,7 @@ func basicConsumeMethod(m *amqpMessage, args []byte) (bool, bool) { m.method = "basic.consume" m.isRequest = true m.request = queue - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": queue, "consumer-tag": consumerTag, "no-local": params[0], @@ -506,7 +506,7 @@ func basicConsumeMethod(m *amqpMessage, args []byte) (bool, bool) { "no-wait": params[3], } if args[offset+1] != frameEndOctet && m.parseArguments { - arguments := make(common.MapStr) + arguments := make(mapstr.M) _, err, exists := getTable(arguments, args, offset+1) if !err && exists { m.fields["arguments"] = arguments @@ -524,7 +524,7 @@ func basicConsumeOkMethod(m *amqpMessage, args []byte) (bool, bool) { return false, false } m.method = "basic.consume-ok" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "consumer-tag": consumerTag, } return true, true @@ -540,7 +540,7 @@ func basicCancelMethod(m *amqpMessage, args []byte) (bool, bool) { m.isRequest = true m.request = consumerTag params := getBitParams(args[offset]) - m.fields = common.MapStr{ + m.fields = mapstr.M{ "consumer-tag": consumerTag, "no-wait": params[0], } @@ -554,7 +554,7 @@ func basicCancelOkMethod(m *amqpMessage, args []byte) (bool, bool) { return false, false } m.method = "basic.cancel-ok" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "consumer-tag": consumerTag, } return true, true @@ -573,7 +573,7 @@ func basicPublishMethod(m *amqpMessage, args []byte) (bool, bool) { } params := getBitParams(args[nextOffset]) m.method = "basic.publish" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "routing-key": routingKey, "mandatory": params[0], "immediate": params[1], @@ -607,7 +607,7 @@ func basicReturnMethod(m *amqpMessage, args []byte) (bool, bool) { return false, false } m.method = "basic.return" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "exchange": exchange, "routing-key": routingKey, "reply-code": code, @@ -636,7 +636,7 @@ func basicDeliverMethod(m *amqpMessage, args []byte) (bool, bool) { return false, false } m.method = "basic.deliver" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "consumer-tag": consumerTag, "delivery-tag": deliveryTag, "redelivered": params[0], @@ -659,7 +659,7 @@ func basicGetMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[offset]) m.isRequest = true m.request = queue - m.fields = common.MapStr{ + m.fields = mapstr.M{ "queue": queue, "no-ack": params[0], } @@ -679,7 +679,7 @@ func basicGetOkMethod(m *amqpMessage, args []byte) (bool, bool) { return false, false } m.method = "basic.get-ok" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "delivery-tag": binary.BigEndian.Uint64(args[0:8]), "redelivered": params[0], "routing-key": routingKey, @@ -700,7 +700,7 @@ func basicAckMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[8]) m.method = "basic.ack" m.isRequest = true - m.fields = common.MapStr{ + m.fields = mapstr.M{ "delivery-tag": binary.BigEndian.Uint64(args[0:8]), "multiple": params[0], } @@ -712,7 +712,7 @@ func basicNackMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[8]) m.method = "basic.nack" m.isRequest = true - m.fields = common.MapStr{ + m.fields = mapstr.M{ "delivery-tag": binary.BigEndian.Uint64(args[0:8]), "multiple": params[0], "requeue": params[1], @@ -725,7 +725,7 @@ func basicRejectMethod(m *amqpMessage, args []byte) (bool, bool) { tag := binary.BigEndian.Uint64(args[0:8]) m.isRequest = true m.method = "basic.reject" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "delivery-tag": tag, "multiple": params[0], } @@ -737,7 +737,7 @@ func basicRecoverMethod(m *amqpMessage, args []byte) (bool, bool) { params := getBitParams(args[0]) m.isRequest = true m.method = "basic.recover" - m.fields = common.MapStr{ + m.fields = mapstr.M{ "requeue": params[0], } return true, true diff --git a/packetbeat/protos/amqp/amqp_parser.go b/packetbeat/protos/amqp/amqp_parser.go index a623219ca0b..2e3a63989ce 100644 --- a/packetbeat/protos/amqp/amqp_parser.go +++ b/packetbeat/protos/amqp/amqp_parser.go @@ -23,6 +23,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func (amqp *amqpPlugin) amqpMessageParser(s *amqpStream) (ok bool, complete bool) { @@ -223,7 +224,7 @@ func getMessageProperties(s *amqpStream, data []byte) bool { } if hasProperty(prop1, headersProp) { - headers := common.MapStr{} + headers := mapstr.M{} next, err, exists := getTable(headers, data, offset) if !err && exists { m.fields["headers"] = headers diff --git a/packetbeat/protos/amqp/amqp_structs.go b/packetbeat/protos/amqp/amqp_structs.go index ada8441c45a..7abba4fc914 100644 --- a/packetbeat/protos/amqp/amqp_structs.go +++ b/packetbeat/protos/amqp/amqp_structs.go @@ -21,6 +21,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type amqpMethod func(*amqpMessage, []byte) (bool, bool) @@ -204,7 +205,7 @@ type amqpMessage struct { parseArguments bool // mapstr containing all the options for the methods and header fields - fields common.MapStr + fields mapstr.M body []byte bodySize uint64 @@ -236,7 +237,7 @@ type amqpTransaction struct { toString bool notes []string - amqp common.MapStr + amqp mapstr.M timer *time.Timer } diff --git a/packetbeat/protos/amqp/amqp_test.go b/packetbeat/protos/amqp/amqp_test.go index 9b7f7dca35c..726b3d10997 100644 --- a/packetbeat/protos/amqp/amqp_test.go +++ b/packetbeat/protos/amqp/amqp_test.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" @@ -62,7 +63,7 @@ func testTCPTuple() *common.TCPTuple { return t } -func expectTransaction(t *testing.T, e *eventStore) common.MapStr { +func expectTransaction(t *testing.T, e *eventStore) mapstr.M { if len(e.events) == 0 { t.Error("No transaction") return nil @@ -176,7 +177,7 @@ func TestAmqp_QueueDeclaration(t *testing.T) { assert.Equal(t, false, m.fields["exclusive"]) assert.Equal(t, true, m.fields["auto-delete"]) assert.Equal(t, true, m.fields["no-wait"]) - _, exists := m.fields["arguments"].(common.MapStr) + _, exists := m.fields["arguments"].(mapstr.M) if exists { t.Errorf("Arguments field should not be present") } @@ -209,7 +210,7 @@ func TestAmqp_ExchangeDeclaration(t *testing.T) { assert.Equal(t, false, m.fields["passive"]) assert.Equal(t, false, m.fields["no-wait"]) assert.Equal(t, "topic", m.fields["exchange-type"]) - _, exists := m.fields["arguments"].(common.MapStr) + _, exists := m.fields["arguments"].(mapstr.M) if exists { t.Errorf("Arguments field should not be present") } @@ -242,7 +243,7 @@ func TestAmqp_BasicConsume(t *testing.T) { assert.Equal(t, false, m.fields["exclusive"]) assert.Equal(t, true, m.fields["no-local"]) assert.Equal(t, false, m.fields["no-wait"]) - _, exists := m.fields["arguments"].(common.MapStr) + _, exists := m.fields["arguments"].(mapstr.M) if exists { t.Errorf("Arguments field should not be present") } @@ -302,7 +303,7 @@ func TestAmqp_ExchangeBind(t *testing.T) { assert.Equal(t, "MSFT", m.fields["routing-key"]) assert.Equal(t, "test2 test1", m.request) assert.Equal(t, false, m.fields["no-wait"]) - _, exists := m.fields["arguments"].(common.MapStr) + _, exists := m.fields["arguments"].(mapstr.M) if exists { t.Errorf("Arguments field should not be present") } @@ -334,7 +335,7 @@ func TestAmqp_ExchangeUnbindTransaction(t *testing.T) { assert.Equal(t, "exchange.unbind test2 test1", trans["request"]) assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, common.OK_STATUS, trans["status"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -382,7 +383,7 @@ func TestAmqp_PublishMessage(t *testing.T) { assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, body, trans["request"]) assert.Equal(t, common.OK_STATUS, trans["status"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -429,7 +430,7 @@ func TestAmqp_DeliverMessage(t *testing.T) { assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, "kikoo", trans["response"]) assert.Equal(t, common.OK_STATUS, trans["status"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -474,7 +475,7 @@ func TestAmqp_MessagePropertiesFields(t *testing.T) { } else if ok && priority != 6 { t.Errorf("Wrong argument") } - headers, ok := m.fields["headers"].(common.MapStr) + headers, ok := m.fields["headers"].(mapstr.M) if !ok { t.Errorf("Headers should be present") } @@ -552,7 +553,7 @@ func TestAmqp_NoWaitQueueDeleteMethod(t *testing.T) { assert.Equal(t, "queue.delete", trans["method"]) assert.Equal(t, "queue.delete TestThomas", trans["request"]) assert.Equal(t, "amqp", trans["type"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -585,7 +586,7 @@ func TestAmqp_RejectMessage(t *testing.T) { assert.Equal(t, "basic.reject 1", trans["request"]) assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, common.ERROR_STATUS, trans["status"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -677,7 +678,7 @@ func TestAmqp_MaxBodyLength(t *testing.T) { assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, "I'm a very [...]", trans["request"]) assert.Equal(t, common.OK_STATUS, trans["status"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -708,7 +709,7 @@ func TestAmqp_MaxBodyLength(t *testing.T) { assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, "65 65 65 65 65 65 65 65 65 65 [...]", trans["request"]) assert.Equal(t, common.OK_STATUS, trans["status"]) - fields, ok = trans["amqp"].(common.MapStr) + fields, ok = trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -742,13 +743,13 @@ func TestAmqp_HideArguments(t *testing.T) { assert.Equal(t, "queue.declare", trans["method"]) assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, "queue.declare TestHeader", trans["request"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } assert.Equal(t, false, fields["durable"]) assert.Equal(t, true, fields["auto-delete"]) - _, exists := fields["arguments"].(common.MapStr) + _, exists := fields["arguments"].(mapstr.M) if exists { t.Errorf("Arguments field should not be present") } @@ -766,7 +767,7 @@ func TestAmqp_HideArguments(t *testing.T) { trans = expectTransaction(t, results) assert.Equal(t, "basic.publish", trans["method"]) assert.Equal(t, "amqp", trans["type"]) - fields, ok = trans["amqp"].(common.MapStr) + fields, ok = trans["amqp"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -805,7 +806,7 @@ func TestAmqp_RecoverMethod(t *testing.T) { assert.Equal(t, "basic.recover", trans["request"]) assert.Equal(t, "amqp", trans["type"]) assert.Equal(t, common.OK_STATUS, trans["status"]) - assert.Equal(t, common.MapStr{"requeue": true}, trans["amqp"]) + assert.Equal(t, mapstr.M{"requeue": true}, trans["amqp"]) } // this is a specific rabbitMQ method @@ -854,7 +855,7 @@ func TestAmqp_GetTable(t *testing.T) { if !complete { t.Errorf("Message should be complete") } - args, ok := m.fields["arguments"].(common.MapStr) + args, ok := m.fields["arguments"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -916,7 +917,7 @@ func TestAmqp_TableInception(t *testing.T) { assert.Equal(t, "exchange.declare", m.method) assert.Equal(t, "test1", m.fields["exchange"]) - args, ok := m.fields["arguments"].(common.MapStr) + args, ok := m.fields["arguments"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -928,14 +929,14 @@ func TestAmqp_TableInception(t *testing.T) { } else if ok && bigInt != 2000000000000000 { t.Errorf("Wrong argument") } - inception, ok := args["inception"].(common.MapStr) + inception, ok := args["inception"].(mapstr.M) if !ok { t.Errorf("Field should be present") } assert.Equal(t, "DREAMS", inception["incep1"]) assert.Equal(t, "MARION", inception["incep2"]) - limbo, ok := inception["limbo"].(common.MapStr) + limbo, ok := inception["limbo"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -963,7 +964,7 @@ func TestAmqp_ArrayFields(t *testing.T) { if !complete { t.Errorf("Message should be complete") } - args, ok := m.fields["arguments"].(common.MapStr) + args, ok := m.fields["arguments"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -987,13 +988,13 @@ func TestAmqp_ArrayFields(t *testing.T) { if !complete { t.Errorf("Message should be complete") } - args, ok = m.fields["arguments"].(common.MapStr) + args, ok = m.fields["arguments"].(mapstr.M) if !ok { t.Errorf("Field should be present") } assert.Equal(t, "a lot of arrays!", args["test"]) - arrayFloat, ok := args["arrayfloat"].(common.MapStr) + arrayFloat, ok := args["arrayfloat"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -1001,7 +1002,7 @@ func TestAmqp_ArrayFields(t *testing.T) { assert.Equal(t, 28.8, arrayFloat["1"]) assert.Equal(t, 33.3, arrayFloat["2"]) - arrayBool, ok := args["arraybool"].(common.MapStr) + arrayBool, ok := args["arraybool"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -1011,7 +1012,7 @@ func TestAmqp_ArrayFields(t *testing.T) { assert.Equal(t, true, arrayBool["3"]) assert.Equal(t, true, arrayBool["4"]) - arrayString, ok := args["arraystring"].(common.MapStr) + arrayString, ok := args["arraystring"].(mapstr.M) if !ok { t.Errorf("Field should be present") } @@ -1042,7 +1043,7 @@ func TestAmqp_WrongTable(t *testing.T) { if !complete { t.Errorf("Message should be complete") } - _, exists := m.fields["arguments"].(common.MapStr) + _, exists := m.fields["arguments"].(mapstr.M) if exists { t.Errorf("Field should not exist") } @@ -1065,7 +1066,7 @@ func TestAmqp_WrongTable(t *testing.T) { if !complete { t.Errorf("Message should be complete") } - _, exists = m.fields["arguments"].(common.MapStr) + _, exists = m.fields["arguments"].(mapstr.M) if exists { t.Errorf("Field should not exist") } @@ -1075,7 +1076,7 @@ func TestAmqp_WrongTable(t *testing.T) { func TestAmqp_isError(t *testing.T) { trans := &amqpTransaction{ method: "channel.close", - amqp: common.MapStr{ + amqp: mapstr.M{ "reply-code": 200, }, } @@ -1146,7 +1147,7 @@ func TestAmqp_ConnectionCloseNoError(t *testing.T) { assert.Equal(t, common.OK_STATUS, trans["status"]) assert.Nil(t, trans["notes"]) - fields, ok := trans["amqp"].(common.MapStr) + fields, ok := trans["amqp"].(mapstr.M) assert.True(t, ok) code, ok := fields["reply-code"].(uint16) assert.True(t, ok) diff --git a/packetbeat/protos/cassandra/pub.go b/packetbeat/protos/cassandra/pub.go index 82effab92d3..94f7b9fb101 100644 --- a/packetbeat/protos/cassandra/pub.go +++ b/packetbeat/protos/cassandra/pub.go @@ -22,6 +22,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/protos" @@ -78,7 +79,7 @@ func (pub *transPub) createEvent(requ, resp *message) beat.Event { fields := evt.Fields fields["type"] = pbf.Event.Dataset - cassandra := common.MapStr{} + cassandra := mapstr.M{} status := common.OK_STATUS // requ can be null, if the message is a PUSHed message @@ -90,7 +91,7 @@ func (pub *transPub) createEvent(requ, resp *message) beat.Event { if pub.sendRequest { if pub.sendRequestHeader { if requ.data == nil { - requ.data = common.MapStr{} + requ.data = mapstr.M{} } requ.data["headers"] = requ.header } @@ -116,7 +117,7 @@ func (pub *transPub) createEvent(requ, resp *message) beat.Event { if pub.sendResponse { if pub.sendResponseHeader { if resp.data == nil { - resp.data = common.MapStr{} + resp.data = mapstr.M{} } resp.data["headers"] = resp.header } diff --git a/packetbeat/protos/dhcpv4/dhcpv4.go b/packetbeat/protos/dhcpv4/dhcpv4.go index 8041b73a275..f1b2b82be50 100644 --- a/packetbeat/protos/dhcpv4/dhcpv4.go +++ b/packetbeat/protos/dhcpv4/dhcpv4.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -124,7 +125,7 @@ func (p *dhcpv4Plugin) parseDHCPv4(pkt *protos.Packet) *beat.Event { fields["type"] = pbf.Event.Dataset fields["status"] = "OK" - dhcpData := common.MapStr{ + dhcpData := mapstr.M{ "op_code": strings.ToLower(v4.OpcodeToString()), "hardware_type": v4.HwTypeToString(), "hops": v4.HopCount(), // Set to non-zero by relays. diff --git a/packetbeat/protos/dhcpv4/dhcpv4_test.go b/packetbeat/protos/dhcpv4/dhcpv4_test.go index 0b07ab88ea2..14cb63f85b1 100644 --- a/packetbeat/protos/dhcpv4/dhcpv4_test.go +++ b/packetbeat/protos/dhcpv4/dhcpv4_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" "github.com/elastic/beats/v7/packetbeat/publish" + "github.com/elastic/elastic-agent-libs/mapstr" ) var _ protos.UDPPlugin = &dhcpv4Plugin{} @@ -96,35 +97,35 @@ func TestParseDHCPRequest(t *testing.T) { expected := beat.Event{ Timestamp: pkt.Ts, - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "dhcpv4", "status": "OK", - "source": common.MapStr{ + "source": mapstr.M{ "ip": "0.0.0.0", "port": 68, "bytes": 272, }, - "destination": common.MapStr{ + "destination": mapstr.M{ "ip": "255.255.255.255", "port": 67, }, - "client": common.MapStr{ + "client": mapstr.M{ "ip": "0.0.0.0", "port": 68, "bytes": 272, }, - "server": common.MapStr{ + "server": mapstr.M{ "ip": "255.255.255.255", "port": 67, }, - "event": common.MapStr{ + "event": mapstr.M{ "category": []string{"network"}, "type": []string{"connection", "protocol"}, "dataset": "dhcpv4", "kind": "event", "start": pkt.Ts, }, - "network": common.MapStr{ + "network": mapstr.M{ "type": "ipv4", "direction": "unknown", "transport": "udp", @@ -132,10 +133,10 @@ func TestParseDHCPRequest(t *testing.T) { "bytes": 272, "community_id": "1:t9O1j0qj71O4wJM7gnaHtgmfev8=", }, - "related": common.MapStr{ + "related": mapstr.M{ "ip": []string{"0.0.0.0", "255.255.255.255"}, }, - "dhcpv4": common.MapStr{ + "dhcpv4": mapstr.M{ "client_mac": "00:0b:82:01:fc:42", "flags": "unicast", "hardware_type": "Ethernet", @@ -143,7 +144,7 @@ func TestParseDHCPRequest(t *testing.T) { "op_code": "bootrequest", "seconds": 0, "transaction_id": "0x00003d1e", - "option": common.MapStr{ + "option": mapstr.M{ "message_type": "request", "parameter_request_list": []string{ "Subnet Mask", @@ -181,35 +182,35 @@ func TestParseDHCPACK(t *testing.T) { expected := beat.Event{ Timestamp: pkt.Ts, - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "dhcpv4", "status": "OK", - "source": common.MapStr{ + "source": mapstr.M{ "ip": "192.168.0.1", "port": 67, "bytes": 300, }, - "destination": common.MapStr{ + "destination": mapstr.M{ "ip": "192.168.0.10", "port": 68, }, - "client": common.MapStr{ + "client": mapstr.M{ "ip": "192.168.0.10", "port": 68, }, - "server": common.MapStr{ + "server": mapstr.M{ "ip": "192.168.0.1", "port": 67, "bytes": 300, }, - "event": common.MapStr{ + "event": mapstr.M{ "category": []string{"network"}, "type": []string{"connection", "protocol"}, "dataset": "dhcpv4", "kind": "event", "start": pkt.Ts, }, - "network": common.MapStr{ + "network": mapstr.M{ "type": "ipv4", "direction": "unknown", "transport": "udp", @@ -217,10 +218,10 @@ func TestParseDHCPACK(t *testing.T) { "bytes": 300, "community_id": "1:VbRSZnvQqvLiQRhYHLrdVI17sLQ=", }, - "related": common.MapStr{ + "related": mapstr.M{ "ip": []string{"192.168.0.1", "192.168.0.10"}, }, - "dhcpv4": common.MapStr{ + "dhcpv4": mapstr.M{ "assigned_ip": "192.168.0.10", "client_mac": "00:0b:82:01:fc:42", "flags": "unicast", @@ -229,7 +230,7 @@ func TestParseDHCPACK(t *testing.T) { "op_code": "bootreply", "seconds": 0, "transaction_id": "0x00003d1e", - "option": common.MapStr{ + "option": mapstr.M{ "ip_address_lease_time_sec": 3600, "message_type": "ack", "rebinding_time_sec": 3150, diff --git a/packetbeat/protos/dhcpv4/options.go b/packetbeat/protos/dhcpv4/options.go index 68ec4e8c32c..2715696343e 100644 --- a/packetbeat/protos/dhcpv4/options.go +++ b/packetbeat/protos/dhcpv4/options.go @@ -26,11 +26,11 @@ import ( "github.com/insomniacslk/dhcp/dhcpv4" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func optionsToMap(options []dhcpv4.Option) (common.MapStr, error) { - opts := common.MapStr{} +func optionsToMap(options []dhcpv4.Option) (mapstr.M, error) { + opts := mapstr.M{} for _, opt := range options { if opt.Code() == dhcpv4.OptionEnd { @@ -82,9 +82,9 @@ func optionsToMap(options []dhcpv4.Option) (common.MapStr, error) { opts.Put("dns_servers", dnsServers) case *dhcpv4.OptVIVC: - var subOptions []common.MapStr + var subOptions []mapstr.M for _, vendorOpt := range v.Identifiers { - subOptions = append(subOptions, common.MapStr{ + subOptions = append(subOptions, mapstr.M{ "id": vendorOpt.EntID, "data": hex.EncodeToString(vendorOpt.Data), }) diff --git a/packetbeat/protos/dns/dns.go b/packetbeat/protos/dns/dns.go index 04be88a36b1..da78d7c432b 100644 --- a/packetbeat/protos/dns/dns.go +++ b/packetbeat/protos/dns/dns.go @@ -40,6 +40,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" + "github.com/elastic/elastic-agent-libs/mapstr" ) type dnsPlugin struct { @@ -371,7 +372,7 @@ func (dns *dnsPlugin) publishTransaction(t *dnsTransaction) { fields["type"] = "dns" fields["status"] = common.ERROR_STATUS - dnsEvent := common.MapStr{} + dnsEvent := mapstr.M{} fields["dns"] = dnsEvent if t.request != nil && t.response != nil { @@ -443,11 +444,11 @@ func (dns *dnsPlugin) expireTransaction(t *dnsTransaction) { } // Adds the DNS message data to the supplied MapStr. -func addDNSToMapStr(m common.MapStr, pbf *pb.Fields, dns *mkdns.Msg, authority bool, additional bool) { +func addDNSToMapStr(m mapstr.M, pbf *pb.Fields, dns *mkdns.Msg, authority bool, additional bool) { m["id"] = dns.Id m["op_code"] = dnsOpCodeToString(dns.Opcode) - m["flags"] = common.MapStr{ + m["flags"] = mapstr.M{ "authoritative": dns.Authoritative, "truncated_response": dns.Truncated, "recursion_desired": dns.RecursionDesired, @@ -484,7 +485,7 @@ func addDNSToMapStr(m common.MapStr, pbf *pb.Fields, dns *mkdns.Msg, authority b if len(dns.Question) > 0 { q := dns.Question[0] - qMapStr := common.MapStr{ + qMapStr := mapstr.M{ "name": q.Name, "type": dnsTypeToString(q.Qtype), "class": dnsClassToString(q.Qclass), @@ -552,8 +553,8 @@ func addDNSToMapStr(m common.MapStr, pbf *pb.Fields, dns *mkdns.Msg, authority b } } -func optToMapStr(rrOPT *mkdns.OPT) common.MapStr { - optMapStr := common.MapStr{ +func optToMapStr(rrOPT *mkdns.OPT) mapstr.M { + optMapStr := mapstr.M{ "do": rrOPT.Do(), // true if DNSSEC "version": strconv.FormatUint(uint64(rrOPT.Version()), 10), "udp_size": rrOPT.UDPSize(), @@ -588,9 +589,9 @@ func optToMapStr(rrOPT *mkdns.OPT) common.MapStr { // rrsToMapStr converts an slice of RR's to an slice of MapStr's and optionally // returns a list of the IP addresses found in the resource records. -func rrsToMapStrs(records []mkdns.RR, ipList bool) ([]common.MapStr, []string) { +func rrsToMapStrs(records []mkdns.RR, ipList bool) ([]mapstr.M, []string) { var allIPs []string - mapStrSlice := make([]common.MapStr, 0, len(records)) + mapStrSlice := make([]mapstr.M, 0, len(records)) for _, rr := range records { rrHeader := rr.Header() @@ -654,8 +655,8 @@ func rrToString(rr mkdns.RR) string { return b.String() } -func rrToMapStr(rr mkdns.RR, ipList bool) (common.MapStr, []string) { - mapStr := common.MapStr{} +func rrToMapStr(rr mkdns.RR, ipList bool) (mapstr.M, []string) { + mapStr := mapstr.M{} rrType := rr.Header().Rrtype var ips []string diff --git a/packetbeat/protos/dns/dns_test.go b/packetbeat/protos/dns/dns_test.go index 1ba03b2bd61..57f604fd3db 100644 --- a/packetbeat/protos/dns/dns_test.go +++ b/packetbeat/protos/dns/dns_test.go @@ -38,6 +38,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" "github.com/elastic/beats/v7/packetbeat/publish" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Test Constants @@ -131,7 +132,7 @@ func newPacket(t common.IPPortTuple, payload []byte) *protos.Packet { // expectResult returns one MapStr result from the Dns results channel. If // no result is available then the test fails. -func expectResult(t testing.TB, e *eventStore) common.MapStr { +func expectResult(t testing.TB, e *eventStore) mapstr.M { if len(e.events) == 0 { t.Error("No transaction") return nil @@ -143,13 +144,13 @@ func expectResult(t testing.TB, e *eventStore) common.MapStr { } // Retrieves a map value. The key should be the full dotted path to the element. -func mapValue(t testing.TB, m common.MapStr, key string) interface{} { +func mapValue(t testing.TB, m mapstr.M, key string) interface{} { t.Helper() return mapValueHelper(t, m, strings.Split(key, ".")) } // Retrieves nested MapStr values. -func mapValueHelper(t testing.TB, m common.MapStr, keys []string) interface{} { +func mapValueHelper(t testing.TB, m mapstr.M, keys []string) interface{} { t.Helper() key := keys[0] @@ -166,9 +167,9 @@ func mapValueHelper(t testing.TB, m common.MapStr, keys []string) interface{} { switch typ := value.(type) { default: t.Fatalf("Expected %s to return a MapStr but got %v.", key, value) - case common.MapStr: + case mapstr.M: return mapValueHelper(t, typ, keys[1:]) - case []common.MapStr: + case []mapstr.M: var values []interface{} for _, m := range typ { values = append(values, mapValueHelper(t, m, keys[1:])) @@ -203,7 +204,7 @@ func mapValueHelper(t testing.TB, m common.MapStr, keys []string) interface{} { // dns.authorities // dns.additionals_count // dns.additionals -func assertMapStrData(t testing.TB, m common.MapStr, q dnsTestMessage) { +func assertMapStrData(t testing.TB, m mapstr.M, q dnsTestMessage) { t.Helper() assertRequest(t, m, q) @@ -257,7 +258,7 @@ func assertMapStrData(t testing.TB, m common.MapStr, q dnsTestMessage) { } } -func assertRequest(t testing.TB, m common.MapStr, q dnsTestMessage) { +func assertRequest(t testing.TB, m mapstr.M, q dnsTestMessage) { t.Helper() assert.Equal(t, "dns", mapValue(t, m, "type")) @@ -280,7 +281,7 @@ func assertRequest(t testing.TB, m common.MapStr, q dnsTestMessage) { } // Assert that the specified flags are set. -func assertFlags(t testing.TB, m common.MapStr, flags []string) { +func assertFlags(t testing.TB, m mapstr.M, flags []string) { for _, expected := range flags { var key string switch expected { diff --git a/packetbeat/protos/dns/names_test.go b/packetbeat/protos/dns/names_test.go index e241d1294ce..3e83066e72d 100644 --- a/packetbeat/protos/dns/names_test.go +++ b/packetbeat/protos/dns/names_test.go @@ -32,17 +32,17 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/packetbeat/pb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type dnsTestMsg struct { rawData []byte - question common.MapStr - answers []common.MapStr - authorities []common.MapStr - additionals []common.MapStr - opt common.MapStr + question mapstr.M + answers []mapstr.M + authorities []mapstr.M + additionals []mapstr.M + opt mapstr.M } // DNS messages for testing. @@ -59,7 +59,7 @@ var ( 0x21, 0x51, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x65, 0x6c, 0x61, 0x73, 0x74, 0x69, 0x63, 0x02, 0x63, 0x6f, 0x00, 0x00, 0x1e, 0x00, 0x01, }, - question: common.MapStr{ + question: mapstr.M{ "type": "NXT", "name": "elastic.co", }, @@ -70,7 +70,7 @@ var ( 0x21, 0x51, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x65, 0x6c, 0x61, 0x73, 0x74, 0x69, 0x63, 0x02, 0x63, 0x6f, 0x00, 0xff, 0x00, 0x00, 0x01, }, - question: common.MapStr{ + question: mapstr.M{ "type": "65280", "name": "elastic.co", }, @@ -82,11 +82,11 @@ var ( 0x04, 0x69, 0x65, 0x74, 0x66, 0x03, 0x6f, 0x72, 0x67, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x29, 0x10, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, }, - question: common.MapStr{ + question: mapstr.M{ "type": "A", "name": "www.ietf.org", }, - opt: common.MapStr{ + opt: mapstr.M{ "version": "0", "do": true, }, @@ -94,7 +94,7 @@ var ( ) // oracleRRs and rrs should be sorted in the same order -func assertRRs(t testing.TB, oracleRRs []common.MapStr, rrs []common.MapStr) { +func assertRRs(t testing.TB, oracleRRs []mapstr.M, rrs []mapstr.M) { assert.Equal(t, len(oracleRRs), len(rrs)) for i, oracleRR := range oracleRRs { rr := rrs[i] @@ -111,27 +111,27 @@ func assertDNSMessage(t testing.TB, q dnsTestMsg) { t.Error("failed to decode dns data") } - mapStr := common.MapStr{} + mapStr := mapstr.M{} addDNSToMapStr(mapStr, pb.NewFields(), dns, true, true) if q.question != nil { for k, v := range q.question { - assert.NotNil(t, mapStr["question"].(common.MapStr)[k]) - assert.Equal(t, v, mapStr["question"].(common.MapStr)[k]) + assert.NotNil(t, mapStr["question"].(mapstr.M)[k]) + assert.Equal(t, v, mapStr["question"].(mapstr.M)[k]) } } if len(q.answers) > 0 { - assertRRs(t, q.answers, mapStr["answer"].([]common.MapStr)) + assertRRs(t, q.answers, mapStr["answer"].([]mapstr.M)) } if len(q.authorities) > 0 { - assertRRs(t, q.authorities, mapStr["authorities"].([]common.MapStr)) + assertRRs(t, q.authorities, mapStr["authorities"].([]mapstr.M)) } if len(q.additionals) > 0 { - assertRRs(t, q.additionals, mapStr["additionals"].([]common.MapStr)) + assertRRs(t, q.additionals, mapStr["additionals"].([]mapstr.M)) } if q.opt != nil { for k, v := range q.opt { - assert.NotNil(t, mapStr["opt"].(common.MapStr)[k]) - assert.Equal(t, v, mapStr["opt"].(common.MapStr)[k]) + assert.NotNil(t, mapStr["opt"].(mapstr.M)[k]) + assert.Equal(t, v, mapStr["opt"].(mapstr.M)[k]) } } } diff --git a/packetbeat/protos/http/event.go b/packetbeat/protos/http/event.go index abad8d4357b..92d4935b121 100644 --- a/packetbeat/protos/http/event.go +++ b/packetbeat/protos/http/event.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ProtocolFields contains HTTP fields. This contains all the HTTP fields from @@ -70,10 +71,10 @@ type ProtocolFields struct { ResponseBodyBytes int64 `ecs:"response.body.bytes"` // HTTP request headers. - RequestHeaders common.MapStr `packetbeat:"request.headers"` + RequestHeaders mapstr.M `packetbeat:"request.headers"` // HTTP response headers. - ResponseHeaders common.MapStr `packetbeat:"response.headers"` + ResponseHeaders mapstr.M `packetbeat:"response.headers"` // HTTP response mime-type. ResponseMIMEType string `ecs:"response.mime_type"` diff --git a/packetbeat/protos/http/http.go b/packetbeat/protos/http/http.go index 9cc436f84f0..4b62edb36f1 100644 --- a/packetbeat/protos/http/http.go +++ b/packetbeat/protos/http/http.go @@ -37,6 +37,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -653,7 +654,7 @@ func (http *httpPlugin) publishTransaction(event beat.Event) { http.results(event) } -func (http *httpPlugin) collectHeaders(m *message) common.MapStr { +func (http *httpPlugin) collectHeaders(m *message) mapstr.M { hdrs := map[string]interface{}{} hdrs["content-length"] = m.contentLength diff --git a/packetbeat/protos/http/http_test.go b/packetbeat/protos/http/http_test.go index 4179a047d56..a4c8f3feafc 100644 --- a/packetbeat/protos/http/http_test.go +++ b/packetbeat/protos/http/http_test.go @@ -37,6 +37,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" "github.com/elastic/beats/v7/packetbeat/publish" + "github.com/elastic/elastic-agent-libs/mapstr" ) type testParser struct { @@ -1331,7 +1332,7 @@ func testCreateTCPTuple() *common.TCPTuple { } // Helper function to read from the Publisher Queue -func expectTransaction(t *testing.T, e *eventStore) common.MapStr { +func expectTransaction(t *testing.T, e *eventStore) mapstr.M { if len(e.events) == 0 { t.Error("No transaction") return nil @@ -1740,12 +1741,12 @@ func TestHttpParser_hostHeader(t *testing.T) { for _, test := range []struct { title, host string port uint16 - expected common.MapStr + expected mapstr.M }{ { title: "domain alone", host: "elasticsearch", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": "elasticsearch", "url.full": "http://elasticsearch/_cat/shards", }, @@ -1754,7 +1755,7 @@ func TestHttpParser_hostHeader(t *testing.T) { title: "domain with port", port: 9200, host: "elasticsearch:9200", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": "elasticsearch", "url.full": "http://elasticsearch:9200/_cat/shards", }, @@ -1762,7 +1763,7 @@ func TestHttpParser_hostHeader(t *testing.T) { { title: "ipv4", host: "127.0.0.1", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": nil, "url.full": "http://127.0.0.1/_cat/shards", }, @@ -1771,7 +1772,7 @@ func TestHttpParser_hostHeader(t *testing.T) { title: "ipv4 with port", port: 9200, host: "127.0.0.1:9200", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": nil, "url.full": "http://127.0.0.1:9200/_cat/shards", }, @@ -1779,7 +1780,7 @@ func TestHttpParser_hostHeader(t *testing.T) { { title: "ipv6 unboxed", host: "fd00::42", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": nil, "url.full": "http://[fd00::42]/_cat/shards", }, @@ -1787,7 +1788,7 @@ func TestHttpParser_hostHeader(t *testing.T) { { title: "ipv6 boxed", host: "[fd00::42]", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": nil, "url.full": "http://[fd00::42]/_cat/shards", }, @@ -1796,7 +1797,7 @@ func TestHttpParser_hostHeader(t *testing.T) { title: "ipv6 boxed with port", port: 9200, host: "[::1]:9200", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": nil, "url.full": "http://[::1]:9200/_cat/shards", }, @@ -1806,7 +1807,7 @@ func TestHttpParser_hostHeader(t *testing.T) { // This one is now illegal but it seems at some point the RFC // didn't enforce the brackets when the port was omitted. host: "fd00::1234", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": nil, "url.full": "http://[fd00::1234]/_cat/shards", }, @@ -1815,7 +1816,7 @@ func TestHttpParser_hostHeader(t *testing.T) { title: "non-matching port", port: 80, host: "myhost:9200", - expected: common.MapStr{ + expected: mapstr.M{ "destination.domain": "myhost", "url.full": "http://myhost:9200/_cat/shards", "error.message": []string{"Unmatched request", "Host header port number mismatch"}, @@ -1842,7 +1843,7 @@ func TestHttpParser_hostHeader(t *testing.T) { if expected != nil { assert.Nil(t, err, field) } else { - assert.Equal(t, common.ErrKeyNotFound, err, field) + assert.Equal(t, mapstr.ErrKeyNotFound, err, field) } } }) @@ -1857,12 +1858,12 @@ func TestHttpParser_Extension(t *testing.T) { http := httpModForTests(&store) for _, test := range []struct { title, path string - expected common.MapStr + expected mapstr.M }{ { title: "Zip Extension", path: "/files.zip", - expected: common.MapStr{ + expected: mapstr.M{ "url.full": "http://abc.com/files.zip", "url.extension": "zip", }, @@ -1870,7 +1871,7 @@ func TestHttpParser_Extension(t *testing.T) { { title: "No Extension", path: "/files", - expected: common.MapStr{ + expected: mapstr.M{ "url.full": "http://abc.com/files", "url.extension": nil, }, @@ -1893,7 +1894,7 @@ func TestHttpParser_Extension(t *testing.T) { if expected != nil { assert.Nil(t, err, field) } else { - assert.Equal(t, common.ErrKeyNotFound, err, field) + assert.Equal(t, mapstr.ErrKeyNotFound, err, field) } } }) diff --git a/packetbeat/protos/icmp/icmp.go b/packetbeat/protos/icmp/icmp.go index d6804322c43..a5f106809a3 100644 --- a/packetbeat/protos/icmp/icmp.go +++ b/packetbeat/protos/icmp/icmp.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/flows" "github.com/elastic/beats/v7/packetbeat/pb" @@ -297,7 +298,7 @@ func (icmp *icmpPlugin) publishTransaction(trans *icmpTransaction) { fields["status"] = common.OK_STATUS } - icmpEvent := common.MapStr{ + icmpEvent := mapstr.M{ "version": trans.tuple.icmpVersion, } fields["icmp"] = icmpEvent @@ -311,7 +312,7 @@ func (icmp *icmpPlugin) publishTransaction(trans *icmpTransaction) { pbf.Event.Start = trans.request.ts pbf.Source.Bytes = int64(trans.request.length) - request := common.MapStr{ + request := mapstr.M{ "message": humanReadable(&trans.tuple, trans.request), "type": trans.request.Type, "code": trans.request.code, @@ -326,7 +327,7 @@ func (icmp *icmpPlugin) publishTransaction(trans *icmpTransaction) { pbf.Event.End = trans.response.ts pbf.Destination.Bytes = int64(trans.response.length) - response := common.MapStr{ + response := mapstr.M{ "message": humanReadable(&trans.tuple, trans.response), "type": trans.response.Type, "code": trans.response.code, diff --git a/packetbeat/protos/memcache/binary.go b/packetbeat/protos/memcache/binary.go index ee831055317..fe7f2c806b3 100644 --- a/packetbeat/protos/memcache/binary.go +++ b/packetbeat/protos/memcache/binary.go @@ -24,8 +24,8 @@ package memcache // init function. import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/streambuf" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -456,7 +456,7 @@ func makeSerializeBinary( ) eventFn { command := code.String() eventType := typ.String() - return func(msg *message, event common.MapStr) error { + return func(msg *message, event mapstr.M) error { event["command"] = command event["type"] = eventType event["opcode"] = msg.opcode.String() diff --git a/packetbeat/protos/memcache/commands.go b/packetbeat/protos/memcache/commands.go index 399647bac5a..e58ae6e87f4 100644 --- a/packetbeat/protos/memcache/commands.go +++ b/packetbeat/protos/memcache/commands.go @@ -21,8 +21,8 @@ package memcache // binary/text protocol based commands with setters and serializers. import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/streambuf" + "github.com/elastic/elastic-agent-libs/mapstr" ) type commandType struct { @@ -32,7 +32,7 @@ type commandType struct { event eventFn } -type eventFn func(msg *message, event common.MapStr) error +type eventFn func(msg *message, event mapstr.M) error type argDef struct { parse argParser @@ -82,11 +82,11 @@ func setByteCount(msg *message, count uint32) { msg.bytes = uint(count) } -func serializeNop(msg *message, event common.MapStr) error { +func serializeNop(msg *message, event mapstr.M) error { return nil } -func serializeArgs(msg *message, event common.MapStr, args []argDef) error { +func serializeArgs(msg *message, event mapstr.M, args []argDef) error { for _, arg := range args { if err := arg.serialize(msg, event); err != nil { return err @@ -96,45 +96,45 @@ func serializeArgs(msg *message, event common.MapStr, args []argDef) error { } func serializeValue(name string) eventFn { - return func(msg *message, event common.MapStr) error { + return func(msg *message, event mapstr.M) error { event[name] = msg.value return nil } } func serializeValue2(name string) eventFn { - return func(msg *message, event common.MapStr) error { + return func(msg *message, event mapstr.M) error { event[name] = msg.value2 return nil } } -func serializeFlags(msg *message, event common.MapStr) error { +func serializeFlags(msg *message, event mapstr.M) error { event["flags"] = msg.flags return nil } -func serializeKeys(msg *message, event common.MapStr) error { +func serializeKeys(msg *message, event mapstr.M) error { event["keys"] = msg.keys return nil } -func serializeExpTime(msg *message, event common.MapStr) error { +func serializeExpTime(msg *message, event mapstr.M) error { event["exptime"] = msg.exptime return nil } -func serializeByteCount(msg *message, event common.MapStr) error { +func serializeByteCount(msg *message, event mapstr.M) error { event["bytes"] = msg.bytes return nil } -func serializeStats(msg *message, event common.MapStr) error { +func serializeStats(msg *message, event mapstr.M) error { event["stats"] = msg.stats return nil } -func serializeCas(msg *message, event common.MapStr) error { +func serializeCas(msg *message, event mapstr.M) error { event["cas_unique"] = msg.casUnique return nil } diff --git a/packetbeat/protos/memcache/memcache.go b/packetbeat/protos/memcache/memcache.go index 39bfccd255a..e7a6b6332b9 100644 --- a/packetbeat/protos/memcache/memcache.go +++ b/packetbeat/protos/memcache/memcache.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" @@ -202,7 +203,7 @@ func (mc *memcache) finishTransaction(t *transaction) error { func (mc *memcache) onTransaction(t *transaction) { event := beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, } t.Event(&event) debug("publish event: %s", event) @@ -219,7 +220,7 @@ func (m *message) String() string { return commandCodeStrings[m.command.code] } -func (m *message) Event(event common.MapStr) error { +func (m *message) Event(event mapstr.M) error { if m.command == nil { return errInvalidMessage } @@ -228,13 +229,13 @@ func (m *message) Event(event common.MapStr) error { func (m *message) SubEvent( name string, - event common.MapStr, -) (common.MapStr, error) { + event mapstr.M, +) (mapstr.M, error) { if m == nil { return nil, nil } - msgEvent := common.MapStr{} + msgEvent := mapstr.M{} event[name] = msgEvent return msgEvent, m.Event(msgEvent) } @@ -390,7 +391,7 @@ func (t *transaction) Event(event *beat.Event) error { return err } - mc := common.MapStr{} + mc := mapstr.M{} event.Fields["memcache"] = mc msg := t.request diff --git a/packetbeat/protos/memcache/memcache_test.go b/packetbeat/protos/memcache/memcache_test.go index b09abbb4b5a..91f371b00b8 100644 --- a/packetbeat/protos/memcache/memcache_test.go +++ b/packetbeat/protos/memcache/memcache_test.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/packetbeat/procs" + "github.com/elastic/elastic-agent-libs/mapstr" ) type memcacheTest struct { @@ -75,9 +76,9 @@ func makeBinMessage( return binParseNoFail(t, buf.Bytes()) } -func makeTransactionEvent(t *testing.T, trans *transaction) common.MapStr { +func makeTransactionEvent(t *testing.T, trans *transaction) mapstr.M { event := beat.Event{ - Fields: common.MapStr{}, + Fields: mapstr.M{}, } err := trans.Event(&event) if err != nil { diff --git a/packetbeat/protos/memcache/parse_test.go b/packetbeat/protos/memcache/parse_test.go index e1ac77160f5..7fb61759e78 100644 --- a/packetbeat/protos/memcache/parse_test.go +++ b/packetbeat/protos/memcache/parse_test.go @@ -24,8 +24,8 @@ import ( "testing" "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/streambuf" + "github.com/elastic/elastic-agent-libs/mapstr" ) type testParser struct { @@ -278,8 +278,8 @@ func prepareBinMessage( return buf, err } -func makeMessageEvent(t *testing.T, msg *message) common.MapStr { - event := common.MapStr{} +func makeMessageEvent(t *testing.T, msg *message) mapstr.M { + event := mapstr.M{} err := msg.Event(event) if err != nil { t.Fatalf("generating message event structure failed with: %v", err) diff --git a/packetbeat/protos/memcache/text.go b/packetbeat/protos/memcache/text.go index 86baa8d0627..6c4be073221 100644 --- a/packetbeat/protos/memcache/text.go +++ b/packetbeat/protos/memcache/text.go @@ -29,8 +29,8 @@ import ( "bytes" "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/streambuf" + "github.com/elastic/elastic-agent-libs/mapstr" ) type textCommandType struct { @@ -124,7 +124,7 @@ var argNoReply = argDef{ parser.message.noreply = b return err }, - serialize: func(msg *message, event common.MapStr) error { + serialize: func(msg *message, event mapstr.M) error { event["noreply"] = msg.noreply return nil }, @@ -135,7 +135,7 @@ var argErrorMessage = argDef{ parser.message.errorMsg = memcacheString{buf.Bytes()} return nil }, - serialize: func(msg *message, event common.MapStr) error { + serialize: func(msg *message, event mapstr.M) error { event["error_msg"] = msg.errorMsg return nil }, @@ -414,7 +414,7 @@ func makeIValueArg(name string) argDef { msg.ivalue = v }) }, - serialize: func(msg *message, event common.MapStr) error { + serialize: func(msg *message, event mapstr.M) error { event[name] = msg.ivalue return nil }, @@ -428,7 +428,7 @@ func makeIValue2Arg(name string) argDef { msg.ivalue2 = v }) }, - serialize: func(msg *message, event common.MapStr) error { + serialize: func(msg *message, event mapstr.M) error { event[name] = msg.ivalue2 return nil }, @@ -684,7 +684,7 @@ func serializeRequest( ) eventFn { command := code.String() eventType := typ.String() - return func(msg *message, event common.MapStr) error { + return func(msg *message, event mapstr.M) error { event["command"] = command event["type"] = eventType return serializeArgs(msg, event, args) @@ -698,7 +698,7 @@ func serializeDataRequest( ) eventFn { command := code.String() eventType := typ.String() - return func(msg *message, event common.MapStr) error { + return func(msg *message, event mapstr.M) error { event["command"] = command event["type"] = eventType event["count_values"] = msg.countValues @@ -716,7 +716,7 @@ func serializeDataResponse( ) eventFn { response := code.String() eventType := typ.String() - return func(msg *message, event common.MapStr) error { + return func(msg *message, event mapstr.M) error { event["command"] = response event["type"] = eventType event["count_values"] = msg.countValues @@ -727,26 +727,26 @@ func serializeDataResponse( } } -func serializeUnknown(msg *message, event common.MapStr) error { +func serializeUnknown(msg *message, event mapstr.M) error { event["line"] = msg.commandLine event["command"] = memcacheCmdUNKNOWN.String() event["type"] = memcacheUnknownType.String() return nil } -func serializeCounterResponse(msg *message, event common.MapStr) error { +func serializeCounterResponse(msg *message, event mapstr.M) error { event["command"] = memcacheResCounterOp.String() event["type"] = memcacheCounterMsg.String() event["value"] = msg.value return nil } -func serializeRawArgs(msg *message, event common.MapStr) error { +func serializeRawArgs(msg *message, event mapstr.M) error { event["raw_args"] = memcacheString{msg.rawArgs} return nil } -func serializeAutomove(msg *message, event common.MapStr) error { +func serializeAutomove(msg *message, event mapstr.M) error { var s string switch msg.value { case 0: diff --git a/packetbeat/protos/mongodb/mongodb.go b/packetbeat/protos/mongodb/mongodb.go index 0fcc5464b45..7bf413c41a8 100644 --- a/packetbeat/protos/mongodb/mongodb.go +++ b/packetbeat/protos/mongodb/mongodb.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" @@ -281,7 +282,7 @@ func newTransaction(requ, resp *mongodbMessage) *transaction { // fill request if requ != nil { - trans.mongodb = common.MapStr{} + trans.mongodb = mapstr.M{} trans.event = requ.event trans.method = requ.method diff --git a/packetbeat/protos/mongodb/mongodb_parser.go b/packetbeat/protos/mongodb/mongodb_parser.go index 1abc6f890f2..6a1b2b943fe 100644 --- a/packetbeat/protos/mongodb/mongodb_parser.go +++ b/packetbeat/protos/mongodb/mongodb_parser.go @@ -23,8 +23,8 @@ import ( "strings" "sync" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "gopkg.in/mgo.v2/bson" ) @@ -77,7 +77,7 @@ func mongodbMessageParser(s *stream) (bool, bool) { debugf("opCode = %d (%v)", s.message.opCode, s.message.opCode) // then split depending on operation type - s.message.event = common.MapStr{} + s.message.event = mapstr.M{} switch s.message.opCode { case opReply: diff --git a/packetbeat/protos/mongodb/mongodb_structs.go b/packetbeat/protos/mongodb/mongodb_structs.go index ad466206798..4870e1516ed 100644 --- a/packetbeat/protos/mongodb/mongodb_structs.go +++ b/packetbeat/protos/mongodb/mongodb_structs.go @@ -23,6 +23,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type mongodbMessage struct { @@ -53,7 +54,7 @@ type mongodbMessage struct { // Other fields vary very much depending on operation type // lets just put them in a map - event common.MapStr + event mapstr.M } // Represent a stream being parsed that contains a mongodb message @@ -87,9 +88,9 @@ type transaction struct { bytesOut int bytesIn int - mongodb common.MapStr + mongodb mapstr.M - event common.MapStr + event mapstr.M method string resource string error string diff --git a/packetbeat/protos/mongodb/mongodb_test.go b/packetbeat/protos/mongodb/mongodb_test.go index a1415dfa0a2..6fb4ad0df22 100644 --- a/packetbeat/protos/mongodb/mongodb_test.go +++ b/packetbeat/protos/mongodb/mongodb_test.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" + "github.com/elastic/elastic-agent-libs/mapstr" ) type eventStore struct { @@ -67,7 +68,7 @@ func testTCPTuple() *common.TCPTuple { // Helper function to read from the results Queue. Raises // an error if nothing is found in the queue. -func expectTransaction(t *testing.T, e *eventStore) common.MapStr { +func expectTransaction(t *testing.T, e *eventStore) mapstr.M { if len(e.events) == 0 { t.Error("No transaction") return nil diff --git a/packetbeat/protos/mysql/mysql.go b/packetbeat/protos/mysql/mysql.go index 1170cfc579f..173653c7604 100644 --- a/packetbeat/protos/mysql/mysql.go +++ b/packetbeat/protos/mysql/mysql.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" @@ -97,7 +98,7 @@ type mysqlTransaction struct { notes []string isError bool - mysql common.MapStr + mysql mapstr.M requestRaw string responseRaw string @@ -732,7 +733,7 @@ func (mysql *mysqlPlugin) receivedMysqlRequest(msg *mysqlMessage) { trans.query = query trans.method = method - trans.mysql = common.MapStr{} + trans.mysql = mapstr.M{} trans.notes = msg.notes @@ -756,7 +757,7 @@ func (mysql *mysqlPlugin) receivedMysqlResponse(msg *mysqlMessage) { } // save json details - trans.mysql.Update(common.MapStr{ + trans.mysql.Update(mapstr.M{ "affected_rows": msg.affectedRows, "insert_id": msg.insertID, "num_rows": msg.numberOfRows, diff --git a/packetbeat/protos/mysql/mysql_test.go b/packetbeat/protos/mysql/mysql_test.go index dd942399073..cae6f05b9c2 100644 --- a/packetbeat/protos/mysql/mysql_test.go +++ b/packetbeat/protos/mysql/mysql_test.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" @@ -493,7 +494,7 @@ func testTCPTuple() *common.TCPTuple { } // Helper function to read from the Publisher Queue -func expectTransaction(t *testing.T, e *eventStore) common.MapStr { +func expectTransaction(t *testing.T, e *eventStore) mapstr.M { if len(e.events) == 0 { t.Error("No transaction") return nil diff --git a/packetbeat/protos/nfs/nfs.go b/packetbeat/protos/nfs/nfs.go index 9537f9924ec..983996a4d78 100644 --- a/packetbeat/protos/nfs/nfs.go +++ b/packetbeat/protos/nfs/nfs.go @@ -19,8 +19,8 @@ package nfs import ( "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/packetbeat/pb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type nfs struct { @@ -30,8 +30,8 @@ type nfs struct { event beat.Event } -func (nfs *nfs) getRequestInfo(xdr *xdr) common.MapStr { - nfsInfo := common.MapStr{ +func (nfs *nfs) getRequestInfo(xdr *xdr) mapstr.M { + nfsInfo := mapstr.M{ "version": nfs.vers, } diff --git a/packetbeat/protos/nfs/request_handler.go b/packetbeat/protos/nfs/request_handler.go index 1225f9f6012..49aba94e9e7 100644 --- a/packetbeat/protos/nfs/request_handler.go +++ b/packetbeat/protos/nfs/request_handler.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/protos/tcp" @@ -98,7 +99,7 @@ func (r *rpc) handleCall(xid string, xdr *xdr, ts time.Time, tcptuple *common.TC } // build event only if it's a nfs packet - rpcInfo := common.MapStr{ + rpcInfo := mapstr.M{ "xid": xid, } @@ -111,7 +112,7 @@ func (r *rpc) handleCall(xid string, xdr *xdr, ts time.Time, tcptuple *common.TC rpcInfo["auth_flavor"] = "none" case 1: rpcInfo["auth_flavor"] = "unix" - cred := common.MapStr{} + cred := mapstr.M{} credXdr := makeXDR(authOpaque) cred["stamp"] = credXdr.getUInt() machine := credXdr.getString() @@ -194,13 +195,13 @@ func (r *rpc) handleReply(xid string, xdr *xdr, ts time.Time, tcptuple *common.T nfs.pbf.Destination.Bytes = int64(xdr.size()) fields := nfs.event.Fields - rpcInfo := fields["rpc"].(common.MapStr) + rpcInfo := fields["rpc"].(mapstr.M) status := int(xdr.getUInt()) rpcInfo["status"] = acceptStatus[status] // populate nfs info for successfully executed requests if status == 0 { - nfsInfo := fields["nfs"].(common.MapStr) + nfsInfo := fields["nfs"].(mapstr.M) nfsInfo["status"] = nfs.getNFSReplyStatus(xdr) } else { nfs.pbf.Event.Outcome = "failure" diff --git a/packetbeat/protos/pgsql/pgsql.go b/packetbeat/protos/pgsql/pgsql.go index 20ad2fc5461..555fc557c59 100644 --- a/packetbeat/protos/pgsql/pgsql.go +++ b/packetbeat/protos/pgsql/pgsql.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" @@ -97,7 +98,7 @@ type pgsqlTransaction struct { notes []string isError bool - pgsql common.MapStr + pgsql mapstr.M requestRaw string responseRaw string @@ -412,7 +413,7 @@ func (pgsql *pgsqlPlugin) receivedPgsqlRequest(msg *pgsqlMessage) { trans.src, trans.dst = trans.dst, trans.src } - trans.pgsql = common.MapStr{} + trans.pgsql = mapstr.M{} trans.query = query trans.method = getQueryMethod(query) trans.bytesIn = msg.size @@ -445,12 +446,12 @@ func (pgsql *pgsqlPlugin) receivedPgsqlResponse(msg *pgsqlMessage) { return } - trans.pgsql.Update(common.MapStr{ + trans.pgsql.Update(mapstr.M{ "num_rows": msg.numberOfRows, "num_fields": msg.numberOfFields, }) if msg.isError { - trans.pgsql.Update(common.MapStr{ + trans.pgsql.Update(mapstr.M{ "error_code": msg.errorCode, "error_message": msg.errorInfo, "error_severity": msg.errorSeverity, diff --git a/packetbeat/protos/pgsql/pgsql_test.go b/packetbeat/protos/pgsql/pgsql_test.go index 82075eaacae..4925fb7b90f 100644 --- a/packetbeat/protos/pgsql/pgsql_test.go +++ b/packetbeat/protos/pgsql/pgsql_test.go @@ -31,6 +31,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" @@ -326,7 +327,7 @@ func testTCPTuple() *common.TCPTuple { } // Helper function to read from the Publisher Queue -func expectTransaction(t *testing.T, e *eventStore) common.MapStr { +func expectTransaction(t *testing.T, e *eventStore) mapstr.M { if len(e.events) == 0 { t.Error("No transaction") return nil diff --git a/packetbeat/protos/thrift/thrift.go b/packetbeat/protos/thrift/thrift.go index 04c73747f7f..a343c60843e 100644 --- a/packetbeat/protos/thrift/thrift.go +++ b/packetbeat/protos/thrift/thrift.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/packetbeat/pb" "github.com/elastic/beats/v7/packetbeat/procs" @@ -1104,7 +1105,7 @@ func (thrift *thriftPlugin) publishTransactions() { fields := evt.Fields fields["type"] = pbf.Event.Dataset fields["status"] = status - thriftFields := common.MapStr{} + thriftFields := mapstr.M{} fields["thrift"] = thriftFields if t.request != nil { diff --git a/packetbeat/protos/tls/alerts.go b/packetbeat/protos/tls/alerts.go index 4da5c7d5b75..48b2e57f28d 100644 --- a/packetbeat/protos/tls/alerts.go +++ b/packetbeat/protos/tls/alerts.go @@ -21,8 +21,8 @@ import ( "errors" "fmt" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type ( @@ -88,8 +88,8 @@ func (alertCode alertCode) String() string { return fmt.Sprintf("(unknown:0x%02x)", int(alertCode)) } -func (alert alert) toMap(source string) common.MapStr { - return common.MapStr{ +func (alert alert) toMap(source string) mapstr.M { + return mapstr.M{ "severity": alert.severity.String(), "code": int(alert.code), "type": alert.code.String(), diff --git a/packetbeat/protos/tls/extensions.go b/packetbeat/protos/tls/extensions.go index 1d6ab71e0dc..34068b1ec60 100644 --- a/packetbeat/protos/tls/extensions.go +++ b/packetbeat/protos/tls/extensions.go @@ -21,8 +21,8 @@ import ( "fmt" "strconv" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ExtensionID is the 16-bit identifier for an extension @@ -30,7 +30,7 @@ type ExtensionID uint16 // Extensions stores the data from parsed extensions type Extensions struct { - Parsed common.MapStr + Parsed mapstr.M Raw map[ExtensionID][]byte InOrder []ExtensionID } @@ -82,7 +82,7 @@ func ParseExtensions(buffer bufferView) Extensions { limit := 2 + int(extensionsLength) result := Extensions{ - Parsed: common.MapStr{}, + Parsed: mapstr.M{}, Raw: make(map[ExtensionID][]byte), } @@ -168,7 +168,7 @@ func ignoreContent(_ bufferView) interface{} { func parseStatusReq(buffer bufferView) interface{} { if buffer.length() == 0 { // Initial server response. - return common.MapStr{"response": true} + return mapstr.M{"response": true} } // Client query. var ( @@ -182,7 +182,7 @@ func parseStatusReq(buffer bufferView) interface{} { if code != 1 { typ = fmt.Sprint(code) } - return common.MapStr{"type": typ, "responder_id_list_length": list, "request_extensions": exts} + return mapstr.M{"type": typ, "responder_id_list_length": list, "request_extensions": exts} } func expectEmpty(buffer bufferView) interface{} { diff --git a/packetbeat/protos/tls/parse.go b/packetbeat/protos/tls/parse.go index 7aae974bdd1..1efeb811f29 100644 --- a/packetbeat/protos/tls/parse.go +++ b/packetbeat/protos/tls/parse.go @@ -27,9 +27,9 @@ import ( "fmt" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/streambuf" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type direction uint8 @@ -212,8 +212,8 @@ func (header *recordHeader) isValid() bool { return header.version.major == 3 && header.length <= maxTLSRecordLength } -func (hello *helloMessage) toMap() common.MapStr { - m := common.MapStr{ +func (hello *helloMessage) toMap() mapstr.M { + m := mapstr.M{ "version": fmt.Sprintf("%d.%d", hello.version.major, hello.version.minor), } if len(hello.sessionID) != 0 { @@ -621,8 +621,8 @@ func getKeySize(key interface{}) int { } // certToMap takes an x509 cert and converts it into a map. -func certToMap(cert *x509.Certificate) common.MapStr { - certMap := common.MapStr{ +func certToMap(cert *x509.Certificate) mapstr.M { + certMap := mapstr.M{ "signature_algorithm": cert.SignatureAlgorithm.String(), "public_key_algorithm": toString(cert.PublicKeyAlgorithm), "serial_number": cert.SerialNumber.Text(10), @@ -646,8 +646,8 @@ func certToMap(cert *x509.Certificate) common.MapStr { return certMap } -func toMap(name *pkix.Name) common.MapStr { - result := common.MapStr{} +func toMap(name *pkix.Name) mapstr.M { + result := mapstr.M{} fields := []struct { name string value interface{} diff --git a/packetbeat/protos/tls/parse_test.go b/packetbeat/protos/tls/parse_test.go index 00d3c0d27da..564104ed550 100644 --- a/packetbeat/protos/tls/parse_test.go +++ b/packetbeat/protos/tls/parse_test.go @@ -30,9 +30,9 @@ import ( "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/streambuf" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -120,7 +120,7 @@ func sBuf(t *testing.T, hexString string) *streambuf.Buffer { return streambuf.New(bytes) } -func mapGet(t *testing.T, m common.MapStr, key string) interface{} { +func mapGet(t *testing.T, m mapstr.M, key string) interface{} { value, err := m.GetValue(key) assert.NoError(t, err) return value @@ -370,7 +370,7 @@ func TestRandom(t *testing.T) { for i, test := range []struct { msg string - want common.MapStr + want mapstr.M }{ { msg: "16030100ba010000b603032338f219562c78ca216984f33434bfe952354edf50" + @@ -379,8 +379,8 @@ func TestRandom(t *testing.T) { "12746573742e6974762e6f72616e67652e6672000b000403000102000a000a00" + "08001700190018001600230000000d0020001e06010602060305010502050304" + "0104020403030103020303020102020203000500050100000000000f000101", - want: common.MapStr{ - "extensions": common.MapStr{ + want: mapstr.M{ + "extensions": mapstr.M{ "_unparsed_": []string{ "15", }, @@ -410,7 +410,7 @@ func TestRandom(t *testing.T) { "(unknown:0x0202)", "ecdsa_sha1", }, - "status_request": common.MapStr{ + "status_request": mapstr.M{ "type": "ocsp", "responder_id_list_length": uint16(0), "request_extensions": uint16(0), @@ -476,15 +476,15 @@ func TestRandom(t *testing.T) { "4f6616c21b8fd24e08aadd2c9c43944df5088e2bdbf121649ca1e405e1e95695" + "d52afa1c265b123344a9f5594b661e7d3406b0f6d60c7f776a9723bcec995f4b" + "4da3e6d42dc446b6a33904b7a56f74ba53010006", - want: common.MapStr{ - "extensions": common.MapStr{ + want: mapstr.M{ + "extensions": mapstr.M{ "_unparsed_": []string{ "renegotiation_info", }, "ec_points_formats": []string{ "uncompressed", }, - "status_request": common.MapStr{ + "status_request": mapstr.M{ "response": true, }, }, diff --git a/packetbeat/protos/tls/tls.go b/packetbeat/protos/tls/tls.go index 03e464126c3..dedf9f2cad2 100644 --- a/packetbeat/protos/tls/tls.go +++ b/packetbeat/protos/tls/tls.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/protos" "github.com/elastic/beats/v7/packetbeat/protos/applayer" "github.com/elastic/beats/v7/packetbeat/protos/tcp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type stream struct { @@ -276,7 +277,7 @@ func (plugin *tlsPlugin) createEvent(conn *tlsConnectionData) beat.Event { tls := ecs.Tls{ Established: conn.handshakeCompleted > 1, } - detailed := common.MapStr{} + detailed := mapstr.M{} emptyHello := &helloMessage{} var clientHello, serverHello *helloMessage @@ -359,7 +360,7 @@ func (plugin *tlsPlugin) createEvent(conn *tlsConnectionData) beat.Event { } numAlerts := len(client.parser.alerts) + len(server.parser.alerts) - alerts := make([]common.MapStr, 0, numAlerts) + alerts := make([]mapstr.M, 0, numAlerts) alertTypes := make([]string, 0, numAlerts) for _, alert := range client.parser.alerts { alerts = append(alerts, alert.toMap("client")) @@ -489,7 +490,7 @@ func hashCert(cert *x509.Certificate, algos []*FingerprintAlgorithm, req map[str } } -func (plugin *tlsPlugin) getCerts(certs []*x509.Certificate) (common.MapStr, []common.MapStr) { +func (plugin *tlsPlugin) getCerts(certs []*x509.Certificate) (mapstr.M, []mapstr.M) { if len(certs) == 0 { return nil, nil } @@ -497,7 +498,7 @@ func (plugin *tlsPlugin) getCerts(certs []*x509.Certificate) (common.MapStr, []c if len(certs) == 1 { return cert, nil } - chain := make([]common.MapStr, len(certs)-1) + chain := make([]mapstr.M, len(certs)-1) for idx := 1; idx < len(certs); idx++ { chain[idx-1] = certToMap(certs[idx]) } diff --git a/packetbeat/protos/tls/tls_test.go b/packetbeat/protos/tls/tls_test.go index b36534289d6..9040b239847 100644 --- a/packetbeat/protos/tls/tls_test.go +++ b/packetbeat/protos/tls/tls_test.go @@ -36,6 +36,7 @@ import ( "github.com/elastic/beats/v7/packetbeat/procs" "github.com/elastic/beats/v7/packetbeat/protos" "github.com/elastic/beats/v7/packetbeat/publish" + "github.com/elastic/elastic-agent-libs/mapstr" ) type eventStore struct { @@ -136,7 +137,7 @@ func TestAlert(t *testing.T) { if !assert.NoError(t, err) { t.Fatal(err) } - alerts := alertsIf.([]common.MapStr) + alerts := alertsIf.([]mapstr.M) assert.True(t, ok) assert.Len(t, alerts, 1) severity, ok := alerts[0]["severity"] @@ -252,12 +253,12 @@ func TestOCSPStatus(t *testing.T) { t.Fatalf("unexected number of results: got:%d want:1", len(results.events)) } - want := common.MapStr{ - "client": common.MapStr{ + want := mapstr.M{ + "client": mapstr.M{ "ip": "192.168.0.1", "port": int64(6512), }, - "event": common.MapStr{ + "event": mapstr.M{ "dataset": "tls", "kind": "event", "category": []string{ @@ -268,40 +269,40 @@ func TestOCSPStatus(t *testing.T) { "protocol", }, }, - "destination": common.MapStr{ + "destination": mapstr.M{ "ip": "192.168.0.2", "port": int64(27017), }, - "network": common.MapStr{ + "network": mapstr.M{ "type": "ipv4", "transport": "tcp", "protocol": "tls", "direction": "unknown", "community_id": "1:jKfewJN/czjTuEpVvsKdYXXiMzs=", }, - "related": common.MapStr{ + "related": mapstr.M{ "ip": []string{ "192.168.0.1", "192.168.0.2", }, }, - "server": common.MapStr{ + "server": mapstr.M{ "ip": "192.168.0.2", "port": int64(27017), }, - "source": common.MapStr{ + "source": mapstr.M{ "port": int64(6512), "ip": "192.168.0.1", }, "status": "Error", - "tls": common.MapStr{ + "tls": mapstr.M{ "cipher": "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", - "detailed": common.MapStr{ + "detailed": mapstr.M{ "client_certificate_requested": false, "ocsp_response": "successful", - "server_certificate_chain": []common.MapStr{ + "server_certificate_chain": []mapstr.M{ { - "issuer": common.MapStr{ + "issuer": mapstr.M{ "common_name": "Orange Devices Root LAB CA", "country": "FR", "distinguished_name": "CN=Orange Devices Root LAB CA,OU=FOR LAB USE ONLY,O=Orange,C=FR", @@ -314,7 +315,7 @@ func TestOCSPStatus(t *testing.T) { "public_key_size": 4096, "serial_number": "1492448539999078269498416841973088004758827", "signature_algorithm": "SHA256-RSA", - "subject": common.MapStr{ + "subject": mapstr.M{ "common_name": "Orange Devices PKI TV LAB CA", "country": "FR", "distinguished_name": "CN=Orange Devices PKI TV LAB CA,OU=FOR LAB USE ONLY,O=Orange,C=FR", @@ -324,7 +325,7 @@ func TestOCSPStatus(t *testing.T) { "version_number": 3, }, { - "issuer": common.MapStr{ + "issuer": mapstr.M{ "common_name": "Orange Devices Root LAB CA", "country": "FR", "distinguished_name": "CN=Orange Devices Root LAB CA,OU=FOR LAB USE ONLY,O=Orange,C=FR", @@ -337,7 +338,7 @@ func TestOCSPStatus(t *testing.T) { "public_key_size": 4096, "serial_number": "1492246295378596931754418352553114016724120", "signature_algorithm": "SHA256-RSA", - "subject": common.MapStr{ + "subject": mapstr.M{ "common_name": "Orange Devices Root LAB CA", "country": "FR", "distinguished_name": "CN=Orange Devices Root LAB CA,OU=FOR LAB USE ONLY,O=Orange,C=FR", @@ -347,15 +348,15 @@ func TestOCSPStatus(t *testing.T) { "version_number": 3, }, }, - "server_hello": common.MapStr{ - "extensions": common.MapStr{ + "server_hello": mapstr.M{ + "extensions": mapstr.M{ "_unparsed_": []string{ "renegotiation_info", }, "ec_points_formats": []string{ "uncompressed", }, - "status_request": common.MapStr{ + "status_request": mapstr.M{ "response": true, }, }, @@ -368,14 +369,14 @@ func TestOCSPStatus(t *testing.T) { }, "established": false, "resumed": false, - "server": common.MapStr{ + "server": mapstr.M{ "issuer": "CN=Orange Devices PKI TV LAB CA,OU=FOR LAB USE ONLY,O=Orange,C=FR", - "hash": common.MapStr{ + "hash": mapstr.M{ "sha1": "D8A11028DAD7E34F5D7F6D41DE01743D8B3CE553", }, "not_after": time.Date(2022, 6, 3, 13, 38, 16, 0, time.UTC), "not_before": time.Date(2021, 6, 3, 13, 38, 16, 0, time.UTC), - "x509": common.MapStr{ + "x509": mapstr.M{ "alternative_names": []string{ "*.ena1.orange.fr", "*.itv.orange.fr", @@ -391,7 +392,7 @@ func TestOCSPStatus(t *testing.T) { "*.pp-ntv1.orange.fr", "*.pp-ntv2.orange.fr", }, - "issuer": common.MapStr{ + "issuer": mapstr.M{ "common_name": "Orange Devices PKI TV LAB CA", "country": "FR", "distinguished_name": "CN=Orange Devices PKI TV LAB CA,OU=FOR LAB USE ONLY,O=Orange,C=FR", @@ -404,7 +405,7 @@ func TestOCSPStatus(t *testing.T) { "public_key_size": 256, "serial_number": "189790697042017246339292011338547986350262673379", "signature_algorithm": "SHA256-RSA", - "subject": common.MapStr{ + "subject": mapstr.M{ "common_name": "server2 test PKI TV LAB", "country": "FR", "distinguished_name": "CN=server2 test PKI TV LAB,OU=Orange,C=FR", @@ -523,7 +524,7 @@ func TestInterleavedRecords(t *testing.T) { alerts, err := event.GetValue("tls.detailed.alerts") assert.NoError(t, err) - assert.Len(t, alerts.([]common.MapStr), 2) + assert.Len(t, alerts.([]mapstr.M), 2) } func TestCompletedHandshake(t *testing.T) { diff --git a/packetbeat/publish/publish.go b/packetbeat/publish/publish.go index f754dd8c545..c7c8ae898c3 100644 --- a/packetbeat/publish/publish.go +++ b/packetbeat/publish/publish.go @@ -27,6 +27,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/packetbeat/pb" + "github.com/elastic/elastic-agent-libs/mapstr" ) type TransactionPublisher struct { @@ -87,7 +88,7 @@ func (p *TransactionPublisher) CreateReporter( // load and register the module it's fields, tags and processors settings meta := struct { Index string `config:"index"` - Event common.EventMetadata `config:",inline"` + Event mapstr.EventMetadata `config:",inline"` Processors processors.PluginConfig `config:"processors"` KeepNull bool `config:"keep_null"` }{} @@ -111,7 +112,7 @@ func (p *TransactionPublisher) CreateReporter( clientConfig.PublishMode = beat.DropIfFull } if meta.Index != "" { - clientConfig.Processing.Meta = common.MapStr{"raw_index": meta.Index} + clientConfig.Processing.Meta = mapstr.M{"raw_index": meta.Index} } client, err := p.pipeline.ConnectWith(clientConfig) diff --git a/packetbeat/publish/publish_test.go b/packetbeat/publish/publish_test.go index 98b5dfc91e3..0bfa7ed08ca 100644 --- a/packetbeat/publish/publish_test.go +++ b/packetbeat/publish/publish_test.go @@ -31,12 +31,13 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/packetbeat/pb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func testEvent() beat.Event { return beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "test", "src": &common.Endpoint{}, "dst": &common.Endpoint{}, @@ -98,7 +99,7 @@ func TestPublish(t *testing.T) { event := func() *beat.Event { return &beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "type": "test", "_packetbeat": &pb.Fields{ Source: &ecs.Source{ diff --git a/winlogbeat/beater/eventlogger.go b/winlogbeat/beater/eventlogger.go index d83bdfcadbf..f0b713882cc 100644 --- a/winlogbeat/beater/eventlogger.go +++ b/winlogbeat/beater/eventlogger.go @@ -29,6 +29,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/add_formatted_index" "github.com/elastic/beats/v7/libbeat/publisher/pipetool" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/winlogbeat/checkpoint" "github.com/elastic/beats/v7/winlogbeat/eventlog" @@ -36,14 +37,14 @@ import ( type eventLogger struct { source eventlog.EventLog - eventMeta common.EventMetadata + eventMeta mapstr.EventMetadata processors beat.ProcessorList keepNull bool log *logp.Logger } type eventLoggerConfig struct { - common.EventMetadata `config:",inline"` // Fields and tags to add to events. + mapstr.EventMetadata `config:",inline"` // Fields and tags to add to events. Processors processors.PluginConfig `config:"processors"` Index fmtstr.EventFormatString `config:"index"` diff --git a/winlogbeat/beater/eventlogger_test.go b/winlogbeat/beater/eventlogger_test.go index b59eb58e6c5..8724e2c5074 100644 --- a/winlogbeat/beater/eventlogger_test.go +++ b/winlogbeat/beater/eventlogger_test.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestProcessorsForConfig(t *testing.T) { @@ -51,7 +52,7 @@ func TestProcessorsForConfig(t *testing.T) { } for description, test := range testCases { if test.event.Fields == nil { - test.event.Fields = common.MapStr{} + test.event.Fields = mapstr.M{} } config, err := eventLoggerConfigFromString(test.configStr) if err != nil { diff --git a/winlogbeat/cmd/root.go b/winlogbeat/cmd/root.go index 7bd35f1597d..f8aaf2edf07 100644 --- a/winlogbeat/cmd/root.go +++ b/winlogbeat/cmd/root.go @@ -20,10 +20,10 @@ package cmd import ( "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/publisher/processing" "github.com/elastic/beats/v7/winlogbeat/beater" + "github.com/elastic/elastic-agent-libs/mapstr" // Register fields. _ "github.com/elastic/beats/v7/winlogbeat/include" @@ -38,8 +38,8 @@ const ( ) // withECSVersion is a modifier that adds ecs.version to events. -var withECSVersion = processing.WithFields(common.MapStr{ - "ecs": common.MapStr{ +var withECSVersion = processing.WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) diff --git a/winlogbeat/eventlog/bench_test.go b/winlogbeat/eventlog/bench_test.go index 1d2c075c767..f635f998b9d 100644 --- a/winlogbeat/eventlog/bench_test.go +++ b/winlogbeat/eventlog/bench_test.go @@ -30,7 +30,7 @@ import ( "golang.org/x/sys/windows/svc/eventlog" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const gigabyte = 1 << 30 @@ -74,7 +74,7 @@ func TestBenchmarkRead(t *testing.T) { func benchmarkEventLog(api string, batchSize int) func(b *testing.B) { return func(b *testing.B) { - conf := common.MapStr{ + conf := mapstr.M{ "name": providerName, "batch_read_size": batchSize, "no_more_events": "stop", diff --git a/winlogbeat/eventlog/eventlog.go b/winlogbeat/eventlog/eventlog.go index 43b0b98d094..ce5a9d2ef2c 100644 --- a/winlogbeat/eventlog/eventlog.go +++ b/winlogbeat/eventlog/eventlog.go @@ -24,10 +24,10 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/winlogbeat/checkpoint" "github.com/elastic/beats/v7/winlogbeat/sys/winevent" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Debug selectors used in this package. @@ -85,7 +85,7 @@ func (e Record) ToEvent() beat.Event { win.Delete("time_created") win.Put("api", e.API) - m := common.MapStr{ + m := mapstr.M{ "winlog": win, } @@ -116,7 +116,7 @@ func (e Record) ToEvent() beat.Event { } // rename will rename a map entry overriding any previous value -func rename(m common.MapStr, oldKey, newKey string) { +func rename(m mapstr.M, oldKey, newKey string) { v, err := m.GetValue(oldKey) if err != nil { return diff --git a/winlogbeat/sys/winevent/event.go b/winlogbeat/sys/winevent/event.go index 4054626ed8f..0709d951e52 100644 --- a/winlogbeat/sys/winevent/event.go +++ b/winlogbeat/sys/winevent/event.go @@ -24,10 +24,10 @@ import ( "strings" "time" - "github.com/elastic/beats/v7/libbeat/common" libxml "github.com/elastic/beats/v7/libbeat/common/encoding/xml" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/winlogbeat/sys" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Debug selectors used in this package. @@ -88,9 +88,9 @@ type Event struct { RenderErr []string } -func (e Event) Fields() common.MapStr { +func (e Event) Fields() mapstr.M { // Windows Log Specific data - win := common.MapStr{} + win := mapstr.M{} AddOptional(win, "channel", e.Channel) AddOptional(win, "event_id", fmt.Sprint(e.EventIdentifier.ID)) @@ -114,7 +114,7 @@ func (e Event) Fields() common.MapStr { AddOptional(win, "message", sys.RemoveWindowsLineEndings(e.Message)) if e.User.Identifier != "" { - user := common.MapStr{ + user := mapstr.M{ "identifier": e.User.Identifier, } win["user"] = user diff --git a/winlogbeat/sys/winevent/event_test.go b/winlogbeat/sys/winevent/event_test.go index 62fc3a7d6b6..413183ee51d 100644 --- a/winlogbeat/sys/winevent/event_test.go +++ b/winlogbeat/sys/winevent/event_test.go @@ -26,7 +26,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const allXML = ` @@ -83,7 +83,7 @@ func TestXML(t *testing.T) { tests := []struct { xml string event Event - mapstr common.MapStr + mapstr mapstr.M }{ { xml: allXML, @@ -153,10 +153,10 @@ func TestXML(t *testing.T) { }, }, }, - mapstr: common.MapStr{ + mapstr: mapstr.M{ "event_id": "0", "time_created": time.Time{}, - "user_data": common.MapStr{ + "user_data": mapstr.M{ "Id": "{00000000-0000-0000-0000-000000000000}", "xml_name": "Operation_ClientFailure", }, diff --git a/winlogbeat/sys/winevent/maputil.go b/winlogbeat/sys/winevent/maputil.go index 82ae2ad2a3c..b6338f2d6b5 100644 --- a/winlogbeat/sys/winevent/maputil.go +++ b/winlogbeat/sys/winevent/maputil.go @@ -22,14 +22,14 @@ import ( "reflect" "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/winlogbeat/sys" + "github.com/elastic/elastic-agent-libs/mapstr" ) // AddOptional adds a key and value to the given MapStr if the value is not the // zero value for the type of v. It is safe to call the function with a nil // MapStr. -func AddOptional(m common.MapStr, key string, v interface{}) { +func AddOptional(m mapstr.M, key string, v interface{}) { if m != nil && !isZero(v) { _, _ = m.Put(key, v) } @@ -41,12 +41,12 @@ func AddOptional(m common.MapStr, key string, v interface{}) { // // The new dictionary is added to the given MapStr and it is also returned for // convenience purposes. -func AddPairs(m common.MapStr, key string, pairs []KeyValue) common.MapStr { +func AddPairs(m mapstr.M, key string, pairs []KeyValue) mapstr.M { if len(pairs) == 0 { return nil } - h := make(common.MapStr, len(pairs)) + h := make(mapstr.M, len(pairs)) for i, kv := range pairs { // Ignore empty values. if kv.Value == "" { @@ -62,7 +62,7 @@ func AddPairs(m common.MapStr, key string, pairs []KeyValue) common.MapStr { // Do not overwrite. _, err := h.GetValue(k) - if err == common.ErrKeyNotFound { + if err == mapstr.ErrKeyNotFound { _, _ = h.Put(k, sys.RemoveWindowsLineEndings(kv.Value)) } else { debugf("Dropping key/value (k=%s, v=%s) pair because key already "+ diff --git a/x-pack/auditbeat/module/system/host/host.go b/x-pack/auditbeat/module/system/host/host.go index 30df5acc64e..e5d91544f36 100644 --- a/x-pack/auditbeat/module/system/host/host.go +++ b/x-pack/auditbeat/module/system/host/host.go @@ -19,10 +19,10 @@ import ( "github.com/joeshaw/multierror" "github.com/elastic/beats/v7/auditbeat/datastore" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo" "github.com/elastic/go-sysinfo/types" ) @@ -114,8 +114,8 @@ func (host *Host) changeDetectionHash() uint64 { return h.Sum64() } -func (host *Host) toMapStr() common.MapStr { - mapstr := common.MapStr{ +func (host *Host) toMapStr() mapstr.M { + mapstr := mapstr.M{ // https://github.com/elastic/ecs#-host-fields "uptime": host.Uptime, "boottime": host.Info.BootTime, @@ -126,7 +126,7 @@ func (host *Host) toMapStr() common.MapStr { "architecture": host.Info.Architecture, // https://github.com/elastic/ecs#-operating-system-fields - "os": common.MapStr{ + "os": mapstr.M{ "platform": host.Info.OS.Platform, "name": host.Info.OS.Name, "family": host.Info.OS.Family, @@ -340,8 +340,8 @@ func hostEvent(host *Host, eventType string, action eventAction) mb.Event { hostFields := host.toMapStr() event := mb.Event{ - RootFields: common.MapStr{ - "event": common.MapStr{ + RootFields: mapstr.M{ + "event": mapstr.M{ "kind": eventType, "category": []string{"host"}, "type": []string{action.Type()}, @@ -353,7 +353,7 @@ func hostEvent(host *Host, eventType string, action eventAction) mb.Event { } // Copy select host.* fields in case add_host_metadata is not configured. - hostTopLevel := common.MapStr{} + hostTopLevel := mapstr.M{} hostFields.CopyFieldsTo(hostTopLevel, "architecture") hostFields.CopyFieldsTo(hostTopLevel, "containerized") hostFields.CopyFieldsTo(hostTopLevel, "hostname") diff --git a/x-pack/auditbeat/module/system/login/login.go b/x-pack/auditbeat/module/system/login/login.go index 19d1753e23b..1013b089494 100644 --- a/x-pack/auditbeat/module/system/login/login.go +++ b/x-pack/auditbeat/module/system/login/login.go @@ -13,10 +13,10 @@ import ( "time" "github.com/elastic/beats/v7/auditbeat/datastore" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -163,8 +163,8 @@ func (ms *MetricSet) readAndEmit(report mb.ReporterV2) int { func (ms *MetricSet) loginEvent(loginRecord *LoginRecord) mb.Event { event := mb.Event{ Timestamp: loginRecord.Timestamp, - RootFields: common.MapStr{ - "event": common.MapStr{ + RootFields: mapstr.M{ + "event": mapstr.M{ "kind": eventTypeEvent, "action": loginRecord.Type.string(), "origin": loginRecord.Origin, diff --git a/x-pack/auditbeat/module/system/login/login_test.go b/x-pack/auditbeat/module/system/login/login_test.go index fcdb5f6983a..7a5000ff2bf 100644 --- a/x-pack/auditbeat/module/system/login/login_test.go +++ b/x-pack/auditbeat/module/system/login/login_test.go @@ -21,8 +21,8 @@ import ( "github.com/elastic/beats/v7/auditbeat/core" abtest "github.com/elastic/beats/v7/auditbeat/testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { @@ -255,7 +255,7 @@ func TestBtmp(t *testing.T) { "Timestamp is not equal: %+v", events[3].Timestamp) } -func checkFieldValue(t *testing.T, mapstr common.MapStr, fieldName string, fieldValue interface{}) { +func checkFieldValue(t *testing.T, mapstr mapstr.M, fieldName string, fieldValue interface{}) { value, err := mapstr.GetValue(fieldName) if assert.NoError(t, err) { switch v := value.(type) { diff --git a/x-pack/auditbeat/module/system/package/package.go b/x-pack/auditbeat/module/system/package/package.go index 8909fbdafbf..9d879be50ba 100644 --- a/x-pack/auditbeat/module/system/package/package.go +++ b/x-pack/auditbeat/module/system/package/package.go @@ -25,12 +25,12 @@ import ( "github.com/joeshaw/multierror" "github.com/elastic/beats/v7/auditbeat/datastore" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/auditbeat/cache" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -136,47 +136,47 @@ func (pkg Package) Hash() uint64 { return h.Sum64() } -func (pkg Package) toMapStr() (common.MapStr, common.MapStr) { - mapstr := common.MapStr{ +func (pkg Package) toMapStr() (mapstr.M, mapstr.M) { + mapStr := mapstr.M{ "name": pkg.Name, "version": pkg.Version, } - ecsMapstr := common.MapStr{ + ecsMapstr := mapstr.M{ "name": pkg.Name, "version": pkg.Version, } if pkg.Release != "" { - mapstr.Put("release", pkg.Release) + mapStr.Put("release", pkg.Release) } if pkg.Arch != "" { - mapstr.Put("arch", pkg.Arch) + mapStr.Put("arch", pkg.Arch) ecsMapstr.Put("architecture", pkg.License) } if pkg.License != "" { - mapstr.Put("license", pkg.License) + mapStr.Put("license", pkg.License) ecsMapstr.Put("license", pkg.License) } if !pkg.InstallTime.IsZero() { - mapstr.Put("installtime", pkg.InstallTime) + mapStr.Put("installtime", pkg.InstallTime) ecsMapstr.Put("installed", pkg.InstallTime) } if pkg.Size != 0 { - mapstr.Put("size", pkg.Size) + mapStr.Put("size", pkg.Size) ecsMapstr.Put("size", pkg.Size) } if pkg.Summary != "" { - mapstr.Put("summary", pkg.Summary) + mapStr.Put("summary", pkg.Summary) ecsMapstr.Put("description", pkg.Summary) } if pkg.URL != "" { - mapstr.Put("url", pkg.URL) + mapStr.Put("url", pkg.URL) ecsMapstr.Put("reference", pkg.URL) } @@ -184,7 +184,7 @@ func (pkg Package) toMapStr() (common.MapStr, common.MapStr) { ecsMapstr.Put("type", pkg.Type) } - return mapstr, ecsMapstr + return mapStr, ecsMapstr } // entityID creates an ID that uniquely identifies this package across machines. @@ -373,8 +373,8 @@ func convertToPackage(cacheValues []interface{}) []*Package { func (ms *MetricSet) packageEvent(pkg *Package, eventType string, action eventAction) mb.Event { pkgFields, ecsPkgFields := pkg.toMapStr() event := mb.Event{ - RootFields: common.MapStr{ - "event": common.MapStr{ + RootFields: mapstr.M{ + "event": mapstr.M{ "kind": eventType, "category": []string{"package"}, "type": []string{action.Type()}, diff --git a/x-pack/auditbeat/module/system/process/process.go b/x-pack/auditbeat/module/system/process/process.go index 8895222cec8..25e4687d48e 100644 --- a/x-pack/auditbeat/module/system/process/process.go +++ b/x-pack/auditbeat/module/system/process/process.go @@ -18,12 +18,12 @@ import ( "github.com/elastic/beats/v7/auditbeat/datastore" "github.com/elastic/beats/v7/auditbeat/helper/hasher" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/auditbeat/cache" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo" "github.com/elastic/go-sysinfo/types" ) @@ -117,13 +117,13 @@ func (p Process) Hash() uint64 { return h.Sum64() } -func (p Process) toMapStr() common.MapStr { - return common.MapStr{ +func (p Process) toMapStr() mapstr.M { + return mapstr.M{ // https://github.com/elastic/ecs#-process-fields "name": p.Info.Name, "args": p.Info.Args, "pid": p.Info.PID, - "parent": common.MapStr{ + "parent": mapstr.M{ "pid": p.Info.PPID, }, "working_directory": p.Info.CWD, @@ -341,8 +341,8 @@ func (ms *MetricSet) enrichProcess(process *Process) { func (ms *MetricSet) processEvent(process *Process, eventType string, action eventAction) mb.Event { event := mb.Event{ - RootFields: common.MapStr{ - "event": common.MapStr{ + RootFields: mapstr.M{ + "event": mapstr.M{ "kind": eventType, "category": []string{"process"}, "type": []string{action.Type()}, @@ -394,7 +394,7 @@ func (ms *MetricSet) processEvent(process *Process, eventType string, action eve return event } -func putIfNotEmpty(mapstr *common.MapStr, key string, value string) { +func putIfNotEmpty(mapstr *mapstr.M, key string, value string) { if value != "" { mapstr.Put(key, value) } diff --git a/x-pack/auditbeat/module/system/process/process_test.go b/x-pack/auditbeat/module/system/process/process_test.go index 55a0442f7cc..1740667bf70 100644 --- a/x-pack/auditbeat/module/system/process/process_test.go +++ b/x-pack/auditbeat/module/system/process/process_test.go @@ -14,8 +14,8 @@ import ( "github.com/elastic/beats/v7/auditbeat/core" "github.com/elastic/beats/v7/auditbeat/helper/hasher" abtest "github.com/elastic/beats/v7/auditbeat/testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo/types" ) @@ -138,7 +138,7 @@ func testProcess() *Process { } func TestPutIfNotEmpty(t *testing.T) { - mapstr := common.MapStr{} + mapstr := mapstr.M{} putIfNotEmpty(&mapstr, "key1", "value") value, err := mapstr.GetValue("key1") diff --git a/x-pack/auditbeat/module/system/socket/arch_386.go b/x-pack/auditbeat/module/system/socket/arch_386.go index e2eca4dc473..18e07826a69 100644 --- a/x-pack/auditbeat/module/system/socket/arch_386.go +++ b/x-pack/auditbeat/module/system/socket/arch_386.go @@ -7,9 +7,9 @@ package socket -import "github.com/elastic/beats/v7/libbeat/common" +import "github.com/elastic/elastic-agent-libs/mapstr" -var archVariables = common.MapStr{ +var archVariables = mapstr.M{ // Regular function call parameters 1 to 6 // This calling convention is used internally by the kernel // which is built by default with (-mregparam=3) diff --git a/x-pack/auditbeat/module/system/socket/arch_amd64.go b/x-pack/auditbeat/module/system/socket/arch_amd64.go index b8bd0cf1d43..6d9f29adb65 100644 --- a/x-pack/auditbeat/module/system/socket/arch_amd64.go +++ b/x-pack/auditbeat/module/system/socket/arch_amd64.go @@ -7,9 +7,9 @@ package socket -import "github.com/elastic/beats/v7/libbeat/common" +import "github.com/elastic/elastic-agent-libs/mapstr" -var archVariables = common.MapStr{ +var archVariables = mapstr.M{ // Regular function call parameters 1 to 6 "P1": "%di", "P2": "%si", diff --git a/x-pack/auditbeat/module/system/socket/guess/creds.go b/x-pack/auditbeat/module/system/socket/guess/creds.go index aafd8745fd1..c7826582a13 100644 --- a/x-pack/auditbeat/module/system/socket/guess/creds.go +++ b/x-pack/auditbeat/module/system/socket/guess/creds.go @@ -14,9 +14,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -108,7 +108,7 @@ func (g *guessStructCreds) Terminate() error { } // Extract receives the struct cred dump and discovers the offsets. -func (g *guessStructCreds) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessStructCreds) Extract(ev interface{}) (mapstr.M, bool) { raw := ev.([]byte) if len(raw) != credDumpBytes { return nil, false @@ -130,7 +130,7 @@ func (g *guessStructCreds) Extract(ev interface{}) (common.MapStr, bool) { ptr[offset/4+1] != uint32(os.Getgid()) { return nil, false } - return common.MapStr{ + return mapstr.M{ "STRUCT_CRED_UID": offset, "STRUCT_CRED_GID": offset + 4, "STRUCT_CRED_EUID": offset + 16, diff --git a/x-pack/auditbeat/module/system/socket/guess/cskxmit6.go b/x-pack/auditbeat/module/system/socket/guess/cskxmit6.go index 9c17f5136e8..d77dc7a2bbe 100644 --- a/x-pack/auditbeat/module/system/socket/guess/cskxmit6.go +++ b/x-pack/auditbeat/module/system/socket/guess/cskxmit6.go @@ -13,9 +13,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -163,7 +163,7 @@ func (g *guessInet6CskXmit) Trigger() error { // Extract receives first the sock* from inet_csk_accept, then the arguments // from inet6_csk_xmit. -func (g *guessInet6CskXmit) Extract(event interface{}) (common.MapStr, bool) { +func (g *guessInet6CskXmit) Extract(event interface{}) (mapstr.M, bool) { switch msg := event.(type) { case *sockArgumentGuess: g.sock = msg.Sock @@ -175,7 +175,7 @@ func (g *guessInet6CskXmit) Extract(event interface{}) (common.MapStr, bool) { } if msg.Arg == g.sock { // struct sock * is the first argument - return common.MapStr{ + return mapstr.M{ "INET6_CSK_XMIT_SOCK": g.ctx.Vars["P1"], "INET6_CSK_XMIT_SKBUFF": g.ctx.Vars["P2"], }, true @@ -183,7 +183,7 @@ func (g *guessInet6CskXmit) Extract(event interface{}) (common.MapStr, bool) { // struct sk_buff* is the first argument. Obtain sock* from sk_buff off := indexAligned(msg.Dump[:], ((*[sizeOfPtr]byte)(unsafe.Pointer(&g.sock)))[:], 0, int(sizeOfPtr)) if off != -1 { - return common.MapStr{ + return mapstr.M{ "INET6_CSK_XMIT_SOCK": fmt.Sprintf("+%d(%s)", off, g.ctx.Vars["P1"]), "INET6_CSK_XMIT_SKBUFF": g.ctx.Vars["P1"], }, true diff --git a/x-pack/auditbeat/module/system/socket/guess/deref.go b/x-pack/auditbeat/module/system/socket/guess/deref.go index cc06125c82a..7996a8cd8b3 100644 --- a/x-pack/auditbeat/module/system/socket/guess/deref.go +++ b/x-pack/auditbeat/module/system/socket/guess/deref.go @@ -13,9 +13,9 @@ import ( "strconv" "syscall" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -112,7 +112,7 @@ func (g *guessDeref) MaxRepeats() int { // Extract receives the memory read through a null pointer and checks if it's // zero or garbage. -func (g *guessDeref) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessDeref) Extract(ev interface{}) (mapstr.M, bool) { raw := ev.([]byte) if len(raw) != credDumpBytes { return nil, false @@ -128,7 +128,7 @@ func (g *guessDeref) Extract(ev interface{}) (common.MapStr, bool) { if g.tries--; g.tries > 0 { return nil, true } - return common.MapStr{ + return mapstr.M{ flagName: !g.garbage, }, true } diff --git a/x-pack/auditbeat/module/system/socket/guess/guess.go b/x-pack/auditbeat/module/system/socket/guess/guess.go index 0c632616057..05c2aa4668a 100644 --- a/x-pack/auditbeat/module/system/socket/guess/guess.go +++ b/x-pack/auditbeat/module/system/socket/guess/guess.go @@ -12,9 +12,9 @@ import ( "fmt" "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Context shared with guesses. @@ -22,7 +22,7 @@ type Context struct { // Log is a logger so that guesses can log. Log helper.Logger // Vars is the current set of template variables. - Vars common.MapStr + Vars mapstr.M // Timeout is the maximum time allowed to wait for a guess to complete. Timeout time.Duration } @@ -48,7 +48,7 @@ type Guesser interface { // Extract receives the events generated during trigger. // Done is false when it needs to be called with more events. True when // the guess has completed and results is a map with the discovered values. - Extract(event interface{}) (result common.MapStr, done bool) + Extract(event interface{}) (result mapstr.M, done bool) // Terminate performs cleanup after the guess is complete. Terminate() error } @@ -60,7 +60,7 @@ type RepeatGuesser interface { // NumRepeats returns how many times the guess is repeated. NumRepeats() int // Reduce takes the output of every repetition and returns the final result. - Reduce([]common.MapStr) (common.MapStr, error) + Reduce([]mapstr.M) (mapstr.M, error) } // EventualGuesser is a guess that repeats an undetermined amount of times @@ -87,7 +87,7 @@ type ConditionalGuesser interface { // channel and executes the Trigger function. Each record received through the // channel is passed to the Extract function. Terminates once Extract succeeds // or the timeout expires. -func Guess(guesser Guesser, installer helper.ProbeInstaller, ctx Context) (result common.MapStr, err error) { +func Guess(guesser Guesser, installer helper.ProbeInstaller, ctx Context) (result mapstr.M, err error) { switch v := guesser.(type) { case RepeatGuesser: result, err = guessMultiple(v, installer, ctx) @@ -102,8 +102,8 @@ func Guess(guesser Guesser, installer helper.ProbeInstaller, ctx Context) (resul return result, nil } -func guessMultiple(guess RepeatGuesser, installer helper.ProbeInstaller, ctx Context) (result common.MapStr, err error) { - var results []common.MapStr +func guessMultiple(guess RepeatGuesser, installer helper.ProbeInstaller, ctx Context) (result mapstr.M, err error) { + var results []mapstr.M for idx := 1; idx <= guess.NumRepeats(); idx++ { r, err := guessOnce(guess, installer, ctx) if err != nil { @@ -115,7 +115,7 @@ func guessMultiple(guess RepeatGuesser, installer helper.ProbeInstaller, ctx Con return guess.Reduce(results) } -func guessEventually(guess EventualGuesser, installer helper.ProbeInstaller, ctx Context) (result common.MapStr, err error) { +func guessEventually(guess EventualGuesser, installer helper.ProbeInstaller, ctx Context) (result mapstr.M, err error) { limit := guess.MaxRepeats() for i := 0; i < limit; i++ { ctx.Log.Debugf(" --- %s run #%d", guess.Name(), i) @@ -129,7 +129,7 @@ func guessEventually(guess EventualGuesser, installer helper.ProbeInstaller, ctx return nil, fmt.Errorf("guess %s didn't succeed after %d tries", guess.Name(), limit) } -func guessOnce(guesser Guesser, installer helper.ProbeInstaller, ctx Context) (result common.MapStr, err error) { +func guessOnce(guesser Guesser, installer helper.ProbeInstaller, ctx Context) (result mapstr.M, err error) { if err := guesser.Prepare(ctx); err != nil { return nil, fmt.Errorf("prepare failed: %w", err) } @@ -241,7 +241,7 @@ func guessOnce(guesser Guesser, installer helper.ProbeInstaller, ctx Context) (r } } -func containsAll(requirements []string, dict common.MapStr) bool { +func containsAll(requirements []string, dict mapstr.M) bool { for _, req := range requirements { if _, found := dict[req]; !found { return false @@ -306,7 +306,7 @@ func GuessAll(installer helper.ProbeInstaller, ctx Context) (err error) { return nil } -func isIPv6Enabled(vars common.MapStr) (bool, error) { +func isIPv6Enabled(vars mapstr.M) (bool, error) { iface, err := vars.GetValue("HAS_IPV6") if err != nil { return false, err diff --git a/x-pack/auditbeat/module/system/socket/guess/helpers.go b/x-pack/auditbeat/module/system/socket/guess/helpers.go index aa802fa252a..c808733e372 100644 --- a/x-pack/auditbeat/module/system/socket/guess/helpers.go +++ b/x-pack/auditbeat/module/system/socket/guess/helpers.go @@ -15,7 +15,7 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func createSocket(bindAddr unix.SockaddrInet4) (fd int, addr unix.SockaddrInet4, err error) { @@ -92,7 +92,7 @@ func randomLocalIP() [4]byte { return [4]byte{127, uint8(rand.Intn(256)), uint8(rand.Intn(256)), uint8(1 + rand.Intn(255))} } -func getListField(m common.MapStr, key string) ([]int, error) { +func getListField(m mapstr.M, key string) ([]int, error) { iface, ok := m[key] if !ok { return nil, fmt.Errorf("field %s not found", key) @@ -114,11 +114,11 @@ func getListField(m common.MapStr, key string) ([]int, error) { // Example // Input: [ {"A": [1,2,3,4], "B": [4, 5]}, {"A": [2,3,8], "B": [6]} ] // Output: { "A": [2,3], "B": [] } -func consolidate(partials []common.MapStr) (result common.MapStr, err error) { +func consolidate(partials []mapstr.M) (result mapstr.M, err error) { if len(partials) == 0 { return nil, errors.New("empty resultset to consolidate") } - result = make(common.MapStr) + result = make(mapstr.M) for k, v := range partials[0] { baseList, ok := v.([]int) diff --git a/x-pack/auditbeat/module/system/socket/guess/inetsock.go b/x-pack/auditbeat/module/system/socket/guess/inetsock.go index 8139bcf3ba7..707db38b7e7 100644 --- a/x-pack/auditbeat/module/system/socket/guess/inetsock.go +++ b/x-pack/auditbeat/module/system/socket/guess/inetsock.go @@ -14,9 +14,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Guess the offsets within a struct inet_sock where the local and remote @@ -140,7 +140,7 @@ func (g *guessInetSockIPv4) Trigger() error { // Extract receives the dump of a struct inet_sock* and scans it for the // random local and remote IPs and ports. Will return lists of all the // offsets were each value was found. -func (g *guessInetSockIPv4) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessInetSockIPv4) Extract(ev interface{}) (mapstr.M, bool) { data := ev.([]byte) laddr := g.local.Addr[:] @@ -182,7 +182,7 @@ func (g *guessInetSockIPv4) Extract(ev interface{}) (common.MapStr, bool) { return nil, false } - return common.MapStr{ + return mapstr.M{ "INET_SOCK_LADDR": laddrHits, "INET_SOCK_LPORT": lportHits, "INET_SOCK_RADDR": raddrHits, @@ -197,7 +197,7 @@ func (g *guessInetSockIPv4) NumRepeats() int { // Reduce receives the output from multiple runs (list of offsets for each field) // and for every field it returns the first offset that appeared all the runs. -func (g *guessInetSockIPv4) Reduce(results []common.MapStr) (result common.MapStr, err error) { +func (g *guessInetSockIPv4) Reduce(results []mapstr.M) (result mapstr.M, err error) { if result, err = consolidate(results); err != nil { return nil, err } diff --git a/x-pack/auditbeat/module/system/socket/guess/inetsock6.go b/x-pack/auditbeat/module/system/socket/guess/inetsock6.go index a66e5d14417..4a937a55485 100644 --- a/x-pack/auditbeat/module/system/socket/guess/inetsock6.go +++ b/x-pack/auditbeat/module/system/socket/guess/inetsock6.go @@ -14,9 +14,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -278,7 +278,7 @@ func (g *guessInetSockIPv6) Trigger() error { // Extract scans stores the events from the two different kprobes and then // looks for the result in one of them. -func (g *guessInetSockIPv6) Extract(event interface{}) (common.MapStr, bool) { +func (g *guessInetSockIPv6) Extract(event interface{}) (mapstr.M, bool) { if w, ok := event.(eventWrapper); ok { g.ptrDump = w.event.([]byte) } else { @@ -295,7 +295,7 @@ func (g *guessInetSockIPv6) Extract(event interface{}) (common.MapStr, bool) { return result, ok } -func (g *guessInetSockIPv6) searchStructSock(raw []byte) (common.MapStr, bool) { +func (g *guessInetSockIPv6) searchStructSock(raw []byte) (mapstr.M, bool) { var expected []byte expected = append(expected, g.clientAddr.Addr[:]...) // sck_v6_daddr expected = append(expected, g.serverAddr.Addr[:]...) // sck_v6_rcv_saddr @@ -303,7 +303,7 @@ func (g *guessInetSockIPv6) searchStructSock(raw []byte) (common.MapStr, bool) { if offset == -1 { return nil, false } - return common.MapStr{ + return mapstr.M{ "INET_SOCK_V6_TERM": ":u64", "INET_SOCK_V6_RADDR_A": fmt.Sprintf("+%d", offset), "INET_SOCK_V6_RADDR_B": fmt.Sprintf("+%d", offset+8), @@ -313,7 +313,7 @@ func (g *guessInetSockIPv6) searchStructSock(raw []byte) (common.MapStr, bool) { }, true } -func (g *guessInetSockIPv6) searchIPv6PInfo(raw []byte) (common.MapStr, bool) { +func (g *guessInetSockIPv6) searchIPv6PInfo(raw []byte) (mapstr.M, bool) { offset := bytes.Index(raw, g.serverAddr.Addr[:]) if offset == -1 { return nil, false @@ -323,7 +323,7 @@ func (g *guessInetSockIPv6) searchIPv6PInfo(raw []byte) (common.MapStr, bool) { return nil, false } off := g.offsets[idx] - return common.MapStr{ + return mapstr.M{ "INET_SOCK_V6_TERM": "):u64", "INET_SOCK_V6_RADDR_A": fmt.Sprintf("+%d(+%d", 32, off), "INET_SOCK_V6_RADDR_B": fmt.Sprintf("+%d(+%d", 40, off), diff --git a/x-pack/auditbeat/module/system/socket/guess/inetsockaf.go b/x-pack/auditbeat/module/system/socket/guess/inetsockaf.go index fd53364c892..60fbfed7105 100644 --- a/x-pack/auditbeat/module/system/socket/guess/inetsockaf.go +++ b/x-pack/auditbeat/module/system/socket/guess/inetsockaf.go @@ -12,9 +12,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -137,7 +137,7 @@ func (g *guessInetSockFamily) Trigger() error { } // Extract scans the struct inet_sock* memory for the current address family value. -func (g *guessInetSockFamily) Extract(event interface{}) (common.MapStr, bool) { +func (g *guessInetSockFamily) Extract(event interface{}) (mapstr.M, bool) { raw := event.([]byte) var expected [2]byte var hits []int @@ -151,7 +151,7 @@ func (g *guessInetSockFamily) Extract(event interface{}) (common.MapStr, bool) { if len(hits) == 0 { return nil, false } - return common.MapStr{ + return mapstr.M{ "INET_SOCK_AF": hits, }, true } @@ -162,7 +162,7 @@ func (g *guessInetSockFamily) NumRepeats() int { } // Reduce takes the output of the multiple runs and consolidates a single result. -func (g *guessInetSockFamily) Reduce(results []common.MapStr) (result common.MapStr, err error) { +func (g *guessInetSockFamily) Reduce(results []mapstr.M) (result mapstr.M, err error) { if result, err = consolidate(results); err != nil { return nil, err } diff --git a/x-pack/auditbeat/module/system/socket/guess/iplocalout.go b/x-pack/auditbeat/module/system/socket/guess/iplocalout.go index 0058cf28274..e80b6d4b7c1 100644 --- a/x-pack/auditbeat/module/system/socket/guess/iplocalout.go +++ b/x-pack/auditbeat/module/system/socket/guess/iplocalout.go @@ -13,9 +13,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Guess how to get a struct sock* and an sk_buff* from an ip_local_out() call. @@ -148,7 +148,7 @@ func (g *guessIPLocalOut) Trigger() error { // Extract first receives and saves the sock* from tcp_sendmsg. // Once ip_local_out is called, it analyses the captured arguments to determine // their layout. -func (g *guessIPLocalOut) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessIPLocalOut) Extract(ev interface{}) (mapstr.M, bool) { switch v := ev.(type) { case *sockArgumentGuess: g.sock = v.Sock @@ -162,13 +162,13 @@ func (g *guessIPLocalOut) Extract(ev interface{}) (common.MapStr, bool) { isIpLocalOut := g.ctx.Vars["IP_LOCAL_OUT"] != "ip_local_out_sk" if v.Arg == g.sock { if isIpLocalOut { - return common.MapStr{ + return mapstr.M{ // Second argument to ip_local_out is the struct sock* "IP_LOCAL_OUT_SOCK": g.ctx.Vars["P2"], "IP_LOCAL_OUT_SK_BUFF": g.ctx.Vars["P3"], }, true } - return common.MapStr{ + return mapstr.M{ // Second argument to ip_local_out is the struct sock* "IP_LOCAL_OUT_SOCK": g.ctx.Vars["P1"], "IP_LOCAL_OUT_SK_BUFF": g.ctx.Vars["P2"], @@ -176,7 +176,7 @@ func (g *guessIPLocalOut) Extract(ev interface{}) (common.MapStr, bool) { } off := indexAligned(v.Dump[:], ((*[sizeOfPtr]byte)(unsafe.Pointer(&g.sock)))[:], 0, int(sizeOfPtr)) if off != -1 { - return common.MapStr{ + return mapstr.M{ // struct sock* is a field of struct pointed to by first argument "IP_LOCAL_OUT_SOCK": fmt.Sprintf("+%d(%s)", off, g.ctx.Vars["P1"]), "IP_LOCAL_OUT_SK_BUFF": g.ctx.Vars["P1"], diff --git a/x-pack/auditbeat/module/system/socket/guess/skbuff.go b/x-pack/auditbeat/module/system/socket/guess/skbuff.go index be2a9bd678d..85589f8a4fe 100644 --- a/x-pack/auditbeat/module/system/socket/guess/skbuff.go +++ b/x-pack/auditbeat/module/system/socket/guess/skbuff.go @@ -17,9 +17,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -126,7 +126,7 @@ func (g *guessSkBuffLen) Trigger() error { // Extract scans the sk_buff memory for any values between the expected // payload + [0 ... 128). -func (g *guessSkBuffLen) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessSkBuffLen) Extract(ev interface{}) (mapstr.M, bool) { skbuff := ev.([]byte) if len(skbuff) != skbuffDumpSize || g.written <= 0 { return nil, false @@ -148,7 +148,7 @@ func (g *guessSkBuffLen) Extract(ev interface{}) (common.MapStr, bool) { } } - result := make(common.MapStr) + result := make(mapstr.M) var overhead []int for i := minHeadersSize; i < maxOverhead; i += ipHeaderSizeChunk { if len(results[i]) > 0 { @@ -170,14 +170,14 @@ func (g *guessSkBuffLen) NumRepeats() int { // Reduce takes the output from the multiple runs and returns the offset // which consistently returned the expected length plus a fixed constant. -func (g *guessSkBuffLen) Reduce(results []common.MapStr) (result common.MapStr, err error) { - clones := make([]common.MapStr, 0, len(results)) +func (g *guessSkBuffLen) Reduce(results []mapstr.M) (result mapstr.M, err error) { + clones := make([]mapstr.M, 0, len(results)) for _, res := range results { val, found := res["HEADER_SIZES"] if !found { return nil, errors.New("not all attempts detected offsets") } - m := make(common.MapStr, 1) + m := make(mapstr.M, 1) m["HEADER_SIZES"] = val clones = append(clones, m) } @@ -210,7 +210,7 @@ func (g *guessSkBuffLen) Reduce(results []common.MapStr) (result common.MapStr, return nil, err } - return common.MapStr{ + return mapstr.M{ "SK_BUFF_LEN": list[0], "DETECTED_HEADER_SIZE": headerSize, }, nil @@ -352,7 +352,7 @@ func (g *guessSkBuffProto) Trigger() error { // Extract will scan the sk_buff memory to look for all the uint16-sized memory // locations that contain the expected protocol value. -func (g *guessSkBuffProto) Extract(event interface{}) (common.MapStr, bool) { +func (g *guessSkBuffProto) Extract(event interface{}) (mapstr.M, bool) { raw := event.([]byte) needle := []byte{0x08, 0x00} // ETH_P_IP if g.doIPv6 { @@ -365,7 +365,7 @@ func (g *guessSkBuffProto) Extract(event interface{}) (common.MapStr, bool) { off = indexAligned(raw, needle, off+2, 2) } - return common.MapStr{ + return mapstr.M{ "SK_BUFF_PROTO": hits, }, true } @@ -377,7 +377,7 @@ func (g *guessSkBuffProto) NumRepeats() int { // Reduce uses the partial results from every repetition to figure out the // right offset of the protocol field. -func (g *guessSkBuffProto) Reduce(results []common.MapStr) (result common.MapStr, err error) { +func (g *guessSkBuffProto) Reduce(results []mapstr.M) (result mapstr.M, err error) { if result, err = consolidate(results); err != nil { return nil, err } @@ -621,7 +621,7 @@ func u32At(buf []byte) uintptr { // (any), false : Signals that it needs another event in the current iteration. // nil, true : Finish the current iteration and perform a new one. // (non-nil), true : The guess completed. -func (g *guessSkBuffDataPtr) Extract(event interface{}) (common.MapStr, bool) { +func (g *guessSkBuffDataPtr) Extract(event interface{}) (mapstr.M, bool) { switch v := event.(type) { case *dataDump: g.data = v @@ -636,7 +636,7 @@ func (g *guessSkBuffDataPtr) Extract(event interface{}) (common.MapStr, bool) { if g.dumpOffset >= skbuffDumpSize { // Scanned more memory than its available from sk_buff. Bail out. // Returns a non-nil map so the guess is not repeated until MaxRepeats() - return common.MapStr{"FAILED": true}, true + return mapstr.M{"FAILED": true}, true } // @@ -690,7 +690,7 @@ func (g *guessSkBuffDataPtr) Extract(event interface{}) (common.MapStr, bool) { limit := g.protoOffset + 2 protocolValue := binary.BigEndian.Uint16(g.skbuff[g.protoOffset:]) - scanFields := func(width int, ptrBase uintptr, reader func([]byte) uintptr) common.MapStr { + scanFields := func(width int, ptrBase uintptr, reader func([]byte) uintptr) mapstr.M { var off [3]uintptr for base := g.dumpOffset - width*3; base >= limit; base -= width { for i := 0; i < 3; i++ { @@ -702,7 +702,7 @@ func (g *guessSkBuffDataPtr) Extract(event interface{}) (common.MapStr, bool) { off[2] != -ptrBase && off[2] <= uintptr(ipHdrOff-14) && binary.BigEndian.Uint16(g.data.Data[off[2]+12:]) == protocolValue { - return common.MapStr{ + return mapstr.M{ "SK_BUFF_HEAD": g.dumpOffset, "SK_BUFF_TRANSPORT": base, "SK_BUFF_NETWORK": base + width, diff --git a/x-pack/auditbeat/module/system/socket/guess/sockaddrin.go b/x-pack/auditbeat/module/system/socket/guess/sockaddrin.go index 78ab0197983..2a76d564ba5 100644 --- a/x-pack/auditbeat/module/system/socket/guess/sockaddrin.go +++ b/x-pack/auditbeat/module/system/socket/guess/sockaddrin.go @@ -14,9 +14,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -124,7 +124,7 @@ func (g *guessSockaddrIn) Trigger() error { } // Extract takes the dumped sockaddr_in and scans it for the expected values. -func (g *guessSockaddrIn) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessSockaddrIn) Extract(ev interface{}) (mapstr.M, bool) { arr := ev.([]byte) if len(arr) < 8 { return nil, false @@ -146,7 +146,7 @@ func (g *guessSockaddrIn) Extract(ev interface{}) (common.MapStr, bool) { if offsetOfAddr == -1 { return nil, false } - return common.MapStr{ + return mapstr.M{ "SOCKADDR_IN_AF": offsetOfFamily, "SOCKADDR_IN_PORT": offsetOfPort, "SOCKADDR_IN_ADDR": offsetOfAddr, diff --git a/x-pack/auditbeat/module/system/socket/guess/sockaddrin6.go b/x-pack/auditbeat/module/system/socket/guess/sockaddrin6.go index 5a2af0f7f5d..5564015530b 100644 --- a/x-pack/auditbeat/module/system/socket/guess/sockaddrin6.go +++ b/x-pack/auditbeat/module/system/socket/guess/sockaddrin6.go @@ -13,9 +13,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -144,7 +144,7 @@ func (g *guessSockaddrIn6) Trigger() error { // Extract receives the dumped struct sockaddr_in6 and scans it for the // expected values. -func (g *guessSockaddrIn6) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessSockaddrIn6) Extract(ev interface{}) (mapstr.M, bool) { arr := ev.([]byte) if len(arr) < 8 { return nil, false @@ -166,7 +166,7 @@ func (g *guessSockaddrIn6) Extract(ev interface{}) (common.MapStr, bool) { if offsetOfAddr == -1 { return nil, false } - return common.MapStr{ + return mapstr.M{ "SOCKADDR_IN6_AF": offsetOfFamily, "SOCKADDR_IN6_PORT": offsetOfPort, "SOCKADDR_IN6_ADDRA": offsetOfAddr, diff --git a/x-pack/auditbeat/module/system/socket/guess/socketsk.go b/x-pack/auditbeat/module/system/socket/guess/socketsk.go index fa9c0b454c4..5ebc0ab7de6 100644 --- a/x-pack/auditbeat/module/system/socket/guess/socketsk.go +++ b/x-pack/auditbeat/module/system/socket/guess/socketsk.go @@ -12,9 +12,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Guess the offset of (struct socket*)->sk (type struct sock*) @@ -112,7 +112,7 @@ func (g *guessSocketSock) Trigger() error { // Extract first receives the sock* from sock_init_data, then uses it to // scan the dump from inet_release. -func (g *guessSocketSock) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessSocketSock) Extract(ev interface{}) (mapstr.M, bool) { if v, ok := ev.(*sockEvent); ok { if g.initData != nil { return nil, false @@ -134,7 +134,7 @@ func (g *guessSocketSock) Extract(ev interface{}) (common.MapStr, bool) { return nil, false } - return common.MapStr{ + return mapstr.M{ "SOCKET_SOCK": off, }, true } diff --git a/x-pack/auditbeat/module/system/socket/guess/syscallargs.go b/x-pack/auditbeat/module/system/socket/guess/syscallargs.go index f3a17017ab2..3930e7134b9 100644 --- a/x-pack/auditbeat/module/system/socket/guess/syscallargs.go +++ b/x-pack/auditbeat/module/system/socket/guess/syscallargs.go @@ -12,9 +12,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) /* @@ -112,7 +112,7 @@ func (g *guessSyscallArgs) Trigger() error { } // Extract check which set of kprobe arguments received the magic values. -func (g *guessSyscallArgs) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessSyscallArgs) Extract(ev interface{}) (mapstr.M, bool) { args, ok := ev.(*syscallGuess) if !ok { return nil, false @@ -120,7 +120,7 @@ func (g *guessSyscallArgs) Extract(ev interface{}) (common.MapStr, bool) { if args.RegP1 == g.expected[0] && args.RegP2 == g.expected[1] { // Current kernel uses the old calling convention. - return common.MapStr{ + return mapstr.M{ "SYS_P1": g.ctx.Vars["_SYS_P1"], "SYS_P2": g.ctx.Vars["_SYS_P2"], "SYS_P3": g.ctx.Vars["_SYS_P3"], @@ -134,7 +134,7 @@ func (g *guessSyscallArgs) Extract(ev interface{}) (common.MapStr, bool) { // This hardcodes %di as arg1, which is only true for amd64, but this // calling convention is only used under amd64. if args.PtP1 == g.expected[0] && args.PtP2 == g.expected[1] { - return common.MapStr{ + return mapstr.M{ "SYS_P1": "+0x70(%di)", "SYS_P2": "+0x68(%di)", "SYS_P3": "+0x60(%di)", diff --git a/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgargs.go b/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgargs.go index 9e705ad2ab9..faa3910ba5f 100644 --- a/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgargs.go +++ b/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgargs.go @@ -10,9 +10,9 @@ package guess import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Guess the position of size parameter in tcp_sendmsg. @@ -94,7 +94,7 @@ func (g *guessTCPSendMsg) Trigger() (err error) { // Extract receives the arguments from the tcp_sendmsg call and checks // which one contains the number of bytes written by trigger. -func (g *guessTCPSendMsg) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessTCPSendMsg) Extract(ev interface{}) (mapstr.M, bool) { event := ev.(*tcpSendMsgArgCountGuess) if g.written <= 0 { g.ctx.Log.Errorf("write failed for guess") @@ -112,7 +112,7 @@ func (g *guessTCPSendMsg) Extract(ev interface{}) (common.MapStr, bool) { default: return nil, false } - return common.MapStr{ + return mapstr.M{ "TCP_SENDMSG_LEN": lenParam, }, true } diff --git a/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgsk.go b/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgsk.go index 55000d7f8f2..450a336df6e 100644 --- a/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgsk.go +++ b/x-pack/auditbeat/module/system/socket/guess/tcpsendmsgsk.go @@ -12,9 +12,9 @@ import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Guess how to get a struct sock* from tcp_sendmsg parameters. It can be: @@ -103,7 +103,7 @@ func (g *guessTcpSendmsgSock) Trigger() (err error) { // Extract checks which of the arguments to tcp_sendmsg contains the expected // value (address of destination). -func (g *guessTcpSendmsgSock) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessTcpSendmsgSock) Extract(ev interface{}) (mapstr.M, bool) { event := ev.(*tcpSendMsgSockGuess) if g.written <= 0 { g.ctx.Log.Errorf("write failed for guess") @@ -125,7 +125,7 @@ func (g *guessTcpSendmsgSock) Extract(ev interface{}) (common.MapStr, bool) { default: return nil, false } - return common.MapStr{ + return mapstr.M{ "TCP_SENDMSG_SOCK": param, }, true } diff --git a/x-pack/auditbeat/module/system/socket/guess/udpsendmsg.go b/x-pack/auditbeat/module/system/socket/guess/udpsendmsg.go index 66711d57d7d..5ab70f92a48 100644 --- a/x-pack/auditbeat/module/system/socket/guess/udpsendmsg.go +++ b/x-pack/auditbeat/module/system/socket/guess/udpsendmsg.go @@ -10,9 +10,9 @@ package guess import ( "golang.org/x/sys/unix" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // guess udp_sendmsg arguments: @@ -90,20 +90,20 @@ func (g *guessUDPSendMsg) Terminate() error { return g.cs.Cleanup() } -func (g *guessUDPSendMsg) Extract(ev interface{}) (common.MapStr, bool) { +func (g *guessUDPSendMsg) Extract(ev interface{}) (mapstr.M, bool) { if g.length == 0 { return nil, false } event := ev.(*udpSendMsgCountGuess) if event.Param3 == g.length { - return common.MapStr{ + return mapstr.M{ "UDP_SENDMSG_SOCK": g.ctx.Vars["P1"], "UDP_SENDMSG_MSG": g.ctx.Vars["P2"], "UDP_SENDMSG_LEN": g.ctx.Vars["P3"], }, true } if event.Param4 == g.length { - return common.MapStr{ + return mapstr.M{ "UDP_SENDMSG_SOCK": g.ctx.Vars["P2"], "UDP_SENDMSG_MSG": g.ctx.Vars["P3"], "UDP_SENDMSG_LEN": g.ctx.Vars["P4"], diff --git a/x-pack/auditbeat/module/system/socket/helper/probes.go b/x-pack/auditbeat/module/system/socket/helper/probes.go index ddffe15d318..03ed11b8540 100644 --- a/x-pack/auditbeat/module/system/socket/helper/probes.go +++ b/x-pack/auditbeat/module/system/socket/helper/probes.go @@ -13,8 +13,8 @@ import ( "strings" "text/template" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ProbeDef couples a probe with a decoder factory. @@ -24,14 +24,14 @@ type ProbeDef struct { } // ApplyTemplate returns a new probe definition after expanding all templates. -func (pdef ProbeDef) ApplyTemplate(vars common.MapStr) ProbeDef { +func (pdef ProbeDef) ApplyTemplate(vars mapstr.M) ProbeDef { pdef.Probe.Address = applyTemplate(pdef.Probe.Address, vars) pdef.Probe.Fetchargs = applyTemplate(pdef.Probe.Fetchargs, vars) pdef.Probe.Filter = applyTemplate(pdef.Probe.Filter, vars) return pdef } -func applyTemplate(s string, vars common.MapStr) string { +func applyTemplate(s string, vars mapstr.M) string { buf := &bytes.Buffer{} if err := template.Must(template.New("").Parse(s)).Execute(buf, vars); err != nil { panic(err) diff --git a/x-pack/auditbeat/module/system/socket/kprobes.go b/x-pack/auditbeat/module/system/socket/kprobes.go index f56d542020a..97c0d7a6219 100644 --- a/x-pack/auditbeat/module/system/socket/kprobes.go +++ b/x-pack/auditbeat/module/system/socket/kprobes.go @@ -15,9 +15,9 @@ import ( "github.com/joeshaw/multierror" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // This is how many data we dump from sk_buff->data to read full packet headers @@ -101,7 +101,7 @@ func WithGroup(name string) ProbeTransform { } // WithTemplates expands templates in probes before they are installed. -func WithTemplates(vars common.MapStr) ProbeTransform { +func WithTemplates(vars mapstr.M) ProbeTransform { return func(probe helper.ProbeDef) helper.ProbeDef { return probe.ApplyTemplate(vars) } diff --git a/x-pack/auditbeat/module/system/socket/socket_linux.go b/x-pack/auditbeat/module/system/socket/socket_linux.go index f6d38003b88..2cb4f7e5190 100644 --- a/x-pack/auditbeat/module/system/socket/socket_linux.go +++ b/x-pack/auditbeat/module/system/socket/socket_linux.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/guess" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-perf" "github.com/elastic/go-sysinfo" "github.com/elastic/go-sysinfo/providers/linux" @@ -66,7 +67,7 @@ var defaultMounts = []*mountPoint{ // MetricSet for system/socket. type MetricSet struct { system.SystemMetricSet - templateVars common.MapStr + templateVars mapstr.M config Config log *logp.Logger detailLog *logp.Logger @@ -131,7 +132,7 @@ func newSocketMetricset(config Config, base mb.BaseMetricSet) (*MetricSet, error } ms := &MetricSet{ SystemMetricSet: system.NewSystemMetricSet(base), - templateVars: make(common.MapStr), + templateVars: make(mapstr.M), config: config, log: logger, isDebug: logp.IsDebug(metricsetName), diff --git a/x-pack/auditbeat/module/system/socket/state.go b/x-pack/auditbeat/module/system/socket/state.go index c1e6865c2c2..9a95571466c 100644 --- a/x-pack/auditbeat/module/system/socket/state.go +++ b/x-pack/auditbeat/module/system/socket/state.go @@ -26,6 +26,7 @@ import ( "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/dns" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system/socket/helper" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-libaudit/v2/aucoalesce" ) @@ -934,14 +935,14 @@ func (f *flow) toEvent(final bool) (ev mb.Event, err error) { localAddr := f.local.addr remoteAddr := f.remote.addr - local := common.MapStr{ + local := mapstr.M{ "ip": localAddr.IP.String(), "port": localAddr.Port, "packets": f.local.packets, "bytes": f.local.bytes, } - remote := common.MapStr{ + remote := mapstr.M{ "ip": remoteAddr.IP.String(), "port": remoteAddr.Port, "packets": f.remote.packets, @@ -976,12 +977,12 @@ func (f *flow) toEvent(final bool) (ev mb.Event, err error) { eventType = append(eventType, "connection") } - root := common.MapStr{ + root := mapstr.M{ "source": src, "client": src, "destination": dst, "server": dst, - "network": common.MapStr{ + "network": mapstr.M{ "direction": f.dir.String(), "type": inetType.String(), "transport": f.proto.String(), @@ -995,7 +996,7 @@ func (f *flow) toEvent(final bool) (ev mb.Event, err error) { Protocol: uint8(f.proto), }), }, - "event": common.MapStr{ + "event": mapstr.M{ "kind": "event", "action": "network_flow", "category": []string{"network", "network_traffic"}, @@ -1004,7 +1005,7 @@ func (f *flow) toEvent(final bool) (ev mb.Event, err error) { "end": f.lastSeenTime, "duration": f.lastSeenTime.Sub(f.createdTime).Nanoseconds(), }, - "flow": common.MapStr{ + "flow": mapstr.M{ "final": final, "complete": f.complete, }, @@ -1021,12 +1022,12 @@ func (f *flow) toEvent(final bool) (ev mb.Event, err error) { root.Put("related.ip", relatedIPs) } - metricset := common.MapStr{ + metricset := mapstr.M{ "kernel_sock_address": fmt.Sprintf("0x%x", f.sock), } if f.pid != 0 { - process := common.MapStr{ + process := mapstr.M{ "pid": int(f.pid), } if f.process != nil { diff --git a/x-pack/auditbeat/module/system/socket/template.go b/x-pack/auditbeat/module/system/socket/template.go index 43d520cd1c9..aa1f5702e65 100644 --- a/x-pack/auditbeat/module/system/socket/template.go +++ b/x-pack/auditbeat/module/system/socket/template.go @@ -13,12 +13,13 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/auditbeat/tracing" + "github.com/elastic/elastic-agent-libs/mapstr" ) // baseTemplateVars contains the substitution variables useful to write KProbes // in a portable fashion. During setup it will be populated with arch-dependent // variables and guessed offsets. -var baseTemplateVars = common.MapStr{ +var baseTemplateVars = mapstr.M{ // Constants to make KProbes more readable "AF_INET": 2, "AF_INET6": 10, diff --git a/x-pack/auditbeat/module/system/user/user.go b/x-pack/auditbeat/module/system/user/user.go index e51c970b290..f5d9b5d684b 100644 --- a/x-pack/auditbeat/module/system/user/user.go +++ b/x-pack/auditbeat/module/system/user/user.go @@ -24,12 +24,12 @@ import ( "github.com/joeshaw/multierror" "github.com/elastic/beats/v7/auditbeat/datastore" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/auditbeat/cache" "github.com/elastic/beats/v7/x-pack/auditbeat/module/system" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -154,8 +154,8 @@ func (user User) Hash() uint64 { return h.Sum64() } -func (user User) toMapStr() common.MapStr { - evt := common.MapStr{ +func (user User) toMapStr() mapstr.M { + evt := mapstr.M{ "name": user.Name, "uid": user.UID, "gid": user.GID, @@ -176,9 +176,9 @@ func (user User) toMapStr() common.MapStr { } if len(user.Groups) > 0 { - var groupMapStr []common.MapStr + var groupMapStr []mapstr.M for _, group := range user.Groups { - groupMapStr = append(groupMapStr, common.MapStr{ + groupMapStr = append(groupMapStr, mapstr.M{ "name": group.Name, "gid": group.Gid, "id": group.Gid, @@ -455,18 +455,18 @@ func (ms *MetricSet) reportChanges(report mb.ReporterV2) error { func (ms *MetricSet) userEvent(user *User, eventType string, action eventAction) mb.Event { event := mb.Event{ - RootFields: common.MapStr{ - "event": common.MapStr{ + RootFields: mapstr.M{ + "event": mapstr.M{ "kind": eventType, "category": []string{"iam"}, "type": []string{action.Type()}, "action": action.String(), }, - "user": common.MapStr{ + "user": mapstr.M{ "id": user.UID, "name": user.Name, }, - "related": common.MapStr{ + "related": mapstr.M{ "user": []string{user.Name}, }, "message": userMessage(user, action), @@ -480,12 +480,12 @@ func (ms *MetricSet) userEvent(user *User, eventType string, action eventAction) primaryGroup := user.PrimaryGroup() if primaryGroup != nil { - event.RootFields.Put("user.group", common.MapStr{ + event.RootFields.Put("user.group", mapstr.M{ "id": primaryGroup.Gid, "name": primaryGroup.Name, }) } else if user.GID != "" { // fallback to just filling out the GID - event.RootFields.Put("user.group", common.MapStr{ + event.RootFields.Put("user.group", mapstr.M{ "id": user.GID, }) } diff --git a/x-pack/dockerlogbeat/pipelinemanager/clientLogReader.go b/x-pack/dockerlogbeat/pipelinemanager/clientLogReader.go index 6fb52fb52aa..1bef7aaf455 100644 --- a/x-pack/dockerlogbeat/pipelinemanager/clientLogReader.go +++ b/x-pack/dockerlogbeat/pipelinemanager/clientLogReader.go @@ -15,11 +15,11 @@ import ( "github.com/docker/docker/api/types/backend" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/acker" helper "github.com/elastic/beats/v7/libbeat/common/docker" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/dockerlogbeat/pipereader" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ClientLogger collects logs for a docker container logging to stdout and stderr, using the FIFO provided by the docker daemon. @@ -33,7 +33,7 @@ type ClientLogger struct { // ContainerMeta is the metadata object for the container we get from docker ContainerMeta logger.Info // ContainerECSMeta is a container metadata object appended to every event - ContainerECSMeta common.MapStr + ContainerECSMeta mapstr.M // logFile is the FIFO reader that reads from the docker container stdio logFile *pipereader.PipeReader // client is the libbeat client object that sends logs upstream @@ -107,7 +107,7 @@ func (cl *ClientLogger) ConsumePipelineAndSend() { } // constructECSContainerData creates an ES-ready MapString object with container metadata. -func constructECSContainerData(metadata logger.Info) common.MapStr { +func constructECSContainerData(metadata logger.Info) mapstr.M { var containerImageName, containerImageTag string if idx := strings.IndexRune(metadata.ContainerImageName, ':'); idx >= 0 { @@ -115,11 +115,11 @@ func constructECSContainerData(metadata logger.Info) common.MapStr { containerImageTag = string([]rune(metadata.ContainerImageName)[idx+1:]) } - return common.MapStr{ + return mapstr.M{ "labels": helper.DeDotLabels(metadata.ContainerLabels, true), "id": metadata.ContainerID, "name": helper.ExtractContainerName([]string{metadata.ContainerName}), - "image": common.MapStr{ + "image": mapstr.M{ "name": containerImageName, "tag": containerImageTag, }, @@ -142,10 +142,10 @@ func (cl *ClientLogger) publishLoop(reader chan logdriver.LogEntry) { cl.client.Publish(beat.Event{ Timestamp: time.Unix(0, entry.TimeNano), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": line, "container": cl.ContainerECSMeta, - "host": common.MapStr{ + "host": mapstr.M{ "name": cl.hostname, }, }, diff --git a/x-pack/dockerlogbeat/pipelinemanager/clientLogReader_test.go b/x-pack/dockerlogbeat/pipelinemanager/clientLogReader_test.go index 4158ba554ef..f4bd8be5e8c 100644 --- a/x-pack/dockerlogbeat/pipelinemanager/clientLogReader_test.go +++ b/x-pack/dockerlogbeat/pipelinemanager/clientLogReader_test.go @@ -19,9 +19,9 @@ import ( "github.com/docker/docker/daemon/logger/jsonfilelog" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/dockerlogbeat/pipelinemock" "github.com/elastic/beats/v7/x-pack/dockerlogbeat/pipereader" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConfigHosts(t *testing.T) { @@ -55,7 +55,7 @@ func TestNewClient(t *testing.T) { event := testReturn(t, client) assert.Equal(t, event.Fields["message"], logString) - assert.Equal(t, event.Fields["container"].(common.MapStr)["name"], "testContainer") + assert.Equal(t, event.Fields["container"].(mapstr.M)["name"], "testContainer") } // setupTestReader sets up the "read side" of the pipeline, spawing a goroutine to read and event and send it back to the test. diff --git a/x-pack/filebeat/input/awscloudwatch/input_integration_test.go b/x-pack/filebeat/input/awscloudwatch/input_integration_test.go index a6d17d647f2..928fb8a750d 100644 --- a/x-pack/filebeat/input/awscloudwatch/input_integration_test.go +++ b/x-pack/filebeat/input/awscloudwatch/input_integration_test.go @@ -37,6 +37,7 @@ import ( "github.com/elastic/beats/v7/libbeat/statestore" "github.com/elastic/beats/v7/libbeat/statestore/storetest" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -47,7 +48,7 @@ const ( logGroupNamePrefix = "filebeat-log-group-integtest-" ) -var cloudwatchConfig = common.MapStr{ +var cloudwatchConfig = mapstr.M{ "start_position": "beginning", "scan_frequency": 10 * time.Second, "api_timeout": 120 * time.Second, @@ -83,7 +84,7 @@ func getTerraformOutputs(t *testing.T) terraformOutputData { return rtn } -func assertMetric(t *testing.T, snapshot common.MapStr, name string, value interface{}) { +func assertMetric(t *testing.T, snapshot mapstr.M, name string, value interface{}) { n, _ := snapshot.GetValue(inputID + "." + name) assert.EqualValues(t, value, n, name) } @@ -218,7 +219,7 @@ func TestInputWithLogGroupNamePrefix(t *testing.T) { t.Fatal(err) } - snap := common.MapStr(monitoring.CollectStructSnapshot( + snap := mapstr.M(monitoring.CollectStructSnapshot( monitoring.GetNamespace("dataset").GetRegistry(), monitoring.Full, false)) diff --git a/x-pack/filebeat/input/awscloudwatch/input_test.go b/x-pack/filebeat/input/awscloudwatch/input_test.go index 1e298a1d067..bf0b5c598af 100644 --- a/x-pack/filebeat/input/awscloudwatch/input_test.go +++ b/x-pack/filebeat/input/awscloudwatch/input_test.go @@ -11,11 +11,11 @@ import ( "github.com/stretchr/testify/assert" + "github.com/elastic/elastic-agent-libs/mapstr" + awssdk "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/cloudwatchlogsiface" - - "github.com/elastic/beats/v7/libbeat/common" ) func TestGetStartPosition(t *testing.T) { @@ -165,18 +165,18 @@ func TestCreateEvent(t *testing.T) { Timestamp: awssdk.Int64(1600000000000), } - expectedEventFields := common.MapStr{ + expectedEventFields := mapstr.M{ "message": "test-message-1", - "event": common.MapStr{ + "event": mapstr.M{ "id": *logEvent.EventId, }, "log.file.path": "logGroup1" + "/" + *logEvent.LogStreamName, - "awscloudwatch": common.MapStr{ + "awscloudwatch": mapstr.M{ "log_group": "logGroup1", "log_stream": *logEvent.LogStreamName, "ingestion_time": time.Unix(*logEvent.IngestionTime/1000, 0), }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", "region": "us-east-1", }, diff --git a/x-pack/filebeat/input/awscloudwatch/processor.go b/x-pack/filebeat/input/awscloudwatch/processor.go index 558e91d5da5..89d99e4d178 100644 --- a/x-pack/filebeat/input/awscloudwatch/processor.go +++ b/x-pack/filebeat/input/awscloudwatch/processor.go @@ -11,10 +11,10 @@ import ( "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) type logProcessor struct { @@ -54,19 +54,19 @@ func (p *logProcessor) publish(ack *awscommon.EventACKTracker, event *beat.Event func createEvent(logEvent cloudwatchlogs.FilteredLogEvent, logGroup string, regionName string) beat.Event { event := beat.Event{ Timestamp: time.Unix(*logEvent.Timestamp/1000, 0).UTC(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": *logEvent.Message, "log.file.path": logGroup + "/" + *logEvent.LogStreamName, - "event": common.MapStr{ + "event": mapstr.M{ "id": *logEvent.EventId, "ingested": time.Now(), }, - "awscloudwatch": common.MapStr{ + "awscloudwatch": mapstr.M{ "log_group": logGroup, "log_stream": *logEvent.LogStreamName, "ingestion_time": time.Unix(*logEvent.IngestionTime/1000, 0), }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", "region": regionName, }, diff --git a/x-pack/filebeat/input/awss3/config_test.go b/x-pack/filebeat/input/awss3/config_test.go index e465d5b1b01..b24cb9ccd55 100644 --- a/x-pack/filebeat/input/awss3/config_test.go +++ b/x-pack/filebeat/input/awss3/config_test.go @@ -17,6 +17,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/match" "github.com/elastic/beats/v7/libbeat/reader/parser" "github.com/elastic/beats/v7/libbeat/reader/readfile" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestConfig(t *testing.T) { @@ -54,7 +55,7 @@ func TestConfig(t *testing.T) { queueURL string s3Bucket string nonAWSS3Bucket string - config common.MapStr + config mapstr.M expectedErr string expectedCfg func(queueURL, s3Bucket string, nonAWSS3Bucket string) config }{ @@ -63,7 +64,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, }, "", @@ -74,7 +75,7 @@ func TestConfig(t *testing.T) { "", s3Bucket, "", - common.MapStr{ + mapstr.M{ "bucket_arn": s3Bucket, "number_of_workers": 5, }, @@ -90,9 +91,9 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, - "file_selectors": []common.MapStr{ + "file_selectors": []mapstr.M{ { "regex": "/CloudTrail/", }, @@ -116,7 +117,7 @@ func TestConfig(t *testing.T) { "", "", "", - common.MapStr{ + mapstr.M{ "queue_url": "", "bucket_arn": "", "non_aws_bucket_name": "", @@ -129,7 +130,7 @@ func TestConfig(t *testing.T) { queueURL, s3Bucket, "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "bucket_arn": s3Bucket, }, @@ -141,7 +142,7 @@ func TestConfig(t *testing.T) { queueURL, "", nonAWSS3Bucket, - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "non_aws_bucket_name": nonAWSS3Bucket, }, @@ -153,7 +154,7 @@ func TestConfig(t *testing.T) { "", s3Bucket, nonAWSS3Bucket, - common.MapStr{ + mapstr.M{ "bucket_arn": s3Bucket, "non_aws_bucket_name": nonAWSS3Bucket, }, @@ -165,7 +166,7 @@ func TestConfig(t *testing.T) { queueURL, s3Bucket, nonAWSS3Bucket, - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "bucket_arn": s3Bucket, "non_aws_bucket_name": nonAWSS3Bucket, @@ -178,7 +179,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "api_timeout": "0", }, @@ -190,7 +191,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "visibility_timeout": "0", }, @@ -202,7 +203,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "visibility_timeout": "12h1ns", }, @@ -214,7 +215,7 @@ func TestConfig(t *testing.T) { "", s3Bucket, "", - common.MapStr{ + mapstr.M{ "bucket_arn": s3Bucket, "bucket_list_interval": "0", }, @@ -226,7 +227,7 @@ func TestConfig(t *testing.T) { "", s3Bucket, "", - common.MapStr{ + mapstr.M{ "bucket_arn": s3Bucket, "number_of_workers": "0", }, @@ -238,7 +239,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "max_number_of_messages": "0", }, @@ -250,7 +251,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "buffer_size": "0", }, @@ -262,7 +263,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "max_bytes": "0", }, @@ -274,7 +275,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "expand_event_list_from_field": "Records", "content_type": "text/plain", @@ -287,7 +288,7 @@ func TestConfig(t *testing.T) { "", s3Bucket, "", - common.MapStr{ + mapstr.M{ "bucket_arn": s3Bucket, "expand_event_list_from_field": "Records", "content_type": "text/plain", @@ -300,7 +301,7 @@ func TestConfig(t *testing.T) { "", "", nonAWSS3Bucket, - common.MapStr{ + mapstr.M{ "non_aws_bucket_name": nonAWSS3Bucket, "number_of_workers": 5, }, @@ -316,7 +317,7 @@ func TestConfig(t *testing.T) { "", "", nonAWSS3Bucket, - common.MapStr{ + mapstr.M{ "non_aws_bucket_name": nonAWSS3Bucket, "number_of_workers": 5, "fips_enabled": true, @@ -329,7 +330,7 @@ func TestConfig(t *testing.T) { "", s3Bucket, "", - common.MapStr{ + mapstr.M{ "bucket_arn": s3Bucket, "number_of_workers": 5, "path_style": true, @@ -342,7 +343,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "number_of_workers": 5, "path_style": true, @@ -355,7 +356,7 @@ func TestConfig(t *testing.T) { "", s3Bucket, "", - common.MapStr{ + mapstr.M{ "bucket_arn": s3Bucket, "number_of_workers": 5, "provider": "asdf", @@ -368,7 +369,7 @@ func TestConfig(t *testing.T) { queueURL, "", "", - common.MapStr{ + mapstr.M{ "queue_url": queueURL, "number_of_workers": 5, "provider": "asdf", diff --git a/x-pack/filebeat/input/awss3/input_integration_test.go b/x-pack/filebeat/input/awss3/input_integration_test.go index d112e6a4c35..cc3f9da86ff 100644 --- a/x-pack/filebeat/input/awss3/input_integration_test.go +++ b/x-pack/filebeat/input/awss3/input_integration_test.go @@ -22,6 +22,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws/external" @@ -225,7 +226,7 @@ func TestInputRunSQS(t *testing.T) { t.Fatal(err) } - snap := common.MapStr(monitoring.CollectStructSnapshot( + snap := mapstr.M(monitoring.CollectStructSnapshot( monitoring.GetNamespace("dataset").GetRegistry(), monitoring.Full, false)) @@ -288,7 +289,7 @@ func TestInputRunS3(t *testing.T) { t.Fatal(err) } - snap := common.MapStr(monitoring.CollectStructSnapshot( + snap := mapstr.M(monitoring.CollectStructSnapshot( monitoring.GetNamespace("dataset").GetRegistry(), monitoring.Full, false)) @@ -302,7 +303,7 @@ func TestInputRunS3(t *testing.T) { assertMetric(t, snap, "s3_events_created_total", 12) } -func assertMetric(t *testing.T, snapshot common.MapStr, name string, value interface{}) { +func assertMetric(t *testing.T, snapshot mapstr.M, name string, value interface{}) { n, _ := snapshot.GetValue(inputID + "." + name) assert.EqualValues(t, value, n, name) } @@ -493,7 +494,7 @@ func TestInputRunSNS(t *testing.T) { t.Fatal(err) } - snap := common.MapStr(monitoring.CollectStructSnapshot( + snap := mapstr.M(monitoring.CollectStructSnapshot( monitoring.GetNamespace("dataset").GetRegistry(), monitoring.Full, false)) diff --git a/x-pack/filebeat/input/awss3/s3_objects.go b/x-pack/filebeat/input/awss3/s3_objects.go index 726619945bf..8111afe18d1 100644 --- a/x-pack/filebeat/input/awss3/s3_objects.go +++ b/x-pack/filebeat/input/awss3/s3_objects.go @@ -20,12 +20,12 @@ import ( "time" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/monitoring" "github.com/elastic/beats/v7/libbeat/reader" @@ -324,26 +324,26 @@ func (p *s3ObjectProcessor) publish(ack *awscommon.EventACKTracker, event *beat. func (p *s3ObjectProcessor) createEvent(message string, offset int64) beat.Event { event := beat.Event{ Timestamp: time.Now().UTC(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": message, - "log": common.MapStr{ + "log": mapstr.M{ "offset": offset, - "file": common.MapStr{ + "file": mapstr.M{ "path": p.s3RequestURL, }, }, - "aws": common.MapStr{ - "s3": common.MapStr{ - "bucket": common.MapStr{ + "aws": mapstr.M{ + "s3": mapstr.M{ + "bucket": mapstr.M{ "name": p.s3Obj.S3.Bucket.Name, "arn": p.s3Obj.S3.Bucket.ARN, }, - "object": common.MapStr{ + "object": mapstr.M{ "key": p.s3Obj.S3.Object.Key, }, }, }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": p.s3Obj.Provider, "region": p.s3Obj.AWSRegion, }, @@ -386,7 +386,7 @@ func isStreamGzipped(r *bufio.Reader) (bool, error) { } // s3Metadata returns a map containing the selected S3 object metadata keys. -func s3Metadata(resp *s3.GetObjectResponse, keys ...string) common.MapStr { +func s3Metadata(resp *s3.GetObjectResponse, keys ...string) mapstr.M { if len(keys) == 0 { return nil } @@ -441,7 +441,7 @@ func s3Metadata(resp *s3.GetObjectResponse, keys ...string) common.MapStr { } // Select the matching headers from the config. - metadata := common.MapStr{} + metadata := mapstr.M{} for _, key := range keys { key = strings.ToLower(key) diff --git a/x-pack/filebeat/input/awss3/script_session.go b/x-pack/filebeat/input/awss3/script_session.go index aad0539665e..4ded757939b 100644 --- a/x-pack/filebeat/input/awss3/script_session.go +++ b/x-pack/filebeat/input/awss3/script_session.go @@ -13,8 +13,8 @@ import ( "github.com/pkg/errors" "go.uber.org/zap" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -47,11 +47,11 @@ func newSession(p *goja.Program, conf scriptConfig, test bool) (*session, error) timeout: conf.Timeout, } - // Register common.MapStr as being a simple map[string]interface{} for + // Register mapstr.M as being a simple map[string]interface{} for // treatment within the JS VM. - s.vm.RegisterSimpleMapType(reflect.TypeOf(common.MapStr(nil)), + s.vm.RegisterSimpleMapType(reflect.TypeOf(mapstr.M(nil)), func(i interface{}) map[string]interface{} { - return map[string]interface{}(i.(common.MapStr)) + return map[string]interface{}(i.(mapstr.M)) }, ) @@ -144,7 +144,7 @@ func (s *session) runParseFunc(n string) (out []s3EventV2, err error) { if r := recover(); r != nil { s.log.Errorw("The javascript script caused an unexpected panic "+ "while parsing a notification. Recovering, but please report this.", - "notification", common.MapStr{"original": n}, + "notification", mapstr.M{"original": n}, "panic", r, zap.Stack("stack")) err = fmt.Errorf("unexpected panic in javascript script: %v", r) diff --git a/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go b/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go index 68ed2996731..01f5830d749 100644 --- a/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go +++ b/x-pack/filebeat/input/azureeventhub/azureeventhub_integration_test.go @@ -21,10 +21,11 @@ import ( "github.com/elastic/beats/v7/filebeat/input" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( - azureConfig = common.MustNewConfigFrom(common.MapStr{ + azureConfig = common.MustNewConfigFrom(mapstr.M{ "storage_account_key": lookupEnv("STORAGE_ACCOUNT_NAME"), "storage_account": lookupEnv("STORAGE_ACCOUNT_KEY"), "storage_account_container": ephContainerName, diff --git a/x-pack/filebeat/input/azureeventhub/input.go b/x-pack/filebeat/input/azureeventhub/input.go index 7d70bffddde..f9b6596eb87 100644 --- a/x-pack/filebeat/input/azureeventhub/input.go +++ b/x-pack/filebeat/input/azureeventhub/input.go @@ -22,6 +22,7 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" eventhub "github.com/Azure/azure-event-hubs-go/v3" "github.com/Azure/azure-event-hubs-go/v3/eph" @@ -174,7 +175,7 @@ func (a *azureInput) Wait() { func (a *azureInput) processEvents(event *eventhub.Event, partitionID string) bool { timestamp := time.Now() - azure := common.MapStr{ + azure := mapstr.M{ // partitionID is only mapped in the non-eph option which is not available yet, this field will be temporary unavailable //"partition_id": partitionID, "eventhub": a.config.EventHubName, @@ -187,7 +188,7 @@ func (a *azureInput) processEvents(event *eventhub.Event, partitionID string) bo azure.Put("enqueued_time", event.SystemProperties.EnqueuedTime) ok := a.outlet.OnEvent(beat.Event{ Timestamp: timestamp, - Fields: common.MapStr{ + Fields: mapstr.M{ "message": msg, "azure": azure, }, diff --git a/x-pack/filebeat/input/azureeventhub/input_test.go b/x-pack/filebeat/input/azureeventhub/input_test.go index c4dec83d61c..58b8043e981 100644 --- a/x-pack/filebeat/input/azureeventhub/input_test.go +++ b/x-pack/filebeat/input/azureeventhub/input_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" eventhub "github.com/Azure/azure-event-hubs-go/v3" "github.com/stretchr/testify/assert" @@ -113,7 +114,7 @@ func TestParseMultipleMessages(t *testing.T) { } func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "connection_string": "Endpoint=sb://something", "eventhub": "insights-operational-logs", "storage_account": "someaccount", diff --git a/x-pack/filebeat/input/cloudfoundry/input.go b/x-pack/filebeat/input/cloudfoundry/input.go index 4931cb035ca..86f0f9b42c3 100644 --- a/x-pack/filebeat/input/cloudfoundry/input.go +++ b/x-pack/filebeat/input/cloudfoundry/input.go @@ -16,13 +16,14 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/feature" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry" ) type cloudfoundryEvent interface { Timestamp() time.Time - ToFields() common.MapStr + ToFields() mapstr.M } func Plugin() v2.Plugin { diff --git a/x-pack/filebeat/input/gcppubsub/input.go b/x-pack/filebeat/input/gcppubsub/input.go index d71af4bb2d4..db666360e29 100644 --- a/x-pack/filebeat/input/gcppubsub/input.go +++ b/x-pack/filebeat/input/gcppubsub/input.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/atomic" "github.com/elastic/beats/v7/libbeat/common/useragent" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -201,8 +202,8 @@ func makeEvent(topicID string, msg *pubsub.Message) beat.Event { event := beat.Event{ Timestamp: msg.PublishTime.UTC(), - Fields: common.MapStr{ - "event": common.MapStr{ + Fields: mapstr.M{ + "event": mapstr.M{ "id": id, "created": time.Now().UTC(), }, diff --git a/x-pack/filebeat/input/gcppubsub/input_test.go b/x-pack/filebeat/input/gcppubsub/input_test.go index 56a2cd3574e..db29a940d92 100644 --- a/x-pack/filebeat/input/gcppubsub/input_test.go +++ b/x-pack/filebeat/input/gcppubsub/input_test.go @@ -11,11 +11,11 @@ import ( "testing" "github.com/elastic/beats/v7/filebeat/input/inputtest" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewInputDone(t *testing.T) { - config := common.MapStr{ + config := mapstr.M{ "project_id": "some-project", "topic": "sometopic", "subscription.name": "subscription", diff --git a/x-pack/filebeat/input/http_endpoint/handler.go b/x-pack/filebeat/input/http_endpoint/handler.go index af22969fa88..3f4956e3a98 100644 --- a/x-pack/filebeat/input/http_endpoint/handler.go +++ b/x-pack/filebeat/input/http_endpoint/handler.go @@ -18,6 +18,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/jsontransform" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const headerContentEncoding = "Content-Encoding" @@ -76,13 +77,13 @@ func (h *httpHandler) sendResponse(w http.ResponseWriter, status int, message st } } -func (h *httpHandler) publishEvent(obj, headers common.MapStr) error { +func (h *httpHandler) publishEvent(obj, headers mapstr.M) error { event := beat.Event{ Timestamp: time.Now().UTC(), - Fields: common.MapStr{}, + Fields: mapstr.M{}, } if h.preserveOriginalEvent { - event.Fields["event"] = common.MapStr{ + event.Fields["event"] = mapstr.M{ "original": obj.String(), } } @@ -98,7 +99,7 @@ func (h *httpHandler) publishEvent(obj, headers common.MapStr) error { return nil } -func httpReadJSON(body io.Reader) (objs []common.MapStr, rawMessages []json.RawMessage, status int, err error) { +func httpReadJSON(body io.Reader) (objs []mapstr.M, rawMessages []json.RawMessage, status int, err error) { if body == http.NoBody { return nil, nil, http.StatusNotAcceptable, errBodyEmpty } @@ -109,7 +110,7 @@ func httpReadJSON(body io.Reader) (objs []common.MapStr, rawMessages []json.RawM return obj, rawMessage, http.StatusOK, err } -func decodeJSON(body io.Reader) (objs []common.MapStr, rawMessages []json.RawMessage, err error) { +func decodeJSON(body io.Reader) (objs []mapstr.M, rawMessages []json.RawMessage, err error) { decoder := json.NewDecoder(body) for decoder.More() { var raw json.RawMessage @@ -145,7 +146,7 @@ func decodeJSON(body io.Reader) (objs []common.MapStr, rawMessages []json.RawMes return objs, rawMessages, nil } -func decodeJSONArray(raw *bytes.Reader) (objs []common.MapStr, rawMessages []json.RawMessage, err error) { +func decodeJSONArray(raw *bytes.Reader) (objs []mapstr.M, rawMessages []json.RawMessage, err error) { dec := newJSONDecoder(raw) token, err := dec.Token() if err != nil { @@ -181,8 +182,8 @@ func decodeJSONArray(raw *bytes.Reader) (objs []common.MapStr, rawMessages []jso return objs, rawMessages, nil } -func getIncludedHeaders(r *http.Request, headerConf []string) (includedHeaders common.MapStr) { - includedHeaders = common.MapStr{} +func getIncludedHeaders(r *http.Request, headerConf []string) (includedHeaders mapstr.M) { + includedHeaders = mapstr.M{} for _, header := range headerConf { if value, found := r.Header[header]; found { includedHeaders[common.DeDot(header)] = value diff --git a/x-pack/filebeat/input/http_endpoint/handler_test.go b/x-pack/filebeat/input/http_endpoint/handler_test.go index 0365c3cc995..bbd2e8f8f71 100644 --- a/x-pack/filebeat/input/http_endpoint/handler_test.go +++ b/x-pack/filebeat/input/http_endpoint/handler_test.go @@ -18,15 +18,15 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func Test_httpReadJSON(t *testing.T) { tests := []struct { name string body string - wantObjs []common.MapStr + wantObjs []mapstr.M wantStatus int wantErr bool wantRawMessage []json.RawMessage @@ -34,13 +34,13 @@ func Test_httpReadJSON(t *testing.T) { { name: "single object", body: `{"a": 42, "b": "c"}`, - wantObjs: []common.MapStr{{"a": int64(42), "b": "c"}}, + wantObjs: []mapstr.M{{"a": int64(42), "b": "c"}}, wantStatus: http.StatusOK, }, { name: "array accepted", body: `[{"a":"b"},{"c":"d"}]`, - wantObjs: []common.MapStr{{"a": "b"}, {"c": "d"}}, + wantObjs: []mapstr.M{{"a": "b"}, {"c": "d"}}, wantStatus: http.StatusOK, }, { @@ -60,7 +60,7 @@ func Test_httpReadJSON(t *testing.T) { { name: "sequence of objects accepted (CRLF)", body: `{"a":1}` + "\r" + `{"a":2}`, - wantObjs: []common.MapStr{{"a": int64(1)}, {"a": int64(2)}}, + wantObjs: []mapstr.M{{"a": int64(1)}, {"a": int64(2)}}, wantStatus: http.StatusOK, }, { @@ -71,19 +71,19 @@ func Test_httpReadJSON(t *testing.T) { []byte(`{"a":"1"}`), []byte(`{"a":"2"}`), }, - wantObjs: []common.MapStr{{"a": "1"}, {"a": "2"}}, + wantObjs: []mapstr.M{{"a": "1"}, {"a": "2"}}, wantStatus: http.StatusOK, }, { name: "sequence of objects accepted (SP)", body: `{"a":"2"} {"a":"2"}`, - wantObjs: []common.MapStr{{"a": "2"}, {"a": "2"}}, + wantObjs: []mapstr.M{{"a": "2"}, {"a": "2"}}, wantStatus: http.StatusOK, }, { name: "sequence of objects accepted (no separator)", body: `{"a":"2"}{"a":"2"}`, - wantObjs: []common.MapStr{{"a": "2"}, {"a": "2"}}, + wantObjs: []mapstr.M{{"a": "2"}, {"a": "2"}}, wantStatus: http.StatusOK, }, { @@ -103,7 +103,7 @@ func Test_httpReadJSON(t *testing.T) { []byte(`{"a":"3"}`), []byte(`{"a":"4"}`), }, - wantObjs: []common.MapStr{{"a": "1"}, {"a": "2"}, {"a": "3"}, {"a": "4"}}, + wantObjs: []mapstr.M{{"a": "1"}, {"a": "2"}, {"a": "3"}, {"a": "4"}}, wantStatus: http.StatusOK, }, { @@ -115,7 +115,7 @@ func Test_httpReadJSON(t *testing.T) { []byte(`{"a":3.14}`), []byte(`{"a":-4}`), }, - wantObjs: []common.MapStr{ + wantObjs: []mapstr.M{ {"a": int64(1)}, {"a": false}, {"a": 3.14}, @@ -155,9 +155,9 @@ func (p *publisher) Publish(event beat.Event) { func Test_apiResponse(t *testing.T) { testCases := []struct { - name string // Sub-test name. - request *http.Request // Input request. - events []common.MapStr // Expected output events. + name string // Sub-test name. + request *http.Request // Input request. + events []mapstr.M // Expected output events. }{ { name: "single event", @@ -166,9 +166,9 @@ func Test_apiResponse(t *testing.T) { req.Header.Set("Content-Type", "application/json") return req }(), - events: []common.MapStr{ + events: []mapstr.M{ { - "json": common.MapStr{ + "json": mapstr.M{ "id": int64(0), }, }, @@ -187,9 +187,9 @@ func Test_apiResponse(t *testing.T) { req.Header.Set("Content-Encoding", "gzip") return req }(), - events: []common.MapStr{ + events: []mapstr.M{ { - "json": common.MapStr{ + "json": mapstr.M{ "id": int64(0), }, }, @@ -213,14 +213,14 @@ func Test_apiResponse(t *testing.T) { req.Header.Set("Content-Encoding", "gzip") return req }(), - events: []common.MapStr{ + events: []mapstr.M{ { - "json": common.MapStr{ + "json": mapstr.M{ "id": int64(0), }, }, { - "json": common.MapStr{ + "json": mapstr.M{ "id": int64(1), }, }, diff --git a/x-pack/filebeat/input/httpjson/chain.go b/x-pack/filebeat/input/httpjson/chain.go index c6d202aa9f1..5cf2a8f6d5c 100644 --- a/x-pack/filebeat/input/httpjson/chain.go +++ b/x-pack/filebeat/input/httpjson/chain.go @@ -57,8 +57,8 @@ package httpjson import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/httpcommon" + "github.com/elastic/elastic-agent-libs/mapstr" ) // chainConfig for chain request. @@ -81,7 +81,7 @@ type stepConfig struct { type requestChainConfig struct { URL *urlConfig `config:"url" validate:"required"` Method string `config:"method" validate:"required"` - Body *common.MapStr `config:"body"` + Body *mapstr.M `config:"body"` Transforms transformsConfig `config:"transforms"` Transport httpcommon.HTTPTransportSettings `config:",inline"` diff --git a/x-pack/filebeat/input/httpjson/config_request.go b/x-pack/filebeat/input/httpjson/config_request.go index 0bd35db7158..7fe070c540a 100644 --- a/x-pack/filebeat/input/httpjson/config_request.go +++ b/x-pack/filebeat/input/httpjson/config_request.go @@ -12,8 +12,8 @@ import ( "strings" "time" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/httpcommon" + "github.com/elastic/elastic-agent-libs/mapstr" ) type retryConfig struct { @@ -88,7 +88,7 @@ func (u *urlConfig) Unpack(in string) error { type requestConfig struct { URL *urlConfig `config:"url" validate:"required"` Method string `config:"method" validate:"required"` - Body *common.MapStr `config:"body"` + Body *mapstr.M `config:"body"` EncodeAs string `config:"encode_as"` Retry retryConfig `config:"retry"` RedirectForwardHeaders bool `config:"redirect.forward_headers"` diff --git a/x-pack/filebeat/input/httpjson/cursor.go b/x-pack/filebeat/input/httpjson/cursor.go index f603a12b0d8..8b000a6d7a2 100644 --- a/x-pack/filebeat/input/httpjson/cursor.go +++ b/x-pack/filebeat/input/httpjson/cursor.go @@ -6,8 +6,8 @@ package httpjson import ( inputcursor "github.com/elastic/beats/v7/filebeat/input/v2/input-cursor" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) type cursor struct { @@ -15,7 +15,7 @@ type cursor struct { cfg cursorConfig - state common.MapStr + state mapstr.M } func newCursor(cfg cursorConfig, log *logp.Logger) *cursor { @@ -29,7 +29,7 @@ func (c *cursor) load(cursor *inputcursor.Cursor) { } if c.state == nil { - c.state = common.MapStr{} + c.state = mapstr.M{} } if err := cursor.Unpack(&c.state); err != nil { @@ -46,7 +46,7 @@ func (c *cursor) update(trCtx *transformContext) { } if c.state == nil { - c.state = common.MapStr{} + c.state = mapstr.M{} } for k, cfg := range c.cfg { @@ -58,9 +58,9 @@ func (c *cursor) update(trCtx *transformContext) { } } -func (c *cursor) clone() common.MapStr { +func (c *cursor) clone() mapstr.M { if c == nil || c.state == nil { - return common.MapStr{} + return mapstr.M{} } return c.state.Clone() } diff --git a/x-pack/filebeat/input/httpjson/cursor_test.go b/x-pack/filebeat/input/httpjson/cursor_test.go index 7f451abe3ce..d750ceebcc7 100644 --- a/x-pack/filebeat/input/httpjson/cursor_test.go +++ b/x-pack/filebeat/input/httpjson/cursor_test.go @@ -12,6 +12,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestCursorUpdate(t *testing.T) { @@ -19,8 +20,8 @@ func TestCursorUpdate(t *testing.T) { name string baseConfig map[string]interface{} trCtx *transformContext - initialState common.MapStr - expectedState common.MapStr + initialState mapstr.M + expectedState mapstr.M }{ { name: "update an unexisting value", @@ -30,8 +31,8 @@ func TestCursorUpdate(t *testing.T) { }, }, trCtx: emptyTransformContext(), - initialState: common.MapStr{}, - expectedState: common.MapStr{ + initialState: mapstr.M{}, + expectedState: mapstr.M{ "entry1": "v1", }, }, @@ -44,15 +45,15 @@ func TestCursorUpdate(t *testing.T) { }, trCtx: func() *transformContext { trCtx := emptyTransformContext() - trCtx.lastResponse.body = common.MapStr{ + trCtx.lastResponse.body = mapstr.M{ "foo": "v2", } return trCtx }(), - initialState: common.MapStr{ + initialState: mapstr.M{ "entry1": "v1", }, - expectedState: common.MapStr{ + expectedState: mapstr.M{ "entry1": "v2", }, }, @@ -72,12 +73,12 @@ func TestCursorUpdate(t *testing.T) { }, }, trCtx: emptyTransformContext(), - initialState: common.MapStr{ + initialState: mapstr.M{ "entry1": "v1", "entry2": "v2", "entry3": "v3", }, - expectedState: common.MapStr{ + expectedState: mapstr.M{ "entry1": "v1", "entry2": "v2", "entry3": "v3", @@ -92,10 +93,10 @@ func TestCursorUpdate(t *testing.T) { }, }, trCtx: emptyTransformContext(), - initialState: common.MapStr{ + initialState: mapstr.M{ "entry1": "v1", }, - expectedState: common.MapStr{ + expectedState: mapstr.M{ "entry1": "", }, }, diff --git a/x-pack/filebeat/input/httpjson/input.go b/x-pack/filebeat/input/httpjson/input.go index be5b51963db..6c68cfcf0a6 100644 --- a/x-pack/filebeat/input/httpjson/input.go +++ b/x-pack/filebeat/input/httpjson/input.go @@ -19,11 +19,11 @@ import ( v2 "github.com/elastic/beats/v7/filebeat/input/v2" inputcursor "github.com/elastic/beats/v7/filebeat/input/v2/input-cursor" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/transport/httpcommon" "github.com/elastic/beats/v7/libbeat/common/useragent" "github.com/elastic/beats/v7/libbeat/feature" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-concert/ctxtool" "github.com/elastic/go-concert/timed" ) @@ -207,14 +207,14 @@ func checkRedirect(config *requestConfig, log *logp.Logger) func(*http.Request, } } -func makeEvent(body common.MapStr) (beat.Event, error) { +func makeEvent(body mapstr.M) (beat.Event, error) { bodyBytes, err := json.Marshal(body) if err != nil { return beat.Event{}, err } now := timeNow() - fields := common.MapStr{ - "event": common.MapStr{ + fields := mapstr.M{ + "event": mapstr.M{ "created": now, }, "message": string(bodyBytes), diff --git a/x-pack/filebeat/input/httpjson/pagination.go b/x-pack/filebeat/input/httpjson/pagination.go index 9f09fcc0692..edae8782546 100644 --- a/x-pack/filebeat/input/httpjson/pagination.go +++ b/x-pack/filebeat/input/httpjson/pagination.go @@ -11,8 +11,8 @@ import ( "net/http" "net/url" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const paginationNamespace = "pagination" @@ -45,11 +45,11 @@ func newPagination(config config, httpClient *httpClient, log *logp.Logger) *pag rts, _ := newBasicTransformsFromConfig(config.Request.Transforms, requestNamespace, log) pts, _ := newBasicTransformsFromConfig(config.Response.Pagination, paginationNamespace, log) - body := func() *common.MapStr { + body := func() *mapstr.M { if config.Response.RequestBodyOnPagination { return config.Request.Body } - return &common.MapStr{} + return &mapstr.M{} }() requestFactory := newPaginationRequestFactory( @@ -65,7 +65,7 @@ func newPagination(config config, httpClient *httpClient, log *logp.Logger) *pag return pagination } -func newPaginationRequestFactory(method, encodeAs string, url url.URL, body *common.MapStr, ts []basicTransform, authConfig *authConfig, log *logp.Logger) *requestFactory { +func newPaginationRequestFactory(method, encodeAs string, url url.URL, body *mapstr.M, ts []basicTransform, authConfig *authConfig, log *logp.Logger) *requestFactory { // config validation already checked for errors here rf := &requestFactory{ url: url, diff --git a/x-pack/filebeat/input/httpjson/request.go b/x-pack/filebeat/input/httpjson/request.go index 356de79ac9f..8c6ecb101e1 100644 --- a/x-pack/filebeat/input/httpjson/request.go +++ b/x-pack/filebeat/input/httpjson/request.go @@ -17,8 +17,8 @@ import ( "github.com/PaesslerAG/jsonpath" inputcursor "github.com/elastic/beats/v7/filebeat/input/v2/input-cursor" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const requestNamespace = "request" @@ -94,7 +94,7 @@ func (rf *requestFactory) newRequest(ctx *transformContext) (transformable, erro type requestFactory struct { url url.URL method string - body *common.MapStr + body *mapstr.M transforms []basicTransform user string password string diff --git a/x-pack/filebeat/input/httpjson/request_test.go b/x-pack/filebeat/input/httpjson/request_test.go index c631f8db5c8..2bb5364e8ca 100644 --- a/x-pack/filebeat/input/httpjson/request_test.go +++ b/x-pack/filebeat/input/httpjson/request_test.go @@ -16,6 +16,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" beattest "github.com/elastic/beats/v7/libbeat/publisher/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestCtxAfterDoRequest(t *testing.T) { @@ -75,17 +76,17 @@ func TestCtxAfterDoRequest(t *testing.T) { assert.EqualValues( t, - common.MapStr{"timestamp": "2002-10-02T15:00:00Z"}, + mapstr.M{"timestamp": "2002-10-02T15:00:00Z"}, trCtx.cursorMap(), ) assert.EqualValues( t, - &common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, + &mapstr.M{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, trCtx.firstEventClone(), ) assert.EqualValues( t, - &common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, + &mapstr.M{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, trCtx.lastEventClone(), ) lastResp := trCtx.lastResponseClone() @@ -96,7 +97,7 @@ func TestCtxAfterDoRequest(t *testing.T) { &response{ page: 1, url: *(newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T14%3A50%3A00Z"))), - body: common.MapStr{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, + body: mapstr.M{"@timestamp": "2002-10-02T15:00:00Z", "foo": "bar"}, }, lastResp, ) @@ -106,19 +107,19 @@ func TestCtxAfterDoRequest(t *testing.T) { assert.EqualValues( t, - common.MapStr{"timestamp": "2002-10-02T15:00:01Z"}, + mapstr.M{"timestamp": "2002-10-02T15:00:01Z"}, trCtx.cursorMap(), ) assert.EqualValues( t, - &common.MapStr{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, + &mapstr.M{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, trCtx.firstEventClone(), ) assert.EqualValues( t, - &common.MapStr{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, + &mapstr.M{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, trCtx.lastEventClone(), ) @@ -128,7 +129,7 @@ func TestCtxAfterDoRequest(t *testing.T) { &response{ page: 1, url: *(newURL(fmt.Sprintf("%s?%s", testServer.URL, "%24filter=alertCreationTime+ge+2002-10-02T15%3A00%3A00Z"))), - body: common.MapStr{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, + body: mapstr.M{"@timestamp": "2002-10-02T15:00:01Z", "foo": "bar"}, }, lastResp, ) diff --git a/x-pack/filebeat/input/httpjson/response.go b/x-pack/filebeat/input/httpjson/response.go index 029e51e56a7..4e2c2727db3 100644 --- a/x-pack/filebeat/input/httpjson/response.go +++ b/x-pack/filebeat/input/httpjson/response.go @@ -9,8 +9,8 @@ import ( "net/http" "net/url" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const responseNamespace = "response" @@ -40,10 +40,10 @@ func (resp *response) clone() *response { c := make([]interface{}, len(t)) copy(c, t) clone.body = c - case common.MapStr: + case mapstr.M: clone.body = t.Clone() case map[string]interface{}: - clone.body = common.MapStr(t).Clone() + clone.body = mapstr.M(t).Clone() } return clone @@ -168,7 +168,7 @@ func (resp *response) asTransformables(log *logp.Logger) []transformable { tr := transformable{} tr.setHeader(resp.header.Clone()) tr.setURL(resp.url) - tr.setBody(common.MapStr(m).Clone()) + tr.setBody(mapstr.M(m).Clone()) ts = append(ts, tr) } @@ -191,14 +191,14 @@ func (resp *response) asTransformables(log *logp.Logger) []transformable { return ts } -func (resp *response) templateValues() common.MapStr { +func (resp *response) templateValues() mapstr.M { if resp == nil { - return common.MapStr{} + return mapstr.M{} } - return common.MapStr{ + return mapstr.M{ "header": resp.header.Clone(), "page": resp.page, - "url": common.MapStr{ + "url": mapstr.M{ "value": resp.url.String(), "params": resp.url.Query(), }, diff --git a/x-pack/filebeat/input/httpjson/response_test.go b/x-pack/filebeat/input/httpjson/response_test.go index 86bdb811cb1..19e8baca873 100644 --- a/x-pack/filebeat/input/httpjson/response_test.go +++ b/x-pack/filebeat/input/httpjson/response_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestTemplateValues(t *testing.T) { @@ -20,7 +20,7 @@ func TestTemplateValues(t *testing.T) { header: http.Header{ "Authorization": []string{"Bearer token"}, }, - body: common.MapStr{ + body: mapstr.M{ "param": "value", }, } diff --git a/x-pack/filebeat/input/httpjson/split.go b/x-pack/filebeat/input/httpjson/split.go index 1b6d6deb8c1..d9eea047d09 100644 --- a/x-pack/filebeat/input/httpjson/split.go +++ b/x-pack/filebeat/input/httpjson/split.go @@ -9,8 +9,8 @@ import ( "fmt" "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -101,9 +101,9 @@ func (s *split) run(ctx *transformContext, resp transformable, ch chan<- maybeMs } // split recursively executes the split processor chain. -func (s *split) split(ctx *transformContext, root common.MapStr, ch chan<- maybeMsg) error { +func (s *split) split(ctx *transformContext, root mapstr.M, ch chan<- maybeMsg) error { v, err := root.GetValue(s.targetInfo.Name) - if err != nil && err != common.ErrKeyNotFound { //nolint:errorlint // common.ErrKeyNotFound is never wrapped by GetValue. + if err != nil && err != mapstr.ErrKeyNotFound { //nolint:errorlint // mapstr.ErrKeyNotFound is never wrapped by GetValue. return err } @@ -209,7 +209,7 @@ func (s *split) split(ctx *transformContext, root common.MapStr, ch chan<- maybe // sendMessage sends an array or map split result value, v, on ch after performing // any necessary transformations. If key is "", the value is an element of an array. -func (s *split) sendMessage(ctx *transformContext, root common.MapStr, key string, v interface{}, ch chan<- maybeMsg) error { +func (s *split) sendMessage(ctx *transformContext, root mapstr.M, key string, v interface{}, ch chan<- maybeMsg) error { obj, ok := toMapStr(v) if !ok { return errExpectedSplitObj @@ -247,22 +247,22 @@ func (s *split) sendMessage(ctx *transformContext, root common.MapStr, key strin return nil } -func toMapStr(v interface{}) (common.MapStr, bool) { +func toMapStr(v interface{}) (mapstr.M, bool) { if v == nil { - return common.MapStr{}, false + return mapstr.M{}, false } switch t := v.(type) { - case common.MapStr: + case mapstr.M: return t, true case map[string]interface{}: - return common.MapStr(t), true + return mapstr.M(t), true } - return common.MapStr{}, false + return mapstr.M{}, false } // sendMessage sends a string split result value, v, on ch after performing any // necessary transformations. If key is "", the value is an element of an array. -func (s *split) sendMessageSplitString(ctx *transformContext, root common.MapStr, v string, ch chan<- maybeMsg) error { +func (s *split) sendMessageSplitString(ctx *transformContext, root mapstr.M, v string, ch chan<- maybeMsg) error { clone := root.Clone() _, _ = clone.Put(s.targetInfo.Name, v) diff --git a/x-pack/filebeat/input/httpjson/split_test.go b/x-pack/filebeat/input/httpjson/split_test.go index 3e5bff77c79..d010d48c79c 100644 --- a/x-pack/filebeat/input/httpjson/split_test.go +++ b/x-pack/filebeat/input/httpjson/split_test.go @@ -11,6 +11,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestSplit(t *testing.T) { @@ -21,7 +22,7 @@ func TestSplit(t *testing.T) { config *splitConfig ctx *transformContext resp transformable - expectedMessages []common.MapStr + expectedMessages []mapstr.M expectedErr error }{ { @@ -38,7 +39,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "this": "is kept", "alerts": []interface{}{ map[string]interface{}{ @@ -66,7 +67,7 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { "this": "is kept", "alerts.this_is": "also kept", @@ -105,7 +106,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "this": "is not kept", "alerts": []interface{}{ map[string]interface{}{ @@ -127,7 +128,7 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { "this_is": "kept", "entities.id": "id1", @@ -161,7 +162,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "this": "is not kept", "alerts": []interface{}{ map[string]interface{}{ @@ -183,7 +184,7 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { "something": "else", "foo": "set for each", @@ -207,7 +208,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "response": []interface{}{ map[string]interface{}{ "Event": map[string]interface{}{ @@ -225,19 +226,19 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", - "Attributes": common.MapStr{ + "Attributes": mapstr.M{ "key": "value", }, }, }, { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", - "Attributes": common.MapStr{ + "Attributes": mapstr.M{ "key2": "value2", }, }, @@ -257,7 +258,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "response": []interface{}{ map[string]interface{}{ "Event": map[string]interface{}{ @@ -267,9 +268,9 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", }, }, @@ -287,11 +288,11 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "response": []interface{}{}, }, }, - expectedMessages: []common.MapStr{}, + expectedMessages: []mapstr.M{}, expectedErr: errEmptyRootField, }, { @@ -307,19 +308,19 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "@timestamp": "1234567890", "nextPageToken": "tok", "items": []interface{}{ - common.MapStr{"foo": "bar"}, - common.MapStr{ + mapstr.M{"foo": "bar"}, + mapstr.M{ "baz": "buzz", - "splitHere": common.MapStr{ + "splitHere": mapstr.M{ "splitMore": []interface{}{ - common.MapStr{ + mapstr.M{ "deepest1": "data", }, - common.MapStr{ + mapstr.M{ "deepest2": "data", }, }, @@ -328,10 +329,10 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ {"foo": "bar"}, - {"baz": "buzz", "splitHere": common.MapStr{"splitMore": common.MapStr{"deepest1": "data"}}}, - {"baz": "buzz", "splitHere": common.MapStr{"splitMore": common.MapStr{"deepest2": "data"}}}, + {"baz": "buzz", "splitHere": mapstr.M{"splitMore": mapstr.M{"deepest1": "data"}}}, + {"baz": "buzz", "splitHere": mapstr.M{"splitMore": mapstr.M{"deepest2": "data"}}}, }, }, { @@ -343,12 +344,12 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "@timestamp": "1234567890", "items": "Line 1\nLine 2\nLine 3", }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ {"@timestamp": "1234567890", "items": "Line 1"}, {"@timestamp": "1234567890", "items": "Line 2"}, {"@timestamp": "1234567890", "items": "Line 3"}, @@ -371,7 +372,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "response": []interface{}{ map[string]interface{}{ "Event": map[string]interface{}{ @@ -390,21 +391,21 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", "Attributes": []interface{}{}, - "OtherAttributes": common.MapStr{ + "OtherAttributes": mapstr.M{ "key": "value", }, }, }, { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", "Attributes": []interface{}{}, - "OtherAttributes": common.MapStr{ + "OtherAttributes": mapstr.M{ "key2": "value2", }, }, @@ -429,7 +430,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "response": []interface{}{ map[string]interface{}{ "Event": map[string]interface{}{ @@ -447,19 +448,19 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", - "OtherAttributes": common.MapStr{ + "OtherAttributes": mapstr.M{ "key": "value", }, }, }, { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", - "OtherAttributes": common.MapStr{ + "OtherAttributes": mapstr.M{ "key2": "value2", }, }, @@ -486,7 +487,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "response": []interface{}{ map[string]interface{}{ "Event": map[string]interface{}{ @@ -504,12 +505,12 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", - "Attributes": common.MapStr{}, - "OtherAttributes": common.MapStr{ + "Attributes": mapstr.M{}, + "OtherAttributes": mapstr.M{ "key": "value", }, }, @@ -536,7 +537,7 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "response": []interface{}{ map[string]interface{}{ "Event": map[string]interface{}{ @@ -553,11 +554,11 @@ func TestSplit(t *testing.T) { }, }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ { - "Event": common.MapStr{ + "Event": mapstr.M{ "timestamp": "1606324417", - "OtherAttributes": common.MapStr{ + "OtherAttributes": mapstr.M{ "key": "value", }, }, @@ -580,13 +581,13 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "@timestamp": "1234567890", "items": "", "other_items": "Line 1\nLine 2\nLine 3", }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ {"@timestamp": "1234567890", "items": "", "other_items": "Line 1"}, {"@timestamp": "1234567890", "items": "", "other_items": "Line 2"}, {"@timestamp": "1234567890", "items": "", "other_items": "Line 3"}, @@ -607,12 +608,12 @@ func TestSplit(t *testing.T) { }, ctx: emptyTransformContext(), resp: transformable{ - "body": common.MapStr{ + "body": mapstr.M{ "@timestamp": "1234567890", "other_items": "Line 1\nLine 2\nLine 3", }, }, - expectedMessages: []common.MapStr{ + expectedMessages: []mapstr.M{ {"@timestamp": "1234567890", "other_items": "Line 1"}, {"@timestamp": "1234567890", "other_items": "Line 2"}, {"@timestamp": "1234567890", "other_items": "Line 3"}, diff --git a/x-pack/filebeat/input/httpjson/transform.go b/x-pack/filebeat/input/httpjson/transform.go index dc534bb2de1..1ec4ac78762 100644 --- a/x-pack/filebeat/input/httpjson/transform.go +++ b/x-pack/filebeat/input/httpjson/transform.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const logName = "httpjson.transforms" @@ -25,34 +26,34 @@ type transforms []transform type transformContext struct { lock sync.RWMutex cursor *cursor - firstEvent *common.MapStr - lastEvent *common.MapStr + firstEvent *mapstr.M + lastEvent *mapstr.M lastResponse *response } func emptyTransformContext() *transformContext { return &transformContext{ cursor: &cursor{}, - lastEvent: &common.MapStr{}, - firstEvent: &common.MapStr{}, + lastEvent: &mapstr.M{}, + firstEvent: &mapstr.M{}, lastResponse: &response{}, } } -func (ctx *transformContext) cursorMap() common.MapStr { +func (ctx *transformContext) cursorMap() mapstr.M { ctx.lock.RLock() defer ctx.lock.RUnlock() return ctx.cursor.clone() } -func (ctx *transformContext) lastEventClone() *common.MapStr { +func (ctx *transformContext) lastEventClone() *mapstr.M { ctx.lock.RLock() defer ctx.lock.RUnlock() clone := ctx.lastEvent.Clone() return &clone } -func (ctx *transformContext) firstEventClone() *common.MapStr { +func (ctx *transformContext) firstEventClone() *mapstr.M { ctx.lock.RLock() defer ctx.lock.RUnlock() clone := ctx.firstEvent.Clone() @@ -78,13 +79,13 @@ func (ctx *transformContext) updateCursor() { ctx.cursor.update(newCtx) } -func (ctx *transformContext) updateLastEvent(e common.MapStr) { +func (ctx *transformContext) updateLastEvent(e mapstr.M) { ctx.lock.Lock() defer ctx.lock.Unlock() *ctx.lastEvent = e } -func (ctx *transformContext) updateFirstEvent(e common.MapStr) { +func (ctx *transformContext) updateFirstEvent(e mapstr.M) { ctx.lock.Lock() defer ctx.lock.Unlock() *ctx.firstEvent = e @@ -99,15 +100,15 @@ func (ctx *transformContext) updateLastResponse(r response) { func (ctx *transformContext) clearIntervalData() { ctx.lock.Lock() defer ctx.lock.Unlock() - ctx.lastEvent = &common.MapStr{} - ctx.firstEvent = &common.MapStr{} + ctx.lastEvent = &mapstr.M{} + ctx.firstEvent = &mapstr.M{} ctx.lastResponse = &response{} } -type transformable common.MapStr +type transformable mapstr.M -func (tr transformable) access() common.MapStr { - return common.MapStr(tr) +func (tr transformable) access() mapstr.M { + return mapstr.M(tr) } func (tr transformable) Put(k string, v interface{}) { @@ -140,20 +141,20 @@ func (tr transformable) header() http.Header { return header } -func (tr transformable) setBody(v common.MapStr) { +func (tr transformable) setBody(v mapstr.M) { tr.Put("body", v) } -func (tr transformable) body() common.MapStr { +func (tr transformable) body() mapstr.M { val, err := tr.GetValue("body") if err != nil { // if it does not exist, initialize it - body := common.MapStr{} + body := mapstr.M{} tr.setBody(body) return body } - body, _ := val.(common.MapStr) + body, _ := val.(mapstr.M) return body } @@ -187,7 +188,7 @@ type basicTransform interface { type maybeMsg struct { err error - msg common.MapStr + msg mapstr.M } func (e maybeMsg) failed() bool { return e.err != nil } diff --git a/x-pack/filebeat/input/httpjson/transform_append.go b/x-pack/filebeat/input/httpjson/transform_append.go index c41d3dbcdb8..bd365966ecf 100644 --- a/x-pack/filebeat/input/httpjson/transform_append.go +++ b/x-pack/filebeat/input/httpjson/transform_append.go @@ -9,6 +9,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const appendName = "append" @@ -135,7 +136,7 @@ func (append *appendt) run(ctx *transformContext, tr transformable) (transformab return tr, nil } -func appendToCommonMap(m common.MapStr, key string, val interface{}) error { +func appendToCommonMap(m mapstr.M, key string, val interface{}) error { var value interface{} strVal, isString := val.(string) if found, _ := m.HasKey(key); found { diff --git a/x-pack/filebeat/input/httpjson/transform_append_test.go b/x-pack/filebeat/input/httpjson/transform_append_test.go index 086b4293ce4..68659727214 100644 --- a/x-pack/filebeat/input/httpjson/transform_append_test.go +++ b/x-pack/filebeat/input/httpjson/transform_append_test.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewAppend(t *testing.T) { @@ -144,20 +145,20 @@ func TestAppendFunctions(t *testing.T) { name: "appendBody", tfunc: appendBody, paramCtx: &transformContext{}, - paramTr: transformable{"body": common.MapStr{"a_key": "a_value"}}, + paramTr: transformable{"body": mapstr.M{"a_key": "a_value"}}, paramKey: "a_key", paramVal: "another_value", - expectedTr: transformable{"body": common.MapStr{"a_key": []interface{}{"a_value", "another_value"}}}, + expectedTr: transformable{"body": mapstr.M{"a_key": []interface{}{"a_value", "another_value"}}}, expectedErr: nil, }, { name: "appendBodyWithSingleValue", tfunc: appendBody, paramCtx: &transformContext{}, - paramTr: transformable{"body": common.MapStr{}}, + paramTr: transformable{"body": mapstr.M{}}, paramKey: "a_key", paramVal: "a_value", - expectedTr: transformable{"body": common.MapStr{"a_key": []interface{}{"a_value"}}}, + expectedTr: transformable{"body": mapstr.M{"a_key": []interface{}{"a_value"}}}, expectedErr: nil, }, { @@ -219,7 +220,7 @@ func TestDifferentAppendValueTypes(t *testing.T) { tr, err = testAppend.run(trCtx, tr) require.NoError(t, err) - exp := common.MapStr{ + exp := mapstr.M{ "p1": []interface{}{ map[string]interface{}{ "param": "value", @@ -248,7 +249,7 @@ func TestDifferentAppendValueTypes(t *testing.T) { tr, err = testAppend.run(trCtx, tr) require.NoError(t, err) - exp = common.MapStr{ + exp = mapstr.M{ "p1": []interface{}{int64(1)}, } @@ -269,7 +270,7 @@ func TestDifferentAppendValueTypes(t *testing.T) { tr, err = testAppend.run(trCtx, tr) require.NoError(t, err) - exp = common.MapStr{ + exp = mapstr.M{ "p1": []interface{}{"1"}, } diff --git a/x-pack/filebeat/input/httpjson/transform_delete.go b/x-pack/filebeat/input/httpjson/transform_delete.go index afb36086126..d0737baa881 100644 --- a/x-pack/filebeat/input/httpjson/transform_delete.go +++ b/x-pack/filebeat/input/httpjson/transform_delete.go @@ -9,6 +9,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const deleteName = "delete" @@ -104,8 +105,8 @@ func (delete *delete) run(ctx *transformContext, tr transformable) (transformabl return tr, nil } -func deleteFromCommonMap(m common.MapStr, key string) error { - if err := m.Delete(key); err != common.ErrKeyNotFound { //nolint:errorlint // common.ErrKeyNotFound is never wrapped by Delete. +func deleteFromCommonMap(m mapstr.M, key string) error { + if err := m.Delete(key); err != mapstr.ErrKeyNotFound { //nolint:errorlint // mapstr.ErrKeyNotFound is never wrapped by Delete. return err } return nil diff --git a/x-pack/filebeat/input/httpjson/transform_delete_test.go b/x-pack/filebeat/input/httpjson/transform_delete_test.go index 414b1dba187..40b81630cd2 100644 --- a/x-pack/filebeat/input/httpjson/transform_delete_test.go +++ b/x-pack/filebeat/input/httpjson/transform_delete_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewDelete(t *testing.T) { @@ -141,9 +142,9 @@ func TestDeleteFunctions(t *testing.T) { name: "deleteBody", tfunc: deleteBody, paramCtx: &transformContext{}, - paramTr: transformable{"body": common.MapStr{"a_key": "a_value"}}, + paramTr: transformable{"body": mapstr.M{"a_key": "a_value"}}, paramKey: "a_key", - expectedTr: transformable{"body": common.MapStr{}}, + expectedTr: transformable{"body": mapstr.M{}}, expectedErr: nil, }, { diff --git a/x-pack/filebeat/input/httpjson/transform_set.go b/x-pack/filebeat/input/httpjson/transform_set.go index 77fb2c41d72..6f5d0bb781e 100644 --- a/x-pack/filebeat/input/httpjson/transform_set.go +++ b/x-pack/filebeat/input/httpjson/transform_set.go @@ -11,6 +11,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) var errNewURLValueNotSet = errors.New("the new url.value was not set") @@ -121,7 +122,7 @@ func (set *set) run(ctx *transformContext, tr transformable) (transformable, err return tr, nil } -func setToCommonMap(m common.MapStr, key string, val interface{}) error { +func setToCommonMap(m mapstr.M, key string, val interface{}) error { if _, err := m.Put(key, val); err != nil { return err } diff --git a/x-pack/filebeat/input/httpjson/transform_set_test.go b/x-pack/filebeat/input/httpjson/transform_set_test.go index 9c173556de8..88986eb66ab 100644 --- a/x-pack/filebeat/input/httpjson/transform_set_test.go +++ b/x-pack/filebeat/input/httpjson/transform_set_test.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewSet(t *testing.T) { @@ -112,10 +113,10 @@ func TestSetFunctions(t *testing.T) { name: "setBody", tfunc: setBody, paramCtx: &transformContext{}, - paramTr: transformable{"body": common.MapStr{}}, + paramTr: transformable{"body": mapstr.M{}}, paramKey: "a_key", paramVal: "a_value", - expectedTr: transformable{"body": common.MapStr{"a_key": "a_value"}}, + expectedTr: transformable{"body": mapstr.M{"a_key": "a_value"}}, expectedErr: nil, }, { @@ -184,7 +185,7 @@ func TestDifferentSetValueTypes(t *testing.T) { tr, err = testAppend.run(trCtx, tr) require.NoError(t, err) - exp := common.MapStr{ + exp := mapstr.M{ "p1": map[string]interface{}{ "param": "value", }, @@ -211,7 +212,7 @@ func TestDifferentSetValueTypes(t *testing.T) { tr, err = testAppend.run(trCtx, tr) require.NoError(t, err) - exp = common.MapStr{ + exp = mapstr.M{ "p1": int64(1), } @@ -232,7 +233,7 @@ func TestDifferentSetValueTypes(t *testing.T) { tr, err = testAppend.run(trCtx, tr) require.NoError(t, err) - exp = common.MapStr{ + exp = mapstr.M{ "p1": "1", } diff --git a/x-pack/filebeat/input/httpjson/transform_test.go b/x-pack/filebeat/input/httpjson/transform_test.go index 5432334ecdf..cdc8bb0345f 100644 --- a/x-pack/filebeat/input/httpjson/transform_test.go +++ b/x-pack/filebeat/input/httpjson/transform_test.go @@ -12,26 +12,27 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEmptyTransformContext(t *testing.T) { ctx := emptyTransformContext() assert.Equal(t, &cursor{}, ctx.cursor) - assert.Equal(t, &common.MapStr{}, ctx.lastEvent) - assert.Equal(t, &common.MapStr{}, ctx.firstEvent) + assert.Equal(t, &mapstr.M{}, ctx.lastEvent) + assert.Equal(t, &mapstr.M{}, ctx.firstEvent) assert.Equal(t, &response{}, ctx.lastResponse) } func TestEmptyTransformable(t *testing.T) { tr := transformable{} - assert.Equal(t, common.MapStr{}, tr.body()) + assert.Equal(t, mapstr.M{}, tr.body()) assert.Equal(t, http.Header{}, tr.header()) } func TestTransformableNilClone(t *testing.T) { var tr transformable cl := tr.Clone() - assert.Equal(t, common.MapStr{}, cl.body()) + assert.Equal(t, mapstr.M{}, cl.body()) assert.Equal(t, http.Header{}, cl.header()) } @@ -41,7 +42,7 @@ func TestTransformableClone(t *testing.T) { _, _ = body.Put("key", "value") tr.setBody(body) cl := tr.Clone() - assert.Equal(t, common.MapStr{"key": "value"}, cl.body()) + assert.Equal(t, mapstr.M{"key": "value"}, cl.body()) assert.Equal(t, http.Header{}, cl.header()) } diff --git a/x-pack/filebeat/input/httpjson/value_tpl_test.go b/x-pack/filebeat/input/httpjson/value_tpl_test.go index efdbd1c043e..fbda35178ff 100644 --- a/x-pack/filebeat/input/httpjson/value_tpl_test.go +++ b/x-pack/filebeat/input/httpjson/value_tpl_test.go @@ -12,10 +12,10 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/useragent" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/version" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestValueTpl(t *testing.T) { @@ -34,9 +34,9 @@ func TestValueTpl(t *testing.T) { name: "can access Go types in context", value: `[[.last_response.header.Get "foo"]] [[.last_response.url.params.Get "foo"]] [[.url.Host]] [[.url.Query.Get "bar"]]`, paramCtx: &transformContext{ - firstEvent: &common.MapStr{}, - lastEvent: &common.MapStr{}, - lastResponse: newTestResponse(common.MapStr{"param": 25}, http.Header{"Foo": []string{"bar"}}, "http://localhost?foo=bar"), + firstEvent: &mapstr.M{}, + lastEvent: &mapstr.M{}, + lastResponse: newTestResponse(mapstr.M{"param": 25}, http.Header{"Foo": []string{"bar"}}, "http://localhost?foo=bar"), }, paramTr: transformable{"url": newURL("http://localhost?bar=bazz")}, paramDefVal: "", @@ -46,9 +46,9 @@ func TestValueTpl(t *testing.T) { name: "can render values from ctx", value: "[[.last_response.body.param]]", paramCtx: &transformContext{ - firstEvent: &common.MapStr{}, - lastEvent: &common.MapStr{}, - lastResponse: newTestResponse(common.MapStr{"param": 25}, nil, ""), + firstEvent: &mapstr.M{}, + lastEvent: &mapstr.M{}, + lastResponse: newTestResponse(mapstr.M{"param": 25}, nil, ""), }, paramTr: transformable{}, paramDefVal: "", @@ -58,7 +58,7 @@ func TestValueTpl(t *testing.T) { name: "can render default value if execute fails", value: "[[.last_response.body.does_not_exist]]", paramCtx: &transformContext{ - lastEvent: &common.MapStr{}, + lastEvent: &mapstr.M{}, }, paramTr: transformable{}, paramDefVal: "25", @@ -192,8 +192,8 @@ func TestValueTpl(t *testing.T) { name: "func getRFC5988Link", value: `[[ getRFC5988Link "previous" .last_response.header.Link ]]`, paramCtx: &transformContext{ - firstEvent: &common.MapStr{}, - lastEvent: &common.MapStr{}, + firstEvent: &mapstr.M{}, + lastEvent: &mapstr.M{}, lastResponse: newTestResponse( nil, http.Header{"Link": []string{ @@ -312,10 +312,10 @@ func TestValueTpl(t *testing.T) { `[[join .last_response.body.narr ","]] [[join .last_response.body.singlevalstr ","]] ` + `[[join .last_response.body.singlevalint ","]]`, paramCtx: &transformContext{ - firstEvent: &common.MapStr{}, - lastEvent: &common.MapStr{}, + firstEvent: &mapstr.M{}, + lastEvent: &mapstr.M{}, lastResponse: newTestResponse( - common.MapStr{ + mapstr.M{ "strarr": []string{ "foo", "bar", @@ -342,10 +342,10 @@ func TestValueTpl(t *testing.T) { name: "func sprintf", value: `[[sprintf "%q:%d" (join .last_response.body.arr ",") 1]]`, paramCtx: &transformContext{ - firstEvent: &common.MapStr{}, - lastEvent: &common.MapStr{}, + firstEvent: &mapstr.M{}, + lastEvent: &mapstr.M{}, lastResponse: newTestResponse( - common.MapStr{ + mapstr.M{ "arr": []string{ "foo", "bar", @@ -513,7 +513,7 @@ func TestValueTpl(t *testing.T) { } } -func newTestResponse(body common.MapStr, header http.Header, url string) *response { +func newTestResponse(body mapstr.M, header http.Header, url string) *response { resp := &response{ header: http.Header{}, } diff --git a/x-pack/filebeat/input/netflow/case.go b/x-pack/filebeat/input/netflow/case.go index fefd2ab307c..62e2a9aeeff 100644 --- a/x-pack/filebeat/input/netflow/case.go +++ b/x-pack/filebeat/input/netflow/case.go @@ -9,8 +9,8 @@ import ( "sync" "unicode" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record" + "github.com/elastic/elastic-agent-libs/mapstr" ) var fieldNameConverter = caseConverter{ @@ -33,8 +33,8 @@ func (c *caseConverter) memoize(nfName, converted string) string { return converted } -func (c *caseConverter) ToSnakeCase(orig record.Map) common.MapStr { - result := common.MapStr(make(map[string]interface{}, len(orig))) +func (c *caseConverter) ToSnakeCase(orig record.Map) mapstr.M { + result := mapstr.M(make(map[string]interface{}, len(orig))) c.rwMutex.RLock() defer c.rwMutex.RUnlock() diff --git a/x-pack/filebeat/input/netflow/convert.go b/x-pack/filebeat/input/netflow/convert.go index 9f2de13c652..6b53d63bc61 100644 --- a/x-pack/filebeat/input/netflow/convert.go +++ b/x-pack/filebeat/input/netflow/convert.go @@ -17,10 +17,10 @@ import ( "github.com/cespare/xxhash/v2" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/flowhash" "github.com/elastic/beats/v7/libbeat/conditions" "github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record" + "github.com/elastic/elastic-agent-libs/mapstr" ) func toBeatEvent(flow record.Record, internalNetworks []string) (event beat.Event) { @@ -57,7 +57,7 @@ func toBeatEventCommon(flow record.Record) beat.Event { } // ECS Fields -- event - ecsEvent := common.MapStr{ + ecsEvent := mapstr.M{ "created": time.Now().UTC(), "kind": "event", "category": []string{"network_traffic", "network"}, @@ -67,14 +67,14 @@ func toBeatEventCommon(flow record.Record) beat.Event { ecsEvent["type"] = []string{"connection"} } // ECS Fields -- device - ecsDevice := common.MapStr{} + ecsDevice := mapstr.M{} if exporter, ok := getKeyString(flow.Exporter, "address"); ok { ecsDevice["ip"] = extractIPFromIPPort(exporter) } return beat.Event{ Timestamp: flow.Timestamp, - Fields: common.MapStr{ + Fields: mapstr.M{ "netflow": fieldNameConverter.ToSnakeCase(flow.Fields), "event": ecsEvent, "observer": ecsDevice, @@ -111,9 +111,9 @@ func optionsToBeatEvent(flow record.Record) beat.Event { func flowToBeatEvent(flow record.Record, internalNetworks []string) beat.Event { event := toBeatEventCommon(flow) - ecsEvent, ok := event.Fields["event"].(common.MapStr) + ecsEvent, ok := event.Fields["event"].(mapstr.M) if !ok { - ecsEvent = common.MapStr{} + ecsEvent = mapstr.M{} event.Fields["event"] = ecsEvent } sysUptime, hasSysUptime := getKeyUint64(flow.Exporter, "uptimeMillis") @@ -154,8 +154,8 @@ func flowToBeatEvent(flow record.Record, internalNetworks []string) beat.Event { flowDirection, hasFlowDirection := getKeyUint64(flow.Fields, "flowDirection") // ECS Fields -- source, destination & related.ip - ecsSource := common.MapStr{} - ecsDest := common.MapStr{} + ecsSource := mapstr.M{} + ecsDest := mapstr.M{} var relatedIP []net.IP // Populate first with WLAN fields @@ -220,7 +220,7 @@ func flowToBeatEvent(flow record.Record, internalNetworks []string) beat.Event { } // ECS Fields -- Flow - ecsFlow := common.MapStr{} + ecsFlow := mapstr.M{} var srcIP, dstIP net.IP var srcPort, dstPort uint16 var protocol IPProtocol @@ -249,7 +249,7 @@ func flowToBeatEvent(flow record.Record, internalNetworks []string) beat.Event { ecsFlow["locality"] = getIPLocality(internalNetworks, srcIP, dstIP).String() // ECS Fields -- network - ecsNetwork := common.MapStr{} + ecsNetwork := mapstr.M{} if proto, found := getKeyUint64(flow.Fields, "protocolIdentifier"); found { ecsNetwork["transport"] = IPProtocol(proto).String() ecsNetwork["iana_number"] = proto @@ -323,7 +323,7 @@ func flowToBeatEvent(flow record.Record, internalNetworks []string) beat.Event { event.Fields["network"] = ecsNetwork } if len(relatedIP) > 0 { - event.Fields["related"] = common.MapStr{"ip": uniqueIPs(relatedIP)} + event.Fields["related"] = mapstr.M{"ip": uniqueIPs(relatedIP)} } return event } diff --git a/x-pack/filebeat/input/netflow/input_test.go b/x-pack/filebeat/input/netflow/input_test.go index 35348619077..3744999fc14 100644 --- a/x-pack/filebeat/input/netflow/input_test.go +++ b/x-pack/filebeat/input/netflow/input_test.go @@ -11,10 +11,10 @@ import ( "testing" "github.com/elastic/beats/v7/filebeat/input/inputtest" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNewInputDone(t *testing.T) { - config := common.MapStr{} + config := mapstr.M{} inputtest.AssertNotStartedInputCanBeDone(t, NewInput, &config) } diff --git a/x-pack/filebeat/input/o365audit/contentblob.go b/x-pack/filebeat/input/o365audit/contentblob.go index 8ac344df44f..598ca5f8b9a 100644 --- a/x-pack/filebeat/input/o365audit/contentblob.go +++ b/x-pack/filebeat/input/o365audit/contentblob.go @@ -13,8 +13,8 @@ import ( "github.com/Azure/go-autorest/autorest" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/filebeat/input/o365audit/poll" + "github.com/elastic/elastic-agent-libs/mapstr" ) // contentBlob is a poll.Transaction that processes "content blobs": @@ -55,9 +55,9 @@ func (c contentBlob) OnResponse(response *http.Response) (actions []poll.Action) if err := readJSONBody(response, &raws); err != nil { return append(actions, poll.Terminate(errors.Wrap(err, "reading body failed"))) } - entries := make([]common.MapStr, len(raws)) + entries := make([]mapstr.M, len(raws)) for idx, raw := range raws { - var entry common.MapStr + var entry mapstr.M if err := json.Unmarshal(raw, &entry); err != nil { return append(actions, poll.Terminate(errors.Wrap(err, "decoding json failed"))) } diff --git a/x-pack/filebeat/input/o365audit/contentblob_test.go b/x-pack/filebeat/input/o365audit/contentblob_test.go index 0e7ea7fb411..6fff2e7cb4b 100644 --- a/x-pack/filebeat/input/o365audit/contentblob_test.go +++ b/x-pack/filebeat/input/o365audit/contentblob_test.go @@ -12,9 +12,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/filebeat/input/o365audit/poll" + "github.com/elastic/elastic-agent-libs/mapstr" ) type contentStore struct { @@ -33,7 +33,7 @@ func (s *contentStore) onEvent(b beat.Event, checkpointUpdate interface{}) error return nil } -func (f *fakePoll) BlobContent(t testing.TB, b poll.Transaction, data []common.MapStr, nextUrl string) poll.Transaction { +func (f *fakePoll) BlobContent(t testing.TB, b poll.Transaction, data []mapstr.M, nextUrl string) poll.Transaction { urls, next := f.deliverResult(t, b, data, nextUrl) if !assert.Empty(t, urls) { t.Fatal("blob returned urls to fetch") @@ -41,8 +41,8 @@ func (f *fakePoll) BlobContent(t testing.TB, b poll.Transaction, data []common.M return next } -func makeEvent(ts time.Time, id string) common.MapStr { - return common.MapStr{ +func makeEvent(ts time.Time, id string) mapstr.M { + return mapstr.M{ "CreationTime": ts.Format(apiDateFormat), "Id": id, } @@ -81,7 +81,7 @@ func TestContentBlob(t *testing.T) { } baseCursor := checkpoint{Timestamp: time.Now()} query := ContentBlob("http://test.localhost/", baseCursor, ctx) - data := []common.MapStr{ + data := []mapstr.M{ makeEvent(now.Add(-time.Hour), "e1"), makeEvent(now.Add(-2*time.Hour), "e2"), makeEvent(now.Add(-30*time.Minute), "e3"), @@ -104,7 +104,7 @@ func TestContentBlobResumeToLine(t *testing.T) { const skip = 3 baseCursor.Line = skip query := ContentBlob("http://test.localhost/", baseCursor, ctx).WithSkipLines(skip) - data := []common.MapStr{ + data := []mapstr.M{ makeEvent(now.Add(-time.Hour), "e1"), makeEvent(now.Add(-2*time.Hour), "e2"), makeEvent(now.Add(-30*time.Minute), "e3"), @@ -127,7 +127,7 @@ func TestContentBlobPaged(t *testing.T) { } baseCursor := checkpoint{Timestamp: time.Now()} query := ContentBlob("http://test.localhost/", baseCursor, ctx) - data := []common.MapStr{ + data := []mapstr.M{ makeEvent(now.Add(-time.Hour), "e1"), makeEvent(now.Add(-2*time.Hour), "e2"), makeEvent(now.Add(-30*time.Minute), "e3"), diff --git a/x-pack/filebeat/input/o365audit/dates.go b/x-pack/filebeat/input/o365audit/dates.go index 30f58d11e01..ff6eb5db989 100644 --- a/x-pack/filebeat/input/o365audit/dates.go +++ b/x-pack/filebeat/input/o365audit/dates.go @@ -10,7 +10,7 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -54,7 +54,7 @@ func (d dateFormats) Parse(str string) (t time.Time, err error) { } // Get a key from a map and cast it to string. -func getString(m common.MapStr, key string) (string, error) { +func getString(m mapstr.M, key string) (string, error) { iValue, err := m.GetValue(key) if err != nil { return "", err @@ -67,7 +67,7 @@ func getString(m common.MapStr, key string) (string, error) { } // Parse a date from the given map key. -func getDateKey(m common.MapStr, key string, formats dateFormats) (t time.Time, err error) { +func getDateKey(m mapstr.M, key string, formats dateFormats) (t time.Time, err error) { str, err := getString(m, key) if err != nil { return t, err diff --git a/x-pack/filebeat/input/o365audit/input.go b/x-pack/filebeat/input/o365audit/input.go index 99bbf79ec1b..c0d59772ab4 100644 --- a/x-pack/filebeat/input/o365audit/input.go +++ b/x-pack/filebeat/input/o365audit/input.go @@ -21,6 +21,7 @@ import ( "github.com/elastic/beats/v7/libbeat/feature" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/filebeat/input/o365audit/poll" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-concert/ctxtool" "github.com/elastic/go-concert/timed" ) @@ -116,7 +117,7 @@ func (inp *o365input) Run( break } if ctx.Cancelation.Err() != err && err != context.Canceled { - msg := common.MapStr{} + msg := mapstr.M{} msg.Put("error.message", err.Error()) msg.Put("event.kind", "pipeline_error") event := beat.Event{ @@ -163,7 +164,7 @@ func (inp *o365input) runOnce( poll.WithContext(ctxtool.FromCanceller(ctx.Cancelation)), poll.WithRequestDecorator( autorest.WithUserAgent(useragent.UserAgent("Filebeat-"+pluginName)), - autorest.WithQueryParameters(common.MapStr{ + autorest.WithQueryParameters(mapstr.M{ "publisherIdentifier": tenantID, }), ), @@ -223,7 +224,7 @@ func initCheckpoint(log *logp.Logger, c cursor.Cursor, maxRetention time.Duratio } // Report returns an action that produces a beat.Event from the given object. -func (env apiEnvironment) Report(raw json.RawMessage, doc common.MapStr, private interface{}) poll.Action { +func (env apiEnvironment) Report(raw json.RawMessage, doc mapstr.M, private interface{}) poll.Action { return func(poll.Enqueuer) error { return env.Callback(env.toBeatEvent(raw, doc), private) } @@ -236,7 +237,7 @@ func (env apiEnvironment) ReportAPIError(err apiError) poll.Action { } } -func (env apiEnvironment) toBeatEvent(raw json.RawMessage, doc common.MapStr) beat.Event { +func (env apiEnvironment) toBeatEvent(raw json.RawMessage, doc mapstr.M) beat.Event { var errs multierror.Errors ts, err := getDateKey(doc, "CreationTime", apiDateFormats) if err != nil { @@ -245,7 +246,7 @@ func (env apiEnvironment) toBeatEvent(raw json.RawMessage, doc common.MapStr) be } b := beat.Event{ Timestamp: ts, - Fields: common.MapStr{ + Fields: mapstr.M{ fieldsPrefix: doc, }, } diff --git a/x-pack/filebeat/input/o365audit/input_test.go b/x-pack/filebeat/input/o365audit/input_test.go index ce95c4c7770..d19efe6e77c 100644 --- a/x-pack/filebeat/input/o365audit/input_test.go +++ b/x-pack/filebeat/input/o365audit/input_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestPreserveOriginalEvent(t *testing.T) { @@ -20,7 +20,7 @@ func TestPreserveOriginalEvent(t *testing.T) { } raw := json.RawMessage(`{"field1":"val1"}`) - doc := common.MapStr{ + doc := mapstr.M{ "field1": "val1", } diff --git a/x-pack/filebeat/input/o365audit/schema.go b/x-pack/filebeat/input/o365audit/schema.go index 77519a8e953..95528076382 100644 --- a/x-pack/filebeat/input/o365audit/schema.go +++ b/x-pack/filebeat/input/o365audit/schema.go @@ -9,7 +9,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type apiError struct { @@ -41,12 +41,12 @@ func (e apiError) ToBeatEvent() beat.Event { code, msg := e.getErrorStrings() return beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ - "error": common.MapStr{ + Fields: mapstr.M{ + "error": mapstr.M{ "code": code, "message": msg, }, - "event": common.MapStr{ + "event": mapstr.M{ "kind": "pipeline_error", }, }, diff --git a/x-pack/filebeat/module/cisco/ios/pipeline_test.go b/x-pack/filebeat/module/cisco/ios/pipeline_test.go index 5b16071cf03..6492eb8eed6 100644 --- a/x-pack/filebeat/module/cisco/ios/pipeline_test.go +++ b/x-pack/filebeat/module/cisco/ios/pipeline_test.go @@ -10,10 +10,10 @@ import ( "testing" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/libbeat/processors/script/javascript" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/isdef" "github.com/elastic/go-lookslike/validator" @@ -200,9 +200,9 @@ func testInput(t *testing.T, input string, p processors.Processor) { } e := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": tc.message, - "input": common.MapStr{ + "input": mapstr.M{ "type": input, }, }, @@ -245,9 +245,9 @@ func BenchmarkPipeline(b *testing.B) { for i := 0; i < b.N; i++ { e := beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": testCases[i%len(testCases)].message, - "input": common.MapStr{ + "input": mapstr.M{ "type": "syslog", }, }, diff --git a/x-pack/filebeat/processors/add_nomad_metadata/matchers.go b/x-pack/filebeat/processors/add_nomad_metadata/matchers.go index 8fd2f99a97b..37b0597464a 100644 --- a/x-pack/filebeat/processors/add_nomad_metadata/matchers.go +++ b/x-pack/filebeat/processors/add_nomad_metadata/matchers.go @@ -13,6 +13,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/libbeat/processors/add_nomad_metadata" + "github.com/elastic/elastic-agent-libs/mapstr" ) // LogPathMatcherName is the name of LogPathMatcher @@ -70,7 +71,7 @@ func newLogsPathMatcher(cfg common.Config) (add_nomad_metadata.Matcher, error) { // MetadataIndex returns the index key to be used for enriching the event with the proper metadata // which is the allocation id from the event `log.file.path` field -func (m *LogPathMatcher) MetadataIndex(event common.MapStr) string { +func (m *LogPathMatcher) MetadataIndex(event mapstr.M) string { value, err := event.GetValue("log.file.path") if err == nil { diff --git a/x-pack/filebeat/processors/add_nomad_metadata/matchers_test.go b/x-pack/filebeat/processors/add_nomad_metadata/matchers_test.go index 0022eac2e35..31150b5d20b 100644 --- a/x-pack/filebeat/processors/add_nomad_metadata/matchers_test.go +++ b/x-pack/filebeat/processors/add_nomad_metadata/matchers_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) const allocID = "43205e0e-3d55-f561-83cb-bed15e23b862" @@ -49,9 +50,9 @@ func executeTest(t *testing.T, cfgLogsPath string, source string, expectedResult logMatcher, err := newLogsPathMatcher(*cfg) assert.Nil(t, err) - input := common.MapStr{ - "log": common.MapStr{ - "file": common.MapStr{ + input := mapstr.M{ + "log": mapstr.M{ + "file": mapstr.M{ "path": source, }, }, diff --git a/x-pack/filebeat/processors/decode_cef/decode_cef.go b/x-pack/filebeat/processors/decode_cef/decode_cef.go index 51b2d62aad3..9795df9ba45 100644 --- a/x-pack/filebeat/processors/decode_cef/decode_cef.go +++ b/x-pack/filebeat/processors/decode_cef/decode_cef.go @@ -17,6 +17,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/x-pack/filebeat/processors/decode_cef/cef" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -136,9 +137,9 @@ func (p *processor) Run(event *beat.Event) (*beat.Event, error) { return event, nil } -func toCEFObject(cefEvent *cef.Event) common.MapStr { +func toCEFObject(cefEvent *cef.Event) mapstr.M { // Add CEF header fields. - cefObject := common.MapStr{"version": strconv.Itoa(cefEvent.Version)} + cefObject := mapstr.M{"version": strconv.Itoa(cefEvent.Version)} if cefEvent.DeviceVendor != "" { cefObject.Put("device.vendor", cefEvent.DeviceVendor) } @@ -160,7 +161,7 @@ func toCEFObject(cefEvent *cef.Event) common.MapStr { // Add CEF extensions (key-value pairs). if len(cefEvent.Extensions) > 0 { - extensions := make(common.MapStr, len(cefEvent.Extensions)) + extensions := make(mapstr.M, len(cefEvent.Extensions)) cefObject.Put("extensions", extensions) for k, field := range cefEvent.Extensions { if field.Interface != nil { @@ -198,7 +199,7 @@ func writeCEFHeaderToECS(cefEvent *cef.Event, event *beat.Event) { } } -func appendErrorMessage(m common.MapStr, msg string) error { +func appendErrorMessage(m mapstr.M, msg string) error { const field = "error.message" list, _ := m.GetValue(field) diff --git a/x-pack/filebeat/processors/decode_cef/decode_cef_test.go b/x-pack/filebeat/processors/decode_cef/decode_cef_test.go index 27fd53252b4..0b5c8607d91 100644 --- a/x-pack/filebeat/processors/decode_cef/decode_cef_test.go +++ b/x-pack/filebeat/processors/decode_cef/decode_cef_test.go @@ -16,7 +16,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) var updateGolden = flag.Bool("update", false, "update golden test files") @@ -25,7 +25,7 @@ func TestProcessorRun(t *testing.T) { type testCase struct { config func() config message string - fields common.MapStr + fields mapstr.M } testCases := map[string]testCase{ @@ -36,7 +36,7 @@ func TestProcessorRun(t *testing.T) { return c }, message: "CEF:1|Trend Micro|Deep Security Manager|1.2.3|600|User Signed In|3|src=10.52.116.160 suser=admin target=admin msg=User signed in from 2001:db8::5", - fields: common.MapStr{ + fields: mapstr.M{ "version": "1", "device.event_class_id": "600", "device.product": "Deep Security Manager", @@ -61,7 +61,7 @@ func TestProcessorRun(t *testing.T) { }, "parse_errors": { message: "CEF:0|Trend Micro|Deep Security Manager|1.2.3|600|User Signed In|Low|msg=User signed in with =xyz", - fields: common.MapStr{ + fields: mapstr.M{ "cef.version": "0", "cef.device.event_class_id": "600", "cef.device.product": "Deep Security Manager", @@ -89,7 +89,7 @@ func TestProcessorRun(t *testing.T) { return c }, message: "CEF:0|Trend Micro|Deep Security Manager|1.2.3|600|User Signed In|3|src=10.52.116.160 suser=admin target=admin msg=User signed in from 2001:db8::5", - fields: common.MapStr{ + fields: mapstr.M{ "cef.version": "0", "cef.device.event_class_id": "600", "cef.device.product": "Deep Security Manager", @@ -111,7 +111,7 @@ func TestProcessorRun(t *testing.T) { return c }, message: "CEF:0|SentinelOne|Mgmt|activityID=1111111111111111111 activityType=3505 siteId=None siteName=None accountId=1222222222222222222 accountName=foo-bar mdr notificationScope=ACCOUNT", - fields: common.MapStr{ + fields: mapstr.M{ "cef.version": "0", "cef.device.product": "Mgmt", "cef.device.vendor": "SentinelOne", @@ -147,7 +147,7 @@ func TestProcessorRun(t *testing.T) { } evt := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": tc.message, }, } @@ -163,7 +163,7 @@ func TestProcessorRun(t *testing.T) { t.Run("not_cef", func(t *testing.T) { evt := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "hello world!", }, } @@ -178,7 +178,7 @@ func TestProcessorRun(t *testing.T) { tc := testCases["custom_target_root"] evt := &beat.Event{ - Fields: common.MapStr{ + Fields: mapstr.M{ "message": "leading garbage" + tc.message, }, } @@ -212,7 +212,7 @@ func TestGolden(t *testing.T) { } } -func readCEFSamples(t testing.TB, source string) []common.MapStr { +func readCEFSamples(t testing.TB, source string) []mapstr.M { f, err := os.Open(source) if err != nil { t.Fatal(err) @@ -226,7 +226,7 @@ func readCEFSamples(t testing.TB, source string) []common.MapStr { t.Fatal(err) } - var samples []common.MapStr + var samples []mapstr.M s := bufio.NewScanner(f) for s.Scan() { data := s.Bytes() @@ -235,8 +235,8 @@ func readCEFSamples(t testing.TB, source string) []common.MapStr { } evt := &beat.Event{ - Fields: common.MapStr{ - "event": common.MapStr{"original": string(data)}, + Fields: mapstr.M{ + "event": mapstr.M{"original": string(data)}, }, } @@ -254,7 +254,7 @@ func readCEFSamples(t testing.TB, source string) []common.MapStr { return samples } -func readGoldenJSON(t testing.TB, source string) []common.MapStr { +func readGoldenJSON(t testing.TB, source string) []mapstr.M { source = source + ".golden.json" f, err := os.Open(source) @@ -265,7 +265,7 @@ func readGoldenJSON(t testing.TB, source string) []common.MapStr { dec := json.NewDecoder(bufio.NewReader(f)) - var events []common.MapStr + var events []mapstr.M if err = dec.Decode(&events); err != nil { t.Fatal(err) } @@ -273,7 +273,7 @@ func readGoldenJSON(t testing.TB, source string) []common.MapStr { return events } -func writeGoldenJSON(t testing.TB, source string, events []common.MapStr) { +func writeGoldenJSON(t testing.TB, source string, events []mapstr.M) { dest := source + ".golden.json" f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) @@ -289,13 +289,13 @@ func writeGoldenJSON(t testing.TB, source string, events []common.MapStr) { } } -func normalize(t testing.TB, m common.MapStr) common.MapStr { +func normalize(t testing.TB, m mapstr.M) mapstr.M { data, err := json.Marshal(m) if err != nil { t.Fatal(err) } - var out common.MapStr + var out mapstr.M if err = json.Unmarshal(data, &out); err != nil { t.Fatal(err) } diff --git a/x-pack/functionbeat/function/beater/functionbeat.go b/x-pack/functionbeat/function/beater/functionbeat.go index cbf1083082f..4572f08e32e 100644 --- a/x-pack/functionbeat/function/beater/functionbeat.go +++ b/x-pack/functionbeat/function/beater/functionbeat.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common/fmtstr" "github.com/elastic/beats/v7/libbeat/outputs/elasticsearch" "github.com/elastic/beats/v7/libbeat/publisher/pipeline" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" @@ -148,7 +149,7 @@ type fnExtraConfig struct { // KeepNull determines whether published events will keep null values or omit them. KeepNull bool `config:"keep_null"` - common.EventMetadata `config:",inline"` // Fields and tags to add to events. + mapstr.EventMetadata `config:",inline"` // Fields and tags to add to events. // ES output index pattern Index fmtstr.EventFormatString `config:"index"` diff --git a/x-pack/functionbeat/function/beater/proccessors_test.go b/x-pack/functionbeat/function/beater/proccessors_test.go index c38649fe2e2..fb4a545c236 100644 --- a/x-pack/functionbeat/function/beater/proccessors_test.go +++ b/x-pack/functionbeat/function/beater/proccessors_test.go @@ -16,6 +16,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/processors" _ "github.com/elastic/beats/v7/libbeat/processors/actions" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestProcessorsForFunction(t *testing.T) { @@ -48,7 +49,7 @@ func TestProcessorsForFunction(t *testing.T) { } for description, test := range testCases { if test.event.Fields == nil { - test.event.Fields = common.MapStr{} + test.event.Fields = mapstr.M{} } config, err := functionConfigFromString(test.configStr) if err != nil { @@ -122,7 +123,7 @@ type setRawIndex struct { func (p *setRawIndex) Run(event *beat.Event) (*beat.Event, error) { if event.Meta == nil { - event.Meta = common.MapStr{} + event.Meta = mapstr.M{} } event.Meta[events.FieldMetaRawIndex] = p.indexStr return event, nil diff --git a/x-pack/functionbeat/provider/aws/aws/transformer/transformer.go b/x-pack/functionbeat/provider/aws/aws/transformer/transformer.go index b77dafcc24e..4d9d65da32f 100644 --- a/x-pack/functionbeat/provider/aws/aws/transformer/transformer.go +++ b/x-pack/functionbeat/provider/aws/aws/transformer/transformer.go @@ -17,7 +17,7 @@ import ( "github.com/awslabs/kinesis-aggregation/go/deaggregator" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Centralize anything related to ECS into a common file. @@ -32,11 +32,11 @@ func CloudwatchLogs(request events.CloudwatchLogsData) []beat.Event { for idx, logEvent := range request.LogEvents { events[idx] = beat.Event{ Timestamp: time.Unix(0, logEvent.Timestamp*1000000), - Fields: common.MapStr{ - "event": common.MapStr{ + Fields: mapstr.M{ + "event": mapstr.M{ "kind": "event", }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", }, "message": logEvent.Message, @@ -57,17 +57,17 @@ func CloudwatchLogs(request events.CloudwatchLogsData) []beat.Event { func APIGatewayProxyRequest(request events.APIGatewayProxyRequest) beat.Event { return beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ - "event": common.MapStr{ + Fields: mapstr.M{ + "event": mapstr.M{ "kind": "event", "category": []string{"network"}, "type": []string{"connection", "protocol"}, }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", "account.id": request.RequestContext.AccountID, }, - "network": common.MapStr{ + "network": mapstr.M{ "transport": "tcp", "protocol": "http", }, @@ -103,11 +103,11 @@ func KinesisEvent(request events.KinesisEvent) ([]beat.Event, error) { for _, deaggRecord := range deaggRecords { events = append(events, beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ - "event": common.MapStr{ + Fields: mapstr.M{ + "event": mapstr.M{ "kind": "event", }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", "region": record.AwsRegion, }, @@ -134,11 +134,11 @@ func KinesisEvent(request events.KinesisEvent) ([]beat.Event, error) { func CloudwatchKinesisEvent(request events.KinesisEvent, base64Encoded, compressed bool) ([]beat.Event, error) { var evts []beat.Event for _, record := range request.Records { - envelopeFields := common.MapStr{ - "event": common.MapStr{ + envelopeFields := mapstr.M{ + "event": mapstr.M{ "kind": "event", }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", "region": record.AwsRegion, }, @@ -205,11 +205,11 @@ func SQS(request events.SQSEvent) []beat.Event { for idx, record := range request.Records { events[idx] = beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ - "event": common.MapStr{ + Fields: mapstr.M{ + "event": mapstr.M{ "kind": "event", }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", "region": record.AWSRegion, }, diff --git a/x-pack/functionbeat/provider/aws/aws/transformer/transformer_test.go b/x-pack/functionbeat/provider/aws/aws/transformer/transformer_test.go index 373c7981c78..165a7971084 100644 --- a/x-pack/functionbeat/provider/aws/aws/transformer/transformer_test.go +++ b/x-pack/functionbeat/provider/aws/aws/transformer/transformer_test.go @@ -20,7 +20,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestCloudwatch(t *testing.T) { @@ -47,11 +47,11 @@ func TestCloudwatch(t *testing.T) { expectedEvent := beat.Event{ Timestamp: expectedTime, - Fields: common.MapStr{ - "event": common.MapStr{ + Fields: mapstr.M{ + "event": mapstr.M{ "kind": "event", }, - "cloud": common.MapStr{ + "cloud": mapstr.M{ "provider": "aws", }, "message": "my interesting message", @@ -94,12 +94,12 @@ func TestKinesis(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(events)) - fields := common.MapStr{ - "cloud": common.MapStr{ + fields := mapstr.M{ + "cloud": mapstr.M{ "provider": "aws", "region": "us-east-1", }, - "event": common.MapStr{ + "event": mapstr.M{ "kind": "event", }, "event_id": "1234", @@ -148,12 +148,12 @@ func TestKinesis(t *testing.T) { assert.NoError(t, err) assert.Equal(t, numRecords, len(events)) - envelopeFields := common.MapStr{ - "cloud": common.MapStr{ + envelopeFields := mapstr.M{ + "cloud": mapstr.M{ "provider": "aws", "region": "us-east-1", }, - "event": common.MapStr{ + "event": mapstr.M{ "kind": "event", }, "event_id": "1234", @@ -167,9 +167,9 @@ func TestKinesis(t *testing.T) { "kinesis_encryption_type": "test", } - var expectedInnerFields []common.MapStr + var expectedInnerFields []mapstr.M for i := 0; i < numRecords; i++ { - expectedInnerFields = append(expectedInnerFields, common.MapStr{ + expectedInnerFields = append(expectedInnerFields, mapstr.M{ "message": fmt.Sprintf("%s %d", "hello world", i), "kinesis_partition_key": fmt.Sprintf("%s %d", "partKey", i), }) @@ -242,12 +242,12 @@ func TestKinesis(t *testing.T) { assert.NoError(t, err) assert.Equal(t, numRecords, len(events)) - envelopeFields := common.MapStr{ - "cloud": common.MapStr{ + envelopeFields := mapstr.M{ + "cloud": mapstr.M{ "provider": "aws", "region": "us-east-1", }, - "event": common.MapStr{ + "event": mapstr.M{ "kind": "event", }, "event_id": "1234", @@ -303,12 +303,12 @@ ciJ9XX0=`), assert.NoError(t, err) assert.Equal(t, 3, len(events)) - envelopeFields := common.MapStr{ - "cloud": common.MapStr{ + envelopeFields := mapstr.M{ + "cloud": mapstr.M{ "provider": "aws", "region": "us-east-1", }, - "event": common.MapStr{ + "event": mapstr.M{ "kind": "event", }, "event_id": "1234", @@ -323,8 +323,8 @@ ciJ9XX0=`), "kinesis_encryption_type": "test", } - expectedInnerFields := []common.MapStr{ - common.MapStr{ + expectedInnerFields := []mapstr.M{ + mapstr.M{ "id": "34933589873972040308280302318523185960554189168873242624", "log_group": "testem", "log_stream": "folyomany", @@ -335,7 +335,7 @@ ciJ9XX0=`), "Minden", }, }, - common.MapStr{ + mapstr.M{ "id": "34933589873994341053478832941664721678826837530379223041", "log_group": "testem", "log_stream": "folyomany", @@ -346,7 +346,7 @@ ciJ9XX0=`), "Minden", }, }, - common.MapStr{ + mapstr.M{ "id": "34933589874016641798677363564806257397099485891885203458", "log_group": "testem", "log_stream": "folyomany", diff --git a/x-pack/functionbeat/provider/local/local/local.go b/x-pack/functionbeat/provider/local/local/local.go index d0d849688d0..88ffe59eeb0 100644 --- a/x-pack/functionbeat/provider/local/local/local.go +++ b/x-pack/functionbeat/provider/local/local/local.go @@ -16,6 +16,7 @@ import ( "github.com/elastic/beats/v7/libbeat/publisher/pipeline" "github.com/elastic/beats/v7/x-pack/functionbeat/function/provider" "github.com/elastic/beats/v7/x-pack/functionbeat/function/telemetry" + "github.com/elastic/elastic-agent-libs/mapstr" ) const stdinName = "stdin" @@ -89,7 +90,7 @@ func (s *StdinFunction) Run(ctx context.Context, client pipeline.ISyncClient, _ func (s *StdinFunction) newEvent(line string) beat.Event { event := beat.Event{ Timestamp: time.Now(), - Fields: common.MapStr{ + Fields: mapstr.M{ "message": line, }, } diff --git a/x-pack/heartbeat/monitors/browser/source/zipurl_test.go b/x-pack/heartbeat/monitors/browser/source/zipurl_test.go index 9c160684642..ef4bf89a578 100644 --- a/x-pack/heartbeat/monitors/browser/source/zipurl_test.go +++ b/x-pack/heartbeat/monitors/browser/source/zipurl_test.go @@ -19,19 +19,20 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/source/fixtures" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestSimpleCases(t *testing.T) { type testCase struct { name string - cfg common.MapStr + cfg mapstr.M tlsServer bool wantFetchErr bool } testCases := []testCase{ { "basics", - common.MapStr{ + mapstr.M{ "folder": "/", "retries": 3, }, @@ -40,7 +41,7 @@ func TestSimpleCases(t *testing.T) { }, { "targetdir", - common.MapStr{ + mapstr.M{ "folder": "/", "retries": 3, "target_directory": "/tmp/synthetics/blah", @@ -50,7 +51,7 @@ func TestSimpleCases(t *testing.T) { }, { "auth success", - common.MapStr{ + mapstr.M{ "folder": "/", "retries": 3, "username": "testuser", @@ -61,7 +62,7 @@ func TestSimpleCases(t *testing.T) { }, { "auth failure", - common.MapStr{ + mapstr.M{ "folder": "/", "retries": 3, "username": "testuser", @@ -72,10 +73,10 @@ func TestSimpleCases(t *testing.T) { }, { "ssl ignore cert errors", - common.MapStr{ + mapstr.M{ "folder": "/", "retries": 3, - "ssl": common.MapStr{ + "ssl": mapstr.M{ "enabled": "true", "verification_mode": "none", }, @@ -85,10 +86,10 @@ func TestSimpleCases(t *testing.T) { }, { "bad ssl", - common.MapStr{ + mapstr.M{ "folder": "/", "retries": 3, - "ssl": common.MapStr{ + "ssl": mapstr.M{ "enabled": "true", "certificate_authorities": []string{}, }, @@ -123,7 +124,7 @@ func TestZipUrlWithSameEtag(t *testing.T) { address, teardown := setupTests(false) defer teardown() - zus, err := dummyZus(common.MapStr{ + zus, err := dummyZus(mapstr.M{ "url": fmt.Sprintf("%s/fixtures/todos.zip", address), "folder": "/", "retries": 3, @@ -145,7 +146,7 @@ func TestZipUrlWithBadUrl(t *testing.T) { _, teardown := setupTests(false) defer teardown() - zus, err := dummyZus(common.MapStr{ + zus, err := dummyZus(mapstr.M{ "url": "http://notahost.notadomaintoehutoeuhn", "folder": "/", "retries": 2, diff --git a/x-pack/heartbeat/monitors/browser/suite.go b/x-pack/heartbeat/monitors/browser/suite.go index c4f52921b41..45ea67b4b47 100644 --- a/x-pack/heartbeat/monitors/browser/suite.go +++ b/x-pack/heartbeat/monitors/browser/suite.go @@ -13,9 +13,10 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/synthexec" + "github.com/elastic/elastic-agent-libs/mapstr" ) -type JourneyLister func(ctx context.Context, suitePath string, params common.MapStr) (journeyNames []string, err error) +type JourneyLister func(ctx context.Context, suitePath string, params mapstr.M) (journeyNames []string, err error) type Suite struct { rawCfg *common.Config diff --git a/x-pack/heartbeat/monitors/browser/suite_test.go b/x-pack/heartbeat/monitors/browser/suite_test.go index db495e676fa..d1c71e5e5e0 100644 --- a/x-pack/heartbeat/monitors/browser/suite_test.go +++ b/x-pack/heartbeat/monitors/browser/suite_test.go @@ -16,6 +16,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/source" "github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/synthexec" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestValidLocal(t *testing.T) { @@ -25,7 +26,7 @@ func TestValidLocal(t *testing.T) { "key1": "value1", "key2": "value2", } - cfg := common.MustNewConfigFrom(common.MapStr{ + cfg := common.MustNewConfigFrom(mapstr.M{ "name": "My Name", "id": "myId", "params": testParams, @@ -33,8 +34,8 @@ func TestValidLocal(t *testing.T) { Tags: []string{"*"}, Match: "*", }, - "source": common.MapStr{ - "local": common.MapStr{ + "source": mapstr.M{ + "local": mapstr.M{ "path": path, }, }, @@ -62,12 +63,12 @@ func TestValidInline(t *testing.T) { "key1": "value1", "key2": "value2", } - cfg := common.MustNewConfigFrom(common.MapStr{ + cfg := common.MustNewConfigFrom(mapstr.M{ "name": "My Name", "id": "myId", "params": testParams, - "source": common.MapStr{ - "inline": common.MapStr{ + "source": mapstr.M{ + "inline": mapstr.M{ "script": script, }, }, @@ -86,10 +87,10 @@ func TestValidInline(t *testing.T) { } func TestNameRequired(t *testing.T) { - cfg := common.MustNewConfigFrom(common.MapStr{ + cfg := common.MustNewConfigFrom(mapstr.M{ "id": "myId", - "source": common.MapStr{ - "inline": common.MapStr{ + "source": mapstr.M{ + "inline": mapstr.M{ "script": "a script", }, }, @@ -99,10 +100,10 @@ func TestNameRequired(t *testing.T) { } func TestIDRequired(t *testing.T) { - cfg := common.MustNewConfigFrom(common.MapStr{ + cfg := common.MustNewConfigFrom(mapstr.M{ "name": "My Name", - "source": common.MapStr{ - "inline": common.MapStr{ + "source": mapstr.M{ + "inline": mapstr.M{ "script": "a script", }, }, @@ -112,8 +113,8 @@ func TestIDRequired(t *testing.T) { } func TestEmptySource(t *testing.T) { - cfg := common.MustNewConfigFrom(common.MapStr{ - "source": common.MapStr{}, + cfg := common.MustNewConfigFrom(mapstr.M{ + "source": mapstr.M{}, }) s, e := NewSuite(cfg) diff --git a/x-pack/heartbeat/monitors/browser/synthexec/enrich.go b/x-pack/heartbeat/monitors/browser/synthexec/enrich.go index e2ad53a20ad..83bd898b25e 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/enrich.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/enrich.go @@ -10,13 +10,13 @@ import ( "github.com/elastic/beats/v7/libbeat/beat/events" "github.com/elastic/beats/v7/libbeat/processors/add_data_stream" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/gofrs/uuid" "github.com/elastic/beats/v7/heartbeat/eventext" "github.com/elastic/beats/v7/heartbeat/monitors/logger" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" ) type enricher func(event *beat.Event, se *SynthEvent, fields StdSuiteFields) error @@ -44,7 +44,7 @@ type journeyEnricher struct { stepCount int // The first URL we visit is the URL for this journey, which is set on the summary event. // We store the URL fields here for use on the summary event. - urlFields common.MapStr + urlFields mapstr.M start time.Time end time.Time } @@ -95,11 +95,11 @@ func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent, fields StdS id = fmt.Sprintf("%s-%s", id, je.journey.Id) name = fmt.Sprintf("%s - %s", name, je.journey.Name) } - eventext.MergeEventFields(event, common.MapStr{ - "event": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "event": mapstr.M{ "type": se.Type, }, - "monitor": common.MapStr{ + "monitor": mapstr.M{ "check_group": je.checkGroup, "id": id, "name": name, @@ -109,8 +109,8 @@ func (je *journeyEnricher) enrich(event *beat.Event, se *SynthEvent, fields StdS // Write suite level fields for suite monitors if !fields.IsInline { - eventext.MergeEventFields(event, common.MapStr{ - "suite": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "suite": mapstr.M{ "id": fields.Id, "name": fields.Name, }, @@ -165,7 +165,7 @@ func (je *journeyEnricher) enrichSynthEvent(event *beat.Event, se *SynthEvent) e if je.urlFields == nil { if urlFields, err := event.GetValue("url"); err == nil { - if ufMap, ok := urlFields.(common.MapStr); ok { + if ufMap, ok := urlFields.(mapstr.M); ok { je.urlFields = ufMap } } @@ -188,24 +188,24 @@ func (je *journeyEnricher) createSummary(event *beat.Event) error { // to inform the journey never ran if !je.start.IsZero() { duration := je.end.Sub(je.start) - eventext.MergeEventFields(event, common.MapStr{ - "monitor": common.MapStr{ - "duration": common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ + "monitor": mapstr.M{ + "duration": mapstr.M{ "us": duration.Microseconds(), }, }, }) } - eventext.MergeEventFields(event, common.MapStr{ + eventext.MergeEventFields(event, mapstr.M{ "url": je.urlFields, - "event": common.MapStr{ + "event": mapstr.M{ "type": "heartbeat/summary", }, - "synthetics": common.MapStr{ + "synthetics": mapstr.M{ "type": "heartbeat/summary", "journey": je.journey, }, - "summary": common.MapStr{ + "summary": mapstr.M{ "up": up, "down": down, }, diff --git a/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go b/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go index 112c9e16ab8..80e5fba178a 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/enrich_test.go @@ -19,9 +19,9 @@ import ( "github.com/elastic/beats/v7/heartbeat/monitors/wrappers" "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/beat/events" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors/add_data_stream" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" "github.com/elastic/go-lookslike/validator" @@ -34,7 +34,7 @@ func makeStepEvent(typ string, ts float64, name string, index int, status string PackageVersion: "1.0.0", Step: &Step{Name: name, Index: index, Status: status}, Error: err, - Payload: common.MapStr{}, + Payload: mapstr.M{}, URL: urlstr, } } @@ -65,14 +65,14 @@ func TestJourneyEnricher(t *testing.T) { TimestampEpochMicros: 1000, PackageVersion: "1.0.0", Journey: journey, - Payload: common.MapStr{}, + Payload: mapstr.M{}, } journeyEnd := &SynthEvent{ Type: JourneyEnd, TimestampEpochMicros: 2000, PackageVersion: "1.0.0", Journey: journey, - Payload: common.MapStr{}, + Payload: mapstr.M{}, } url1 := "http://example.net/url1" url2 := "http://example.net/url2" @@ -90,7 +90,7 @@ func TestJourneyEnricher(t *testing.T) { } suiteValidator := func() validator.Validator { - return lookslike.MustCompile(common.MapStr{ + return lookslike.MustCompile(mapstr.M{ "suite.id": stdFields.Id, "suite.name": stdFields.Name, "monitor.id": fmt.Sprintf("%s-%s", stdFields.Id, journey.Id), @@ -99,7 +99,7 @@ func TestJourneyEnricher(t *testing.T) { }) } inlineValidator := func() validator.Validator { - return lookslike.MustCompile(common.MapStr{ + return lookslike.MustCompile(mapstr.M{ "monitor.id": stdFields.Id, "monitor.name": stdFields.Name, "monitor.type": stdFields.Type, @@ -118,7 +118,7 @@ func TestJourneyEnricher(t *testing.T) { } else { u, _ := url.Parse(url1) // journey end gets a summary - v = append(v, lookslike.MustCompile(common.MapStr{ + v = append(v, lookslike.MustCompile(mapstr.M{ "synthetics.type": "heartbeat/summary", "url": wrappers.URLFields(u), "monitor.duration.us": int64(journeyEnd.Timestamp().Sub(journeyStart.Timestamp()) / time.Microsecond), @@ -184,15 +184,15 @@ func TestEnrichConsoleSynthEvents(t *testing.T) { &journeyEnricher{}, &SynthEvent{ Type: "stderr", - Payload: common.MapStr{ + Payload: mapstr.M{ "message": "Error from synthetics", }, PackageVersion: "1.0.0", }, func(t *testing.T, e *beat.Event, je *journeyEnricher) { - v := lookslike.MustCompile(common.MapStr{ - "synthetics": common.MapStr{ - "payload": common.MapStr{ + v := lookslike.MustCompile(mapstr.M{ + "synthetics": mapstr.M{ + "payload": mapstr.M{ "message": "Error from synthetics", }, "type": "stderr", @@ -208,15 +208,15 @@ func TestEnrichConsoleSynthEvents(t *testing.T) { &journeyEnricher{}, &SynthEvent{ Type: "stdout", - Payload: common.MapStr{ + Payload: mapstr.M{ "message": "debug output", }, PackageVersion: "1.0.0", }, func(t *testing.T, e *beat.Event, je *journeyEnricher) { - v := lookslike.MustCompile(common.MapStr{ - "synthetics": common.MapStr{ - "payload": common.MapStr{ + v := lookslike.MustCompile(mapstr.M{ + "synthetics": mapstr.M{ + "payload": mapstr.M{ "message": "debug output", }, "type": "stdout", @@ -257,7 +257,7 @@ func TestEnrichSynthEvent(t *testing.T) { }, true, func(t *testing.T, e *beat.Event, je *journeyEnricher) { - v := lookslike.MustCompile(common.MapStr{ + v := lookslike.MustCompile(mapstr.M{ "summary": map[string]int{ "up": 0, "down": 1, @@ -277,7 +277,7 @@ func TestEnrichSynthEvent(t *testing.T) { }, true, func(t *testing.T, e *beat.Event, je *journeyEnricher) { - v := lookslike.MustCompile(common.MapStr{ + v := lookslike.MustCompile(mapstr.M{ "summary": map[string]int{ "up": 1, "down": 0, @@ -292,7 +292,7 @@ func TestEnrichSynthEvent(t *testing.T) { &SynthEvent{Type: "journey/end"}, false, func(t *testing.T, e *beat.Event, je *journeyEnricher) { - v := lookslike.MustCompile(common.MapStr{ + v := lookslike.MustCompile(mapstr.M{ "summary": map[string]int{ "up": 1, "down": 0, @@ -371,7 +371,7 @@ func TestNoSummaryOnAfterHook(t *testing.T) { TimestampEpochMicros: 1000, PackageVersion: "1.0.0", Journey: journey, - Payload: common.MapStr{}, + Payload: mapstr.M{}, } syntherr := &SynthError{ Message: "my-errmsg", @@ -383,7 +383,7 @@ func TestNoSummaryOnAfterHook(t *testing.T) { TimestampEpochMicros: 2000, PackageVersion: "1.0.0", Journey: journey, - Payload: common.MapStr{}, + Payload: mapstr.M{}, } cmdStatus := &SynthEvent{ Type: CmdStatus, @@ -421,7 +421,7 @@ func TestNoSummaryOnAfterHook(t *testing.T) { u, _ := url.Parse(badStepUrl) t.Run("summary in journey/end", func(t *testing.T) { - v := lookslike.MustCompile(common.MapStr{ + v := lookslike.MustCompile(mapstr.M{ "synthetics.type": "heartbeat/summary", "url": wrappers.URLFields(u), "monitor.duration.us": int64(journeyEnd.Timestamp().Sub(journeyStart.Timestamp()) / time.Microsecond), @@ -444,7 +444,7 @@ func TestSummaryWithoutJourneyEnd(t *testing.T) { TimestampEpochMicros: 1000, PackageVersion: "1.0.0", Journey: journey, - Payload: common.MapStr{}, + Payload: mapstr.M{}, } cmdStatus := &SynthEvent{ @@ -476,7 +476,7 @@ func TestSummaryWithoutJourneyEnd(t *testing.T) { u, _ := url.Parse(url1) - v := lookslike.MustCompile(common.MapStr{ + v := lookslike.MustCompile(mapstr.M{ "synthetics.type": "heartbeat/summary", "url": wrappers.URLFields(u), "monitor.duration.us": int64(cmdStatus.Timestamp().Sub(journeyStart.Timestamp()) / time.Microsecond), @@ -493,8 +493,8 @@ func TestSummaryWithoutJourneyEnd(t *testing.T) { func TestCreateSummaryEvent(t *testing.T) { baseTime := time.Now() - defaultLogValidator := func(stepCount int) func(t *testing.T, summary common.MapStr, observed []observer.LoggedEntry) { - return func(t *testing.T, summary common.MapStr, observed []observer.LoggedEntry) { + defaultLogValidator := func(stepCount int) func(t *testing.T, summary mapstr.M, observed []observer.LoggedEntry) { + return func(t *testing.T, summary mapstr.M, observed []observer.LoggedEntry) { require.Len(t, observed, 1) require.Equal(t, "Monitor finished", observed[0].Message) @@ -511,9 +511,9 @@ func TestCreateSummaryEvent(t *testing.T) { tests := []struct { name string je *journeyEnricher - expected common.MapStr + expected mapstr.M wantErr bool - logValidator func(t *testing.T, summary common.MapStr, observed []observer.LoggedEntry) + logValidator func(t *testing.T, summary mapstr.M, observed []observer.LoggedEntry) }{{ name: "completed without errors", je: &journeyEnricher{ @@ -523,9 +523,9 @@ func TestCreateSummaryEvent(t *testing.T) { journeyComplete: true, stepCount: 3, }, - expected: common.MapStr{ + expected: mapstr.M{ "monitor.duration.us": int64(10), - "summary": common.MapStr{ + "summary": mapstr.M{ "down": 0, "up": 1, }, @@ -542,9 +542,9 @@ func TestCreateSummaryEvent(t *testing.T) { errorCount: 1, firstError: fmt.Errorf("journey errored"), }, - expected: common.MapStr{ + expected: mapstr.M{ "monitor.duration.us": int64(10), - "summary": common.MapStr{ + "summary": mapstr.M{ "down": 1, "up": 0, }, @@ -560,9 +560,9 @@ func TestCreateSummaryEvent(t *testing.T) { stepCount: 0, journeyComplete: false, }, - expected: common.MapStr{ + expected: mapstr.M{ "monitor.duration.us": int64(10), - "summary": common.MapStr{ + "summary": mapstr.M{ "down": 0, "up": 1, }, @@ -577,13 +577,13 @@ func TestCreateSummaryEvent(t *testing.T) { journeyComplete: false, errorCount: 1, }, - expected: common.MapStr{ - "summary": common.MapStr{ + expected: mapstr.M{ + "summary": mapstr.M{ "down": 1, "up": 0, }, }, - logValidator: func(t *testing.T, summary common.MapStr, observed []observer.LoggedEntry) { + logValidator: func(t *testing.T, summary mapstr.M, observed []observer.LoggedEntry) { // We don't log run data without duration require.Len(t, observed, 1) require.Equal(t, "Error gathering information to log event", observed[0].Message) @@ -598,10 +598,10 @@ func TestCreateSummaryEvent(t *testing.T) { return zapcore.NewTee(in, core) }))) - monitorField := common.MapStr{"id": "my-monitor", "type": "browser"} + monitorField := mapstr.M{"id": "my-monitor", "type": "browser"} e := &beat.Event{ - Fields: common.MapStr{"monitor": monitorField}, + Fields: mapstr.M{"monitor": monitorField}, } err := tt.je.createSummary(e) if tt.wantErr { @@ -611,9 +611,9 @@ func TestCreateSummaryEvent(t *testing.T) { } //nolint:errcheck // There are no new changes to this line but // linter has been activated in the meantime. We'll cleanup separately. - common.MergeFields(tt.expected, common.MapStr{ + mapstr.MergeFields(tt.expected, mapstr.M{ "monitor": monitorField, - "url": common.MapStr{}, + "url": mapstr.M{}, "event.type": "heartbeat/summary", "synthetics.type": "heartbeat/summary", "synthetics.journey": Journey{}, diff --git a/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go b/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go index d91294e0f51..9bf6f38ca13 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/synthexec.go @@ -20,8 +20,8 @@ import ( "github.com/elastic/beats/v7/heartbeat/monitors/jobs" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const debugSelector = "synthexec" @@ -39,7 +39,7 @@ type FilterJourneyConfig struct { } // SuiteJob will run a single journey by name from the given suite. -func SuiteJob(ctx context.Context, suitePath string, params common.MapStr, filterJourneys FilterJourneyConfig, fields StdSuiteFields, extraArgs ...string) (jobs.Job, error) { +func SuiteJob(ctx context.Context, suitePath string, params mapstr.M, filterJourneys FilterJourneyConfig, fields StdSuiteFields, extraArgs ...string) (jobs.Job, error) { // Run the command in the given suitePath, use '.' as the first arg since the command runs // in the correct dir cmdFactory, err := suiteCommandFactory(suitePath, extraArgs...) @@ -71,7 +71,7 @@ func suiteCommandFactory(suitePath string, args ...string) (func() *exec.Cmd, er } // InlineJourneyJob returns a job that runs the given source as a single journey. -func InlineJourneyJob(ctx context.Context, script string, params common.MapStr, fields StdSuiteFields, extraArgs ...string) jobs.Job { +func InlineJourneyJob(ctx context.Context, script string, params mapstr.M, fields StdSuiteFields, extraArgs ...string) jobs.Job { newCmd := func() *exec.Cmd { return exec.Command("elastic-synthetics", append(extraArgs, "--inline")...) } @@ -82,7 +82,7 @@ func InlineJourneyJob(ctx context.Context, script string, params common.MapStr, // startCmdJob adapts commands into a heartbeat job. This is a little awkward given that the command's output is // available via a sequence of events in the multiplexer, while heartbeat jobs are tail recursive continuations. // Here, we adapt one to the other, where each recursive job pulls another item off the chan until none are left. -func startCmdJob(ctx context.Context, newCmd func() *exec.Cmd, stdinStr *string, params common.MapStr, filterJourneys FilterJourneyConfig, fields StdSuiteFields) jobs.Job { +func startCmdJob(ctx context.Context, newCmd func() *exec.Cmd, stdinStr *string, params mapstr.M, filterJourneys FilterJourneyConfig, fields StdSuiteFields) jobs.Job { return func(event *beat.Event) ([]jobs.Job, error) { mpx, err := runCmd(ctx, newCmd(), stdinStr, params, filterJourneys) if err != nil { @@ -113,7 +113,7 @@ func runCmd( ctx context.Context, cmd *exec.Cmd, stdinStr *string, - params common.MapStr, + params mapstr.M, filterJourneys FilterJourneyConfig, ) (mpx *ExecMultiplexer, err error) { mpx = NewExecMultiplexer() @@ -261,7 +261,7 @@ func lineToSynthEventFactory(typ string) func(bytes []byte, text string) (res *S return &SynthEvent{ Type: typ, TimestampEpochMicros: float64(time.Now().UnixMicro()), - Payload: common.MapStr{ + Payload: mapstr.M{ "message": text, }, }, nil diff --git a/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go b/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go index 8aacbaf890a..b5ba46f92a7 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/synthtypes.go @@ -11,8 +11,8 @@ import ( "time" "github.com/elastic/beats/v7/heartbeat/monitors/wrappers" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const JourneyStart = "journey/start" @@ -20,23 +20,23 @@ const JourneyEnd = "journey/end" const CmdStatus = "cmd/status" type SynthEvent struct { - Id string `json:"_id"` - Type string `json:"type"` - PackageVersion string `json:"package_version"` - Step *Step `json:"step"` - Journey *Journey `json:"journey"` - TimestampEpochMicros float64 `json:"@timestamp"` - Payload common.MapStr `json:"payload"` - Blob string `json:"blob"` - BlobMime string `json:"blob_mime"` - Error *SynthError `json:"error"` - URL string `json:"url"` - Status string `json:"status"` - RootFields common.MapStr `json:"root_fields"` + Id string `json:"_id"` + Type string `json:"type"` + PackageVersion string `json:"package_version"` + Step *Step `json:"step"` + Journey *Journey `json:"journey"` + TimestampEpochMicros float64 `json:"@timestamp"` + Payload mapstr.M `json:"payload"` + Blob string `json:"blob"` + BlobMime string `json:"blob_mime"` + Error *SynthError `json:"error"` + URL string `json:"url"` + Status string `json:"status"` + RootFields mapstr.M `json:"root_fields"` index int } -func (se SynthEvent) ToMap() (m common.MapStr) { +func (se SynthEvent) ToMap() (m mapstr.M) { // We don't add @timestamp to the map string since that's specially handled in beat.Event // Use the root fields as a base, and layer additional, stricter, fields on top if se.RootFields != nil { @@ -49,11 +49,11 @@ func (se SynthEvent) ToMap() (m common.MapStr) { } } } else { - m = common.MapStr{} + m = mapstr.M{} } - m.DeepUpdate(common.MapStr{ - "synthetics": common.MapStr{ + m.DeepUpdate(mapstr.M{ + "synthetics": mapstr.M{ "type": se.Type, "package_version": se.PackageVersion, "index": se.index, @@ -122,8 +122,8 @@ func (se *SynthError) String() string { return fmt.Sprintf("%s: %s\n", se.Name, se.Message) } -func (se *SynthError) toMap() common.MapStr { - return common.MapStr{ +func (se *SynthError) toMap() mapstr.M { + return mapstr.M{ "name": se.Name, "message": se.Message, "stack": se.Stack, @@ -138,11 +138,11 @@ func (tu *DurationUs) durationMicros() int64 { return tu.Micros } -func (tu *DurationUs) ToMap() common.MapStr { +func (tu *DurationUs) ToMap() mapstr.M { if tu == nil { return nil } - return common.MapStr{ + return mapstr.M{ "us": tu.durationMicros(), } } @@ -154,8 +154,8 @@ type Step struct { Duration DurationUs `json:"duration"` } -func (s *Step) ToMap() common.MapStr { - return common.MapStr{ +func (s *Step) ToMap() mapstr.M { + return mapstr.M{ "name": s.Name, "index": s.Index, "status": s.Status, @@ -169,15 +169,15 @@ type Journey struct { Tags []string `json:"tags"` } -func (j Journey) ToMap() common.MapStr { +func (j Journey) ToMap() mapstr.M { if len(j.Tags) > 0 { - return common.MapStr{ + return mapstr.M{ "name": j.Name, "id": j.Id, "tags": j.Tags, } } - return common.MapStr{ + return mapstr.M{ "name": j.Name, "id": j.Id, } diff --git a/x-pack/heartbeat/monitors/browser/synthexec/synthtypes_test.go b/x-pack/heartbeat/monitors/browser/synthexec/synthtypes_test.go index e2a0a396ef6..83020ee1c0c 100644 --- a/x-pack/heartbeat/monitors/browser/synthexec/synthtypes_test.go +++ b/x-pack/heartbeat/monitors/browser/synthexec/synthtypes_test.go @@ -11,7 +11,7 @@ import ( "time" "github.com/elastic/beats/v7/heartbeat/monitors/wrappers" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-lookslike" "github.com/elastic/go-lookslike/testslike" @@ -28,14 +28,14 @@ func TestToMap(t *testing.T) { type testCase struct { name string - source common.MapStr - expected common.MapStr + source mapstr.M + expected mapstr.M } testCases := []testCase{ { "root fields with URL", - common.MapStr{ + mapstr.M{ "type": "journey/start", "package_version": "1.2.3", "root_fields": map[string]interface{}{ @@ -46,8 +46,8 @@ func TestToMap(t *testing.T) { }, "url": testUrl.String(), }, - common.MapStr{ - "synthetics": common.MapStr{ + mapstr.M{ + "synthetics": mapstr.M{ "type": "journey/start", "package_version": "1.2.3", "nested": "v1", @@ -58,11 +58,11 @@ func TestToMap(t *testing.T) { }, { "root fields, step metadata", - common.MapStr{ + mapstr.M{ "type": "step/start", "package_version": "1.2.3", - "journey": common.MapStr{"name": "MyJourney", "id": "MyJourney", "tags": []string{"foo"}}, - "step": common.MapStr{"name": "MyStep", "status": "success", "index": 42, "duration": common.MapStr{"us": int64(1232131)}}, + "journey": mapstr.M{"name": "MyJourney", "id": "MyJourney", "tags": []string{"foo"}}, + "step": mapstr.M{"name": "MyStep", "status": "success", "index": 42, "duration": mapstr.M{"us": int64(1232131)}}, "root_fields": map[string]interface{}{ "synthetics": map[string]interface{}{ "nested": "v1", @@ -70,25 +70,25 @@ func TestToMap(t *testing.T) { "truly_at_root": "v2", }, }, - common.MapStr{ - "synthetics": common.MapStr{ + mapstr.M{ + "synthetics": mapstr.M{ "type": "step/start", "package_version": "1.2.3", "nested": "v1", - "journey": common.MapStr{"name": "MyJourney", "id": "MyJourney", "tags": []string{"foo"}}, - "step": common.MapStr{"name": "MyStep", "status": "success", "index": 42, "duration": common.MapStr{"us": int64(1232131)}}, + "journey": mapstr.M{"name": "MyJourney", "id": "MyJourney", "tags": []string{"foo"}}, + "step": mapstr.M{"name": "MyStep", "status": "success", "index": 42, "duration": mapstr.M{"us": int64(1232131)}}, }, "truly_at_root": "v2", }, }, { "weird error, and blob, no URL", - common.MapStr{ + mapstr.M{ "type": "someType", "package_version": "1.2.3", - "journey": common.MapStr{"name": "MyJourney", "id": "MyJourney"}, - "step": common.MapStr{"name": "MyStep", "index": 42, "status": "down", "duration": common.MapStr{"us": int64(1000)}}, - "error": common.MapStr{ + "journey": mapstr.M{"name": "MyJourney", "id": "MyJourney"}, + "step": mapstr.M{"name": "MyStep", "index": 42, "status": "down", "duration": mapstr.M{"us": int64(1000)}}, + "error": mapstr.M{ "name": "MyErrorName", "message": "MyErrorMessage", "stack": "MyErrorStack", @@ -96,13 +96,13 @@ func TestToMap(t *testing.T) { "blob": "ablob", "blob_mime": "application/weird", }, - common.MapStr{ - "synthetics": common.MapStr{ + mapstr.M{ + "synthetics": mapstr.M{ "type": "someType", "package_version": "1.2.3", - "journey": common.MapStr{"name": "MyJourney", "id": "MyJourney"}, - "step": common.MapStr{"name": "MyStep", "index": 42, "status": "down", "duration": common.MapStr{"us": int64(1000)}}, - "error": common.MapStr{ + "journey": mapstr.M{"name": "MyJourney", "id": "MyJourney"}, + "step": mapstr.M{"name": "MyStep", "index": 42, "status": "down", "duration": mapstr.M{"us": int64(1000)}}, + "error": mapstr.M{ "name": "MyErrorName", "message": "MyErrorMessage", "stack": "MyErrorStack", @@ -128,7 +128,7 @@ func TestToMap(t *testing.T) { // Index will always be zero in thee tests, so helpfully include it llvalidator := lookslike.Strict(lookslike.Compose( lookslike.MustCompile(tc.expected), - lookslike.MustCompile(common.MapStr{"synthetics": common.MapStr{"index": 0}}), + lookslike.MustCompile(mapstr.M{"synthetics": mapstr.M{"index": 0}}), )) // Test that even deep maps merge correctly diff --git a/x-pack/libbeat/autodiscover/providers/aws/ec2/ec2.go b/x-pack/libbeat/autodiscover/providers/aws/ec2/ec2.go index fb5268547f3..903e9e93778 100644 --- a/x-pack/libbeat/autodiscover/providers/aws/ec2/ec2.go +++ b/x-pack/libbeat/autodiscover/providers/aws/ec2/ec2.go @@ -8,9 +8,9 @@ import ( "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" awsauto "github.com/elastic/beats/v7/x-pack/libbeat/autodiscover/providers/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) type ec2Instance struct { @@ -18,13 +18,13 @@ type ec2Instance struct { } // toMap converts this ec2Instance into the form consumed as metadata in the autodiscovery process. -func (i *ec2Instance) toMap() common.MapStr { +func (i *ec2Instance) toMap() mapstr.M { architecture, err := i.ec2Instance.Architecture.MarshalValue() if err != nil { logp.Error(errors.Wrap(err, "MarshalValue failed for architecture: ")) } - m := common.MapStr{ + m := mapstr.M{ "image": i.toImage(), "vpc": i.toVpc(), "subnet": i.toSubnet(), @@ -47,57 +47,57 @@ func (i *ec2Instance) instanceID() string { return awsauto.SafeString(i.ec2Instance.InstanceId) } -func (i *ec2Instance) toImage() common.MapStr { - m := common.MapStr{} +func (i *ec2Instance) toImage() mapstr.M { + m := mapstr.M{} m["id"] = awsauto.SafeString(i.ec2Instance.ImageId) return m } -func (i *ec2Instance) toMonitoringState() common.MapStr { +func (i *ec2Instance) toMonitoringState() mapstr.M { monitoringState, err := i.ec2Instance.Monitoring.State.MarshalValue() if err != nil { logp.Error(errors.Wrap(err, "MarshalValue failed for monitoring state: ")) } - m := common.MapStr{} + m := mapstr.M{} m["state"] = monitoringState return m } -func (i *ec2Instance) toPrivate() common.MapStr { - m := common.MapStr{} +func (i *ec2Instance) toPrivate() mapstr.M { + m := mapstr.M{} m["ip"] = awsauto.SafeString(i.ec2Instance.PrivateIpAddress) m["dns_name"] = awsauto.SafeString(i.ec2Instance.PrivateDnsName) return m } -func (i *ec2Instance) toPublic() common.MapStr { - m := common.MapStr{} +func (i *ec2Instance) toPublic() mapstr.M { + m := mapstr.M{} m["ip"] = awsauto.SafeString(i.ec2Instance.PublicIpAddress) m["dns_name"] = awsauto.SafeString(i.ec2Instance.PublicDnsName) return m } -func (i *ec2Instance) toVpc() common.MapStr { - m := common.MapStr{} +func (i *ec2Instance) toVpc() mapstr.M { + m := mapstr.M{} m["id"] = awsauto.SafeString(i.ec2Instance.VpcId) return m } -func (i *ec2Instance) toSubnet() common.MapStr { - m := common.MapStr{} +func (i *ec2Instance) toSubnet() mapstr.M { + m := mapstr.M{} m["id"] = awsauto.SafeString(i.ec2Instance.SubnetId) return m } -func (i *ec2Instance) toKernel() common.MapStr { - m := common.MapStr{} +func (i *ec2Instance) toKernel() mapstr.M { + m := mapstr.M{} m["id"] = awsauto.SafeString(i.ec2Instance.KernelId) return m } -func (i *ec2Instance) toCloudMap() common.MapStr { - m := common.MapStr{} +func (i *ec2Instance) toCloudMap() mapstr.M { + m := mapstr.M{} availabilityZone := awsauto.SafeString(i.ec2Instance.Placement.AvailabilityZone) m["availability_zone"] = availabilityZone m["provider"] = "aws" @@ -105,7 +105,7 @@ func (i *ec2Instance) toCloudMap() common.MapStr { // The region is just an AZ with the last character removed m["region"] = availabilityZone[:len(availabilityZone)-1] - instance := common.MapStr{} + instance := mapstr.M{} instance["id"] = i.instanceID() m["instance"] = instance @@ -113,16 +113,16 @@ func (i *ec2Instance) toCloudMap() common.MapStr { if err != nil { logp.Error(errors.Wrap(err, "MarshalValue failed for instance type: ")) } - machine := common.MapStr{} + machine := mapstr.M{} machine["type"] = instanceType m["machine"] = machine return m } // stateMap converts the State part of the ec2 struct into a friendlier map with 'reason' and 'code' fields. -func (i *ec2Instance) stateMap() (stateMap common.MapStr) { +func (i *ec2Instance) stateMap() (stateMap mapstr.M) { state := i.ec2Instance.State - stateMap = common.MapStr{} + stateMap = mapstr.M{} nameString, err := state.Name.MarshalValue() if err != nil { logp.Error(errors.Wrap(err, "MarshalValue failed for instance state name: ")) diff --git a/x-pack/libbeat/autodiscover/providers/aws/ec2/provider.go b/x-pack/libbeat/autodiscover/providers/aws/ec2/provider.go index 3cab8c3ca28..efa4c7bc3c7 100644 --- a/x-pack/libbeat/autodiscover/providers/aws/ec2/provider.go +++ b/x-pack/libbeat/autodiscover/providers/aws/ec2/provider.go @@ -19,6 +19,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" awsauto "github.com/elastic/beats/v7/x-pack/libbeat/autodiscover/providers/aws" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -130,12 +131,12 @@ func (p *Provider) onWatcherStart(instanceID string, instance *ec2Instance) { "start": true, "provider": p.uuid, "id": instanceID, - "aws": common.MapStr{ + "aws": mapstr.M{ "ec2": instance.toMap(), }, "cloud": instance.toCloudMap(), - "meta": common.MapStr{ - "aws": common.MapStr{ + "meta": mapstr.M{ + "aws": mapstr.M{ "ec2": instance.toMap(), }, "cloud": instance.toCloudMap(), diff --git a/x-pack/libbeat/autodiscover/providers/aws/ec2/provider_test.go b/x-pack/libbeat/autodiscover/providers/aws/ec2/provider_test.go index b22321eeb23..e88edade289 100644 --- a/x-pack/libbeat/autodiscover/providers/aws/ec2/provider_test.go +++ b/x-pack/libbeat/autodiscover/providers/aws/ec2/provider_test.go @@ -13,12 +13,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" awsauto "github.com/elastic/beats/v7/x-pack/libbeat/autodiscover/providers/aws" "github.com/elastic/beats/v7/x-pack/libbeat/autodiscover/providers/aws/test" + "github.com/elastic/elastic-agent-libs/mapstr" ) func Test_internalBuilder(t *testing.T) { @@ -69,12 +69,12 @@ func Test_internalBuilder(t *testing.T) { "id": instance.instanceID(), "provider": uuid, "start": true, - "aws": common.MapStr{ + "aws": mapstr.M{ "ec2": instance.toMap(), }, "cloud": instance.toCloudMap(), - "meta": common.MapStr{ - "aws": common.MapStr{ + "meta": mapstr.M{ + "aws": mapstr.M{ "ec2": instance.toMap(), }, "cloud": instance.toCloudMap(), diff --git a/x-pack/libbeat/autodiscover/providers/aws/elb/lblistener.go b/x-pack/libbeat/autodiscover/providers/aws/elb/lblistener.go index b8cc2a9ab07..272f721cc63 100644 --- a/x-pack/libbeat/autodiscover/providers/aws/elb/lblistener.go +++ b/x-pack/libbeat/autodiscover/providers/aws/elb/lblistener.go @@ -7,8 +7,8 @@ package elb import ( "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2" - "github.com/elastic/beats/v7/libbeat/common" awsauto "github.com/elastic/beats/v7/x-pack/libbeat/autodiscover/providers/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) // lbListener is a tuple type representing an elasticloadbalancingv2.Listener and its associated elasticloadbalancingv2.LoadBalancer. @@ -18,9 +18,9 @@ type lbListener struct { } // toMap converts this lbListener into the form consumed as metadata in the autodiscovery process. -func (l *lbListener) toMap() common.MapStr { +func (l *lbListener) toMap() mapstr.M { // We fully spell out listener_arn to avoid confusion with the ARN for the whole ELB - m := common.MapStr{ + m := mapstr.M{ "listener_arn": l.listener.ListenerArn, "load_balancer_arn": awsauto.SafeString(l.lb.LoadBalancerArn), "host": awsauto.SafeString(l.lb.DNSName), @@ -43,8 +43,8 @@ func (l *lbListener) toMap() common.MapStr { return m } -func (l *lbListener) toCloudMap() common.MapStr { - m := common.MapStr{} +func (l *lbListener) toCloudMap() mapstr.M { + m := mapstr.M{} var azs []string for _, az := range l.lb.AvailabilityZones { @@ -76,9 +76,9 @@ func (l *lbListener) azStrings() []string { } // stateMap converts the State part of the lb struct into a friendlier map with 'reason' and 'code' fields. -func (l *lbListener) stateMap() (stateMap common.MapStr) { +func (l *lbListener) stateMap() (stateMap mapstr.M) { state := l.lb.State - stateMap = common.MapStr{} + stateMap = mapstr.M{} if state.Reason != nil { stateMap["reason"] = *state.Reason } diff --git a/x-pack/libbeat/autodiscover/providers/aws/elb/provider.go b/x-pack/libbeat/autodiscover/providers/aws/elb/provider.go index 8f39ea10ce4..05e40a6124f 100644 --- a/x-pack/libbeat/autodiscover/providers/aws/elb/provider.go +++ b/x-pack/libbeat/autodiscover/providers/aws/elb/provider.go @@ -19,6 +19,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" awsauto "github.com/elastic/beats/v7/x-pack/libbeat/autodiscover/providers/aws" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -138,12 +139,12 @@ func (p *Provider) onWatcherStart(arn string, lbl *lbListener) { "id": arn, "host": lblMap["host"], "port": lblMap["port"], - "aws": common.MapStr{ + "aws": mapstr.M{ "elb": lbl.toMap(), }, "cloud": lbl.toCloudMap(), - "meta": common.MapStr{ - "aws": common.MapStr{ + "meta": mapstr.M{ + "aws": mapstr.M{ "elb": lbl.toMap(), }, "cloud": lbl.toCloudMap(), diff --git a/x-pack/libbeat/autodiscover/providers/aws/elb/provider_test.go b/x-pack/libbeat/autodiscover/providers/aws/elb/provider_test.go index d6f9a918377..e9be9998d9a 100644 --- a/x-pack/libbeat/autodiscover/providers/aws/elb/provider_test.go +++ b/x-pack/libbeat/autodiscover/providers/aws/elb/provider_test.go @@ -14,11 +14,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/bus" "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" awsauto "github.com/elastic/beats/v7/x-pack/libbeat/autodiscover/providers/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) type testEventAccumulator struct { @@ -112,12 +112,12 @@ func Test_internalBuilder(t *testing.T) { "start": true, "host": *lbl.lb.DNSName, "port": *lbl.listener.Port, - "aws": common.MapStr{ + "aws": mapstr.M{ "elb": lbl.toMap(), }, "cloud": lbl.toCloudMap(), - "meta": common.MapStr{ - "aws": common.MapStr{ + "meta": mapstr.M{ + "aws": mapstr.M{ "elb": lbl.toMap(), }, "cloud": lbl.toCloudMap(), diff --git a/x-pack/libbeat/autodiscover/providers/nomad/nomad.go b/x-pack/libbeat/autodiscover/providers/nomad/nomad.go index 17284c49fd7..2b24b148d5b 100644 --- a/x-pack/libbeat/autodiscover/providers/nomad/nomad.go +++ b/x-pack/libbeat/autodiscover/providers/nomad/nomad.go @@ -20,6 +20,7 @@ import ( "github.com/elastic/beats/v7/libbeat/keystore" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/libbeat/common/nomad" + "github.com/elastic/elastic-agent-libs/mapstr" ) // NomadEventKey is the key under which custom metadata is going @@ -198,8 +199,8 @@ func (p *Provider) emit(obj *nomad.Resource, flag string) { flag: true, "host": nodeName, "nomad": allocMeta, - "meta": common.MapStr{ - "nomad": common.MapStrUnion(allocMeta, common.MapStr{ + "meta": mapstr.M{ + "nomad": mapstr.Union(allocMeta, mapstr.M{ "task": task, }), }, @@ -232,20 +233,20 @@ func (p *Provider) generateHints(event bus.Event) bus.Event { // Beat specific. e := bus.Event{} - var tags, container common.MapStr - var meta, tasks common.MapStr + var tags, container mapstr.M + var meta, tasks mapstr.M rawMeta, ok := event["meta"] if ok { - meta = rawMeta.(common.MapStr) + meta = rawMeta.(mapstr.M) if nomadMeta, ok := meta["nomad"]; ok { - meta = nomadMeta.(common.MapStr) + meta = nomadMeta.(mapstr.M) } // The builder base config can configure any of the field values of nomad if need be. e["nomad"] = meta if rawAnn, ok := meta["tags"]; ok { - tags = rawAnn.(common.MapStr) + tags = rawAnn.(mapstr.M) e["tags"] = tags } @@ -258,13 +259,13 @@ func (p *Provider) generateHints(event bus.Event) bus.Event { // Nomad supports different runtimes, so it will not always be _container_ info, but we could add // metadata about the runtime driver. if rawCont, ok := meta["container"]; ok { - container = rawCont.(common.MapStr) + container = rawCont.(mapstr.M) e["container"] = container } // for hints we look at the aggregated task's meta if rawTasks, ok := meta["task"]; ok { - tasks, ok = rawTasks.(common.MapStr) + tasks, ok = rawTasks.(mapstr.M) if !ok { p.logger.Warnf("Could not get meta for the given task: %s", rawTasks) return e diff --git a/x-pack/libbeat/autodiscover/providers/nomad/nomad_test.go b/x-pack/libbeat/autodiscover/providers/nomad/nomad_test.go index 32e94b68d8a..0d150318ce8 100644 --- a/x-pack/libbeat/autodiscover/providers/nomad/nomad_test.go +++ b/x-pack/libbeat/autodiscover/providers/nomad/nomad_test.go @@ -21,6 +21,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/tests/resources" "github.com/elastic/beats/v7/x-pack/libbeat/common/nomad" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestGenerateHints(t *testing.T) { @@ -34,21 +35,21 @@ func TestGenerateHints(t *testing.T) { result: bus.Event{}, }, // Scenarios being tested: - // - logs/multiline.pattern must be a nested common.MapStr under hints.logs + // - logs/multiline.pattern must be a nested mapstr.M under hints.logs // - metrics/module must be found in hints.metrics // - not.to.include must not be part of hints { event: bus.Event{ - "nomad": common.MapStr{ - "allocation": common.MapStr{ + "nomad": mapstr.M{ + "allocation": mapstr.M{ "id": "cf7db85d-c93c-873a-cb37-6d2ea071b0eb", }, "datacenter": []string{"europe-west4"}, }, - "meta": common.MapStr{ - "nomad": common.MapStr{ - "task": getNestedAnnotations(common.MapStr{ - "allocation": common.MapStr{ + "meta": mapstr.M{ + "nomad": mapstr.M{ + "task": getNestedAnnotations(mapstr.M{ + "allocation": mapstr.M{ "id": "f67d087a-fb67-48a8-b526-ac1316f4bc9a", }, "co.elastic.logs/multiline.pattern": "^test", @@ -60,21 +61,21 @@ func TestGenerateHints(t *testing.T) { }, }, result: bus.Event{ - "nomad": common.MapStr{ - "task": getNestedAnnotations(common.MapStr{ - "allocation": common.MapStr{ + "nomad": mapstr.M{ + "task": getNestedAnnotations(mapstr.M{ + "allocation": mapstr.M{ "id": "f67d087a-fb67-48a8-b526-ac1316f4bc9a", }, "not.to.include": "true", }), }, - "hints": common.MapStr{ - "logs": common.MapStr{ - "multiline": common.MapStr{ + "hints": mapstr.M{ + "logs": mapstr.M{ + "multiline": mapstr.M{ "pattern": "^test", }, }, - "metrics": common.MapStr{ + "metrics": mapstr.M{ "module": "prometheus", "period": "10s", }, @@ -174,33 +175,33 @@ func TestEmitEvent(t *testing.T) { "config": []*common.Config{}, "start": true, "host": host, - "nomad": common.MapStr{ - "allocation": common.MapStr{ + "nomad": mapstr.M{ + "allocation": mapstr.M{ "id": UUID.String(), "name": "job.task", "status": "running", }, "datacenter": []string{"europe-west4"}, - "job": common.MapStr{ + "job": mapstr.M{ "name": "my-job", "type": "service", }, "namespace": "default", "region": "global", }, - "meta": common.MapStr{ - "nomad": common.MapStr{ + "meta": mapstr.M{ + "nomad": mapstr.M{ "datacenter": []string{"europe-west4"}, - "job": common.MapStr{ + "job": mapstr.M{ "name": "my-job", "type": "service", }, - "task": common.MapStr{ + "task": mapstr.M{ "group-key": "group.value", "job-key": "job.value", "key1": "task-value", "name": "task1", - "service": common.MapStr{ + "service": mapstr.M{ "name": []string{"web", "nginx"}, "tags": []string{"tag-a", "tag-b", "tag-c", "tag-d"}, }, @@ -208,7 +209,7 @@ func TestEmitEvent(t *testing.T) { }, "namespace": "default", "region": "global", - "allocation": common.MapStr{ + "allocation": mapstr.M{ "id": UUID.String(), "name": "job.task", "status": nomad.AllocClientStatusRunning, @@ -339,8 +340,8 @@ func TestEmitEvent(t *testing.T) { assert.Equal(t, httpmock.GetCallCountInfo()["GET http://127.0.0.1/v1/node/5456bd7a"], 1) } -func getNestedAnnotations(in common.MapStr) common.MapStr { - out := common.MapStr{} +func getNestedAnnotations(in mapstr.M) mapstr.M { + out := mapstr.M{} for k, v := range in { out.Put(k, v) diff --git a/x-pack/libbeat/common/cloudfoundry/config_test.go b/x-pack/libbeat/common/cloudfoundry/config_test.go index 1e951dc24aa..7618a882dbc 100644 --- a/x-pack/libbeat/common/cloudfoundry/config_test.go +++ b/x-pack/libbeat/common/cloudfoundry/config_test.go @@ -12,9 +12,8 @@ import ( "github.com/stretchr/testify/assert" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-ucfg" - - "github.com/elastic/beats/v7/libbeat/common" ) func TestValidation(t *testing.T) { @@ -22,35 +21,35 @@ func TestValidation(t *testing.T) { assert.Error(t, ucfg.New().Unpack(&noIdOrSecret)) var noId Config - assert.Error(t, ucfg.MustNewFrom(common.MapStr{ + assert.Error(t, ucfg.MustNewFrom(mapstr.M{ "api_address": "https://api.dev.cfdev.sh", "client_secret": "client_secret", "shard_id": "beats-test-1", }).Unpack(&noId)) var noSecret Config - assert.Error(t, ucfg.MustNewFrom(common.MapStr{ + assert.Error(t, ucfg.MustNewFrom(mapstr.M{ "api_address": "https://api.dev.cfdev.sh", "client_id": "client_id", "shard_id": "beats-test-1", }).Unpack(&noSecret)) var noAPI Config - assert.Error(t, ucfg.MustNewFrom(common.MapStr{ + assert.Error(t, ucfg.MustNewFrom(mapstr.M{ "client_id": "client_id", "client_secret": "client_secret", "shard_id": "beats-test-1", }).Unpack(&noAPI)) var noShardID Config - assert.Error(t, ucfg.MustNewFrom(common.MapStr{ + assert.Error(t, ucfg.MustNewFrom(mapstr.M{ "api_address": "https://api.dev.cfdev.sh", "client_id": "client_id", "client_secret": "client_secret", }).Unpack(&noShardID)) var valid Config - assert.NoError(t, ucfg.MustNewFrom(common.MapStr{ + assert.NoError(t, ucfg.MustNewFrom(mapstr.M{ "api_address": "https://api.dev.cfdev.sh", "client_id": "client_id", "client_secret": "client_secret", @@ -60,7 +59,7 @@ func TestValidation(t *testing.T) { func TestInitDefaults(t *testing.T) { var cfCfg Config - assert.NoError(t, ucfg.MustNewFrom(common.MapStr{ + assert.NoError(t, ucfg.MustNewFrom(mapstr.M{ "api_address": "https://api.dev.cfdev.sh", "client_id": "client_id", "client_secret": "client_secret", diff --git a/x-pack/libbeat/common/cloudfoundry/events.go b/x-pack/libbeat/common/cloudfoundry/events.go index 4d1a67e2b1b..a1af74929f9 100644 --- a/x-pack/libbeat/common/cloudfoundry/events.go +++ b/x-pack/libbeat/common/cloudfoundry/events.go @@ -12,6 +12,7 @@ import ( "time" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/cloudfoundry/sonde-go/events" ) @@ -89,7 +90,7 @@ type Event interface { Index() string IP() string Tags() map[string]string - ToFields() common.MapStr + ToFields() mapstr.M } // EventWithAppID is the interface all events implement that provide an application ID for the event. @@ -155,17 +156,17 @@ func (e *EventHttpAccess) StatusCode() int32 { return e.statusCode } func (e *EventHttpAccess) ContentLength() int64 { return e.contentLength } func (e *EventHttpAccess) InstanceIndex() int32 { return e.instanceIndex } func (e *EventHttpAccess) Forwarded() []string { return e.forwarded } -func (e *EventHttpAccess) ToFields() common.MapStr { +func (e *EventHttpAccess) ToFields() mapstr.M { fields := baseMapWithApp(e) - fields.DeepUpdate(common.MapStr{ - "http": common.MapStr{ - "response": common.MapStr{ + fields.DeepUpdate(mapstr.M{ + "http": mapstr.M{ + "response": mapstr.M{ "status_code": e.StatusCode(), "method": e.Method(), "bytes": e.ContentLength(), }, }, - "user_agent": common.MapStr{ + "user_agent": mapstr.M{ "original": e.UserAgent(), }, "url": urlMap(e.URI()), @@ -197,12 +198,12 @@ func (e *EventLog) Message() string { return e.message } func (e *EventLog) MessageType() EventLogMessageType { return e.messageType } func (e *EventLog) SourceType() string { return e.sourceType } func (e *EventLog) SourceID() string { return e.sourceID } -func (e *EventLog) ToFields() common.MapStr { +func (e *EventLog) ToFields() mapstr.M { fields := baseMapWithApp(e) - fields.DeepUpdate(common.MapStr{ - "cloudfoundry": common.MapStr{ - e.String(): common.MapStr{ - "source": common.MapStr{ + fields.DeepUpdate(mapstr.M{ + "cloudfoundry": mapstr.M{ + e.String(): mapstr.M{ + "source": mapstr.M{ "instance": e.SourceID(), "type": e.SourceType(), }, @@ -235,11 +236,11 @@ func (e *EventCounter) Tags() map[string]string { return e.tags } func (e *EventCounter) Name() string { return e.name } func (e *EventCounter) Delta() uint64 { return e.delta } func (e *EventCounter) Total() uint64 { return e.total } -func (e *EventCounter) ToFields() common.MapStr { +func (e *EventCounter) ToFields() mapstr.M { fields := baseMap(e) - fields.DeepUpdate(common.MapStr{ - "cloudfoundry": common.MapStr{ - e.String(): common.MapStr{ + fields.DeepUpdate(mapstr.M{ + "cloudfoundry": mapstr.M{ + e.String(): mapstr.M{ "name": e.Name(), "delta": e.Delta(), "total": e.Total(), @@ -270,11 +271,11 @@ func (e *EventValueMetric) Tags() map[string]string { return e.tags } func (e *EventValueMetric) Name() string { return e.name } func (e *EventValueMetric) Value() float64 { return e.value } func (e *EventValueMetric) Unit() string { return e.unit } -func (e *EventValueMetric) ToFields() common.MapStr { +func (e *EventValueMetric) ToFields() mapstr.M { fields := baseMap(e) - fields.DeepUpdate(common.MapStr{ - "cloudfoundry": common.MapStr{ - e.String(): common.MapStr{ + fields.DeepUpdate(mapstr.M{ + "cloudfoundry": mapstr.M{ + e.String(): mapstr.M{ "name": e.Name(), "unit": e.Unit(), "value": e.Value(), @@ -312,11 +313,11 @@ func (e *EventContainerMetric) MemoryBytes() uint64 { return e.memoryBytes func (e *EventContainerMetric) DiskBytes() uint64 { return e.diskBytes } func (e *EventContainerMetric) MemoryBytesQuota() uint64 { return e.memoryBytesQuota } func (e *EventContainerMetric) DiskBytesQuota() uint64 { return e.diskBytesQuota } -func (e *EventContainerMetric) ToFields() common.MapStr { +func (e *EventContainerMetric) ToFields() mapstr.M { fields := baseMapWithApp(e) - fields.DeepUpdate(common.MapStr{ - "cloudfoundry": common.MapStr{ - e.String(): common.MapStr{ + fields.DeepUpdate(mapstr.M{ + "cloudfoundry": mapstr.M{ + e.String(): mapstr.M{ "instance_index": e.InstanceIndex(), "cpu.pct": e.CPUPercentage(), "memory.bytes": e.MemoryBytes(), @@ -350,11 +351,11 @@ func (e *EventError) Tags() map[string]string { return e.tags } func (e *EventError) Message() string { return e.message } func (e *EventError) Code() int32 { return e.code } func (e *EventError) Source() string { return e.source } -func (e *EventError) ToFields() common.MapStr { +func (e *EventError) ToFields() mapstr.M { fields := baseMap(e) - fields.DeepUpdate(common.MapStr{ - "cloudfoundry": common.MapStr{ - e.String(): common.MapStr{ + fields.DeepUpdate(mapstr.M{ + "cloudfoundry": mapstr.M{ + e.String(): mapstr.M{ "source": e.Source(), }, }, @@ -479,8 +480,8 @@ func EnvelopeToEvent(env *events.Envelope) Event { return nil } -func envelopMap(evt Event) common.MapStr { - return common.MapStr{ +func envelopMap(evt Event) mapstr.M { + return mapstr.M{ "origin": evt.Origin(), "deployment": evt.Deployment(), "ip": evt.IP(), @@ -489,16 +490,16 @@ func envelopMap(evt Event) common.MapStr { } } -func baseMap(evt Event) common.MapStr { +func baseMap(evt Event) mapstr.M { tags, meta := tagsToMeta(evt.Tags()) - cf := common.MapStr{ + cf := mapstr.M{ "type": evt.String(), "envelope": envelopMap(evt), } if len(tags) > 0 { cf["tags"] = tags } - result := common.MapStr{ + result := mapstr.M{ "cloudfoundry": cf, } if len(meta) > 0 { @@ -507,9 +508,9 @@ func baseMap(evt Event) common.MapStr { return result } -func tagsToMeta(eventTags map[string]string) (tags common.MapStr, meta common.MapStr) { - tags = common.MapStr{} - meta = common.MapStr{} +func tagsToMeta(eventTags map[string]string) (tags mapstr.M, meta mapstr.M) { + tags = mapstr.M{} + meta = mapstr.M{} for name, value := range eventTags { switch name { case "app_id": @@ -531,7 +532,7 @@ func tagsToMeta(eventTags map[string]string) (tags common.MapStr, meta common.Ma return tags, meta } -func baseMapWithApp(evt EventWithAppID) common.MapStr { +func baseMapWithApp(evt EventWithAppID) mapstr.M { base := baseMap(evt) appID := evt.AppGuid() if appID != "" { @@ -540,14 +541,14 @@ func baseMapWithApp(evt EventWithAppID) common.MapStr { return base } -func urlMap(uri string) common.MapStr { +func urlMap(uri string) mapstr.M { u, err := url.Parse(uri) if err != nil { - return common.MapStr{ + return mapstr.M{ "original": uri, } } - return common.MapStr{ + return mapstr.M{ "original": uri, "scheme": u.Scheme, "port": u.Port(), diff --git a/x-pack/libbeat/common/cloudfoundry/events_test.go b/x-pack/libbeat/common/cloudfoundry/events_test.go index 921d3fde191..9e783644d14 100644 --- a/x-pack/libbeat/common/cloudfoundry/events_test.go +++ b/x-pack/libbeat/common/cloudfoundry/events_test.go @@ -11,10 +11,10 @@ import ( "testing" "time" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/cloudfoundry/sonde-go/events" "github.com/stretchr/testify/assert" + + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEventTypeHttpAccess(t *testing.T) { @@ -74,34 +74,34 @@ func TestEventTypeHttpAccess(t *testing.T) { assert.Equal(t, int32(1), evt.InstanceIndex()) assert.Equal(t, []string{"forwarded"}, evt.Forwarded()) - assert.Equal(t, common.MapStr{ - "cloudfoundry": common.MapStr{ + assert.Equal(t, mapstr.M{ + "cloudfoundry": mapstr.M{ "type": "access", - "envelope": common.MapStr{ + "envelope": mapstr.M{ "origin": "origin", "deployment": "deployment", "ip": "ip", "job": "job", "index": "index", }, - "app": common.MapStr{ + "app": mapstr.M{ "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", }, - "tags": common.MapStr{ + "tags": mapstr.M{ "tag": "value", }, }, - "http": common.MapStr{ - "response": common.MapStr{ + "http": mapstr.M{ + "response": mapstr.M{ "status_code": int32(200), "method": "GET", "bytes": int64(128), }, }, - "user_agent": common.MapStr{ + "user_agent": mapstr.M{ "original": "user_agent", }, - "url": common.MapStr{ + "url": mapstr.M{ "original": "https://uri.full-domain.com:8443/subpath", "scheme": "https", "port": "8443", @@ -145,26 +145,26 @@ func TestEventTypeLog(t *testing.T) { assert.Equal(t, "source_type", evt.SourceType()) assert.Equal(t, "source_instance", evt.SourceID()) - assert.Equal(t, common.MapStr{ - "cloudfoundry": common.MapStr{ + assert.Equal(t, mapstr.M{ + "cloudfoundry": mapstr.M{ "type": "log", - "log": common.MapStr{ - "source": common.MapStr{ + "log": mapstr.M{ + "source": mapstr.M{ "instance": evt.SourceID(), "type": evt.SourceType(), }, }, - "envelope": common.MapStr{ + "envelope": mapstr.M{ "origin": "origin", "deployment": "deployment", "ip": "ip", "job": "job", "index": "index", }, - "app": common.MapStr{ + "app": mapstr.M{ "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", }, - "tags": common.MapStr{ + "tags": mapstr.M{ "tag": "value", }, }, @@ -199,22 +199,22 @@ func TestEventCounter(t *testing.T) { assert.Equal(t, uint64(10), evt.Delta()) assert.Equal(t, uint64(999), evt.Total()) - assert.Equal(t, common.MapStr{ - "cloudfoundry": common.MapStr{ + assert.Equal(t, mapstr.M{ + "cloudfoundry": mapstr.M{ "type": "counter", - "counter": common.MapStr{ + "counter": mapstr.M{ "name": "name", "delta": uint64(10), "total": uint64(999), }, - "envelope": common.MapStr{ + "envelope": mapstr.M{ "origin": "origin", "deployment": "deployment", "ip": "ip", "job": "job", "index": "index", }, - "tags": common.MapStr{ + "tags": mapstr.M{ "tag": "value", }, }, @@ -247,22 +247,22 @@ func TestEventValueMetric(t *testing.T) { assert.Equal(t, 10.1, evt.Value()) assert.Equal(t, "unit", evt.Unit()) - assert.Equal(t, common.MapStr{ - "cloudfoundry": common.MapStr{ + assert.Equal(t, mapstr.M{ + "cloudfoundry": mapstr.M{ "type": "value", - "value": common.MapStr{ + "value": mapstr.M{ "name": "name", "value": 10.1, "unit": "unit", }, - "envelope": common.MapStr{ + "envelope": mapstr.M{ "origin": "origin", "deployment": "deployment", "ip": "ip", "job": "job", "index": "index", }, - "tags": common.MapStr{ + "tags": mapstr.M{ "tag": "value", }, }, @@ -307,10 +307,10 @@ func TestEventContainerMetric(t *testing.T) { assert.Equal(t, uint64(2048), evt.MemoryBytesQuota()) assert.Equal(t, uint64(4096), evt.DiskBytesQuota()) - assert.Equal(t, common.MapStr{ - "cloudfoundry": common.MapStr{ + assert.Equal(t, mapstr.M{ + "cloudfoundry": mapstr.M{ "type": "container", - "container": common.MapStr{ + "container": mapstr.M{ "instance_index": int32(1), "cpu.pct": 0.2, "memory.bytes": uint64(1024), @@ -318,17 +318,17 @@ func TestEventContainerMetric(t *testing.T) { "disk.bytes": uint64(2048), "disk.quota.bytes": uint64(4096), }, - "envelope": common.MapStr{ + "envelope": mapstr.M{ "origin": "origin", "deployment": "deployment", "ip": "ip", "job": "job", "index": "index", }, - "app": common.MapStr{ + "app": mapstr.M{ "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", }, - "tags": common.MapStr{ + "tags": mapstr.M{ "tag": "value", }, }, @@ -361,20 +361,20 @@ func TestEventError(t *testing.T) { assert.Equal(t, int32(100), evt.Code()) assert.Equal(t, "source", evt.Source()) - assert.Equal(t, common.MapStr{ - "cloudfoundry": common.MapStr{ + assert.Equal(t, mapstr.M{ + "cloudfoundry": mapstr.M{ "type": "error", - "error": common.MapStr{ + "error": mapstr.M{ "source": "source", }, - "envelope": common.MapStr{ + "envelope": mapstr.M{ "origin": "origin", "deployment": "deployment", "ip": "ip", "job": "job", "index": "index", }, - "tags": common.MapStr{ + "tags": mapstr.M{ "tag": "value", }, }, @@ -427,35 +427,35 @@ func TestEventTagsWithMetadata(t *testing.T) { assert.Equal(t, "source_type", evt.SourceType()) assert.Equal(t, "source_instance", evt.SourceID()) - assert.Equal(t, common.MapStr{ - "cloudfoundry": common.MapStr{ + assert.Equal(t, mapstr.M{ + "cloudfoundry": mapstr.M{ "type": "log", - "log": common.MapStr{ - "source": common.MapStr{ + "log": mapstr.M{ + "source": mapstr.M{ "instance": evt.SourceID(), "type": evt.SourceType(), }, }, - "envelope": common.MapStr{ + "envelope": mapstr.M{ "origin": "origin", "deployment": "deployment", "ip": "ip", "job": "job", "index": "index", }, - "app": common.MapStr{ + "app": mapstr.M{ "id": "f47ac10b-58cc-4372-a567-0e02b2c3d479", "name": "some-app", }, - "space": common.MapStr{ + "space": mapstr.M{ "id": "e1114e92-155c-11eb-ada9-27b81025a657", "name": "some-space", }, - "org": common.MapStr{ + "org": mapstr.M{ "id": "baeef1ba-155c-11eb-a1af-8f14964c35d2", "name": "some-org", }, - "tags": common.MapStr{ + "tags": mapstr.M{ "custom_tag": "foo", }, }, diff --git a/x-pack/libbeat/common/nomad/metadata.go b/x-pack/libbeat/common/nomad/metadata.go index 51e2fe93ebc..9149806845a 100644 --- a/x-pack/libbeat/common/nomad/metadata.go +++ b/x-pack/libbeat/common/nomad/metadata.go @@ -8,7 +8,8 @@ import ( "regexp" "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/libbeat/common/safemapstr" + "github.com/elastic/elastic-agent-libs/mapstr" + "github.com/elastic/elastic-agent-libs/safemapstr" ) var ( @@ -18,14 +19,14 @@ var ( // MetaGenerator builds metadata objects for allocations type MetaGenerator interface { // ResourceMetadata generates metadata for the given allocation - ResourceMetadata(obj Resource) common.MapStr + ResourceMetadata(obj Resource) mapstr.M // AllocationNodeName returns the name of the node where the Task is allocated AllocationNodeName(id string) (string, error) // GroupMeta returns per-task metadata merged with the group metadata, task // metadata takes will overwrite metadata from the group with the same key - GroupMeta(job *Job) []common.MapStr + GroupMeta(job *Job) []mapstr.M } // MetaGeneratorConfig settings @@ -59,15 +60,15 @@ func NewMetaGeneratorFromConfig(cfg *MetaGeneratorConfig) MetaGenerator { } // ResourceMetadata generates metadata for the given Nomad allocation* -func (g *metaGenerator) ResourceMetadata(obj Resource) common.MapStr { +func (g *metaGenerator) ResourceMetadata(obj Resource) mapstr.M { // default labels that we expose / filter with `IncludeLabels` - meta := common.MapStr{ - "allocation": common.MapStr{ + meta := mapstr.M{ + "allocation": mapstr.M{ "name": obj.Name, "id": obj.ID, "status": obj.ClientStatus, }, - "job": common.MapStr{ + "job": mapstr.M{ "name": *obj.Job.Name, "type": *obj.Job.Type, }, @@ -81,8 +82,8 @@ func (g *metaGenerator) ResourceMetadata(obj Resource) common.MapStr { // Returns an array of per-task metadata aggregating the group metadata into the // task metadata -func (g *metaGenerator) GroupMeta(job *Job) []common.MapStr { - var tasksMeta []common.MapStr +func (g *metaGenerator) GroupMeta(job *Job) []mapstr.M { + var tasksMeta []mapstr.M for _, group := range job.TaskGroups { meta := make(map[string]string, len(job.Meta)) @@ -103,7 +104,7 @@ func (g *metaGenerator) GroupMeta(job *Job) []common.MapStr { } for idx, task := range tasksMeta { - labelMap := common.MapStr{} + labelMap := mapstr.M{} if len(g.IncludeLabels) == 0 { for k, v := range task { @@ -130,8 +131,8 @@ func (g *metaGenerator) GroupMeta(job *Job) []common.MapStr { } // Returns per-task metadata -func (g *metaGenerator) tasksMeta(group *TaskGroup) []common.MapStr { - var tasks []common.MapStr +func (g *metaGenerator) tasksMeta(group *TaskGroup) []mapstr.M { + var tasks []mapstr.M for _, task := range group.Tasks { var svcNames, svcTags, svcCanaryTags []string @@ -141,7 +142,7 @@ func (g *metaGenerator) tasksMeta(group *TaskGroup) []common.MapStr { svcCanaryTags = append(svcCanaryTags, service.CanaryTags...) } - svcMeta := common.MapStr{} + svcMeta := mapstr.M{} if len(svcNames) > 0 { svcMeta["name"] = svcNames } @@ -162,7 +163,7 @@ func (g *metaGenerator) tasksMeta(group *TaskGroup) []common.MapStr { joinMeta[k] = v } - meta := common.MapStr{ + meta := mapstr.M{ "name": task.Name, } if len(svcMeta) > 0 { @@ -179,8 +180,8 @@ func (g *metaGenerator) tasksMeta(group *TaskGroup) []common.MapStr { return tasks } -func generateMapSubset(input common.MapStr, keys []string, dedot bool) common.MapStr { - output := common.MapStr{} +func generateMapSubset(input mapstr.M, keys []string, dedot bool) mapstr.M { + output := mapstr.M{} if input == nil { return output } diff --git a/x-pack/libbeat/common/nomad/metadata_test.go b/x-pack/libbeat/common/nomad/metadata_test.go index 5bd6aa0d973..3a944862be7 100644 --- a/x-pack/libbeat/common/nomad/metadata_test.go +++ b/x-pack/libbeat/common/nomad/metadata_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func newJob(jobID string) *Job { @@ -83,7 +84,7 @@ func TestAllocationMetadata(t *testing.T) { meta := metaGen.ResourceMetadata(alloc) tasks := metaGen.GroupMeta(alloc.Job) - assert.EqualValues(t, common.MapStr{ + assert.EqualValues(t, mapstr.M{ "name": "my-job", "type": "service", }, meta["job"]) @@ -166,12 +167,12 @@ func TestCronJob(t *testing.T) { meta := metaGen.ResourceMetadata(alloc) tasks := metaGen.GroupMeta(alloc.Job) - assert.EqualValues(t, common.MapStr{ + assert.EqualValues(t, mapstr.M{ "name": alloc.Name, "id": allocID, "status": "", }, meta["allocation"]) - assert.EqualValues(t, common.MapStr{ + assert.EqualValues(t, mapstr.M{ "name": *cron.Name, "type": JobTypeBatch, }, meta["job"]) diff --git a/x-pack/libbeat/management/blacklist.go b/x-pack/libbeat/management/blacklist.go index 6ea5088a962..28747020ffb 100644 --- a/x-pack/libbeat/management/blacklist.go +++ b/x-pack/libbeat/management/blacklist.go @@ -13,6 +13,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/match" + "github.com/elastic/elastic-agent-libs/mapstr" ) // ConfigBlacklist takes a ConfigBlocks object and filter it based on the given @@ -35,7 +36,7 @@ func (f *ConfigBlacklistSettings) Unpack(from interface{}) error { } f.Patterns = map[string]string{} - for k, v := range common.MapStr(m).Flatten() { + for k, v := range mapstr.M(m).Flatten() { f.Patterns[k] = fmt.Sprintf("%s", v) } diff --git a/x-pack/libbeat/management/manager.go b/x-pack/libbeat/management/manager.go index 2cb3cd4b475..2ef2a351ad3 100644 --- a/x-pack/libbeat/management/manager.go +++ b/x-pack/libbeat/management/manager.go @@ -17,6 +17,7 @@ import ( "github.com/elastic/elastic-agent-client/v7/pkg/client" "github.com/elastic/elastic-agent-client/v7/pkg/proto" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" @@ -181,7 +182,7 @@ func (cm *Manager) updateStatusWithError(err error) { func (cm *Manager) OnConfig(s string) { cm.UpdateStatus(lbmanagement.Configuring, "Updating configuration") - var configMap common.MapStr + var configMap mapstr.M uconfig, err := common.NewConfigFrom(s) if err != nil { err = errors.Wrap(err, "config blocks unsuccessfully generated") @@ -327,7 +328,7 @@ func (cm *Manager) reload(t string, blocks []*ConfigBlock) error { return nil } -func (cm *Manager) toConfigBlocks(cfg common.MapStr) (ConfigBlocks, error) { +func (cm *Manager) toConfigBlocks(cfg mapstr.M) (ConfigBlocks, error) { blocks := map[string][]*ConfigBlock{} // Extract all registered values beat can respond to diff --git a/x-pack/libbeat/management/manager_test.go b/x-pack/libbeat/management/manager_test.go index 113c19efe5c..3ac8d401caa 100644 --- a/x-pack/libbeat/management/manager_test.go +++ b/x-pack/libbeat/management/manager_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/elastic-agent-client/v7/pkg/proto" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/reload" @@ -29,7 +30,7 @@ output: hosts: - localhost:9200` - var cfg common.MapStr + var cfg mapstr.M uconfig, err := common.NewConfigFrom(input) if err != nil { t.Fatalf("Config blocks unsuccessfully generated: %+v", err) diff --git a/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata.go b/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata.go index e4d459d2a14..58edfcf1986 100644 --- a/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata.go +++ b/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata.go @@ -16,6 +16,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -81,16 +82,16 @@ func (d *addCloudFoundryMetadata) Run(event *beat.Event) (*beat.Event, error) { d.log.Debugf("failed to get application info for GUID(%s): %v", val, err) return event, nil } - event.Fields.DeepUpdate(common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{ + event.Fields.DeepUpdate(mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{ "name": app.Name, }, - "space": common.MapStr{ + "space": mapstr.M{ "id": app.SpaceGuid, "name": app.SpaceName, }, - "org": common.MapStr{ + "org": mapstr.M{ "id": app.OrgGuid, "name": app.OrgName, }, diff --git a/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata_test.go b/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata_test.go index a5dfa7e9558..5ca0b6e8abb 100644 --- a/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata_test.go +++ b/x-pack/libbeat/processors/add_cloudfoundry_metadata/add_cloudfoundry_metadata_test.go @@ -15,9 +15,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/elastic/beats/v7/libbeat/beat" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestNoClient(t *testing.T) { @@ -35,9 +35,9 @@ func TestNoCFApp(t *testing.T) { } evt := beat.Event{ - Fields: common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{}, + Fields: mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{}, }, }, } @@ -52,9 +52,9 @@ func TestCFAppIdInvalid(t *testing.T) { } evt := beat.Event{ - Fields: common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{ + Fields: mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{ "id": 1, }, }, @@ -72,9 +72,9 @@ func TestCFAppNotFound(t *testing.T) { } evt := beat.Event{ - Fields: common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{ + Fields: mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{ "id": mustCreateFakeGuid(), }, }, @@ -101,17 +101,17 @@ func TestCFAppMetadataAlreadyPresent(t *testing.T) { } evt := beat.Event{ - Fields: common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{ + Fields: mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{ "id": guid, "name": "Other App Name", }, - "space": common.MapStr{ + "space": mapstr.M{ "id": app.SpaceGuid, "name": app.SpaceName, }, - "org": common.MapStr{ + "org": mapstr.M{ "id": app.OrgGuid, "name": app.OrgName, }, @@ -119,17 +119,17 @@ func TestCFAppMetadataAlreadyPresent(t *testing.T) { }, } expected := beat.Event{ - Fields: common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{ + Fields: mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{ "id": guid, "name": "Other App Name", }, - "space": common.MapStr{ + "space": mapstr.M{ "id": app.SpaceGuid, "name": app.SpaceName, }, - "org": common.MapStr{ + "org": mapstr.M{ "id": app.OrgGuid, "name": app.OrgName, }, @@ -157,26 +157,26 @@ func TestCFAppUpdated(t *testing.T) { } evt := beat.Event{ - Fields: common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{ + Fields: mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{ "id": guid, }, }, }, } expected := beat.Event{ - Fields: common.MapStr{ - "cloudfoundry": common.MapStr{ - "app": common.MapStr{ + Fields: mapstr.M{ + "cloudfoundry": mapstr.M{ + "app": mapstr.M{ "id": guid, "name": app.Name, }, - "space": common.MapStr{ + "space": mapstr.M{ "id": app.SpaceGuid, "name": app.SpaceName, }, - "org": common.MapStr{ + "org": mapstr.M{ "id": app.OrgGuid, "name": app.OrgName, }, diff --git a/x-pack/libbeat/processors/add_nomad_metadata/cache.go b/x-pack/libbeat/processors/add_nomad_metadata/cache.go index eb92f839fc0..b6ebb318fbe 100644 --- a/x-pack/libbeat/processors/add_nomad_metadata/cache.go +++ b/x-pack/libbeat/processors/add_nomad_metadata/cache.go @@ -8,27 +8,27 @@ import ( "sync" "time" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) type cache struct { sync.Mutex timeout time.Duration deleted map[string]time.Time // key -> when should this obj be deleted - metadata map[string]common.MapStr + metadata map[string]mapstr.M } func newCache(cleanupTimeout time.Duration) *cache { c := &cache{ timeout: cleanupTimeout, deleted: make(map[string]time.Time), - metadata: make(map[string]common.MapStr), + metadata: make(map[string]mapstr.M), } go c.cleanup() return c } -func (c *cache) get(key string) common.MapStr { +func (c *cache) get(key string) mapstr.M { c.Lock() defer c.Unlock() // add lifecycle if key was queried @@ -44,7 +44,7 @@ func (c *cache) delete(key string) { c.deleted[key] = time.Now().Add(c.timeout) } -func (c *cache) set(key string, data common.MapStr) { +func (c *cache) set(key string, data mapstr.M) { c.Lock() defer c.Unlock() delete(c.deleted, key) diff --git a/x-pack/libbeat/processors/add_nomad_metadata/indexers.go b/x-pack/libbeat/processors/add_nomad_metadata/indexers.go index d5635ba67e5..943acfb9d65 100644 --- a/x-pack/libbeat/processors/add_nomad_metadata/indexers.go +++ b/x-pack/libbeat/processors/add_nomad_metadata/indexers.go @@ -11,6 +11,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/libbeat/common/nomad" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -33,7 +34,7 @@ type Indexer interface { // MetadataIndex holds a pair of index to metadata type MetadataIndex struct { Index string - Data common.MapStr + Data mapstr.M } // Indexers holds a collections of Indexer objects and the associated lock diff --git a/x-pack/libbeat/processors/add_nomad_metadata/matchers.go b/x-pack/libbeat/processors/add_nomad_metadata/matchers.go index adc25e2676e..4e2eb26554c 100644 --- a/x-pack/libbeat/processors/add_nomad_metadata/matchers.go +++ b/x-pack/libbeat/processors/add_nomad_metadata/matchers.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/outputs/codec" "github.com/elastic/beats/v7/libbeat/outputs/codec/format" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -28,7 +29,7 @@ type Matcher interface { // MetadataIndex returns the index string to use in annotation lookups for the given // event. A previous indexer should have generated that index for this to work // This function can return "" if the event doesn't match - MetadataIndex(event common.MapStr) string + MetadataIndex(event mapstr.M) string } // Matchers holds a collections of Matcher objects and the associated lock @@ -67,7 +68,7 @@ func NewMatchers(configs PluginConfig) *Matchers { } // MetadataIndex returns the index string for the first matcher from the Registry returning one -func (m *Matchers) MetadataIndex(event common.MapStr) string { +func (m *Matchers) MetadataIndex(event mapstr.M) string { m.RLock() defer m.RUnlock() for _, matcher := range m.matchers { @@ -116,7 +117,7 @@ func NewFieldMatcher(cfg common.Config) (Matcher, error) { } // MetadataIndex returns the first key of an event that satisfies a matcher -func (f *FieldMatcher) MetadataIndex(event common.MapStr) string { +func (f *FieldMatcher) MetadataIndex(event mapstr.M) string { for _, field := range f.MatchFields { keyIface, err := event.GetValue(field) if err == nil { @@ -158,7 +159,7 @@ func NewFieldFormatMatcher(cfg common.Config) (Matcher, error) { // MetadataIndex returns the content of the dynamic field defined by the FieldFormatMatcher, usually, // but not limited to, a concatenation of multiple fields -func (f *FieldFormatMatcher) MetadataIndex(event common.MapStr) string { +func (f *FieldFormatMatcher) MetadataIndex(event mapstr.M) string { bytes, err := f.Codec.Encode("", &beat.Event{ Fields: event, }) diff --git a/x-pack/libbeat/processors/add_nomad_metadata/nomad.go b/x-pack/libbeat/processors/add_nomad_metadata/nomad.go index 425860bc0aa..f89c2d8d2fd 100644 --- a/x-pack/libbeat/processors/add_nomad_metadata/nomad.go +++ b/x-pack/libbeat/processors/add_nomad_metadata/nomad.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/x-pack/libbeat/common/nomad" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -152,7 +153,7 @@ func (n *nomadAnnotator) Run(event *beat.Event) (*beat.Event, error) { return event, nil } - event.Fields.DeepUpdate(common.MapStr{ + event.Fields.DeepUpdate(mapstr.M{ "nomad": metadata.Clone(), }) diff --git a/x-pack/metricbeat/cmd/root.go b/x-pack/metricbeat/cmd/root.go index 2a67360bbf9..5f2df6c025e 100644 --- a/x-pack/metricbeat/cmd/root.go +++ b/x-pack/metricbeat/cmd/root.go @@ -11,12 +11,12 @@ import ( "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/publisher/processing" "github.com/elastic/beats/v7/metricbeat/beater" mbcmd "github.com/elastic/beats/v7/metricbeat/cmd" "github.com/elastic/beats/v7/metricbeat/cmd/test" + "github.com/elastic/elastic-agent-libs/mapstr" // Register the includes. _ "github.com/elastic/beats/v7/x-pack/libbeat/include" @@ -36,8 +36,8 @@ const ( var RootCmd *cmd.BeatsRootCmd // withECSVersion is a modifier that adds ecs.version to events. -var withECSVersion = processing.WithFields(common.MapStr{ - "ecs": common.MapStr{ +var withECSVersion = processing.WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) diff --git a/x-pack/metricbeat/module/aws/aws.go b/x-pack/metricbeat/module/aws/aws.go index e11b5a1126a..e4715f85ec9 100644 --- a/x-pack/metricbeat/module/aws/aws.go +++ b/x-pack/metricbeat/module/aws/aws.go @@ -19,9 +19,9 @@ import ( "github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi" "github.com/aws/aws-sdk-go-v2/service/sts" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) // Config defines all required and optional parameters for aws metricsets @@ -203,9 +203,9 @@ func StringInSlice(str string, list []string) (bool, int) { func InitEvent(regionName string, accountName string, accountID string, timestamp time.Time) mb.Event { event := mb.Event{ Timestamp: timestamp, - MetricSetFields: common.MapStr{}, - ModuleFields: common.MapStr{}, - RootFields: common.MapStr{}, + MetricSetFields: mapstr.M{}, + ModuleFields: mapstr.M{}, + RootFields: mapstr.M{}, } event.RootFields.Put("cloud.provider", "aws") diff --git a/x-pack/metricbeat/module/aws/billing/billing.go b/x-pack/metricbeat/module/aws/billing/billing.go index b7da00495a7..23ac3586874 100644 --- a/x-pack/metricbeat/module/aws/billing/billing.go +++ b/x-pack/metricbeat/module/aws/billing/billing.go @@ -21,11 +21,11 @@ import ( "github.com/aws/aws-sdk-go-v2/service/organizations" "github.com/aws/aws-sdk-go-v2/service/organizations/organizationsiface" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" awscommon "github.com/elastic/beats/v7/x-pack/libbeat/common/aws" "github.com/elastic/beats/v7/x-pack/metricbeat/module/aws" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -313,7 +313,7 @@ func (m *MetricSet) addCostMetrics(metrics map[string]costexplorer.MetricValue, event := aws.InitEvent("", m.AccountName, m.AccountID, time.Now()) // add group definition - event.MetricSetFields.Put("group_definition", common.MapStr{ + event.MetricSetFields.Put("group_definition", mapstr.M{ "key": *groupDefinition.Key, "type": groupDefinition.Type, }) @@ -327,7 +327,7 @@ func (m *MetricSet) addCostMetrics(metrics map[string]costexplorer.MetricValue, continue } - value := common.MapStr{ + value := mapstr.M{ "amount": costFloat, "unit": &cost.Unit, } diff --git a/x-pack/metricbeat/module/aws/billing/billing_integration_test.go b/x-pack/metricbeat/module/aws/billing/billing_integration_test.go index 67b4d03811e..d9056ff3816 100644 --- a/x-pack/metricbeat/module/aws/billing/billing_integration_test.go +++ b/x-pack/metricbeat/module/aws/billing/billing_integration_test.go @@ -14,10 +14,10 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/aws" "github.com/elastic/beats/v7/x-pack/metricbeat/module/aws/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -34,8 +34,8 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - resultTypeIs := func(resultType string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + resultTypeIs := func(resultType string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue("aws.billing.group_definition.key") if err == nil { // Check for Cost Explorer billing metrics diff --git a/x-pack/metricbeat/module/aws/elb/elb_integration_test.go b/x-pack/metricbeat/module/aws/elb/elb_integration_test.go index 4126937bc3d..100aa017468 100644 --- a/x-pack/metricbeat/module/aws/elb/elb_integration_test.go +++ b/x-pack/metricbeat/module/aws/elb/elb_integration_test.go @@ -11,14 +11,14 @@ import ( "fmt" "testing" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/aws/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { - namespaceIs := func(namespace string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + namespaceIs := func(namespace string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue("aws.cloudwatch.namespace") return err == nil && v == namespace } diff --git a/x-pack/metricbeat/module/awsfargate/task_stats/data.go b/x-pack/metricbeat/module/awsfargate/task_stats/data.go index 79e63664bfb..7b9f0919090 100644 --- a/x-pack/metricbeat/module/awsfargate/task_stats/data.go +++ b/x-pack/metricbeat/module/awsfargate/task_stats/data.go @@ -10,8 +10,8 @@ import ( "github.com/aws/aws-sdk-go-v2/aws/arn" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -28,7 +28,7 @@ func eventsMapping(r mb.ReporterV2, statsList []Stats) { func createEvent(stats *Stats) mb.Event { e := mb.Event{ Timestamp: time.Time(stats.Time), - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "cpu": createCPUFields(stats), "memory": createMemoryFields(stats), "network": createNetworkFields(stats), @@ -71,11 +71,11 @@ func getRegionAndClusterName(labels map[string]string) (regionName string, clust return } -func createRootFields(stats *Stats, regionName string) common.MapStr { - rootFields := common.MapStr{ - "container": common.MapStr{ +func createRootFields(stats *Stats, regionName string) mapstr.M { + rootFields := mapstr.M{ + "container": mapstr.M{ "id": stats.Container.DockerId, - "image": common.MapStr{ + "image": mapstr.M{ "name": stats.Container.Image, }, "name": stats.Container.Name, @@ -85,7 +85,7 @@ func createRootFields(stats *Stats, regionName string) common.MapStr { // add cloud.region if regionName != "" { - cloud := common.MapStr{ + cloud := mapstr.M{ "region": regionName, } rootFields.Put("cloud", cloud) @@ -93,63 +93,63 @@ func createRootFields(stats *Stats, regionName string) common.MapStr { return rootFields } -func createCPUFields(stats *Stats) common.MapStr { - return common.MapStr{ +func createCPUFields(stats *Stats) mapstr.M { + return mapstr.M{ "core": stats.cpuStats.PerCPUUsage, - "total": common.MapStr{ + "total": mapstr.M{ "pct": stats.cpuStats.TotalUsage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.cpuStats.TotalUsageNormalized, }, }, - "kernel": common.MapStr{ + "kernel": mapstr.M{ "ticks": stats.cpuStats.UsageInKernelmode, "pct": stats.cpuStats.UsageInKernelmodePercentage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.cpuStats.UsageInKernelmodePercentageNormalized, }, }, - "user": common.MapStr{ + "user": mapstr.M{ "ticks": stats.cpuStats.UsageInUsermode, "pct": stats.cpuStats.UsageInUsermodePercentage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.cpuStats.UsageInUsermodePercentageNormalized, }, }, - "system": common.MapStr{ + "system": mapstr.M{ "ticks": stats.cpuStats.SystemUsage, "pct": stats.cpuStats.SystemUsagePercentage, - "norm": common.MapStr{ + "norm": mapstr.M{ "pct": stats.cpuStats.SystemUsagePercentageNormalized, }, }, } } -func createMemoryFields(stats *Stats) common.MapStr { - var memoryFields common.MapStr +func createMemoryFields(stats *Stats) mapstr.M { + var memoryFields mapstr.M if stats.memoryStats.Commit+stats.memoryStats.CommitPeak+stats.memoryStats.PrivateWorkingSet > 0 { - memoryFields = common.MapStr{ - "commit": common.MapStr{ + memoryFields = mapstr.M{ + "commit": mapstr.M{ "total": stats.memoryStats.Commit, "peak": stats.memoryStats.CommitPeak, }, - "private_working_set": common.MapStr{ + "private_working_set": mapstr.M{ "total": stats.memoryStats.PrivateWorkingSet, }, } } else { - memoryFields = common.MapStr{ + memoryFields = mapstr.M{ "stats": stats.memoryStats.Stats, - "fail": common.MapStr{ + "fail": mapstr.M{ "count": stats.memoryStats.Failcnt, }, "limit": stats.memoryStats.Limit, - "rss": common.MapStr{ + "rss": mapstr.M{ "total": stats.memoryStats.TotalRss, "pct": stats.memoryStats.TotalRssP, }, - "usage": common.MapStr{ + "usage": mapstr.M{ "total": stats.memoryStats.Usage, "pct": stats.memoryStats.UsageP, "max": stats.memoryStats.MaxUsage, @@ -160,17 +160,17 @@ func createMemoryFields(stats *Stats) common.MapStr { return memoryFields } -func createNetworkFields(stats *Stats) common.MapStr { - networkFields := common.MapStr{} +func createNetworkFields(stats *Stats) mapstr.M { + networkFields := mapstr.M{} for _, n := range stats.networkStats { networkFields.Put(n.NameInterface, - common.MapStr{"inbound": common.MapStr{ + mapstr.M{"inbound": mapstr.M{ "bytes": n.Total.RxBytes, "dropped": n.Total.RxDropped, "errors": n.Total.RxErrors, "packets": n.Total.RxPackets, }, - "outbound": common.MapStr{ + "outbound": mapstr.M{ "bytes": n.Total.TxBytes, "dropped": n.Total.TxDropped, "errors": n.Total.TxErrors, @@ -180,12 +180,12 @@ func createNetworkFields(stats *Stats) common.MapStr { return networkFields } -func createDiskIOFields(stats *Stats) common.MapStr { - return common.MapStr{ +func createDiskIOFields(stats *Stats) mapstr.M { + return mapstr.M{ "reads": stats.blkioStats.reads, "writes": stats.blkioStats.writes, "total": stats.blkioStats.totals, - "read": common.MapStr{ + "read": mapstr.M{ "ops": stats.blkioStats.serviced.reads, "bytes": stats.blkioStats.servicedBytes.reads, "rate": stats.blkioStats.reads, @@ -193,7 +193,7 @@ func createDiskIOFields(stats *Stats) common.MapStr { "wait_time": stats.blkioStats.waitTime.reads, "queued": stats.blkioStats.queued.reads, }, - "write": common.MapStr{ + "write": mapstr.M{ "ops": stats.blkioStats.serviced.writes, "bytes": stats.blkioStats.servicedBytes.writes, "rate": stats.blkioStats.writes, @@ -201,7 +201,7 @@ func createDiskIOFields(stats *Stats) common.MapStr { "wait_time": stats.blkioStats.waitTime.writes, "queued": stats.blkioStats.queued.writes, }, - "summary": common.MapStr{ + "summary": mapstr.M{ "ops": stats.blkioStats.serviced.totals, "bytes": stats.blkioStats.servicedBytes.totals, "rate": stats.blkioStats.totals, diff --git a/x-pack/metricbeat/module/azure/add_metadata.go b/x-pack/metricbeat/module/azure/add_metadata.go index 7e82281962c..23bf4500eb8 100644 --- a/x-pack/metricbeat/module/azure/add_metadata.go +++ b/x-pack/metricbeat/module/azure/add_metadata.go @@ -5,11 +5,11 @@ package azure import ( - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) -func addHostMetadata(event *mb.Event, metricList common.MapStr) { +func addHostMetadata(event *mb.Event, metricList mapstr.M) { hostFieldTable := map[string]string{ "percentage_cpu.avg": "host.cpu.usage", "network_in_total.total": "host.network.ingress.bytes", diff --git a/x-pack/metricbeat/module/azure/app_insights/data.go b/x-pack/metricbeat/module/azure/app_insights/data.go index 1cd661cc3a0..f45f5eaed23 100644 --- a/x-pack/metricbeat/module/azure/app_insights/data.go +++ b/x-pack/metricbeat/module/azure/app_insights/data.go @@ -13,8 +13,8 @@ import ( "github.com/Azure/go-autorest/autorest/date" "github.com/elastic/beats/v7/x-pack/metricbeat/module/azure" + "github.com/elastic/elastic-agent-libs/mapstr" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" ) @@ -162,7 +162,7 @@ func getValue(metric MetricValue) []MetricValue { } func createSegEvent(parentMetricValue MetricValue, metricValue MetricValue, applicationId string, namespace string) mb.Event { - metricList := common.MapStr{} + metricList := mapstr.M{} for key, metric := range metricValue.Value { metricList.Put(key, metric) } @@ -179,18 +179,18 @@ func createSegEvent(parentMetricValue MetricValue, metricValue MetricValue, appl return event } -func createEvent(start *date.Time, end *date.Time, applicationId string, namespace string, metricList common.MapStr) mb.Event { +func createEvent(start *date.Time, end *date.Time, applicationId string, namespace string, metricList mapstr.M) mb.Event { event := mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "application_id": applicationId, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "start_date": start, "end_date": end, }, Timestamp: end.Time, } - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("cloud.provider", "azure") if namespace == "" { event.ModuleFields.Put("metrics", metricList) @@ -203,7 +203,7 @@ func createEvent(start *date.Time, end *date.Time, applicationId string, namespa } func createNoSegEvent(values []MetricValue, applicationId string, namespace string) mb.Event { - metricList := common.MapStr{} + metricList := mapstr.M{} for _, value := range values { for key, metric := range value.Value { metricList.Put(key, metric) diff --git a/x-pack/metricbeat/module/azure/app_insights/data_test.go b/x-pack/metricbeat/module/azure/app_insights/data_test.go index f65f9f84b1d..56e723f078e 100644 --- a/x-pack/metricbeat/module/azure/app_insights/data_test.go +++ b/x-pack/metricbeat/module/azure/app_insights/data_test.go @@ -11,7 +11,7 @@ import ( "github.com/Azure/go-autorest/autorest/date" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestEventMapping(t *testing.T) { @@ -47,9 +47,9 @@ func TestEventMapping(t *testing.T) { val2, _ := event.MetricSetFields.GetValue("end_date") assert.Equal(t, val2, &startDate) val3, _ := event.ModuleFields.GetValue("metrics.requests_count") - assert.Equal(t, val3, common.MapStr{"sum": 12}) + assert.Equal(t, val3, mapstr.M{"sum": 12}) val5, _ := event.ModuleFields.GetValue("metrics.requests_failed") - assert.Equal(t, val5, common.MapStr{"sum": 10}) + assert.Equal(t, val5, mapstr.M{"sum": 10}) val4, _ := event.ModuleFields.GetValue("application_id") assert.Equal(t, val4, applicationId) diff --git a/x-pack/metricbeat/module/azure/billing/data.go b/x-pack/metricbeat/module/azure/billing/data.go index 9aafb87d992..2a5f4e2acbd 100644 --- a/x-pack/metricbeat/module/azure/billing/data.go +++ b/x-pack/metricbeat/module/azure/billing/data.go @@ -13,8 +13,8 @@ import ( "github.com/shopspring/decimal" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) func EventsMapping(subscriptionId string, results Usage) []mb.Event { @@ -22,15 +22,15 @@ func EventsMapping(subscriptionId string, results Usage) []mb.Event { if len(results.UsageDetails) > 0 { for _, usageDetail := range results.UsageDetails { event := mb.Event{ - ModuleFields: common.MapStr{ - "resource": common.MapStr{ + ModuleFields: mapstr.M{ + "resource": mapstr.M{ "type": usageDetail.ConsumedService, "group": getResourceGroupFromId(*usageDetail.InstanceID), "name": usageDetail.InstanceName, }, "subscription_id": usageDetail.SubscriptionGUID, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "pretax_cost": usageDetail.PretaxCost, "department_name": usageDetail.DepartmentName, "product": usageDetail.Product, @@ -42,7 +42,7 @@ func EventsMapping(subscriptionId string, results Usage) []mb.Event { }, Timestamp: time.Now().UTC(), } - event.RootFields = common.MapStr{} + event.RootFields = mapstr.M{} event.RootFields.Put("cloud.provider", "azure") event.RootFields.Put("cloud.region", usageDetail.InstanceLocation) event.RootFields.Put("cloud.instance.name", usageDetail.InstanceName) @@ -73,13 +73,13 @@ func EventsMapping(subscriptionId string, results Usage) []mb.Event { parsedDate = time.Now().UTC() } event := mb.Event{ - RootFields: common.MapStr{ + RootFields: mapstr.M{ "cloud.provider": "azure", }, - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "subscription_id": subscriptionId, }, - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "actual_cost": actualCost, "forecast_cost": forecastCost, "usage_date": parsedDate, diff --git a/x-pack/metricbeat/module/azure/config.go b/x-pack/metricbeat/module/azure/config.go index 489b40d0450..3993cf16773 100644 --- a/x-pack/metricbeat/module/azure/config.go +++ b/x-pack/metricbeat/module/azure/config.go @@ -7,9 +7,9 @@ package azure import ( "time" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/pkg/errors" + + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -18,7 +18,7 @@ const ( ) var ( - AzureEnvs = common.MapStr{ + AzureEnvs = mapstr.M{ "https://management.azure.com/": "https://login.microsoftonline.com/", "https://management.usgovcloudapi.net/": "https://login.microsoftonline.us/", "https://management.chinacloudapi.cn/": "https://login.chinacloudapi.cn/", diff --git a/x-pack/metricbeat/module/azure/data.go b/x-pack/metricbeat/module/azure/data.go index 55b5d5440ee..f728dd0ddc2 100644 --- a/x-pack/metricbeat/module/azure/data.go +++ b/x-pack/metricbeat/module/azure/data.go @@ -11,8 +11,8 @@ import ( "time" "unicode" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -67,7 +67,7 @@ func EventsMapping(metrics []Metric, client *Client, report mb.ReporterV2) error } for timestamp, groupTimeValues := range groupByTimeMetrics { var event mb.Event - var metricList common.MapStr + var metricList mapstr.M var vm VmResource // group events by dimension values exists, validDimensions := returnAllDimensions(defaultMetric.Dimensions) @@ -91,7 +91,7 @@ func EventsMapping(metrics []Metric, client *Client, report mb.ReporterV2) error } // manageAndReportEvent function will handle event creation and report -func manageAndReportEvent(client *Client, report mb.ReporterV2, event mb.Event, metricList common.MapStr, vm VmResource, timestamp time.Time, defaultMetric Metric, resource Resource, groupedValues []MetricValue) { +func manageAndReportEvent(client *Client, report mb.ReporterV2, event mb.Event, metricList mapstr.M, vm VmResource, timestamp time.Time, defaultMetric Metric, resource Resource, groupedValues []MetricValue) { event, metricList = createEvent(timestamp, defaultMetric, resource, groupedValues) if client.Config.AddCloudMetadata { vm = client.GetVMForMetaData(&resource, groupedValues) @@ -153,23 +153,23 @@ func ReplaceUpperCase(src string) string { } // createEvent will create a new base event -func createEvent(timestamp time.Time, metric Metric, resource Resource, metricValues []MetricValue) (mb.Event, common.MapStr) { +func createEvent(timestamp time.Time, metric Metric, resource Resource, metricValues []MetricValue) (mb.Event, mapstr.M) { event := mb.Event{ - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "timegrain": metric.TimeGrain, "namespace": metric.Namespace, - "resource": common.MapStr{ + "resource": mapstr.M{ "type": resource.Type, "group": resource.Group, "name": resource.Name, }, "subscription_id": resource.Subscription, }, - MetricSetFields: common.MapStr{}, + MetricSetFields: mapstr.M{}, Timestamp: timestamp, - RootFields: common.MapStr{ - "cloud": common.MapStr{ + RootFields: mapstr.M{ + "cloud": mapstr.M{ "provider": "azure", "region": resource.Location, }, @@ -195,7 +195,7 @@ func createEvent(timestamp time.Time, metric Metric, resource Resource, metricVa } } - metricList := common.MapStr{} + metricList := mapstr.M{} for _, value := range metricValues { metricNameString := fmt.Sprintf("%s", managePropertyName(value.name)) if value.min != nil { diff --git a/x-pack/metricbeat/module/azure/data_test.go b/x-pack/metricbeat/module/azure/data_test.go index cca7edc0233..409980b6459 100644 --- a/x-pack/metricbeat/module/azure/data_test.go +++ b/x-pack/metricbeat/module/azure/data_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestReturnAllDimensions(t *testing.T) { @@ -122,5 +122,5 @@ func TestCreateEvent(t *testing.T) { if !assert.NoError(t, err) { t.Fatal(err) } - assert.Equal(t, val.(common.MapStr), common.MapStr{"total": total}) + assert.Equal(t, val.(mapstr.M), mapstr.M{"total": total}) } diff --git a/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go b/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go index 3d9eef010bc..72e3b0024f3 100644 --- a/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go +++ b/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go @@ -11,11 +11,10 @@ import ( "testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/azure/test" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" - mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" ) @@ -47,8 +46,8 @@ func TestDataMultipleDimensions(t *testing.T) { "metrics": []map[string]interface{}{{"namespace": "Microsoft.KeyVault/vaults", "name": []string{"Availability"}, "dimensions": []map[string]interface{}{{"name": "ActivityName", "value": "*"}}}}}} metricSet := mbtest.NewFetcher(t, config) - metricSet.WriteEventsCond(t, "/", func(m common.MapStr) bool { - if m["azure"].(common.MapStr)["dimensions"].(common.MapStr)["activity_name"] == "secretget" { + metricSet.WriteEventsCond(t, "/", func(m mapstr.M) bool { + if m["azure"].(mapstr.M)["dimensions"].(mapstr.M)["activity_name"] == "secretget" { return true } return false diff --git a/x-pack/metricbeat/module/azure/monitor/monitor_test.go b/x-pack/metricbeat/module/azure/monitor/monitor_test.go index bddc34190b2..75e86815d1a 100644 --- a/x-pack/metricbeat/module/azure/monitor/monitor_test.go +++ b/x-pack/metricbeat/module/azure/monitor/monitor_test.go @@ -13,10 +13,11 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( - missingResourcesConfig = common.MapStr{ + missingResourcesConfig = mapstr.M{ "module": "azure", "period": "60s", "metricsets": []string{"monitor"}, @@ -26,7 +27,7 @@ var ( "tenant_id": "unique identifier", } - resourceConfig = common.MapStr{ + resourceConfig = mapstr.M{ "module": "azure", "period": "60s", "metricsets": []string{"monitor"}, @@ -34,7 +35,7 @@ var ( "client_id": "unique identifier", "subscription_id": "unique identifier", "tenant_id": "unique identifier", - "resources": []common.MapStr{ + "resources": []mapstr.M{ { "resource_id": "test", "metrics": []map[string]interface{}{ diff --git a/x-pack/metricbeat/module/azure/storage/storage_test.go b/x-pack/metricbeat/module/azure/storage/storage_test.go index 2512ab6660e..2da5c891f60 100644 --- a/x-pack/metricbeat/module/azure/storage/storage_test.go +++ b/x-pack/metricbeat/module/azure/storage/storage_test.go @@ -13,10 +13,11 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( - missingResourcesConfig = common.MapStr{ + missingResourcesConfig = mapstr.M{ "module": "azure", "period": "60s", "metricsets": []string{"storage"}, @@ -26,7 +27,7 @@ var ( "tenant_id": "unique identifier", } - resourceConfig = common.MapStr{ + resourceConfig = mapstr.M{ "module": "azure", "period": "60s", "metricsets": []string{"storage"}, @@ -34,7 +35,7 @@ var ( "client_id": "unique identifier", "subscription_id": "unique identifier", "tenant_id": "unique identifier", - "resources": []common.MapStr{ + "resources": []mapstr.M{ { "resource_id": "test", "metrics": []map[string]interface{}{ diff --git a/x-pack/metricbeat/module/cloudfoundry/container/container_test.go b/x-pack/metricbeat/module/cloudfoundry/container/container_test.go index 67b5ca5227a..6babd24dda8 100644 --- a/x-pack/metricbeat/module/cloudfoundry/container/container_test.go +++ b/x-pack/metricbeat/module/cloudfoundry/container/container_test.go @@ -17,12 +17,12 @@ import ( "github.com/cloudfoundry/sonde-go/events" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -60,7 +60,7 @@ func TestMetricSet(t *testing.T) { events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms) require.NotEmpty(t, events) - expectedFields := common.MapStr{ + expectedFields := mapstr.M{ "cloudfoundry.app.id": "1234", "cloudfoundry.container.cpu.pct": float64(0.1234), "cloudfoundry.container.disk.bytes": uint64(0), diff --git a/x-pack/metricbeat/module/cloudfoundry/counter/counter_test.go b/x-pack/metricbeat/module/cloudfoundry/counter/counter_test.go index 76e95ca4078..b23867cbee9 100644 --- a/x-pack/metricbeat/module/cloudfoundry/counter/counter_test.go +++ b/x-pack/metricbeat/module/cloudfoundry/counter/counter_test.go @@ -15,12 +15,12 @@ import ( "github.com/cloudfoundry/sonde-go/events" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -58,7 +58,7 @@ func TestMetricSet(t *testing.T) { events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms) require.NotEmpty(t, events) - expectedFields := common.MapStr{ + expectedFields := mapstr.M{ "cloudfoundry.counter.delta": uint64(123), "cloudfoundry.counter.name": "requests", "cloudfoundry.counter.total": uint64(1234), diff --git a/x-pack/metricbeat/module/cloudfoundry/v1_test.go b/x-pack/metricbeat/module/cloudfoundry/v1_test.go index 17f315d287d..32b629e5f74 100644 --- a/x-pack/metricbeat/module/cloudfoundry/v1_test.go +++ b/x-pack/metricbeat/module/cloudfoundry/v1_test.go @@ -12,10 +12,10 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" cfcommon "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestDispatcher(t *testing.T) { @@ -24,7 +24,7 @@ func TestDispatcher(t *testing.T) { assertEventType := func(t *testing.T, expected string, e mb.Event) { t.Helper() - cf := e.RootFields["cloudfoundry"].(common.MapStr) + cf := e.RootFields["cloudfoundry"].(mapstr.M) assert.Equal(t, expected, cf["type"]) } diff --git a/x-pack/metricbeat/module/cloudfoundry/value/value_test.go b/x-pack/metricbeat/module/cloudfoundry/value/value_test.go index 1ea448cca77..910aab6d589 100644 --- a/x-pack/metricbeat/module/cloudfoundry/value/value_test.go +++ b/x-pack/metricbeat/module/cloudfoundry/value/value_test.go @@ -17,12 +17,12 @@ import ( "github.com/cloudfoundry/sonde-go/events" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -60,7 +60,7 @@ func TestMetricSet(t *testing.T) { events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms) require.NotEmpty(t, events) - expectedFields := common.MapStr{ + expectedFields := mapstr.M{ "cloudfoundry.envelope.deployment": "test", "cloudfoundry.envelope.index": "index", "cloudfoundry.envelope.ip": "127.0.0.1", diff --git a/x-pack/metricbeat/module/containerd/blkio/blkio.go b/x-pack/metricbeat/module/containerd/blkio/blkio.go index 527a68c3dd5..96f976ba080 100644 --- a/x-pack/metricbeat/module/containerd/blkio/blkio.go +++ b/x-pack/metricbeat/module/containerd/blkio/blkio.go @@ -7,9 +7,8 @@ package blkio import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/x-pack/metricbeat/module/containerd" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" @@ -106,9 +105,9 @@ func (m *metricset) Fetch(reporter mb.ReporterV2) error { } for _, event := range events { // setting ECS container.id and module field containerd.namespace - containerFields := common.MapStr{} - moduleFields := common.MapStr{} - rootFields := common.MapStr{} + containerFields := mapstr.M{} + moduleFields := mapstr.M{} + rootFields := mapstr.M{} cID := containerd.GetAndDeleteCid(event) namespace := containerd.GetAndDeleteNamespace(event) diff --git a/x-pack/metricbeat/module/containerd/cpu/cpu.go b/x-pack/metricbeat/module/containerd/cpu/cpu.go index 0adc7e43697..3744f651faa 100644 --- a/x-pack/metricbeat/module/containerd/cpu/cpu.go +++ b/x-pack/metricbeat/module/containerd/cpu/cpu.go @@ -9,10 +9,10 @@ import ( "time" "github.com/elastic/beats/v7/x-pack/metricbeat/module/containerd" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/prometheus" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/mb/parse" @@ -122,9 +122,9 @@ func (m *metricset) Fetch(reporter mb.ReporterV2) error { for _, event := range events { // setting ECS container.id and module field containerd.namespace - containerFields := common.MapStr{} - moduleFields := common.MapStr{} - rootFields := common.MapStr{} + containerFields := mapstr.M{} + moduleFields := mapstr.M{} + rootFields := mapstr.M{} cID := containerd.GetAndDeleteCid(event) namespace := containerd.GetAndDeleteNamespace(event) @@ -201,7 +201,7 @@ func (m *metricset) Fetch(reporter mb.ReporterV2) error { return nil } -func setContCpus(event common.MapStr, perContainerCpus map[string]int) { +func setContCpus(event mapstr.M, perContainerCpus map[string]int) { val, err := event.GetValue("id") if err != nil { return diff --git a/x-pack/metricbeat/module/containerd/helper.go b/x-pack/metricbeat/module/containerd/helper.go index b4fac5f72fd..9b78e2af4fe 100644 --- a/x-pack/metricbeat/module/containerd/helper.go +++ b/x-pack/metricbeat/module/containerd/helper.go @@ -4,10 +4,10 @@ package containerd -import "github.com/elastic/beats/v7/libbeat/common" +import "github.com/elastic/elastic-agent-libs/mapstr" // GetAndDeleteCid deletes and returns container id from an event -func GetAndDeleteCid(event common.MapStr) (cID string) { +func GetAndDeleteCid(event mapstr.M) (cID string) { if containerID, ok := event["id"]; ok { cID = (containerID).(string) event.Delete("id") @@ -16,7 +16,7 @@ func GetAndDeleteCid(event common.MapStr) (cID string) { } // GetAndDeleteNamespace deletes and returns namespace from an event -func GetAndDeleteNamespace(event common.MapStr) (namespace string) { +func GetAndDeleteNamespace(event mapstr.M) (namespace string) { if ns, ok := event["namespace"]; ok { namespace = (ns).(string) event.Delete("namespace") diff --git a/x-pack/metricbeat/module/containerd/memory/memory.go b/x-pack/metricbeat/module/containerd/memory/memory.go index 0442f051fdc..7af7b6e7640 100644 --- a/x-pack/metricbeat/module/containerd/memory/memory.go +++ b/x-pack/metricbeat/module/containerd/memory/memory.go @@ -7,9 +7,8 @@ package memory import ( "fmt" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/libbeat/common/cfgwarn" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/beats/v7/metricbeat/helper/prometheus" "github.com/elastic/beats/v7/metricbeat/mb" @@ -114,9 +113,9 @@ func (m *metricset) Fetch(reporter mb.ReporterV2) error { for _, event := range events { // setting ECS container.id and module field containerd.namespace - containerFields := common.MapStr{} - moduleFields := common.MapStr{} - rootFields := common.MapStr{} + containerFields := mapstr.M{} + moduleFields := mapstr.M{} + rootFields := mapstr.M{} cID := containerd.GetAndDeleteCid(event) namespace := containerd.GetAndDeleteNamespace(event) diff --git a/x-pack/metricbeat/module/enterprisesearch/health/data.go b/x-pack/metricbeat/module/enterprisesearch/health/data.go index 32fa3849788..c2620bff247 100644 --- a/x-pack/metricbeat/module/enterprisesearch/health/data.go +++ b/x-pack/metricbeat/module/enterprisesearch/health/data.go @@ -10,11 +10,11 @@ import ( "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -84,8 +84,8 @@ func eventMapping(report mb.ReporterV2, input []byte, isXpack bool) error { // All events need to have a cluster_uuid to work with Stack Monitoring event := mb.Event{ - ModuleFields: common.MapStr{}, - MetricSetFields: common.MapStr{}, + ModuleFields: mapstr.M{}, + MetricSetFields: mapstr.M{}, } event.ModuleFields.Put("cluster_uuid", data["cluster_uuid"]) diff --git a/x-pack/metricbeat/module/enterprisesearch/stats/data.go b/x-pack/metricbeat/module/enterprisesearch/stats/data.go index d11767b316e..ea20ded1745 100644 --- a/x-pack/metricbeat/module/enterprisesearch/stats/data.go +++ b/x-pack/metricbeat/module/enterprisesearch/stats/data.go @@ -10,11 +10,11 @@ import ( "github.com/joeshaw/multierror" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/helper/elastic" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) var ( @@ -166,8 +166,8 @@ func eventMapping(report mb.ReporterV2, input []byte, isXpack bool) error { // All events need to have a cluster_uuid to work with Stack Monitoring event := mb.Event{ - ModuleFields: common.MapStr{}, - MetricSetFields: common.MapStr{}, + ModuleFields: mapstr.M{}, + MetricSetFields: mapstr.M{}, } event.ModuleFields.Put("cluster_uuid", data["cluster_uuid"]) diff --git a/x-pack/metricbeat/module/gcp/billing/billing.go b/x-pack/metricbeat/module/gcp/billing/billing.go index fe075938623..6cbe88359d5 100644 --- a/x-pack/metricbeat/module/gcp/billing/billing.go +++ b/x-pack/metricbeat/module/gcp/billing/billing.go @@ -17,11 +17,11 @@ import ( "google.golang.org/api/iterator" "google.golang.org/api/option" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -274,7 +274,7 @@ func (m *MetricSet) queryBigQuery(ctx context.Context, client *bigquery.Client, func createEvents(rowItems []bigquery.Value, projectID string) mb.Event { event := mb.Event{} - event.MetricSetFields = common.MapStr{ + event.MetricSetFields = mapstr.M{ "invoice_month": rowItems[0], "project_id": rowItems[1], "project_name": rowItems[2], @@ -283,7 +283,7 @@ func createEvents(rowItems []bigquery.Value, projectID string) mb.Event { "total": rowItems[5], } - event.RootFields = common.MapStr{ + event.RootFields = mapstr.M{ "cloud.provider": "gcp", "cloud.project.id": projectID, "cloud.project.name": rowItems[2], diff --git a/x-pack/metricbeat/module/gcp/compute/compute_integration_test.go b/x-pack/metricbeat/module/gcp/compute/compute_integration_test.go index 71d41a283a0..2d5d04d135d 100644 --- a/x-pack/metricbeat/module/gcp/compute/compute_integration_test.go +++ b/x-pack/metricbeat/module/gcp/compute/compute_integration_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp/metrics" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -33,8 +33,8 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - metricPrefixIs := func(metricPrefix string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + metricPrefixIs := func(metricPrefix string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue(metricPrefix) return err == nil && v != nil } diff --git a/x-pack/metricbeat/module/gcp/dataproc/dataproc_integration_test.go b/x-pack/metricbeat/module/gcp/dataproc/dataproc_integration_test.go index ce1a58821f7..f19222b0b9a 100644 --- a/x-pack/metricbeat/module/gcp/dataproc/dataproc_integration_test.go +++ b/x-pack/metricbeat/module/gcp/dataproc/dataproc_integration_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp/metrics" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -33,8 +33,8 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - metricPrefixIs := func(metricPrefix string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + metricPrefixIs := func(metricPrefix string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue(metricPrefix) return err == nil && v != nil } diff --git a/x-pack/metricbeat/module/gcp/firestore/firestore_integration_test.go b/x-pack/metricbeat/module/gcp/firestore/firestore_integration_test.go index 8783f3144fd..858e4840a9c 100644 --- a/x-pack/metricbeat/module/gcp/firestore/firestore_integration_test.go +++ b/x-pack/metricbeat/module/gcp/firestore/firestore_integration_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp/metrics" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -33,8 +33,8 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - metricPrefixIs := func(metricPrefix string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + metricPrefixIs := func(metricPrefix string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue(metricPrefix) return err == nil && v != nil } diff --git a/x-pack/metricbeat/module/gcp/loadbalancing/loadbalancing_integration_test.go b/x-pack/metricbeat/module/gcp/loadbalancing/loadbalancing_integration_test.go index ce58cc04bd7..bd98e781824 100644 --- a/x-pack/metricbeat/module/gcp/loadbalancing/loadbalancing_integration_test.go +++ b/x-pack/metricbeat/module/gcp/loadbalancing/loadbalancing_integration_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp/metrics" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -33,8 +33,8 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - metricPrefixIs := func(metricPrefix string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + metricPrefixIs := func(metricPrefix string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue(metricPrefix) return err == nil && v != nil } diff --git a/x-pack/metricbeat/module/gcp/metadata.go b/x-pack/metricbeat/module/gcp/metadata.go index ae962ea51c8..cff7c5cb705 100644 --- a/x-pack/metricbeat/module/gcp/metadata.go +++ b/x-pack/metricbeat/module/gcp/metadata.go @@ -10,7 +10,7 @@ import ( monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // MetadataService must be implemented by GCP services that requires non out-of-the box code that is not fulfil by the Stackdriver @@ -61,8 +61,8 @@ type MetadataCollectorInputData struct { // MetadataCollectorData contains the set of ECS and normal labels that we extract from GCP services type MetadataCollectorData struct { - Labels common.MapStr - ECS common.MapStr + Labels mapstr.M + ECS mapstr.M } // Identity must be implemented by GCP services that can add some short of data to group their metrics (like instance diff --git a/x-pack/metricbeat/module/gcp/metrics/metricset.go b/x-pack/metricbeat/module/gcp/metrics/metricset.go index f24f7701e83..4920799ca92 100644 --- a/x-pack/metricbeat/module/gcp/metrics/metricset.go +++ b/x-pack/metricbeat/module/gcp/metrics/metricset.go @@ -18,11 +18,11 @@ import ( "google.golang.org/genproto/googleapis/api/metric" monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -228,10 +228,10 @@ func (m *MetricSet) eventMapping(ctx context.Context, tss []timeSeriesWithAligne for _, groupedEvents := range tsGrouped { event := mb.Event{ Timestamp: groupedEvents[0].Timestamp, - ModuleFields: common.MapStr{ + ModuleFields: mapstr.M{ "labels": groupedEvents[0].Labels, }, - MetricSetFields: common.MapStr{}, + MetricSetFields: mapstr.M{}, } for _, singleEvent := range groupedEvents { @@ -339,7 +339,7 @@ func (m *MetricSet) getMetadata(out *metric.MetricDescriptor, metricsWithMeta ma return metricsWithMeta } -func addHostFields(groupedEvents []KeyValuePoint) common.MapStr { +func addHostFields(groupedEvents []KeyValuePoint) mapstr.M { hostRootFields := groupedEvents[0].ECS // add host.id and host.name if hostID, err := groupedEvents[0].ECS.GetValue("cloud.instance.id"); err == nil { diff --git a/x-pack/metricbeat/module/gcp/metrics/response_parser.go b/x-pack/metricbeat/module/gcp/metrics/response_parser.go index e4d6399f782..9604c21402e 100644 --- a/x-pack/metricbeat/module/gcp/metrics/response_parser.go +++ b/x-pack/metricbeat/module/gcp/metrics/response_parser.go @@ -12,9 +12,9 @@ import ( "github.com/pkg/errors" "google.golang.org/genproto/googleapis/monitoring/v3" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp" + "github.com/elastic/elastic-agent-libs/mapstr" ) func newIncomingFieldExtractor(l *logp.Logger, mc metricsConfig) *incomingFieldExtractor { @@ -30,8 +30,8 @@ type incomingFieldExtractor struct { type KeyValuePoint struct { Key string Value interface{} - Labels common.MapStr - ECS common.MapStr + Labels mapstr.M + ECS mapstr.M Timestamp time.Time } diff --git a/x-pack/metricbeat/module/gcp/pubsub/pubsub_integration_test.go b/x-pack/metricbeat/module/gcp/pubsub/pubsub_integration_test.go index e42dab93549..115059c0009 100644 --- a/x-pack/metricbeat/module/gcp/pubsub/pubsub_integration_test.go +++ b/x-pack/metricbeat/module/gcp/pubsub/pubsub_integration_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp/metrics" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -33,8 +33,8 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - metricPrefixIs := func(metricPrefix string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + metricPrefixIs := func(metricPrefix string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue(metricPrefix) return err == nil && v != nil } diff --git a/x-pack/metricbeat/module/gcp/storage/storage_integration_test.go b/x-pack/metricbeat/module/gcp/storage/storage_integration_test.go index e89926bffe6..be273deb917 100644 --- a/x-pack/metricbeat/module/gcp/storage/storage_integration_test.go +++ b/x-pack/metricbeat/module/gcp/storage/storage_integration_test.go @@ -13,9 +13,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp/metrics" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestFetch(t *testing.T) { @@ -33,8 +33,8 @@ func TestFetch(t *testing.T) { } func TestData(t *testing.T) { - metricPrefixIs := func(metricPrefix string) func(e common.MapStr) bool { - return func(e common.MapStr) bool { + metricPrefixIs := func(metricPrefix string) func(e mapstr.M) bool { + return func(e mapstr.M) bool { v, err := e.GetValue(metricPrefix) return err == nil && v != nil } diff --git a/x-pack/metricbeat/module/gcp/timeseries_metadata_collector.go b/x-pack/metricbeat/module/gcp/timeseries_metadata_collector.go index 4fc5a34c9da..72a6c43b4ed 100644 --- a/x-pack/metricbeat/module/gcp/timeseries_metadata_collector.go +++ b/x-pack/metricbeat/module/gcp/timeseries_metadata_collector.go @@ -13,7 +13,7 @@ import ( "github.com/pkg/errors" monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // NewStackdriverCollectorInputData returns a ready to use MetadataCollectorInputData to be sent to Metadata collectors @@ -43,7 +43,7 @@ type StackdriverTimeSeriesMetadataCollector struct { // Metadata parses a Timeseries object to return its metadata divided into "unknown" (first object) and ECS (second // object https://www.elastic.co/guide/en/ecs/master/index.html) func (s *StackdriverTimeSeriesMetadataCollector) Metadata(ctx context.Context, in *monitoringpb.TimeSeries) (MetadataCollectorData, error) { - m := common.MapStr{} + m := mapstr.M{} var availabilityZone, accountID string @@ -52,9 +52,9 @@ func (s *StackdriverTimeSeriesMetadataCollector) Metadata(ctx context.Context, i accountID = in.Resource.Labels[TimeSeriesResponsePathForECSAccountID] } - ecs := common.MapStr{ - ECSCloud: common.MapStr{ - ECSCloudAccount: common.MapStr{ + ecs := mapstr.M{ + ECSCloud: mapstr.M{ + ECSCloudAccount: mapstr.M{ ECSCloudAccountID: accountID, ECSCloudAccountName: accountID, }, @@ -127,7 +127,7 @@ func (s *StackdriverTimeSeriesMetadataCollector) Metadata(ctx context.Context, i // ID returns a unique generated ID for an event when no service is implemented to get a "better" ID.`El trickerionEl trickerion func (s *StackdriverTimeSeriesMetadataCollector) ID(ctx context.Context, in *MetadataCollectorInputData) (string, error) { - m := common.MapStr{ + m := mapstr.M{ KeyTimestamp: in.Timestamp.UnixNano(), } diff --git a/x-pack/metricbeat/module/iis/application_pool/reader.go b/x-pack/metricbeat/module/iis/application_pool/reader.go index 0be37546c22..e89c214da9c 100644 --- a/x-pack/metricbeat/module/iis/application_pool/reader.go +++ b/x-pack/metricbeat/module/iis/application_pool/reader.go @@ -10,8 +10,8 @@ package application_pool import ( "strings" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/windows/pdh" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/go-sysinfo" "github.com/pkg/errors" @@ -163,10 +163,10 @@ func (r *Reader) mapEvents(values map[string][]pdh.CounterValue) map[string]mb.E events := make(map[string]mb.Event) for _, appPool := range r.applicationPools { events[appPool.name] = mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "name": appPool.name, }, - RootFields: common.MapStr{}, + RootFields: mapstr.M{}, } for counterPath, value := range values { for _, val := range value { diff --git a/x-pack/metricbeat/module/mssql/performance/performance.go b/x-pack/metricbeat/module/mssql/performance/performance.go index da15da02302..f58351d15c9 100644 --- a/x-pack/metricbeat/module/mssql/performance/performance.go +++ b/x-pack/metricbeat/module/mssql/performance/performance.go @@ -9,11 +9,10 @@ import ( "fmt" "strings" - "github.com/elastic/beats/v7/libbeat/common" - "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/metricbeat/module/mssql" + "github.com/elastic/elastic-agent-libs/mapstr" ) type performanceCounter struct { @@ -100,7 +99,7 @@ WHERE counter_name = 'SQL Compilations/sec' } }() - mapStr := common.MapStr{} + mapStr := mapstr.M{} for rows.Next() { var row performanceCounter if err = rows.Scan(&row.objectName, &row.counterName, &row.instanceName, &row.counterValue); err != nil { diff --git a/x-pack/metricbeat/module/mssql/transaction_log/transaction_log.go b/x-pack/metricbeat/module/mssql/transaction_log/transaction_log.go index a974ffdab9b..5700066995c 100644 --- a/x-pack/metricbeat/module/mssql/transaction_log/transaction_log.go +++ b/x-pack/metricbeat/module/mssql/transaction_log/transaction_log.go @@ -10,10 +10,10 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/metricbeat/module/mssql" + "github.com/elastic/elastic-agent-libs/mapstr" ) type dbInfo struct { @@ -79,13 +79,13 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) { } for _, db := range dbs { - moduleFields := common.MapStr{ - "database": common.MapStr{ + moduleFields := mapstr.M{ + "database": mapstr.M{ "id": db.id, "name": db.name, }, } - metricsetFields := common.MapStr{} + metricsetFields := mapstr.M{} spaceUsage, err := m.getLogSpaceUsageForDb(db.name) if err != nil { @@ -122,7 +122,7 @@ func (m *MetricSet) Close() error { return m.db.Close() } -func (m *MetricSet) getLogSpaceUsageForDb(dbName string) (common.MapStr, error) { +func (m *MetricSet) getLogSpaceUsageForDb(dbName string) (mapstr.M, error) { // According to MS docs a single result is always returned for this query row := m.db.QueryRow(fmt.Sprintf(`USE [%s]; SELECT * FROM sys.dm_db_log_space_usage;`, dbName)) @@ -135,20 +135,20 @@ func (m *MetricSet) getLogSpaceUsageForDb(dbName string) (common.MapStr, error) return nil, err } - return common.MapStr{ - "total": common.MapStr{ + return mapstr.M{ + "total": mapstr.M{ "bytes": res.totalLogSizeInBytes, }, - "used": common.MapStr{ + "used": mapstr.M{ "bytes": res.usedLogSpaceInBytes, "pct": res.usedLogSpaceInPercent, }, - "since_last_backup": common.MapStr{ + "since_last_backup": mapstr.M{ "bytes": res.logSpaceInBytesSinceLastBackup, }, }, nil } -func (m *MetricSet) getLogStats(db dbInfo) (common.MapStr, error) { +func (m *MetricSet) getLogStats(db dbInfo) (mapstr.M, error) { // According to MS docs a single result is always returned for this query row := m.db.QueryRow(fmt.Sprintf(`USE [%s]; SELECT database_id,total_log_size_mb,active_log_size_mb,log_backup_time,log_since_last_log_backup_mb,log_since_last_checkpoint_mb,log_recovery_size_mb FROM sys.dm_db_log_stats(%d);`, db.name, db.id)) @@ -160,18 +160,18 @@ func (m *MetricSet) getLogStats(db dbInfo) (common.MapStr, error) { return nil, err } - result := common.MapStr{ - "total_size": common.MapStr{ + result := mapstr.M{ + "total_size": mapstr.M{ "bytes": res.sizeMB * 1048576, }, - "active_size": common.MapStr{ + "active_size": mapstr.M{ "bytes": res.activeSizeMB * 1048576, }, "backup_time": res.backupTime, - "since_last_checkpoint": common.MapStr{ + "since_last_checkpoint": mapstr.M{ "bytes": res.sinceLastCheckpointMB * 1048576, }, - "recovery_size": common.MapStr{ + "recovery_size": mapstr.M{ "bytes": res.recoverySizeMB, }, } diff --git a/x-pack/metricbeat/module/oracle/performance/buffer_cache_hit_ratio.go b/x-pack/metricbeat/module/oracle/performance/buffer_cache_hit_ratio.go index a480d818e9a..0b0fb8ad41c 100644 --- a/x-pack/metricbeat/module/oracle/performance/buffer_cache_hit_ratio.go +++ b/x-pack/metricbeat/module/oracle/performance/buffer_cache_hit_ratio.go @@ -10,8 +10,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/metricbeat/module/oracle" + "github.com/elastic/elastic-agent-libs/mapstr" ) type bufferCacheHitRatio struct { @@ -53,12 +53,12 @@ FROM V$BUFFER_POOL_STATISTICS`) } // addTempFreeSpaceData is specific to the TEMP Tablespace. -func (m *MetricSet) addBufferCacheRatioData(bs []bufferCacheHitRatio) map[string]common.MapStr { - out := make(map[string]common.MapStr) +func (m *MetricSet) addBufferCacheRatioData(bs []bufferCacheHitRatio) map[string]mapstr.M { + out := make(map[string]mapstr.M) for _, bufferCacheHitRatio := range bs { if _, found := out[bufferCacheHitRatio.name.String]; !found { - out[bufferCacheHitRatio.name.String] = common.MapStr{} + out[bufferCacheHitRatio.name.String] = mapstr.M{} } _, _ = out[bufferCacheHitRatio.name.String].Put("buffer_pool", bufferCacheHitRatio.name.String) diff --git a/x-pack/metricbeat/module/oracle/performance/cursors.go b/x-pack/metricbeat/module/oracle/performance/cursors.go index e9da4ef0007..ebc5a1df61d 100644 --- a/x-pack/metricbeat/module/oracle/performance/cursors.go +++ b/x-pack/metricbeat/module/oracle/performance/cursors.go @@ -10,8 +10,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/metricbeat/module/oracle" + "github.com/elastic/elastic-agent-libs/mapstr" ) type cursorsByUsernameAndMachine struct { @@ -84,11 +84,11 @@ func (e *performanceExtractor) cursorsByUsernameAndMachine(ctx context.Context) return results, nil } -func (m *MetricSet) addCursorByUsernameAndMachine(cs []cursorsByUsernameAndMachine) []common.MapStr { - out := make([]common.MapStr, 0) +func (m *MetricSet) addCursorByUsernameAndMachine(cs []cursorsByUsernameAndMachine) []mapstr.M { + out := make([]mapstr.M, 0) for _, v := range cs { - ms := common.MapStr{} + ms := mapstr.M{} oracle.SetSqlValue(m.Logger(), ms, "username", &oracle.StringValue{NullString: v.username}) oracle.SetSqlValue(m.Logger(), ms, "machine", &oracle.StringValue{NullString: v.machine}) @@ -136,8 +136,8 @@ func (e *performanceExtractor) totalCursors(ctx context.Context) (*totalCursors, return &dest, nil } -func (m *MetricSet) addCursorData(cs *totalCursors) common.MapStr { - out := make(common.MapStr) +func (m *MetricSet) addCursorData(cs *totalCursors) mapstr.M { + out := make(mapstr.M) oracle.SetSqlValue(m.Logger(), out, "cursors.opened.total", &oracle.Int64Value{NullInt64: cs.totalCursors}) oracle.SetSqlValue(m.Logger(), out, "cursors.opened.current", &oracle.Int64Value{NullInt64: cs.currentCursors}) diff --git a/x-pack/metricbeat/module/oracle/performance/library_cache.go b/x-pack/metricbeat/module/oracle/performance/library_cache.go index 076874f3b1b..fec3b1b4a7f 100644 --- a/x-pack/metricbeat/module/oracle/performance/library_cache.go +++ b/x-pack/metricbeat/module/oracle/performance/library_cache.go @@ -10,8 +10,8 @@ import ( "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/x-pack/metricbeat/module/oracle" + "github.com/elastic/elastic-agent-libs/mapstr" ) type libraryCache struct { @@ -53,8 +53,8 @@ func (e *performanceExtractor) libraryCache(ctx context.Context) ([]libraryCache return results, nil } -func (m *MetricSet) addLibraryCacheData(ls []libraryCache) common.MapStr { - out := common.MapStr{} +func (m *MetricSet) addLibraryCacheData(ls []libraryCache) mapstr.M { + out := mapstr.M{} for _, v := range ls { if v.name.Valid { diff --git a/x-pack/metricbeat/module/oracle/performance/metricset_test.go b/x-pack/metricbeat/module/oracle/performance/metricset_test.go index 628ece13626..b4ae7129809 100644 --- a/x-pack/metricbeat/module/oracle/performance/metricset_test.go +++ b/x-pack/metricbeat/module/oracle/performance/metricset_test.go @@ -11,10 +11,10 @@ import ( _ "github.com/godror/godror" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/tests/compose" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" "github.com/elastic/beats/v7/x-pack/metricbeat/module/oracle" + "github.com/elastic/elastic-agent-libs/mapstr" ) func TestData(t *testing.T) { @@ -22,8 +22,8 @@ func TestData(t *testing.T) { f := mbtest.NewReportingMetricSetV2WithContext(t, getConfig(r.Host())) - findKey := func(key string) func(common.MapStr) bool { - return func(in common.MapStr) bool { + findKey := func(key string) func(mapstr.M) bool { + return func(in mapstr.M) bool { _, err := in.GetValue("oracle.performance." + key) return err == nil } diff --git a/x-pack/metricbeat/module/oracle/sql.go b/x-pack/metricbeat/module/oracle/sql.go index fb0e7551f41..1a56deead3c 100644 --- a/x-pack/metricbeat/module/oracle/sql.go +++ b/x-pack/metricbeat/module/oracle/sql.go @@ -8,12 +8,12 @@ import ( "database/sql" "errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" + "github.com/elastic/elastic-agent-libs/mapstr" ) // SetSqlValueWithParentKey avoid setting an invalid empty value value on Metricbeat event -func SetSqlValueWithParentKey(logger *logp.Logger, output map[string]common.MapStr, parentTargetFieldName, targetFieldName string, v SqlValue) { +func SetSqlValueWithParentKey(logger *logp.Logger, output map[string]mapstr.M, parentTargetFieldName, targetFieldName string, v SqlValue) { ms, ok := output[parentTargetFieldName] if !ok { logger.Debug(errors.New("no parent key found")) @@ -24,7 +24,7 @@ func SetSqlValueWithParentKey(logger *logp.Logger, output map[string]common.MapS } //SetSqlValue avoid setting an invalid empty value value on Metricbeat event -func SetSqlValue(logger *logp.Logger, output common.MapStr, targetFieldName string, v SqlValue) { +func SetSqlValue(logger *logp.Logger, output mapstr.M, targetFieldName string, v SqlValue) { if v.isValid() { if _, err := output.Put(targetFieldName, v.Value()); err != nil { logger.Debug(errors.New("error trying to set value on common.Mapstr")) diff --git a/x-pack/metricbeat/module/oracle/tablespace/data.go b/x-pack/metricbeat/module/oracle/tablespace/data.go index ef51fe823a7..526c65e85b7 100644 --- a/x-pack/metricbeat/module/oracle/tablespace/data.go +++ b/x-pack/metricbeat/module/oracle/tablespace/data.go @@ -8,10 +8,10 @@ import ( "context" "github.com/elastic/beats/v7/x-pack/metricbeat/module/oracle" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/mb" ) @@ -38,8 +38,8 @@ func (m *MetricSet) extract(ctx context.Context, extractor tablespaceExtractMeth // transform is the T of an ETL (refer to the 'extract' method above if you need to see the origin). Transforms the data // to create a Kibana/Elasticsearch friendly JSON. Data from Oracle is pretty fragmented by design so a lot of data // was necessary. Data is organized by Tablespace entity (Tablespaces might contain one or more data files) -func (m *MetricSet) transform(in *extractedData) (out map[string]common.MapStr) { - out = make(map[string]common.MapStr, 0) +func (m *MetricSet) transform(in *extractedData) (out map[string]mapstr.M) { + out = make(map[string]mapstr.M, 0) for _, dataFile := range in.dataFiles { m.addDataFileData(&dataFile, out) @@ -68,7 +68,7 @@ func (m *MetricSet) extractAndTransform(ctx context.Context) ([]mb.Event, error) } // addTempFreeSpaceData is specific to the TEMP Tablespace. -func (m *MetricSet) addTempFreeSpaceData(tempFreeSpaces []tempFreeSpace, out map[string]common.MapStr) { +func (m *MetricSet) addTempFreeSpaceData(tempFreeSpaces []tempFreeSpace, out map[string]mapstr.M) { for key, cm := range out { val, err := cm.GetValue("name") if err != nil { @@ -88,7 +88,7 @@ func (m *MetricSet) addTempFreeSpaceData(tempFreeSpaces []tempFreeSpace, out map } // addUsedAndFreeSpaceData is specific to all Tablespaces but TEMP -func (m *MetricSet) addUsedAndFreeSpaceData(freeSpaces []usedAndFreeSpace, out map[string]common.MapStr) { +func (m *MetricSet) addUsedAndFreeSpaceData(freeSpaces []usedAndFreeSpace, out map[string]mapstr.M) { for key, cm := range out { val, err := cm.GetValue("name") if err != nil { @@ -109,9 +109,9 @@ func (m *MetricSet) addUsedAndFreeSpaceData(freeSpaces []usedAndFreeSpace, out m } // addDataFileData is a specific data file which generates a JSON output. -func (m *MetricSet) addDataFileData(d *dataFile, output map[string]common.MapStr) { +func (m *MetricSet) addDataFileData(d *dataFile, output map[string]mapstr.M) { if _, found := output[d.hash()]; !found { - output[d.hash()] = common.MapStr{} + output[d.hash()] = mapstr.M{} } _, _ = output[d.hash()].Put("name", d.eventKey()) diff --git a/x-pack/metricbeat/module/prometheus/collector/data.go b/x-pack/metricbeat/module/prometheus/collector/data.go index 1dd83a82980..382bbfa3f8a 100644 --- a/x-pack/metricbeat/module/prometheus/collector/data.go +++ b/x-pack/metricbeat/module/prometheus/collector/data.go @@ -8,11 +8,11 @@ import ( "math" "strconv" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/prometheus/collector" + "github.com/elastic/elastic-agent-libs/mapstr" dto "github.com/prometheus/client_model/go" ) @@ -67,7 +67,7 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr name := *mf.Name metrics := mf.Metric for _, metric := range metrics { - labels := common.MapStr{} + labels := mapstr.M{} if len(metric.Label) != 0 { for _, label := range metric.Label { @@ -81,7 +81,7 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr if counter != nil { if !math.IsNaN(counter.GetValue()) && !math.IsInf(counter.GetValue(), 0) { events = append(events, collector.PromEvent{ - Data: common.MapStr{ + Data: mapstr.M{ name: g.rateCounterFloat64(name, labels, counter.GetValue()), }, Labels: labels, @@ -93,8 +93,8 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr if gauge != nil { if !math.IsNaN(gauge.GetValue()) && !math.IsInf(gauge.GetValue(), 0) { events = append(events, collector.PromEvent{ - Data: common.MapStr{ - name: common.MapStr{ + Data: mapstr.M{ + name: mapstr.M{ "value": gauge.GetValue(), }, }, @@ -107,7 +107,7 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr if summary != nil { if !math.IsNaN(summary.GetSampleSum()) && !math.IsInf(summary.GetSampleSum(), 0) { events = append(events, collector.PromEvent{ - Data: common.MapStr{ + Data: mapstr.M{ name + "_sum": g.rateCounterFloat64(name, labels, summary.GetSampleSum()), name + "_count": g.rateCounterUint64(name, labels, summary.GetSampleCount()), }, @@ -123,8 +123,8 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr quantileLabels := labels.Clone() quantileLabels["quantile"] = strconv.FormatFloat(quantile.GetQuantile(), 'f', -1, 64) events = append(events, collector.PromEvent{ - Data: common.MapStr{ - name: common.MapStr{ + Data: mapstr.M{ + name: mapstr.M{ "value": quantile.GetValue(), }, }, @@ -136,8 +136,8 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr histogram := metric.GetHistogram() if histogram != nil { events = append(events, collector.PromEvent{ - Data: common.MapStr{ - name: common.MapStr{ + Data: mapstr.M{ + name: mapstr.M{ "histogram": PromHistogramToES(g.counterCache, name, labels, histogram), }, }, @@ -153,8 +153,8 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr if untyped != nil { if !math.IsNaN(untyped.GetValue()) && !math.IsInf(untyped.GetValue(), 0) { events = append(events, collector.PromEvent{ - Data: common.MapStr{ - name: common.MapStr{ + Data: mapstr.M{ + name: mapstr.M{ "value": untyped.GetValue(), }, }, @@ -167,8 +167,8 @@ func (g *typedGenerator) GeneratePromEvents(mf *dto.MetricFamily) []collector.Pr } // rateCounterUint64 fills a counter value and optionally adds the rate if rate_counters is enabled -func (g *typedGenerator) rateCounterUint64(name string, labels common.MapStr, value uint64) common.MapStr { - d := common.MapStr{ +func (g *typedGenerator) rateCounterUint64(name string, labels mapstr.M, value uint64) mapstr.M { + d := mapstr.M{ "counter": value, } @@ -180,8 +180,8 @@ func (g *typedGenerator) rateCounterUint64(name string, labels common.MapStr, va } // rateCounterFloat64 fills a counter value and optionally adds the rate if rate_counters is enabled -func (g *typedGenerator) rateCounterFloat64(name string, labels common.MapStr, value float64) common.MapStr { - d := common.MapStr{ +func (g *typedGenerator) rateCounterFloat64(name string, labels mapstr.M, value float64) mapstr.M { + d := mapstr.M{ "counter": value, } diff --git a/x-pack/metricbeat/module/prometheus/collector/histogram.go b/x-pack/metricbeat/module/prometheus/collector/histogram.go index 1d23264a2fb..ebae19c09ca 100644 --- a/x-pack/metricbeat/module/prometheus/collector/histogram.go +++ b/x-pack/metricbeat/module/prometheus/collector/histogram.go @@ -8,9 +8,9 @@ import ( "fmt" "math" - "github.com/elastic/beats/v7/libbeat/common" - dto "github.com/prometheus/client_model/go" + + "github.com/elastic/elastic-agent-libs/mapstr" ) // PromHistogramToES takes a Prometheus histogram and converts it to an ES histogram: @@ -27,7 +27,7 @@ import ( // - undoing counters accumulation for each bucket (counts) // // https://www.elastic.co/guide/en/elasticsearch/reference/master/histogram.html -func PromHistogramToES(cc CounterCache, name string, labels common.MapStr, histogram *dto.Histogram) common.MapStr { +func PromHistogramToES(cc CounterCache, name string, labels mapstr.M, histogram *dto.Histogram) mapstr.M { var values []float64 var counts []uint64 @@ -71,7 +71,7 @@ func PromHistogramToES(cc CounterCache, name string, labels common.MapStr, histo prevCount = bucket.GetCumulativeCount() } - res := common.MapStr{ + res := mapstr.M{ "values": values, "counts": counts, } diff --git a/x-pack/metricbeat/module/prometheus/collector/histogram_test.go b/x-pack/metricbeat/module/prometheus/collector/histogram_test.go index 6db36a3aa9f..d85ed673dd1 100644 --- a/x-pack/metricbeat/module/prometheus/collector/histogram_test.go +++ b/x-pack/metricbeat/module/prometheus/collector/histogram_test.go @@ -16,7 +16,7 @@ import ( dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TestPromHistogramToES tests that calling PromHistogramToES multiple @@ -24,7 +24,7 @@ import ( func TestPromHistogramToES(t *testing.T) { type sample struct { histogram dto.Histogram - expected common.MapStr + expected mapstr.M } cases := map[string]struct { @@ -43,7 +43,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0}, "values": []float64{0.495}, }, @@ -63,7 +63,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0}, "values": []float64{0.495}, }, @@ -79,7 +79,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{2}, "values": []float64{0.495}, }, @@ -99,7 +99,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0}, "values": []float64{0.495}, }, @@ -120,7 +120,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{2, 0}, "values": []float64{0.495, 5.49}, }, @@ -140,7 +140,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{1, 1}, "values": []float64{0.495, 5.49}, }, @@ -160,7 +160,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0, 1}, "values": []float64{0.495, 5.49}, }, @@ -180,7 +180,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0}, "values": []float64{0.495}, }, @@ -201,7 +201,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0, 2}, "values": []float64{0.045, 0.54}, }, @@ -221,7 +221,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{1, 1}, "values": []float64{0.045, 0.54}, }, @@ -241,7 +241,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{1, 0}, "values": []float64{0.045, 0.54}, }, @@ -265,7 +265,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0, 0}, "values": []float64{0.045, 0.54}, }, @@ -290,7 +290,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{1, 0, 1}, "values": []float64{0.045, 0.29000000000000004, 0.74}, }, @@ -314,7 +314,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{1, 1, 1}, "values": []float64{0.045, 0.29000000000000004, 0.74}, }, @@ -338,7 +338,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{1, 0, 1}, "values": []float64{0.045, 0.29000000000000004, 0.74}, }, @@ -362,7 +362,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{0, 0}, "values": []float64{0.045, 0.54}, }, @@ -382,7 +382,7 @@ func TestPromHistogramToES(t *testing.T) { }, }, }, - expected: common.MapStr{ + expected: mapstr.M{ "counts": []uint64{2, 0}, "values": []float64{0.045, 0.54}, }, @@ -392,7 +392,7 @@ func TestPromHistogramToES(t *testing.T) { } metricName := "somemetric" - labels := common.MapStr{} + labels := mapstr.M{} for title, c := range cases { t.Run(title, func(t *testing.T) { diff --git a/x-pack/metricbeat/module/prometheus/remote_write/data.go b/x-pack/metricbeat/module/prometheus/remote_write/data.go index 2258aba0270..72b007ea083 100644 --- a/x-pack/metricbeat/module/prometheus/remote_write/data.go +++ b/x-pack/metricbeat/module/prometheus/remote_write/data.go @@ -16,13 +16,13 @@ import ( dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/model" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/libbeat/logp" p "github.com/elastic/beats/v7/metricbeat/helper/prometheus" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/metricbeat/module/prometheus/remote_write" "github.com/elastic/beats/v7/x-pack/metricbeat/module/prometheus/collector" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -34,7 +34,7 @@ const ( type histogram struct { timestamp time.Time buckets []*dto.Bucket - labels common.MapStr + labels mapstr.M metricName string } @@ -98,12 +98,12 @@ func (g *remoteWriteTypedGenerator) Stop() { // 3. if metrics of histogram type then it is converted to ES histogram // 4. metrics with the same set of labels are grouped into same events func (g remoteWriteTypedGenerator) GenerateEvents(metrics model.Samples) map[string]mb.Event { - var data common.MapStr + var data mapstr.M histograms := map[string]histogram{} eventList := map[string]mb.Event{} for _, metric := range metrics { - labels := common.MapStr{} + labels := mapstr.M{} if metric == nil { continue @@ -131,7 +131,7 @@ func (g remoteWriteTypedGenerator) GenerateEvents(metrics model.Samples) map[str // join metrics with same labels in a single event if _, ok := eventList[labelsHash]; !ok { eventList[labelsHash] = mb.Event{ - ModuleFields: common.MapStr{}, + ModuleFields: mapstr.M{}, Timestamp: metric.Timestamp.Time(), } @@ -148,12 +148,12 @@ func (g remoteWriteTypedGenerator) GenerateEvents(metrics model.Samples) map[str e := eventList[labelsHash] switch promType { case counterType: - data = common.MapStr{ + data = mapstr.M{ name: g.rateCounterFloat64(name, labels, val), } case otherType: - data = common.MapStr{ - name: common.MapStr{ + data = mapstr.M{ + name: mapstr.M{ "value": val, }, } @@ -193,8 +193,8 @@ func (g remoteWriteTypedGenerator) GenerateEvents(metrics model.Samples) map[str } // rateCounterUint64 fills a counter value and optionally adds the rate if rate_counters is enabled -func (g *remoteWriteTypedGenerator) rateCounterUint64(name string, labels common.MapStr, value uint64) common.MapStr { - d := common.MapStr{ +func (g *remoteWriteTypedGenerator) rateCounterUint64(name string, labels mapstr.M, value uint64) mapstr.M { + d := mapstr.M{ "counter": value, } @@ -206,8 +206,8 @@ func (g *remoteWriteTypedGenerator) rateCounterUint64(name string, labels common } // rateCounterFloat64 fills a counter value and optionally adds the rate if rate_counters is enabled -func (g *remoteWriteTypedGenerator) rateCounterFloat64(name string, labels common.MapStr, value float64) common.MapStr { - d := common.MapStr{ +func (g *remoteWriteTypedGenerator) rateCounterFloat64(name string, labels mapstr.M, value float64) mapstr.M { + d := mapstr.M{ "counter": value, } if g.rateCounters { @@ -223,7 +223,7 @@ func (g *remoteWriteTypedGenerator) processPromHistograms(eventList map[string]m labelsHash := histogram.labels.String() + histogram.timestamp.String() if _, ok := eventList[labelsHash]; !ok { eventList[labelsHash] = mb.Event{ - ModuleFields: common.MapStr{}, + ModuleFields: mapstr.M{}, Timestamp: histogram.timestamp, } @@ -239,8 +239,8 @@ func (g *remoteWriteTypedGenerator) processPromHistograms(eventList map[string]m Bucket: histogram.buckets, } name := strings.TrimSuffix(histogram.metricName, "_bucket") - data := common.MapStr{ - name: common.MapStr{ + data := mapstr.M{ + name: mapstr.M{ "histogram": collector.PromHistogramToES(g.counterCache, histogram.metricName, histogram.labels, &hist), }, } @@ -249,7 +249,7 @@ func (g *remoteWriteTypedGenerator) processPromHistograms(eventList map[string]m } // findMetricType evaluates the type of the metric by check the metricname format in order to handle it properly -func (g *remoteWriteTypedGenerator) findMetricType(metricName string, labels common.MapStr) string { +func (g *remoteWriteTypedGenerator) findMetricType(metricName string, labels mapstr.M) string { leLabel := false if _, ok := labels["le"]; ok { leLabel = true diff --git a/x-pack/metricbeat/module/prometheus/remote_write/remote_write_test.go b/x-pack/metricbeat/module/prometheus/remote_write/remote_write_test.go index 1cd2afc8bdb..150520e50d2 100644 --- a/x-pack/metricbeat/module/prometheus/remote_write/remote_write_test.go +++ b/x-pack/metricbeat/module/prometheus/remote_write/remote_write_test.go @@ -14,9 +14,9 @@ import ( "github.com/prometheus/common/model" "github.com/stretchr/testify/assert" - "github.com/elastic/beats/v7/libbeat/common" p "github.com/elastic/beats/v7/metricbeat/helper/prometheus" xcollector "github.com/elastic/beats/v7/x-pack/metricbeat/module/prometheus/collector" + "github.com/elastic/elastic-agent-libs/mapstr" ) // TestGenerateEventsCounter tests counter simple cases @@ -30,7 +30,7 @@ func TestGenerateEventsCounter(t *testing.T) { } g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "listener_name": model.LabelValue("http"), } // first fetch @@ -46,8 +46,8 @@ func TestGenerateEventsCounter(t *testing.T) { } events := g.GenerateEvents(metrics) - expected := common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected := mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(42), "rate": float64(0), }, @@ -71,8 +71,8 @@ func TestGenerateEventsCounter(t *testing.T) { } events = g.GenerateEvents(metrics) - expected = common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected = mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(45), "rate": float64(3), }, @@ -96,7 +96,7 @@ func TestGenerateEventsCounterSameLabels(t *testing.T) { } g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "listener_name": model.LabelValue("http"), } @@ -121,12 +121,12 @@ func TestGenerateEventsCounterSameLabels(t *testing.T) { } events := g.GenerateEvents(metrics) - expected := common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected := mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(42), "rate": float64(0), }, - "net_conntrack_listener_conn_panic_total": common.MapStr{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(43), "rate": float64(0), }, @@ -158,12 +158,12 @@ func TestGenerateEventsCounterSameLabels(t *testing.T) { } events = g.GenerateEvents(metrics) - expected = common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected = mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(45), "rate": float64(3), }, - "net_conntrack_listener_conn_panic_total": common.MapStr{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(47), "rate": float64(4), }, @@ -188,10 +188,10 @@ func TestGenerateEventsCounterDifferentLabels(t *testing.T) { g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "listener_name": model.LabelValue("http"), } - labels2 := common.MapStr{ + labels2 := mapstr.M{ "listener_name": model.LabelValue("http"), "device": model.LabelValue("eth0"), } @@ -226,19 +226,19 @@ func TestGenerateEventsCounterDifferentLabels(t *testing.T) { } events := g.GenerateEvents(metrics) - expected1 := common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected1 := mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(42), "rate": float64(0), }, - "net_conntrack_listener_conn_panic_total": common.MapStr{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(43), "rate": float64(0), }, "labels": labels, } - expected2 := common.MapStr{ - "net_conntrack_listener_conn_panic_total": common.MapStr{ + expected2 := mapstr.M{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(44), "rate": float64(0), }, @@ -281,19 +281,19 @@ func TestGenerateEventsCounterDifferentLabels(t *testing.T) { } events = g.GenerateEvents(metrics) - expected1 = common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected1 = mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(45), "rate": float64(3), }, - "net_conntrack_listener_conn_panic_total": common.MapStr{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(47), "rate": float64(4), }, "labels": labels, } - expected2 = common.MapStr{ - "net_conntrack_listener_conn_panic_total": common.MapStr{ + expected2 = mapstr.M{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(50), "rate": float64(6), }, @@ -319,10 +319,10 @@ func TestGenerateEventsGaugeDifferentLabels(t *testing.T) { } g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "listener_name": model.LabelValue("http"), } - labels2 := common.MapStr{ + labels2 := mapstr.M{ "listener_name": model.LabelValue("http"), "device": model.LabelValue("eth0"), } @@ -366,23 +366,23 @@ func TestGenerateEventsGaugeDifferentLabels(t *testing.T) { } events := g.GenerateEvents(metrics) - expected1 := common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected1 := mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(42), "rate": float64(0), }, - "net_conntrack_listener_conn_panic_total": common.MapStr{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(43), "rate": float64(0), }, "labels": labels, } - expected2 := common.MapStr{ - "net_conntrack_listener_conn_panic_total": common.MapStr{ + expected2 := mapstr.M{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(44), "rate": float64(0), }, - "net_conntrack_listener_conn_open": common.MapStr{ + "net_conntrack_listener_conn_open": mapstr.M{ "value": float64(49), }, "labels": labels2, @@ -433,23 +433,23 @@ func TestGenerateEventsGaugeDifferentLabels(t *testing.T) { } events = g.GenerateEvents(metrics) - expected1 = common.MapStr{ - "net_conntrack_listener_conn_closed_total": common.MapStr{ + expected1 = mapstr.M{ + "net_conntrack_listener_conn_closed_total": mapstr.M{ "counter": float64(45), "rate": float64(3), }, - "net_conntrack_listener_conn_panic_total": common.MapStr{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(47), "rate": float64(4), }, "labels": labels, } - expected2 = common.MapStr{ - "net_conntrack_listener_conn_panic_total": common.MapStr{ + expected2 = mapstr.M{ + "net_conntrack_listener_conn_panic_total": mapstr.M{ "counter": float64(50), "rate": float64(6), }, - "net_conntrack_listener_conn_open": common.MapStr{ + "net_conntrack_listener_conn_open": mapstr.M{ "value": float64(59), }, "labels": labels2, @@ -475,15 +475,15 @@ func TestGenerateEventsQuantilesDifferentLabels(t *testing.T) { g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "runtime": model.LabelValue("linux"), "quantile": model.LabelValue("0.25"), } - labels2 := common.MapStr{ + labels2 := mapstr.M{ "runtime": model.LabelValue("linux"), "quantile": model.LabelValue("0.50"), } - labels3 := common.MapStr{ + labels3 := mapstr.M{ "runtime": model.LabelValue("linux"), } @@ -535,27 +535,27 @@ func TestGenerateEventsQuantilesDifferentLabels(t *testing.T) { } events := g.GenerateEvents(metrics) - expected := common.MapStr{ - "go_gc_duration_seconds": common.MapStr{ + expected := mapstr.M{ + "go_gc_duration_seconds": mapstr.M{ "value": float64(42), }, - "go_gc_duration_seconds_2": common.MapStr{ + "go_gc_duration_seconds_2": mapstr.M{ "value": float64(46), }, "labels": labels, } - expected2 := common.MapStr{ - "go_gc_duration_seconds": common.MapStr{ + expected2 := mapstr.M{ + "go_gc_duration_seconds": mapstr.M{ "value": float64(43), }, "labels": labels2, } - expected3 := common.MapStr{ - "go_gc_duration_seconds_count": common.MapStr{ + expected3 := mapstr.M{ + "go_gc_duration_seconds_count": mapstr.M{ "counter": float64(45), "rate": float64(0), }, - "go_gc_duration_seconds_sum": common.MapStr{ + "go_gc_duration_seconds_sum": mapstr.M{ "counter": float64(44), "rate": float64(0), }, @@ -618,27 +618,27 @@ func TestGenerateEventsQuantilesDifferentLabels(t *testing.T) { } events = g.GenerateEvents(metrics) - expected = common.MapStr{ - "go_gc_duration_seconds": common.MapStr{ + expected = mapstr.M{ + "go_gc_duration_seconds": mapstr.M{ "value": float64(52), }, - "go_gc_duration_seconds_2": common.MapStr{ + "go_gc_duration_seconds_2": mapstr.M{ "value": float64(56), }, "labels": labels, } - expected2 = common.MapStr{ - "go_gc_duration_seconds": common.MapStr{ + expected2 = mapstr.M{ + "go_gc_duration_seconds": mapstr.M{ "value": float64(53), }, "labels": labels2, } - expected3 = common.MapStr{ - "go_gc_duration_seconds_count": common.MapStr{ + expected3 = mapstr.M{ + "go_gc_duration_seconds_count": mapstr.M{ "counter": float64(55), "rate": float64(10), }, - "go_gc_duration_seconds_sum": common.MapStr{ + "go_gc_duration_seconds_sum": mapstr.M{ "counter": float64(54), "rate": float64(10), }, @@ -666,10 +666,10 @@ func TestGenerateEventsHistogramsDifferentLabels(t *testing.T) { } g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "runtime": model.LabelValue("linux"), } - labels2 := common.MapStr{ + labels2 := mapstr.M{ "runtime": model.LabelValue("darwin"), } @@ -809,49 +809,49 @@ func TestGenerateEventsHistogramsDifferentLabels(t *testing.T) { } events := g.GenerateEvents(metrics) - expected := common.MapStr{ - "http_request_duration_seconds": common.MapStr{ - "histogram": common.MapStr{ + expected := mapstr.M{ + "http_request_duration_seconds": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(0.125), float64(0.375), float64(0.75)}, "counts": []uint64{uint64(0), uint64(0), uint64(0)}, }, }, - "http_request_duration_seconds_sum": common.MapStr{ + "http_request_duration_seconds_sum": mapstr.M{ "counter": float64(45), "rate": float64(0), }, - "http_request_duration_seconds_count": common.MapStr{ + "http_request_duration_seconds_count": mapstr.M{ "counter": float64(46), "rate": float64(0), }, - "http_request_bytes": common.MapStr{ - "histogram": common.MapStr{ + "http_request_bytes": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(0.125), float64(0.375), float64(0.75)}, "counts": []uint64{uint64(0), uint64(0), uint64(0)}, }, }, - "http_request_bytes_sum": common.MapStr{ + "http_request_bytes_sum": mapstr.M{ "counter": float64(55), "rate": float64(0), }, - "http_request_bytes_count": common.MapStr{ + "http_request_bytes_count": mapstr.M{ "counter": float64(56), "rate": float64(0), }, "labels": labels, } - expected2 := common.MapStr{ - "http_request_bytes": common.MapStr{ - "histogram": common.MapStr{ + expected2 := mapstr.M{ + "http_request_bytes": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(0.125), float64(0.375), float64(0.75)}, "counts": []uint64{uint64(0), uint64(0), uint64(0)}, }, }, - "http_request_bytes_sum": common.MapStr{ + "http_request_bytes_sum": mapstr.M{ "counter": float64(65), "rate": float64(0), }, - "http_request_bytes_count": common.MapStr{ + "http_request_bytes_count": mapstr.M{ "counter": float64(66), "rate": float64(0), }, @@ -1000,49 +1000,49 @@ func TestGenerateEventsHistogramsDifferentLabels(t *testing.T) { } events = g.GenerateEvents(metrics) - expected = common.MapStr{ - "http_request_duration_seconds": common.MapStr{ - "histogram": common.MapStr{ + expected = mapstr.M{ + "http_request_duration_seconds": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(0.125), float64(0.375), float64(0.75)}, "counts": []uint64{uint64(100), uint64(0), uint64(0)}, }, }, - "http_request_duration_seconds_sum": common.MapStr{ + "http_request_duration_seconds_sum": mapstr.M{ "counter": float64(145), "rate": float64(100), }, - "http_request_duration_seconds_count": common.MapStr{ + "http_request_duration_seconds_count": mapstr.M{ "counter": float64(146), "rate": float64(100), }, - "http_request_bytes": common.MapStr{ - "histogram": common.MapStr{ + "http_request_bytes": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(0.125), float64(0.375), float64(0.75)}, "counts": []uint64{uint64(200), uint64(0), uint64(0)}, }, }, - "http_request_bytes_sum": common.MapStr{ + "http_request_bytes_sum": mapstr.M{ "counter": float64(255), "rate": float64(200), }, - "http_request_bytes_count": common.MapStr{ + "http_request_bytes_count": mapstr.M{ "counter": float64(256), "rate": float64(200), }, "labels": labels, } - expected2 = common.MapStr{ - "http_request_bytes": common.MapStr{ - "histogram": common.MapStr{ + expected2 = mapstr.M{ + "http_request_bytes": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(0.125), float64(0.375), float64(0.75)}, "counts": []uint64{uint64(300), uint64(0), uint64(0)}, }, }, - "http_request_bytes_sum": common.MapStr{ + "http_request_bytes_sum": mapstr.M{ "counter": float64(365), "rate": float64(300), }, - "http_request_bytes_count": common.MapStr{ + "http_request_bytes_count": mapstr.M{ "counter": float64(366), "rate": float64(300), }, @@ -1074,7 +1074,7 @@ func TestGenerateEventsCounterWithDefinedPattern(t *testing.T) { g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "listener_name": model.LabelValue("http"), } @@ -1091,8 +1091,8 @@ func TestGenerateEventsCounterWithDefinedPattern(t *testing.T) { } events := g.GenerateEvents(metrics) - expected := common.MapStr{ - "net_conntrack_listener_conn_closed_mycounter": common.MapStr{ + expected := mapstr.M{ + "net_conntrack_listener_conn_closed_mycounter": mapstr.M{ "counter": float64(42), "rate": float64(0), }, @@ -1116,8 +1116,8 @@ func TestGenerateEventsCounterWithDefinedPattern(t *testing.T) { } events = g.GenerateEvents(metrics) - expected = common.MapStr{ - "net_conntrack_listener_conn_closed_mycounter": common.MapStr{ + expected = mapstr.M{ + "net_conntrack_listener_conn_closed_mycounter": mapstr.M{ "counter": float64(45), "rate": float64(3), }, @@ -1147,7 +1147,7 @@ func TestGenerateEventsHistogramWithDefinedPattern(t *testing.T) { g.counterCache.Start() timestamp := model.Time(424242) - labels := common.MapStr{ + labels := mapstr.M{ "listener_name": model.LabelValue("http"), } @@ -1165,9 +1165,9 @@ func TestGenerateEventsHistogramWithDefinedPattern(t *testing.T) { } events := g.GenerateEvents(metrics) - expected := common.MapStr{ - "net_conntrack_listener_conn_closed_myhistogram": common.MapStr{ - "histogram": common.MapStr{ + expected := mapstr.M{ + "net_conntrack_listener_conn_closed_myhistogram": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(10)}, "counts": []uint64{uint64(0)}, }, @@ -1193,9 +1193,9 @@ func TestGenerateEventsHistogramWithDefinedPattern(t *testing.T) { } events = g.GenerateEvents(metrics) - expected = common.MapStr{ - "net_conntrack_listener_conn_closed_myhistogram": common.MapStr{ - "histogram": common.MapStr{ + expected = mapstr.M{ + "net_conntrack_listener_conn_closed_myhistogram": mapstr.M{ + "histogram": mapstr.M{ "values": []float64{float64(10)}, "counts": []uint64{uint64(3)}, }, diff --git a/x-pack/metricbeat/module/sql/query/query.go b/x-pack/metricbeat/module/sql/query/query.go index 5d43d7f785f..79545472024 100644 --- a/x-pack/metricbeat/module/sql/query/query.go +++ b/x-pack/metricbeat/module/sql/query/query.go @@ -11,10 +11,10 @@ import ( "github.com/jmoiron/sqlx" "github.com/pkg/errors" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cfgwarn" "github.com/elastic/beats/v7/metricbeat/helper/sql" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // represents the response format of the query @@ -107,10 +107,10 @@ func (m *MetricSet) Fetch(ctx context.Context, report mb.ReporterV2) error { return nil } -func (m *MetricSet) getEvent(ms common.MapStr) mb.Event { +func (m *MetricSet) getEvent(ms mapstr.M) mb.Event { return mb.Event{ - RootFields: common.MapStr{ - "sql": common.MapStr{ + RootFields: mapstr.M{ + "sql": mapstr.M{ "driver": m.Driver, "query": m.Query, "metrics": getMetrics(ms), @@ -119,12 +119,12 @@ func (m *MetricSet) getEvent(ms common.MapStr) mb.Event { } } -func getMetrics(ms common.MapStr) (ret common.MapStr) { - ret = common.MapStr{} +func getMetrics(ms mapstr.M) (ret mapstr.M) { + ret = mapstr.M{} - numericMetrics := common.MapStr{} - stringMetrics := common.MapStr{} - boolMetrics := common.MapStr{} + numericMetrics := mapstr.M{} + stringMetrics := mapstr.M{} + boolMetrics := mapstr.M{} for k, v := range ms { switch v.(type) { diff --git a/x-pack/metricbeat/module/statsd/server/data.go b/x-pack/metricbeat/module/statsd/server/data.go index 9935d4db6e5..5140b0dfeb0 100644 --- a/x-pack/metricbeat/module/statsd/server/data.go +++ b/x-pack/metricbeat/module/statsd/server/data.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper/server" + "github.com/elastic/elastic-agent-libs/mapstr" ) var errInvalidPacket = errors.New("invalid statsd packet") @@ -100,7 +101,7 @@ func parse(b []byte) ([]statsdMetric, error) { return metrics, nil } -func eventMapping(metricName string, metricValue interface{}, metricSetFields common.MapStr, mappings map[string]StatsdMapping) { +func eventMapping(metricName string, metricValue interface{}, metricSetFields mapstr.M, mappings map[string]StatsdMapping) { if len(mappings) == 0 { metricSetFields[common.DeDot(metricName)] = metricValue return diff --git a/x-pack/metricbeat/module/statsd/server/data_test.go b/x-pack/metricbeat/module/statsd/server/data_test.go index 2b5d19dd3a2..bf24d945b6d 100644 --- a/x-pack/metricbeat/module/statsd/server/data_test.go +++ b/x-pack/metricbeat/module/statsd/server/data_test.go @@ -14,10 +14,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/metricbeat/helper/server" "github.com/elastic/beats/v7/metricbeat/mb" mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" + "github.com/elastic/elastic-agent-libs/mapstr" ) func init() { @@ -302,12 +302,12 @@ func TestEventMapping(t *testing.T) { for _, test := range []struct { metricName string metricValue interface{} - expected common.MapStr + expected mapstr.M }{ { metricName: "a_job_name_start", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "job_name": "a_job_name", "started": countValue, }, @@ -315,7 +315,7 @@ func TestEventMapping(t *testing.T) { { metricName: "a_job_name_end", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "job_name": "a_job_name", "ended": countValue, }, @@ -323,7 +323,7 @@ func TestEventMapping(t *testing.T) { { metricName: "a_job_name_heartbeat_failure", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "job_name": "a_job_name", "heartbeat_failure": countValue, }, @@ -331,7 +331,7 @@ func TestEventMapping(t *testing.T) { { metricName: "operator_failures_an_operator_name", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "operator_name": "an_operator_name", "failures": countValue, }, @@ -339,7 +339,7 @@ func TestEventMapping(t *testing.T) { { metricName: "operator_successes_an_operator_name", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "operator_name": "an_operator_name", "successes": countValue, }, @@ -347,112 +347,112 @@ func TestEventMapping(t *testing.T) { { metricName: "ti_failures", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_failures": countValue, }, }, { metricName: "ti_successes", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_successes": countValue, }, }, { metricName: "previously_succeeded", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "previously_succeeded": countValue, }, }, { metricName: "zombies_killed", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "zombies_killed": countValue, }, }, { metricName: "scheduler_heartbeat", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "scheduler_heartbeat": countValue, }, }, { metricName: "dag_processing.processes", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_processes": countValue, }, }, { metricName: "dag_processing.manager_stalls", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_file_processor_manager_stalls": countValue, }, }, { metricName: "dag_file_refresh_error", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_file_refresh_error": countValue, }, }, { metricName: "scheduler.tasks.killed_externally", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_killed_externally": countValue, }, }, { metricName: "scheduler.tasks.running", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_running": countValue, }, }, { metricName: "scheduler.tasks.starving", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_starving": countValue, }, }, { metricName: "scheduler.orphaned_tasks.cleared", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_orphaned_cleared": countValue, }, }, { metricName: "scheduler.orphaned_tasks.adopted", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_orphaned_adopted": countValue, }, }, { metricName: "scheduler.critical_section_busy", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "scheduler_critical_section_busy": countValue, }, }, { metricName: "sla_email_notification_failure", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "sla_email_notification_failure": countValue, }, }, { metricName: "ti.start.a_dagid.a_taskid", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dagid", "task_id": "a_taskid", "task_started": countValue, @@ -461,7 +461,7 @@ func TestEventMapping(t *testing.T) { { metricName: "ti.finish.a_dagid.a_taskid.a_status", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dagid", "task_id": "a_taskid", "status": "a_status", @@ -471,21 +471,21 @@ func TestEventMapping(t *testing.T) { { metricName: "dag.callback_exceptions", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_callback_exceptions": countValue, }, }, { metricName: "celery.task_timeout_error", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_celery_timeout_error": countValue, }, }, { metricName: "task_removed_from_dag.a_dagid", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dagid", "task_removed": countValue, }, @@ -493,7 +493,7 @@ func TestEventMapping(t *testing.T) { { metricName: "task_restored_to_dag.a_dagid", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dagid", "task_restored": countValue, }, @@ -501,7 +501,7 @@ func TestEventMapping(t *testing.T) { { metricName: "task_instance_created-an_operator_name", metricValue: countValue, - expected: common.MapStr{ + expected: mapstr.M{ "operator_name": "an_operator_name", "task_created": countValue, }, @@ -509,28 +509,28 @@ func TestEventMapping(t *testing.T) { { metricName: "dagbag_size", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_bag_size": gaugeValue, }, }, { metricName: "dag_processing.import_errors", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_import_errors": gaugeValue, }, }, { metricName: "dag_processing.total_parse_time", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_total_parse_time": gaugeValue, }, }, { metricName: "dag_processing.last_runtime.a_dag_file", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_file": "a_dag_file", "dag_last_runtime": gaugeValue, }, @@ -538,7 +538,7 @@ func TestEventMapping(t *testing.T) { { metricName: "dag_processing.last_run.seconds_ago.a_dag_file", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_file": "a_dag_file", "dag_last_run_seconds_ago": gaugeValue, }, @@ -546,63 +546,63 @@ func TestEventMapping(t *testing.T) { { metricName: "dag_processing.processor_timeouts", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "processor_timeouts": gaugeValue, }, }, { metricName: "scheduler.tasks.without_dagrun", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_without_dagrun": gaugeValue, }, }, { metricName: "scheduler.tasks.running", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_running": gaugeValue, }, }, { metricName: "scheduler.tasks.starving", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_starving": gaugeValue, }, }, { metricName: "scheduler.tasks.executable", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "task_executable": gaugeValue, }, }, { metricName: "executor.open_slots", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "executor_open_slots": gaugeValue, }, }, { metricName: "executor.queued_tasks", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "executor_queued_tasks": gaugeValue, }, }, { metricName: "executor.running_tasks", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "executor_running_tasks": gaugeValue, }, }, { metricName: "pool.open_slots.a_pool_name", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "pool_name": "a_pool_name", "pool_open_slots": gaugeValue, }, @@ -610,7 +610,7 @@ func TestEventMapping(t *testing.T) { { metricName: "pool.queued_slots.a_pool_name", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "pool_name": "a_pool_name", "pool_queued_slots": gaugeValue, }, @@ -618,7 +618,7 @@ func TestEventMapping(t *testing.T) { { metricName: "pool.running_slots.a_pool_name", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "pool_name": "a_pool_name", "pool_running_slots": gaugeValue, }, @@ -626,7 +626,7 @@ func TestEventMapping(t *testing.T) { { metricName: "pool.starving_tasks.a_pool_name", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "pool_name": "a_pool_name", "pool_starving_tasks": gaugeValue, }, @@ -634,42 +634,42 @@ func TestEventMapping(t *testing.T) { { metricName: "smart_sensor_operator.poked_tasks", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "smart_sensor_operator_poked_tasks": gaugeValue, }, }, { metricName: "smart_sensor_operator.poked_success", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "smart_sensor_operator_poked_success": gaugeValue, }, }, { metricName: "smart_sensor_operator.poked_exception", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "smart_sensor_operator_poked_exception": gaugeValue, }, }, { metricName: "smart_sensor_operator.exception_failures", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "smart_sensor_operator_exception_failures": gaugeValue, }, }, { metricName: "smart_sensor_operator.infra_failures", metricValue: gaugeValue, - expected: common.MapStr{ + expected: mapstr.M{ "smart_sensor_operator_infra_failures": gaugeValue, }, }, { metricName: "dagrun.dependency-check.a_dag_id", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dag_id", "dag_dependency_check": timerValue, }, @@ -677,7 +677,7 @@ func TestEventMapping(t *testing.T) { { metricName: "dag.a_dag_id.a_task_id.duration", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dag_id", "task_id": "a_task_id", "task_duration": timerValue, @@ -686,7 +686,7 @@ func TestEventMapping(t *testing.T) { { metricName: "dag_processing.last_duration.a_dag_file", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_file": "a_dag_file", "dag_last_duration": timerValue, }, @@ -694,7 +694,7 @@ func TestEventMapping(t *testing.T) { { metricName: "dagrun.duration.success.a_dag_id", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dag_id", "success_dag_duration": timerValue, }, @@ -702,7 +702,7 @@ func TestEventMapping(t *testing.T) { { metricName: "dagrun.duration.failed.a_dag_id", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dag_id", "failed_dag_duration": timerValue, }, @@ -710,7 +710,7 @@ func TestEventMapping(t *testing.T) { { metricName: "dagrun.schedule_delay.a_dag_id", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dag_id", "dag_schedule_delay": timerValue, }, @@ -718,14 +718,14 @@ func TestEventMapping(t *testing.T) { { metricName: "scheduler.critical_section_duration", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "scheduler_critical_section_duration": timerValue, }, }, { metricName: "dagrun.a_dag_id.first_task_scheduling_delay", metricValue: timerValue, - expected: common.MapStr{ + expected: mapstr.M{ "dag_id": "a_dag_id", "dag_first_task_scheduling_delay": timerValue, }, @@ -733,11 +733,11 @@ func TestEventMapping(t *testing.T) { { metricName: "not_mapped_metric", metricValue: timerValue, - expected: common.MapStr{}, + expected: mapstr.M{}, }, } { t.Run(test.metricName, func(t *testing.T) { - metricSetFields := common.MapStr{} + metricSetFields := mapstr.M{} builtMappings, _ := buildMappings(mappings) eventMapping(test.metricName, test.metricValue, metricSetFields, builtMappings) @@ -1017,11 +1017,11 @@ func TestParseMetrics(t *testing.T) { } type testUDPEvent struct { - event common.MapStr + event mapstr.M meta server.Meta } -func (u *testUDPEvent) GetEvent() common.MapStr { +func (u *testUDPEvent) GetEvent() mapstr.M { return u.event } @@ -1032,7 +1032,7 @@ func (u *testUDPEvent) GetMeta() server.Meta { func process(packets []string, ms *MetricSet) error { for _, d := range packets { udpEvent := &testUDPEvent{ - event: common.MapStr{ + event: mapstr.M{ server.EventDataKey: []byte(d), }, meta: server.Meta{ @@ -1062,20 +1062,20 @@ func TestTagsGrouping(t *testing.T) { events := ms.getEvents() assert.Len(t, events, 2) - actualTags := []common.MapStr{} + actualTags := []mapstr.M{} for _, e := range events { actualTags = append(actualTags, e.RootFields) } - expectedTags := []common.MapStr{ - common.MapStr{ - "labels": common.MapStr{ + expectedTags := []mapstr.M{ + mapstr.M{ + "labels": mapstr.M{ "k1": "v1", "k2": "v2", }, }, - common.MapStr{ - "labels": common.MapStr{ + mapstr.M{ + "labels": mapstr.M{ "k1": "v2", "k2": "v3", }, @@ -1113,7 +1113,7 @@ func TestTagsCleanup(t *testing.T) { events := ms.getEvents() assert.Len(t, events, 1) - assert.Equal(t, events[0].MetricSetFields, common.MapStr{"metric1": map[string]interface{}{"value": float64(3)}}) + assert.Equal(t, events[0].MetricSetFields, mapstr.M{"metric1": map[string]interface{}{"value": float64(3)}}) } func TestSetReset(t *testing.T) { @@ -1170,7 +1170,7 @@ func TestGaugeDeltas(t *testing.T) { events := ms.getEvents() assert.Len(t, events, 1) - assert.Equal(t, events[0].MetricSetFields, common.MapStr{ + assert.Equal(t, events[0].MetricSetFields, mapstr.M{ "metric01": map[string]interface{}{"value": -1.0}, }) @@ -1178,7 +1178,7 @@ func TestGaugeDeltas(t *testing.T) { events = ms.getEvents() assert.Len(t, events, 1) - assert.Equal(t, events[0].MetricSetFields, common.MapStr{ + assert.Equal(t, events[0].MetricSetFields, mapstr.M{ "metric01": map[string]interface{}{"value": -1.0}, }) } @@ -1194,7 +1194,7 @@ func TestCounter(t *testing.T) { events := ms.getEvents() assert.Len(t, events, 1) - assert.Equal(t, events[0].MetricSetFields, common.MapStr{ + assert.Equal(t, events[0].MetricSetFields, mapstr.M{ "metric01": map[string]interface{}{"count": int64(3)}, }) @@ -1202,7 +1202,7 @@ func TestCounter(t *testing.T) { events = ms.getEvents() assert.Len(t, events, 1) - assert.Equal(t, events[0].MetricSetFields, common.MapStr{ + assert.Equal(t, events[0].MetricSetFields, mapstr.M{ "metric01": map[string]interface{}{"count": int64(0)}, }) } @@ -1219,7 +1219,7 @@ func TestCounterSampled(t *testing.T) { events := ms.getEvents() assert.Len(t, events, 1) - assert.Equal(t, events[0].MetricSetFields, common.MapStr{ + assert.Equal(t, events[0].MetricSetFields, mapstr.M{ "metric01": map[string]interface{}{"count": int64(20)}, }) } @@ -1279,7 +1279,7 @@ func TestChangeType(t *testing.T) { events := ms.getEvents() assert.Len(t, events, 1) - assert.Equal(t, common.MapStr{ + assert.Equal(t, mapstr.M{ "metric01": map[string]interface{}{"count": int64(2)}, }, events[0].MetricSetFields) } @@ -1301,7 +1301,7 @@ func BenchmarkIngest(b *testing.B) { events := make([]*testUDPEvent, len(tests)) for i, d := range tests { events[i] = &testUDPEvent{ - event: common.MapStr{ + event: mapstr.M{ server.EventDataKey: []byte(d), }, meta: server.Meta{ diff --git a/x-pack/metricbeat/module/statsd/server/registry.go b/x-pack/metricbeat/module/statsd/server/registry.go index 65951247527..99db2eeefcb 100644 --- a/x-pack/metricbeat/module/statsd/server/registry.go +++ b/x-pack/metricbeat/module/statsd/server/registry.go @@ -9,9 +9,9 @@ import ( "github.com/rcrowley/go-metrics" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/metricbeat/helper/labelhash" + "github.com/elastic/elastic-agent-libs/mapstr" ) var logger = logp.NewLogger("statd") @@ -182,7 +182,7 @@ func (t *samplingTimerSnapshot) Variance() float64 { type metricsGroup struct { tags map[string]string - metrics common.MapStr + metrics mapstr.M } func (r *registry) getMetric(metric interface{}) map[string]interface{} { @@ -242,7 +242,7 @@ func (r *registry) GetAll() []metricsGroup { tagGroups := []metricsGroup{} for tagGroupKey, metricsMap := range r.metrics { - fields := common.MapStr{} + fields := mapstr.M{} for key, m := range metricsMap { // cleanups according to ttl @@ -374,7 +374,7 @@ func (r *registry) GetOrNewSet(name string, tags map[string]string) *setMetric { } func (r *registry) metricHash(tags map[string]string) string { - mapstrTags := common.MapStr{} + mapstrTags := mapstr.M{} for k, v := range tags { mapstrTags[k] = v } diff --git a/x-pack/metricbeat/module/statsd/server/server.go b/x-pack/metricbeat/module/statsd/server/server.go index a32129910fe..d3dcb8e6123 100644 --- a/x-pack/metricbeat/module/statsd/server/server.go +++ b/x-pack/metricbeat/module/statsd/server/server.go @@ -10,10 +10,10 @@ import ( "strings" "time" - "github.com/elastic/beats/v7/libbeat/common" serverhelper "github.com/elastic/beats/v7/metricbeat/helper/server" "github.com/elastic/beats/v7/metricbeat/helper/server/udp" "github.com/elastic/beats/v7/metricbeat/mb" + "github.com/elastic/elastic-agent-libs/mapstr" ) // init registers the MetricSet with the central registry. @@ -169,12 +169,12 @@ func (m *MetricSet) getEvents() []*mb.Event { for idx, tagGroup := range groups { - mapstrTags := common.MapStr{} + mapstrTags := mapstr.M{} for k, v := range tagGroup.tags { mapstrTags[k] = v } - sanitizedMetrics := common.MapStr{} + sanitizedMetrics := mapstr.M{} for k, v := range tagGroup.metrics { eventMapping(k, v, sanitizedMetrics, m.mappings) } @@ -185,7 +185,7 @@ func (m *MetricSet) getEvents() []*mb.Event { events[idx] = &mb.Event{ MetricSetFields: sanitizedMetrics, - RootFields: common.MapStr{"labels": mapstrTags}, + RootFields: mapstr.M{"labels": mapstrTags}, Namespace: m.Module().Name(), } } diff --git a/x-pack/metricbeat/module/syncgateway/replication/data.go b/x-pack/metricbeat/module/syncgateway/replication/data.go index 55b68b7c213..d7a0f2f5b0a 100644 --- a/x-pack/metricbeat/module/syncgateway/replication/data.go +++ b/x-pack/metricbeat/module/syncgateway/replication/data.go @@ -5,11 +5,11 @@ package replication import ( - "github.com/elastic/beats/v7/libbeat/common" s "github.com/elastic/beats/v7/libbeat/common/schema" c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface" "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/beats/v7/x-pack/metricbeat/module/syncgateway" + "github.com/elastic/elastic-agent-libs/mapstr" ) var replicationSchema = s.Schema{ @@ -32,7 +32,7 @@ func eventMapping(r mb.ReporterV2, content *syncgateway.SgResponse) { for replID, replData := range content.Syncgateway.PerReplication { replData, _ := replicationSchema.Apply(replData) r.Event(mb.Event{ - MetricSetFields: common.MapStr{ + MetricSetFields: mapstr.M{ "id": replID, "metrics": replData, }, diff --git a/x-pack/osquerybeat/cmd/root.go b/x-pack/osquerybeat/cmd/root.go index 3f3b2f45852..6910bbf5fdd 100644 --- a/x-pack/osquerybeat/cmd/root.go +++ b/x-pack/osquerybeat/cmd/root.go @@ -7,11 +7,11 @@ package cmd import ( cmd "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/cmd/instance" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/common/cli" "github.com/elastic/beats/v7/libbeat/ecs" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/publisher/processing" + "github.com/elastic/elastic-agent-libs/mapstr" "github.com/spf13/cobra" @@ -26,8 +26,8 @@ const ( ) // withECSVersion is a modifier that adds ecs.version to events. -var withECSVersion = processing.WithFields(common.MapStr{ - "ecs": common.MapStr{ +var withECSVersion = processing.WithFields(mapstr.M{ + "ecs": mapstr.M{ "version": ecs.Version, }, }) diff --git a/x-pack/osquerybeat/internal/pub/publisher.go b/x-pack/osquerybeat/internal/pub/publisher.go index fb01e5deae5..f55f15ad4e1 100644 --- a/x-pack/osquerybeat/internal/pub/publisher.go +++ b/x-pack/osquerybeat/internal/pub/publisher.go @@ -10,11 +10,11 @@ import ( "github.com/elastic/beats/v7/libbeat/beat" "github.com/elastic/beats/v7/libbeat/beat/events" - "github.com/elastic/beats/v7/libbeat/common" "github.com/elastic/beats/v7/libbeat/logp" "github.com/elastic/beats/v7/libbeat/processors" "github.com/elastic/beats/v7/x-pack/osquerybeat/internal/config" "github.com/elastic/beats/v7/x-pack/osquerybeat/internal/ecs" + "github.com/elastic/elastic-agent-libs/mapstr" ) const ( @@ -107,13 +107,13 @@ func processorsForInputsConfig(inputs []config.InputConfig) (procs *processors.P } func hitToEvent(index, eventType, actionID, responseID string, hit map[string]interface{}, ecsm ecs.Mapping, reqData interface{}) beat.Event { - var fields common.MapStr + var fields mapstr.M if len(ecsm) > 0 { // Map ECS fields if the mapping is provided - fields = common.MapStr(ecsm.Map(hit)) + fields = mapstr.M(ecsm.Map(hit)) } else { - fields = common.MapStr{} + fields = mapstr.M{} } // Add event.module for ECS @@ -146,7 +146,7 @@ func hitToEvent(index, eventType, actionID, responseID string, hit map[string]in event.Fields["response_id"] = responseID } if index != "" { - event.Meta = common.MapStr{events.FieldMetaRawIndex: index} + event.Meta = mapstr.M{events.FieldMetaRawIndex: index} } return event diff --git a/x-pack/winlogbeat/module/testing_windows.go b/x-pack/winlogbeat/module/testing_windows.go index 537adafb595..14e35b2ec81 100644 --- a/x-pack/winlogbeat/module/testing_windows.go +++ b/x-pack/winlogbeat/module/testing_windows.go @@ -25,6 +25,7 @@ import ( "github.com/elastic/beats/v7/libbeat/processors/script/javascript" "github.com/elastic/beats/v7/winlogbeat/checkpoint" "github.com/elastic/beats/v7/winlogbeat/eventlog" + "github.com/elastic/elastic-agent-libs/mapstr" // Register javascript modules. _ "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module" @@ -80,7 +81,7 @@ func testPipeline(t testing.TB, evtx string, pipeline string, p *params) { } // Open evtx file. - log, err := eventlog.New(common.MustNewConfigFrom(common.MapStr{ + log, err := eventlog.New(common.MustNewConfigFrom(mapstr.M{ "name": path, "api": "wineventlog", "no_more_events": "stop", @@ -95,7 +96,7 @@ func testPipeline(t testing.TB, evtx string, pipeline string, p *params) { } // Load javascript processor. - processor, err := javascript.New(common.MustNewConfigFrom(common.MapStr{ + processor, err := javascript.New(common.MustNewConfigFrom(mapstr.M{ "file": pipeline, })) if err != nil { @@ -103,7 +104,7 @@ func testPipeline(t testing.TB, evtx string, pipeline string, p *params) { } // Read and process events. - var events []common.MapStr + var events []mapstr.M for stop := false; !stop; { records, err := log.Read() if err == io.EOF { @@ -182,7 +183,7 @@ func assertEqual(t testing.TB, expected, actual interface{}) bool { return false } -func writeGolden(t testing.TB, source string, events []common.MapStr) { +func writeGolden(t testing.TB, source string, events []mapstr.M) { data, err := json.MarshalIndent(events, "", " ") if err != nil { t.Fatal(err) @@ -198,7 +199,7 @@ func writeGolden(t testing.TB, source string, events []common.MapStr) { } } -func readGolden(t testing.TB, source string) []common.MapStr { +func readGolden(t testing.TB, source string) []mapstr.M { inPath := filepath.Join("testdata", filepath.Base(source)+".golden.json") data, err := ioutil.ReadFile(inPath) @@ -206,7 +207,7 @@ func readGolden(t testing.TB, source string) []common.MapStr { t.Fatal(err) } - var events []common.MapStr + var events []mapstr.M if err = json.Unmarshal(data, &events); err != nil { t.Fatal(err) } @@ -217,13 +218,13 @@ func readGolden(t testing.TB, source string) []common.MapStr { return events } -func normalize(t testing.TB, m common.MapStr) common.MapStr { +func normalize(t testing.TB, m mapstr.M) mapstr.M { data, err := json.Marshal(m) if err != nil { t.Fatal(err) } - var out common.MapStr + var out mapstr.M if err = json.Unmarshal(data, &out); err != nil { t.Fatal(err) } @@ -232,7 +233,7 @@ func normalize(t testing.TB, m common.MapStr) common.MapStr { return lowercaseGUIDs(out) } -func filterEvent(m common.MapStr, ignores []string) common.MapStr { +func filterEvent(m mapstr.M, ignores []string) mapstr.M { for _, f := range ignores { m.Delete(f) } @@ -244,7 +245,7 @@ var uppercaseGUIDRegex = regexp.MustCompile(`^{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{ // lowercaseGUIDs finds string fields that look like GUIDs and converts the hex // from uppercase to lowercase. Prior to Windows 2019, GUIDs used uppercase hex // (contrary to RFC 4122). -func lowercaseGUIDs(m common.MapStr) common.MapStr { +func lowercaseGUIDs(m mapstr.M) mapstr.M { for k, v := range m.Flatten() { str, ok := v.(string) if !ok { @@ -264,7 +265,7 @@ var ( // assertFieldsAreDocumented validates that all fields contained in the event // are documented in a fields.yml file. -func assertFieldsAreDocumented(t testing.TB, m common.MapStr) { +func assertFieldsAreDocumented(t testing.TB, m mapstr.M) { t.Helper() loadDocumentedFieldsOnce.Do(func() {