-
Notifications
You must be signed in to change notification settings - Fork 5k
Add autodetection mode for add_docker_metadata #13374
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
Changes from all commits
fa07b9f
3a4bcd8
d123be0
0e087ac
abfc865
5d3c11e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,12 @@ func NewWatcher(host string, tls *TLSConfig, storeShortID bool) (Watcher, error) | |
| return nil, err | ||
| } | ||
|
|
||
| // Extra check to confirm that Docker is available | ||
| _, err = client.Info(context.Background()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this can affect other users of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it could affect others ( |
||
|
|
||
| return NewWatcherWithClient(client, 60*time.Second, storeShortID) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,10 +57,11 @@ type addDockerMetadata struct { | |
| fields []string | ||
| sourceProcessor processors.Processor | ||
|
|
||
| pidFields []string // Field names that contain PIDs. | ||
| cgroups *common.Cache // Cache of PID (int) to cgropus (map[string]string). | ||
| hostFS string // Directory where /proc is found | ||
| dedot bool // If set to true, replace dots in labels with `_`. | ||
| pidFields []string // Field names that contain PIDs. | ||
| cgroups *common.Cache // Cache of PID (int) to cgropus (map[string]string). | ||
| hostFS string // Directory where /proc is found | ||
| dedot bool // If set to true, replace dots in labels with `_`. | ||
| dockerAvailable bool // If Docker exists in env, then it is set to true | ||
| } | ||
|
|
||
| // New constructs a new add_docker_metadata processor. | ||
|
|
@@ -74,13 +75,19 @@ func buildDockerMetadataProcessor(cfg *common.Config, watcherConstructor docker. | |
| return nil, errors.Wrapf(err, "fail to unpack the %v configuration", processorName) | ||
| } | ||
|
|
||
| var dockerAvailable bool | ||
|
|
||
| watcher, err := watcherConstructor(config.Host, config.TLS, config.MatchShortID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err = watcher.Start(); err != nil { | ||
| return nil, err | ||
| dockerAvailable = false | ||
| errorMsg := fmt.Sprintf("%v: docker environment not detected: %v", processorName, err) | ||
| logp.Debug("add_docker_metadata", errorMsg) | ||
| } else { | ||
| dockerAvailable = true | ||
| logp.Debug("add_docker_metadata", "%v: docker environment detected", processorName) | ||
| if err = watcher.Start(); err != nil { | ||
| return nil, errors.Wrap(err, "failed to start watcher") | ||
| } | ||
| } | ||
|
|
||
| // Use extract_field processor to get container ID from source file path. | ||
|
|
@@ -106,6 +113,7 @@ func buildDockerMetadataProcessor(cfg *common.Config, watcherConstructor docker. | |
| pidFields: config.MatchPIDs, | ||
| hostFS: config.HostFS, | ||
| dedot: config.DeDot, | ||
| dockerAvailable: dockerAvailable, | ||
| }, nil | ||
| } | ||
|
|
||
|
|
@@ -121,8 +129,12 @@ func lazyCgroupCacheInit(d *addDockerMetadata) { | |
| } | ||
|
|
||
| func (d *addDockerMetadata) Run(event *beat.Event) (*beat.Event, error) { | ||
| if !d.dockerAvailable { | ||
| return event, nil | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. You could move this code to the very end of the function, before these declarations. |
||
| var cid string | ||
| var err error | ||
|
|
||
| // Extract CID from the filepath contained in the "log.file.path" field. | ||
| if d.sourceProcessor != nil { | ||
| lfp, _ := event.Fields.GetValue("log.file.path") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,20 @@ func init() { | |
| } | ||
| } | ||
|
|
||
| func TestInitializationNoDocker(t *testing.T) { | ||
| var testConfig = common.NewConfig() | ||
| testConfig.SetString("host", -1, "unix:///var/run42/docker.sock") | ||
|
|
||
| p, err := buildDockerMetadataProcessor(testConfig, docker.NewWatcher) | ||
| assert.NoError(t, err, "initializing add_docker_metadata processor") | ||
|
|
||
| input := common.MapStr{} | ||
| result, err := p.Run(&beat.Event{Fields: input}) | ||
| assert.NoError(t, err, "processing an event") | ||
|
|
||
| assert.Equal(t, common.MapStr{}, result.Fields) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding a test for this |
||
|
|
||
| func TestInitialization(t *testing.T) { | ||
| var testConfig = common.NewConfig() | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.