-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filebeat/input/journald: fix field name translation (#30167)
The field names from journald were not being translated to our format when sending the event to the output. This commit fixes it. Fixes 30031
- Loading branch information
Showing
4 changed files
with
115 additions
and
67 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,5 +215,4 @@ func TestInputIncludeMatches(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 linux && cgo && withjournald | ||
// +build linux,cgo,withjournald | ||
|
||
package journald | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path" | ||
"testing" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
) | ||
|
||
func TestInputFieldsTranslation(t *testing.T) { | ||
// A few random keys to verify | ||
var keysToCheck = map[string]string{ | ||
"systemd.user_unit": "log-service.service", | ||
"process.pid": "2084785", | ||
"systemd.transport": "stdout", | ||
"host.hostname": "x-wing", | ||
} | ||
|
||
testCases := map[string]struct { | ||
saveRemoteHostname bool | ||
}{ | ||
"Save hostname enabled": {saveRemoteHostname: true}, | ||
"Save hostname disabled": {saveRemoteHostname: true}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
env := newInputTestingEnvironment(t) | ||
|
||
inp := env.mustCreateInput(common.MapStr{ | ||
"paths": []string{path.Join("testdata", "input-multiline-parser.journal")}, | ||
"include_matches.match": []string{"_SYSTEMD_USER_UNIT=log-service.service"}, | ||
"save_remote_hostname": tc.saveRemoteHostname, | ||
}) | ||
|
||
ctx, cancelInput := context.WithCancel(context.Background()) | ||
env.startInput(ctx, inp) | ||
env.waitUntilEventCount(6) | ||
|
||
for eventIdx, event := range env.pipeline.clients[0].GetEvents() { | ||
for k, v := range keysToCheck { | ||
got, err := event.Fields.GetValue(k) | ||
if err == nil { | ||
if got, want := fmt.Sprint(got), v; got != want { | ||
t.Errorf("expecting key %q to have value '%#v', but got '%#v' instead", k, want, got) | ||
} | ||
} else { | ||
t.Errorf("key %q not found on event %d", k, eventIdx) | ||
} | ||
} | ||
if tc.saveRemoteHostname { | ||
v, err := event.Fields.GetValue("log.source.address") | ||
if err != nil { | ||
t.Errorf("key 'log.source.address' not found on evet %d", eventIdx) | ||
} | ||
|
||
if got, want := fmt.Sprint(v), "x-wing"; got != want { | ||
t.Errorf("expecting key 'log.source.address' to have value '%#v', but got '%#v' instead", want, got) | ||
} | ||
} | ||
} | ||
cancelInput() | ||
}) | ||
} | ||
} |