-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add de_dot plugin to use if need it Signed-off-by: jcriadomarco <[email protected]>
- Loading branch information
Showing
1 changed file
with
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'fluent/plugin/filter' | ||
|
||
module Fluent::Plugin | ||
class DedotFilter < Filter | ||
|
||
Fluent::Plugin.register_filter('dedot', self) | ||
|
||
config_param :de_dot, :bool, default: true | ||
config_param :de_dot_separator, :string, default: '_' | ||
|
||
def initialize | ||
super | ||
end | ||
|
||
def configure(conf) | ||
super | ||
|
||
if @de_dot && @de_dot_separator.include?(".") | ||
raise Fluent::ConfigError, "Invalid de_dot_separator: cannot be or contain '.'" | ||
end | ||
|
||
if @de_dot && @de_dot_nested | ||
log.info "DeDot will recurse nested hashes and arrays" | ||
end | ||
|
||
end | ||
|
||
def filter(tag, time, record) | ||
begin | ||
de_dot(record) if @de_dot | ||
rescue => e | ||
router.emit_error_event(tag, time, record, e) | ||
end | ||
end | ||
|
||
def de_dot(record) | ||
newrecord = {} | ||
|
||
record.each do |key, value| | ||
newkey = key.gsub(/\./, @de_dot_separator) | ||
|
||
# Recurse hashes and arrays: | ||
if value.is_a? Hash | ||
value = de_dot value | ||
elsif value.is_a? Array | ||
value = value.map { |v| v.is_a?(Hash) ? de_dot(v) : v } | ||
end | ||
|
||
newrecord[newkey] = value | ||
end | ||
|
||
newrecord | ||
end | ||
|
||
end | ||
end |