Class: Fluent::Plugin::Sumologic
- Inherits:
-
Output
- Object
- Output
- Fluent::Plugin::Sumologic
- Defined in:
- lib/fluent/plugin/out_sumologic.rb
Constant Summary collapse
- DEFAULT_BUFFER_TYPE =
"memory"- LOGS_DATA_TYPE =
"logs"- METRICS_DATA_TYPE =
"metrics"- DEFAULT_DATA_TYPE =
LOGS_DATA_TYPE- DEFAULT_METRIC_FORMAT_TYPE =
'graphite'
Instance Method Summary collapse
-
#configure(conf) ⇒ Object
This method is called before starting.
-
#dump_log(log) ⇒ Object
Strip sumo_metadata and dump to json.
- #format(tag, time, record) ⇒ Object
- #formatted_to_msgpack_binary ⇒ Object
-
#initialize ⇒ Sumologic
constructor
A new instance of Sumologic.
-
#merge_json(record) ⇒ Object
Used to merge log record into top level json.
- #multi_workers_ready? ⇒ Boolean
-
#shutdown ⇒ Object
This method is called when shutting down.
-
#start ⇒ Object
This method is called when starting.
- #sumo_key(sumo_metadata, chunk) ⇒ Object
-
#sumo_timestamp(time) ⇒ Object
Convert timestamp to 13 digit epoch if necessary.
-
#write(chunk) ⇒ Object
This method is called every flush interval.
Constructor Details
#initialize ⇒ Sumologic
Returns a new instance of Sumologic.
92 93 94 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 92 def initialize super end |
Instance Method Details
#configure(conf) ⇒ Object
This method is called before starting.
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 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 101 def configure(conf) compat_parameters_convert(conf, :buffer) unless conf['endpoint'] =~ URI::regexp raise Fluent::ConfigError, "Invalid SumoLogic endpoint url: #{conf['endpoint']}" end unless conf['data_type'].nil? unless conf['data_type'] =~ /\A(?:logs|metrics)\z/ raise Fluent::ConfigError, "Invalid data_type #{conf['data_type']} must be logs or metrics" end end if conf['data_type'].nil? || conf['data_type'] == LOGS_DATA_TYPE unless conf['log_format'].nil? unless conf['log_format'] =~ /\A(?:json|text|json_merge)\z/ raise Fluent::ConfigError, "Invalid log_format #{conf['log_format']} must be text, json or json_merge" end end end if conf['data_type'] == METRICS_DATA_TYPE && ! conf['metrics_data_type'].nil? unless conf['metrics_data_type'] =~ /\A(?:graphite|carbon2|pronetheus)\z/ raise Fluent::ConfigError, "Invalid metrics_data_type #{conf['metrics_data_type']} must be graphite or carbon2 or prometheus" end end @sumo_conn = SumologicConnection.new(conf['endpoint'], conf['verify_ssl'], conf['open_timeout'].to_i, conf['proxy_uri'], conf['disable_cookies']) super end |
#dump_log(log) ⇒ Object
Strip sumo_metadata and dump to json
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 160 def dump_log(log) log.delete('_sumo_metadata') begin parser = Yajl::Parser.new hash = parser.parse(log[@log_key]) log[@log_key] = hash Yajl.dump(log) rescue Yajl.dump(log) end end |
#format(tag, time, record) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 172 def format(tag, time, record) if defined? time.nsec mstime = time * 1000 + (time.nsec / 1000000) [mstime, record].to_msgpack else [time, record].to_msgpack end end |
#formatted_to_msgpack_binary ⇒ Object
181 182 183 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 181 def formatted_to_msgpack_binary true end |
#merge_json(record) ⇒ Object
Used to merge log record into top level json
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 144 def merge_json(record) if record.has_key?(@log_key) log = record[@log_key].strip if log[0].eql?('{') && log[-1].eql?('}') begin record = record.merge(JSON.parse(log)) record.delete(@log_key) rescue JSON::ParserError # do nothing, ignore end end end record end |
#multi_workers_ready? ⇒ Boolean
96 97 98 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 96 def multi_workers_ready? true end |
#shutdown ⇒ Object
This method is called when shutting down.
139 140 141 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 139 def shutdown super end |
#start ⇒ Object
This method is called when starting.
134 135 136 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 134 def start super end |
#sumo_key(sumo_metadata, chunk) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 185 def sumo_key(, chunk) source_name = ['source'] || @source_name source_name = extract_placeholders(source_name, chunk) unless source_name.nil? source_category = ['category'] || @source_category source_category = extract_placeholders(source_category, chunk) unless source_category.nil? source_host = ['host'] || @source_host source_host = extract_placeholders(source_host, chunk) unless source_host.nil? "#{source_name}:#{source_category}:#{source_host}" end |
#sumo_timestamp(time) ⇒ Object
Convert timestamp to 13 digit epoch if necessary
199 200 201 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 199 def (time) time.to_s.length == 13 ? time : time * 1000 end |
#write(chunk) ⇒ Object
This method is called every flush interval. Write the buffer chunk
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/fluent/plugin/out_sumologic.rb', line 204 def write(chunk) = {} # Sort messages chunk.msgpack_each do |time, record| # plugin dies randomly # https://github.com/uken/fluent-plugin-elasticsearch/commit/8597b5d1faf34dd1f1523bfec45852d380b26601#diff-ae62a005780cc730c558e3e4f47cc544R94 next unless record.is_a? Hash = record.fetch('_sumo_metadata', {:source => record[@source_name_key] }) key = sumo_key(, chunk) log_format = ['log_format'] || @log_format # Strip any unwanted newlines record[@log_key].chomp! if record[@log_key] && record[@log_key].respond_to?(:chomp!) case @data_type when 'logs' case log_format when 'text' log = record[@log_key] unless log.nil? log.strip! end when 'json_merge' if @add_timestamp record = { @timestamp_key => (time) }.merge(record) end log = dump_log(merge_json(record)) else if @add_timestamp record = { @timestamp_key => (time) }.merge(record) end log = dump_log(record) end when 'metrics' log = record[@log_key] unless log.nil? log.strip! end end unless log.nil? if .key?(key) [key].push(log) else [key] = [log] end end end # Push logs to sumo .each do |key, | source_name, source_category, source_host = key.split(':') @sumo_conn.publish( .join("\n"), source_host =source_host, source_category =source_category, source_name =source_name, data_type =@data_type, metric_data_format =@metric_data_format ) end end |