Created
July 9, 2020 22:46
-
-
Save austinpray/49e76936d732e787c55eb959fb6ebb99 to your computer and use it in GitHub Desktop.
parse props from structured loggin
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
{"query"=>"can contain spaces", "span"=>"1h"} | |
{"query"=>"can contain spaces", "span"=>"1h"} | |
{"query"=>"can contain spaces", "span"=>"1h"} | |
{"query"=>"can contain spaces", "span"=>"1h"} | |
{"query"=>"can contain spaces"} | |
{"query"=>"can contain spaces"} | |
{"query"=>"can contain spaces"} | |
{"span"=>"1h"} | |
{"span"=>"1h"} | |
{"span"=>"1h"} | |
{} | |
{} |
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
lines = <<~EOT | |
/monitor query=can contain spaces span=1h | |
/monitor span=1h query=can contain spaces | |
/monitor span="1h" query="can contain spaces" | |
/monitor span='1h' query='can contain spaces' | |
/monitor query=can contain spaces | |
/monitor query="can contain spaces" | |
/monitor query='can contain spaces' | |
/monitor span=1h | |
/monitor span='1h' | |
/monitor span="1h" | |
/monitor | |
ayy | |
EOT | |
def get_props(line) | |
kv_pairs = /\/monitor\s+(.*)/.match(line) | |
unless kv_pairs | |
return {} | |
end | |
parse_kv(kv_pairs[0]).sort.to_h | |
end | |
def parse_kv(line) | |
acc = {} | |
while line | |
match = /(\w+)=(.*?)(?=\w+=)(.*)/.match(line) || /(\w+)=(.*)/.match(line) | |
key, value, rest = match[1..3] | |
acc[key] = /([^'"].*[^'"])/.match(value.strip)[1] | |
line = rest | |
end | |
acc | |
end | |
lines.split("\n").each do |line| | |
puts get_props(line.strip).inspect | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment