Skip to content

Commit 0eec7c4

Browse files
committed
Convert docker-json to Filestream configuration
1 parent 3e3d84a commit 0eec7c4

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

filebeat/input/logv2/convert.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ var logInputExclusiveKeys = []string{
9999
"close_removed",
100100
"close_renamed",
101101
"close_timeout",
102+
"docker-json",
102103
"exclude_files",
103104
"harvester_buffer_size",
104105
"json",
@@ -329,6 +330,57 @@ func handleJSON(logger *logp.Logger, cfg *config.C, parsers *[]any) error {
329330
return nil
330331
}
331332

333+
func handleDockerJson(logger *logp.Logger, cfg, newCfg *config.C, parsers *[]any) error {
334+
hasDockerJson, err := cfg.Has("docker-json", -1)
335+
if err != nil {
336+
return fmt.Errorf("cannot read 'docker-json': %w", err)
337+
}
338+
339+
if !hasDockerJson {
340+
return nil
341+
}
342+
343+
dockerJson, err := cfg.Child("docker-json", -1)
344+
if err != nil {
345+
logger.Warnf("cannot read 'docker-json' as map: %s, ignoring malformed config entry ", err)
346+
return nil
347+
}
348+
349+
newContainerCfg := config.NewConfig()
350+
for _, key := range []string{"stream", "format"} {
351+
has, err := dockerJson.Has(key, -1)
352+
if err != nil {
353+
return fmt.Errorf("cannot read 'docker-json.%s': %w", key, err)
354+
}
355+
356+
if !has {
357+
continue
358+
}
359+
360+
child, err := dockerJson.String(key, -1)
361+
if err != nil {
362+
return fmt.Errorf("cannot read 'docker-json.%s as string: %w", key, err)
363+
}
364+
365+
newContainerCfg.SetString(key, -1, child)
366+
}
367+
368+
*parsers = append(*parsers, map[string]any{
369+
"container": newContainerCfg,
370+
})
371+
372+
symlinks, err := dockerJson.Bool("symlinks", -1)
373+
if err != nil {
374+
return fmt.Errorf("cannot read 'docker-json.symlinks as boolean: %w", err)
375+
}
376+
377+
if err := newCfg.SetBool("prospector.scanner.symlinks", -1, symlinks); err != nil {
378+
return fmt.Errorf("cannot set 'prospector.scanner.symlinks': %w", err)
379+
}
380+
381+
return nil
382+
}
383+
332384
// copyParsers copies any existing 'parsers' from cfg into parsersCfg.
333385
// offset is the offset in parsersCfg to start adding the new ones.
334386
func copyParsers(cfg, parsersCfg *config.C, offset int) error {
@@ -370,6 +422,10 @@ func handleParsers(logger *logp.Logger, cfg, newCfg *config.C) error {
370422
return err
371423
}
372424

425+
if err := handleDockerJson(logger, cfg, newCfg, &parsers); err != nil {
426+
return err
427+
}
428+
373429
// If no parsers were created, return. Any pre-existing parsers
374430
// entry will be maintained and passed as is to Filestream.
375431
if len(parsers) <= 0 {

filebeat/input/logv2/convert_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,48 @@ func TestConvertHandlesSpecialCases(t *testing.T) {
5858
logYamlCfg string
5959
expectedJsonCfg string
6060
}{
61+
"container input": {
62+
logYamlCfg: `
63+
id: container-id
64+
paths:
65+
- /var/log/containers/*.log
66+
docker-json:
67+
stream: stdout
68+
format: docker
69+
symlinks: true
70+
`,
71+
expectedJsonCfg: `
72+
{
73+
"file_identity": {
74+
"native": null
75+
},
76+
"id": "container-id",
77+
"parsers": [
78+
{
79+
"container": {
80+
"format": "docker",
81+
"stream": "stdout"
82+
}
83+
}
84+
],
85+
"paths": [
86+
"/var/log/containers/*.log"
87+
],
88+
"prospector": {
89+
"scanner": {
90+
"symlinks": true,
91+
"fingerprint": {
92+
"enabled": false
93+
}
94+
}
95+
},
96+
"take_over": {
97+
"enabled": true
98+
},
99+
"type": "filestream"
100+
}
101+
`,
102+
},
61103
"file_identity is not set": {
62104
logYamlCfg: `
63105
id: foo

0 commit comments

Comments
 (0)