-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 processor to set timezone for an event #3902
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a45355d
Add add_locale processor
martinscholz83 b2ad649
register processor
martinscholz83 88cc4e7
Add tests
martinscholz83 7d2782e
Change handling of getting local timezone
martinscholz83 7f84cb3
change test
martinscholz83 dd74400
Remove unnecassary config
martinscholz83 eb4dea6
Simplified get zone
martinscholz83 a06dd06
Update changelog
martinscholz83 e6353be
Add description for config.asciidoc
martinscholz83 6914c9a
Fix grammar config.asciidoc
martinscholz83 ed01967
Add timezone to fields.common.yml
martinscholz83 94dca42
Fetch timezone in run method
martinscholz83 f5f4b4c
Add benchmark test
martinscholz83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package actions | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/processors" | ||
) | ||
|
||
type addLocale struct { | ||
timezone string | ||
} | ||
|
||
func init() { | ||
processors.RegisterPlugin("add_locale", newAddLocale) | ||
} | ||
|
||
func newAddLocale(c common.Config) (processors.Processor, error) { | ||
zone, _ := time.Now().Zone() | ||
|
||
l := addLocale{timezone: zone} | ||
|
||
return l, nil | ||
} | ||
|
||
func (l addLocale) Run(event common.MapStr) (common.MapStr, error) { | ||
event.Put("beat.timezone", l.timezone) | ||
|
||
return event, nil | ||
} | ||
|
||
func (l addLocale) String() string { | ||
return "add_locale=" + l.timezone | ||
} |
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,44 @@ | ||
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the requirement to have this field automatically reflect the latest timezone, let's not cache the
timezone
like I requested previous (sorry for the change) and makeRun
always calltime.Now().Zone()
. Based on the benchmark results there wasn't a huge cost to doing this.