Skip to content

Instantly share code, notes, and snippets.

@austinpray
Created July 9, 2020 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinpray/49e76936d732e787c55eb959fb6ebb99 to your computer and use it in GitHub Desktop.
Save austinpray/49e76936d732e787c55eb959fb6ebb99 to your computer and use it in GitHub Desktop.
parse props from structured loggin
{"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"}
{}
{}
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