Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor add_cloud_metadata to handle ECS fields easier #26438

Merged
merged 6 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions libbeat/processors/add_cloud_metadata/add_cloud_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,49 +118,32 @@ func (p *addCloudMetadata) Run(event *beat.Event) (*beat.Event, error) {
return event, nil
}

// If cloud key exists in event already and overwrite flag is set to false, this processor will not overwrite the
// cloud fields. For example aws module writes cloud.instance.* to events already, with overwrite=false,
// add_cloud_metadata should not overwrite these fields with new values.
if !p.initData.overwrite {
cloudValue, _ := event.GetValue("cloud")
if cloudValue != nil {
err := p.extractECSMeta(event, meta)
if err != nil {
return nil, err
}
return event, nil
}
}

err := p.extractECSMeta(event, meta)
err := p.addMeta(event, meta)
if err != nil {
return nil, err
}
_, err = event.PutValue("cloud", meta)
return event, err
}

func (p *addCloudMetadata) String() string {
return "add_cloud_metadata=" + p.getMeta().String()
}

func (p *addCloudMetadata) extractECSMeta(event *beat.Event, meta common.MapStr) error {
// handle ECS fields first
if !p.initData.overwrite {
orchestratorValue, _ := event.GetValue("orchestrator")
if orchestratorValue != nil {
meta.Delete("orchestrator")
return nil
func (p *addCloudMetadata) addMeta(event *beat.Event, meta common.MapStr) 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
// cloud fields. For example aws module writes cloud.instance.* to events already, with overwrite=false,
// add_cloud_metadata should not overwrite these fields with new values.
if !p.initData.overwrite {
cloudValue, _ := event.GetValue(key)
ChrsMark marked this conversation as resolved.
Show resolved Hide resolved
if cloudValue != nil {
continue
}
}
}
orchestratorFields, err := meta.GetValue("orchestrator")
if err == nil {
_, err = event.PutValue("orchestrator", orchestratorFields)
_, err := event.PutValue(key, metaVal)
if err != nil {
return err
}
}
meta.Delete("orchestrator")

return nil
}
2 changes: 1 addition & 1 deletion libbeat/processors/add_cloud_metadata/http_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (f *httpMetadataFetcher) fetchMetadata(ctx context.Context, client http.Cli

// Apply schema.
res.metadata = f.conv(res.metadata)
res.metadata["provider"] = f.provider
res.metadata.Put("cloud.provider", f.provider)

return res
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var alibabaCloudMetadataFetcher = provider{
m["service"] = common.MapStr{
"name": "ECS",
}
return common.MapStr(m)
return common.MapStr{"cloud": m}
}

urls, err := getMetadataURLs(c, ecsMetadataHost, []string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var ec2MetadataFetcher = provider{
"account": s.Object{"id": c.Str("accountId")},
"image": s.Object{"id": c.Str("imageId")},
}.Apply(m)
return out
return common.MapStr{"cloud": out}
}

fetcher, err := newMetadataFetcher(config, "aws", nil, metadataHost, ec2Schema, ec2InstanceIdentityURI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var azureVMMetadataFetcher = provider{
},
"region": c.Str("location"),
}.Apply(m)
return out
return common.MapStr{"cloud": out}
}

fetcher, err := newMetadataFetcher(config, "azure", azHeaders, metadataHost, azSchema, azMetadataURI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var doMetadataFetcher = provider{
"name": c.Str("serviceName"),
},
}.Apply(m)
return out
return common.MapStr{"cloud": out}
}
doMetadataURI := "/metadata/v1.json"

Expand Down
15 changes: 14 additions & 1 deletion libbeat/processors/add_cloud_metadata/provider_google_gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ var gceMetadataFetcher = provider{
out.Put(key, path.Base(p))
}

extractECSField := func(key string, out common.MapStr, meta common.MapStr) {
ecs, _ := out.GetValue(key)
out.Delete(key)
if ecs != nil {
meta.Put(key, ecs.(common.MapStr))
}
}

if instance, ok := m["instance"].(map[string]interface{}); ok {
s.Schema{
"instance": s.Object{
Expand Down Expand Up @@ -121,7 +129,12 @@ var gceMetadataFetcher = provider{
}.ApplyTo(out, project)
}

return out
meta := common.MapStr{}
// call extractECSField for all ECS root fields like orchestrator.*
extractECSField("orchestrator", out, meta)

meta.DeepUpdate(common.MapStr{"cloud": out})
Copy link
Member

@jsoriano jsoriano Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still seems that some special handling is done for orchestrator vs cloud fields. And it may be a bit confusing to have an out, that is not an output of the function at the end.

Wdyt about setting the orchestrator values directly on meta, so they don't have to be moved from out to meta?

And in the same way, perhaps cloud fields could be directly set on meta["cloud"], so this intermediary out is not needed.

Copy link
Member Author

@ChrsMark ChrsMark Jun 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 Good point! I tried to get rid of the extra "cooking" and applied the schema for orchestrator fields on a different map, since unfortunately Apply cannot work in a "DeepUpdate" way so it's not possible to write directly to cloud.* in multiple places since cloud* will be overridden each time Apply is called.

return meta
}

fetcher, err := newMetadataFetcher(config, provider, gceHeaders, metadataHost, gceSchema, gceMetadataURI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func buildOpenstackNovaCreate(scheme string) func(provider string, c *common.Con
m["service"] = common.MapStr{
"name": "Nova",
}
return common.MapStr(m)
return common.MapStr{"cloud": m}
}

urls, err := getMetadataURLsWithScheme(c, scheme, metadataHost, []string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var qcloudMetadataFetcher = provider{
m["service"] = common.MapStr{
"name": "CVM",
}
return common.MapStr(m)
return common.MapStr{"cloud": m}
}

urls, err := getMetadataURLs(c, qcloudMetadataHost, []string{
Expand Down