Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Created May 28, 2019 19:29
Show Gist options
  • Save YanhaoYang/9d762d05631c604d9d84636f111f8d28 to your computer and use it in GitHub Desktop.
Save YanhaoYang/9d762d05631c604d9d84636f111f8d28 to your computer and use it in GitHub Desktop.
Parse Heroku logs and index the logs in Elasticsearch
require 'elasticsearch'
require 'time'
client = Elasticsearch::Client.new host: "172.17.0.1", log: true
mappings = {
mappings: {
log: {
properties: {
ts: { type: 'date' },
service: { type: 'long' },
connect: { type: 'long' },
}
}
}
}
index_name = "logs"
client.indices.create index: index_name, body: mappings
bulk_body = []
File.open('veh.log') do |f|
f.each_line do |ln|
fields = ln.split
next unless fields[3] == "heroku" && fields[4] == "router"
data = {ts: Time.parse(fields[2]).iso8601}
fields.each_with_index do |c, idx|
if c =~ /\A(\w+)="?(.+?)"?\z/
data[$1.to_sym] = $2
end
end
if(pos = data[:path].index("?"))
data[:short_path] = data[:path][0...pos]
else
data[:short_path] = data[:path]
end
if(pos = data[:fwd].index(","))
data[:client_ip] = data[:fwd][0...pos]
else
data[:client_ip] = data[:fwd]
end
data[:connect] = data[:connect].to_i
data[:service] = data[:service].to_i
#client.index index: index_name, type: 'log', body: data, refresh: true
bulk_body << { index: { _index: index_name, _type: 'log'} }
bulk_body << data
if bulk_body.size > 100
client.bulk body: bulk_body
bulk_body.clear
end
end
end
client.bulk body: bulk_body unless bulk_body.empty?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment