Skip to content

Instantly share code, notes, and snippets.

@dovy
Created November 20, 2018 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dovy/9631442906d22b82c4d0e9978fc74038 to your computer and use it in GitHub Desktop.
Save dovy/9631442906d22b82c4d0e9978fc74038 to your computer and use it in GitHub Desktop.
It's a REAL pain to figure out how to grab the data from Google cloud logging. This script lets you grab historic data and save locally so you can process since Cloud Logging Sync's won't do anything historic.
gcloud beta logging read "resource.type=\"dataflow_step\" resource.labels.job_id=\"2018-11-13_09_13_59-6912497806535022683\" OR \"timestamp>=\\\"2018-11-12T00:00:00Z\" OR \"timing:\" timestamp<=\"2200-01-01T00:00:00.000000000Z\" timestamp<\"2018-11-20T20:20:01.065Z\"" --format=json --limit 100000 | jq -rnc --stream 'fromstream(1|truncate_stream(inputs)) | .jsonPayload.message' >> gcs.txt
filename = "gcs.txt"
file = open(filename, "r")
data = file.read()
data = data.replace('\t', '').split("\n---> Method timing:")
data = filter(None, data) # fastest
results = {}
for item in data:
item = item.strip().split('\n')
for i in item:
i = i.split(': ')
keys = ['TOTAL',
'apply_routing',
'apply_scrubbing',
'calculate_consumption',
'calculate_pricing',
'apply_leg_segments',
'write_pricing']
if i[0] not in keys:
continue
if i[0] not in results:
results[i[0]] = []
results[i[0]].append(float(i[1]))
# print(item)
final_results = {}
for key, value in results.iteritems():
final_results[key] = round(reduce(lambda x, y: x + y, value) / len(value), 2)
final_results['TOTAL_WO_WRITING'] = round(final_results['TOTAL'] - final_results['write_pricing'], 2)
from pprint import pprint
pprint(final_results)
print(len(results['TOTAL']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment