Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Created December 17, 2017 20:13
Show Gist options
  • Save DazWilkin/8e1350977d787fb9ae4110fe34d1e3cf to your computer and use it in GitHub Desktop.
Save DazWilkin/8e1350977d787fb9ae4110fe34d1e3cf to your computer and use it in GitHub Desktop.
Cloud Functions "finished" log-line parser
#!/usr/bin/env python2
import re
import sys
# Parse Log lines of the form: "2017-12-17T16:00:00.000000000Z Function execution took X ms, finished with status code: YYY"
TIMESTAMP = "(?P<timestamp>20[1-2][0-9]-[0-1][0-9]-[0-3][0-9]T[012][0-9]:[0-6][0-9]:[0-6][0-9].[0-9]{9}Z)"
DURATION = "(?P<duration>[0-9]+)"
UNITS = "(?P<units>[m]+s)"
CODE = "(?P<code>[234][0-9]{2})"
REGEX = "^{timestamp}\tFunction execution took {duration} {units}, finished with status code: {code}".format(
timestamp=TIMESTAMP,
duration=DURATION,
units=UNITS,
code=CODE
)
LOGRE = re.compile(REGEX)
for line in sys.stdin:
found = LOGRE.match(line)
if found is not None:
value = found.groupdict()
#print ("{}: {}{}".format(value["code"],value["duration"],value["units"]))
if (value["code"] == "200") and (value["units"] == "ms"):
print(value["duration"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment