-
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.
Add add_locale processor to add local timezone to an event (#3902)
The `add_locale` processor adds the host machine's configured timezone to the event under the `beat.timezone` field. This field can be used to apply transformations to the event that are based on the machine's local time. For example, you can then correctly interpret the timestamp placed in logs by syslog. Fixes #3867
- Loading branch information
1 parent
eedd8b0
commit a66a719
Showing
17 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package actions | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/processors" | ||
) | ||
|
||
type addLocale struct{} | ||
|
||
func init() { | ||
processors.RegisterPlugin("add_locale", newAddLocale) | ||
} | ||
|
||
func newAddLocale(c common.Config) (processors.Processor, error) { | ||
return addLocale{}, nil | ||
} | ||
|
||
func (l addLocale) Run(event common.MapStr) (common.MapStr, error) { | ||
zone, _ := time.Now().Zone() | ||
event.Put("beat.timezone", zone) | ||
|
||
return event, nil | ||
} | ||
|
||
func (l addLocale) String() string { | ||
return "add_locale" | ||
} |
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,59 @@ | ||
package actions | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExportTimeZone(t *testing.T) { | ||
var testConfig = common.NewConfig() | ||
|
||
input := common.MapStr{} | ||
|
||
zone, _ := time.Now().In(time.Local).Zone() | ||
|
||
actual := getActualValue(t, testConfig, input) | ||
|
||
expected := common.MapStr{ | ||
"beat": map[string]string{ | ||
"timezone": zone, | ||
}, | ||
} | ||
|
||
assert.Equal(t, expected.String(), actual.String()) | ||
} | ||
|
||
func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr { | ||
if testing.Verbose() { | ||
logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"}) | ||
} | ||
|
||
p, err := newAddLocale(*config) | ||
if err != nil { | ||
logp.Err("Error initializing add_locale") | ||
t.Fatal(err) | ||
} | ||
|
||
actual, err := p.Run(input) | ||
|
||
return actual | ||
} | ||
|
||
func BenchmarkConstruct(b *testing.B) { | ||
var testConfig = common.NewConfig() | ||
|
||
input := common.MapStr{} | ||
|
||
p, err := newAddLocale(*testConfig) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
for i := 0; i < b.N; i++ { | ||
_, err = p.Run(input) | ||
} | ||
} |
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
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
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