Class: Fluent::ReassembleOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_reassemble.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/out_reassemble.rb', line 16

def configure(conf)
    super
    unless config.has_key?('output_tag')
        raise Fluent::ConfigError, "you must set 'output_tag'"
    end
    unless config.has_key?('assemble')
        raise Fluent::ConfigError, "you must set 'assemble'"
    end
    if config.has_key?('tz')
        ENV["TZ"] = config['tz']
    end
    #assemble definition format
    # {extract_key1}:{replaced_key1}:{operation1},{extract_key2}:{replaced_key2}:{operation2},....{extract_keyN}:{replaced_keyN}:{operationN}
    @reassemble_conf = []
    @assemble.split(",").each{ |conf|
        extract, replace, operation = conf.split(":")
        if extract.nil? || extract.empty?
            next
        else
            extract = extract.strip
        end
        if replace.nil? || replace.empty?
            replace = extract
        else
            replace = replace.strip
        end
        unless operation.nil? || operation.empty?
            operation = operation.strip
        end
        @reassemble_conf.push({:extract => extract, :replace => replace, :operation => operation})
    }
    $log.info "reassemble conf : " + @reassemble_conf.to_s
end

#convert(val, operation) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/fluent/plugin/out_reassemble.rb', line 83

def convert(val, operation)
    if val.nil?
        return val
    end
    if operation.nil?
        return val
    end

    o = operation.downcase
    begin
        case o
        when "to_s"
            return val.to_s
        when "to_i"
            return val.to_i
        when "to_f"
            return val.to_f
        when "to_json"
            return val.to_json
        when "bool_to_i"
            if val
                return 1
            else
                return 0
            end
        when "unixtime_to_datetime"
            return Time.at(val.to_i).strftime(@datetime_format)
        when "unixtime_to_date"
            return Time.at(val.to_i).strftime(@date_format)
        when "unixtime_to_time"
            return Time.at(val.to_i).strftime(@time_format)
        when "url_to_host","url_to_domain"
            return URI(val.to_s).host
        when "url_to_path"
            return URI(val.to_s).path
        when /^add_([\d]+)/
            num = o.gsub(/^add_([\d]+)/, '\1').to_i
            return val + num
        when /^sub_([\d]+)/
            num = o.gsub(/^sub_([\d]+)/, '\1').to_i
            return val - num
        when /^mul_([\d]+)/
            num = o.gsub(/^mul_([\d]+)/, '\1').to_i
            return val * num
        when /^div_([\d]+)/
            num = o.gsub(/^div_([\d]+)/, '\1').to_i
            return val / num
        else
            return val
        end
    rescue
        $log.warn $!
        return val
    end

end

#emit(tag, es, chain) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/out_reassemble.rb', line 50

def emit(tag, es, chain)
    chain.next
    es.each {|time,record|
        json = {}
        @reassemble_conf.each { |conf| 
            extract_key = conf[:extract]
            replaced_key = conf[:replace]
            operation = conf[:operation]
            val = convert(traverse(record, extract_key), operation)
            if !(val.nil?)
                json[replaced_key] = val
            elsif @null_to_null
                json[replaced_key] = nil
            elsif @null_to_empty
                json[replaced_key] = ""
            end
        }
        Fluent::Engine.emit(@output_tag, time, json)
    }
end

#traverse(data, key) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/out_reassemble.rb', line 71

def traverse(data, key)
    val = data
    key.split('.').each{ |k|
        if val.is_a?(Hash) && val.has_key?(k)
            val = val[k]
        else
            return nil
        end
    }
    return val
end