forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigquery.py
executable file
·201 lines (168 loc) · 6.97 KB
/
bigquery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Runs bigquery metrics and uploads the result to GCS."""
import argparse
import glob
import os
import pipes
import re
import subprocess
import sys
import time
import traceback
import requests
import ruamel.yaml as yaml
BACKFILL_DAYS = 30
def check(cmd, **kwargs):
"""Logs and runs the command, raising on errors."""
print('Run:', ' '.join(pipes.quote(c) for c in cmd), end=' ', file=sys.stderr)
if hasattr(kwargs.get('stdout'), 'name'):
print(' > %s' % kwargs['stdout'].name, file=sys.stderr)
else:
print()
# If 'stdin' keyword arg is a string run command and communicate string to stdin
if 'stdin' in kwargs and isinstance(kwargs['stdin'], str):
in_string = kwargs['stdin']
kwargs['stdin'] = subprocess.PIPE
proc = subprocess.Popen(cmd, **kwargs)
proc.communicate(input=in_string.encode('utf-8'))
return
subprocess.check_call(cmd, **kwargs)
def validate_metric_name(name):
"""Raise ValueError if name is non-trivial."""
# Regex '$' symbol matches an optional terminating new line
# so we have to check that the name
# doesn't have one if the regex matches.
if not re.match(r'^[\w-]+$', name) or name[-1] == '\n':
raise ValueError(name)
def do_jq(jq_filter, data_filename, out_filename, jq_bin='jq'):
"""Executes jq on a file and outputs the results to a file."""
with open(out_filename, 'w') as out_file:
check([jq_bin, jq_filter, data_filename], stdout=out_file)
class BigQuerier:
def __init__(self, project, bucket_path):
if not project:
raise ValueError('project', project)
self.project = project
if not bucket_path:
print('Not uploading results, no bucket specified.', file=sys.stderr)
self.prefix = bucket_path
def do_query(self, query, out_filename):
"""Executes a bigquery query, outputting the results to a file."""
cmd = [
'bq', 'query', '--format=prettyjson',
'--project_id=%s' % self.project,
'--max_rows=1000000', # Results may have more than 100 rows
query,
]
with open(out_filename, 'w') as out_file:
check(cmd, stdout=out_file)
out_file.write('\n')
def jq_upload(self, config, data_filename):
"""Filters a data file with jq and uploads the results to GCS."""
filtered = 'daily-%s.json' % time.strftime('%Y-%m-%d')
latest = '%s-latest.json' % config['metric']
do_jq(config['jqfilter'], data_filename, filtered)
self.copy(filtered, os.path.join(config['metric'], filtered))
self.copy(filtered, latest)
def run_metric(self, config):
"""Runs query and filters results, uploading data to GCS."""
raw = 'raw-%s.json' % time.strftime('%Y-%m-%d')
self.update_query(config)
self.do_query(config['query'], raw)
self.copy(raw, os.path.join(config['metric'], raw))
consumer_error = False
for consumer in [self.jq_upload]:
try:
consumer(config, raw)
except (
ValueError,
KeyError,
IOError,
requests.exceptions.ConnectionError,
):
print(traceback.format_exc(), file=sys.stderr)
consumer_error = True
if consumer_error:
raise ValueError('Error(s) were thrown by query result consumers.')
def copy(self, src, dest):
"""Use gsutil to copy src to <bucket_path>/dest with minimal caching."""
if not self.prefix:
return # no destination
dest = os.path.join(self.prefix, dest)
check(['gsutil', '-h', 'Cache-Control:max-age=60', 'cp', src, dest])
@staticmethod
def update_query(config):
"""Modifies config['query'] based on the metric configuration."""
last_time = int(time.time() - (60*60*24)*BACKFILL_DAYS)
config['query'] = config['query'].replace('<LAST_DATA_TIME>', str(last_time))
def all_configs(search='**.yaml'):
"""Returns config files in the metrics dir."""
return glob.glob(os.path.join(
os.path.dirname(__file__), 'configs', search))
def ints_to_floats(point):
for key, val in point.items():
if key == 'time':
continue
if isinstance(val, int):
point[key] = float(val)
elif isinstance(val, dict):
point[key] = ints_to_floats(val)
return point
def main(configs, project, bucket_path):
"""Loads metric config files and runs each metric."""
queryer = BigQuerier(project, bucket_path)
# authenticate as the given service account if our environment is providing one
if 'GOOGLE_APPLICATION_CREDENTIALS' in os.environ:
keyfile = os.environ['GOOGLE_APPLICATION_CREDENTIALS']
check(['gcloud', 'auth', 'activate-service-account', f'--key-file={keyfile}'])
# the 'bq show' command is called as a hack to dodge the config prompts that bq presents
# the first time it is run. A newline is passed to stdin to skip the prompt for default project
# when the service account in use has access to multiple projects.
check(['bq', 'show'], stdin='\n')
errs = []
for path in configs or all_configs():
try:
with open(path) as config_raw:
config = yaml.safe_load(config_raw)
if not config:
raise ValueError('invalid yaml: %s.' % path)
config['metric'] = config['metric'].strip()
validate_metric_name(config['metric'])
queryer.run_metric(config)
except (
ValueError,
KeyError,
IOError,
subprocess.CalledProcessError,
):
print(traceback.format_exc(), file=sys.stderr)
errs.append(path)
if errs:
print('Failed %d configs: %s' % (len(errs), ', '.join(errs)))
sys.exit(1)
if __name__ == '__main__':
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
'--config', action='append', help='YAML file describing a metric.')
PARSER.add_argument(
'--project',
default='k8s-gubernator',
help='Charge the specified account for bigquery usage.')
PARSER.add_argument(
'--bucket',
help='Upload results to the specified gcs bucket.')
ARGS = PARSER.parse_args()
main(ARGS.config, ARGS.project, ARGS.bucket)