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

add_cloud_metadata asynchronous initialization #8845

Merged
merged 3 commits into from
Nov 10, 2018
Merged
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
45 changes: 32 additions & 13 deletions libbeat/processors/add_cloud_metadata/add_cloud_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io/ioutil"
"net"
"net/http"
"sync"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -314,34 +315,52 @@ func newCloudMetadata(c *common.Config) (processors.Processor, error) {
return nil, err
}

result := fetchMetadata(fetchers, config.Timeout)
if result == nil {
logp.Info("add_cloud_metadata: hosting provider type not detected.")
return &addCloudMetadata{}, nil
}
acm := new(addCloudMetadata)

logp.Info("add_cloud_metadata: hosting provider type detected as %v, metadata=%v",
result.provider, result.metadata.String())
acm.initFn = func() {
result := fetchMetadata(fetchers, config.Timeout)
if result == nil {
logp.Info("add_cloud_metadata: hosting provider type not detected.")
return
}
acm.metadata = result.metadata
acm.initFn = nil
logp.Info("add_cloud_metadata: hosting provider type detected as %v, metadata=%v",
result.provider, result.metadata.String())
}

return &addCloudMetadata{metadata: result.metadata}, nil
go acm.init()
return acm, nil
}

type addCloudMetadata struct {
initFn func()
initOnce sync.Once
metadata common.MapStr
}

func (p addCloudMetadata) Run(event *beat.Event) (*beat.Event, error) {
if len(p.metadata) == 0 {
func (p *addCloudMetadata) init() {
p.initOnce.Do(p.initFn)
}

func (p *addCloudMetadata) getMeta() common.MapStr {
p.init()
return p.metadata
}

func (p *addCloudMetadata) Run(event *beat.Event) (*beat.Event, error) {
meta := p.getMeta()
if len(meta) == 0 {
return event, nil
}

// This overwrites the meta.cloud if it exists. But the cloud key should be
// reserved for this processor so this should happen.
_, err := event.PutValue("meta.cloud", p.metadata)
_, err := event.PutValue("meta.cloud", meta)

return event, err
}

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