Skip to content
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

Ruby 3.2 compatibility and some minor tweaks. #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.7
3.2.1
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = "test/*_test.rb"
end
task default: %i[test]
8 changes: 4 additions & 4 deletions heroku-log-parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Gem::Specification.new do |s|
s.name = "heroku-log-parser"
s.version = HerokuLogParser::VERSION
s.platform = Gem::Platform::RUBY
s.author = "Ryan Daigle"
s.authors =["Ryan Daigle", "Florian Aßmann"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/rwdaigle/heroku-log-parser"
s.homepage = "https://github.com/boof/heroku-log-parser"
s.summary = "Syslog message parser"
s.description = "Easily parse Heroku's syslog-based application log-stream"

Expand All @@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

if File.exists?('UPGRADING')
if File.exist?('UPGRADING')
s.post_install_message = File.read("UPGRADING")
end

Expand Down Expand Up @@ -49,4 +49,4 @@ Gem::Specification.new do |s|
# s.add_development_dependency('fakeweb')
# s.add_development_dependency('railties')
# s.add_development_dependency('actionmailer')
end
end
50 changes: 25 additions & 25 deletions lib/heroku-log-parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require 'time'

class HerokuLogParser
IS_NIL = "-"

COUNTING_FRAME_REGEX = /^(\d+) /
NEWLINE_REGEX = /\n/
# http://tools.ietf.org/html/rfc5424#page-8
# frame <prority>version time hostname <appname-missing> procid msgid [no structured data = '-'] msg
# 120 <40>1 2013-07-26T18:39:37.489572+00:00 host app web.11 - State changed from starting to up...
LINE_REGEX = /\<(\d+)\>(1) (\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?\+00:00) ([a-z0-9\-\_\.]+) ([a-z0-9\.-]+) ([a-z0-9\-\_\.]+) (\-) (.*)$/

SYSLOG_KEYS = :priority, :syslog_version, :emitted_at, :hostname, :appname, :proc_id, :msg_id, :structured_data, :message

Expand All @@ -9,22 +17,14 @@ class << self
def parse(data_str)
events = []
lines(data_str) do |line|
if(matching = line.match(line_regex))
events << event_data(matching)
end
matching = line.match LINE_REGEX
events << event_data(matching) if matching
end
events
end

protected

# http://tools.ietf.org/html/rfc5424#page-8
# frame <prority>version time hostname <appname-missing> procid msgid [no structured data = '-'] msg
# 120 <40>1 2013-07-26T18:39:37.489572+00:00 host app web.11 - State changed from starting to up...
def line_regex
@line_regex ||= /\<(\d+)\>(1) (\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?\+00:00) ([a-z0-9\-\_\.]+) ([a-z0-9\.-]+) ([a-z0-9\-\_\.]+) (\-) (.*)$/
end

# Heroku's http log drains (https://devcenter.heroku.com/articles/labs-https-drains)
# utilize octet counting framing (http://tools.ietf.org/html/draft-gerhards-syslog-plain-tcp-12#section-3.4.1)
# for transmission of syslog messages over TCP. Properly parse and delimit
Expand All @@ -35,14 +35,14 @@ def line_regex
def lines(data_str, &block)
d = data_str
while d && d.length > 0
if matching = d.match(/^(\d+) /) # if have a counting frame, use it
if matching = d.match(COUNTING_FRAME_REGEX) # if have a counting frame, use it
num_bytes = matching[1].to_i
frame_offset = matching[0].length
line_end = frame_offset + num_bytes
msg = d[frame_offset..(line_end-1)]
yield msg
d = d[line_end..d.length]
elsif matching = d.match(/\n/) # Newlines = explicit message delimiter
elsif matching = d.match(NEWLINE_REGEX) # Newlines = explicit message delimiter
d = matching.post_match
else
STDERR.puts("Unable to parse: #{d}. Full line was: #{data_str.inspect}")
Expand All @@ -53,18 +53,18 @@ def lines(data_str, &block)

# Heroku is missing the appname token, otherwise can treat as standard syslog format
def event_data(matching)
event = {}
event[:priority] = matching[1].to_i
event[:syslog_version] = matching[2].to_i
event[:emitted_at] = nil?(matching[3]) ? nil : Time.parse(matching[3]).utc
event[:hostname] = interpret_nil(matching[4])
event[:appname] = interpret_nil(matching[5])
event[:proc_id] = interpret_nil(matching[6])
event[:msg_id] = interpret_nil(nil)
event[:structured_data] = interpret_nil(matching[7])
event[:message] = interpret_nil(matching[8])
event[:original] = matching[0]
event
{
priority: matching[1].to_i,
syslog_version: matching[2].to_i,
emitted_at: nil?(matching[3]) ? nil : Time.parse(matching[3]).utc,
hostname: interpret_nil(matching[4]),
appname: interpret_nil(matching[5]),
proc_id: interpret_nil(matching[6]),
msg_id: nil,
structured_data: interpret_nil(matching[7]),
message: interpret_nil(matching[8]),
original: matching[0],
}
end

private
Expand All @@ -74,7 +74,7 @@ def interpret_nil(val)
end

def nil?(val)
val == "-"
val == IS_NIL
end
end
end