This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
stackdriverdataflowbigquery.py
277 lines (260 loc) · 9.86 KB
/
stackdriverdataflowbigquery.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright 2020 Google LLC.
#
# 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.
# [START parsing Stackdriver logs from pubsub_to_bigquery]
import argparse
import logging
import json
import re
import ast
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import SetupOptions
from apache_beam.options.pipeline_options import StandardOptions
def iterate_multidimensional(my_dict):
return_json = {
"insertId": None,
"logName": None,
"receiveTimestamp": None,
"textPayload": None,
"timestamp": None,
"trace": None
}
for k,v in my_dict.items():
if(isinstance(v,dict)):
iterate_multidimensional(v)
continue
if k in return_json:
return_json[k] = v
return return_json
def iterate_textpayload_multidimensional(my_dict):
return_dict = {
"error_type": None,
"session_id": None,
"caller_id": None,
"email": None,
"code": None,
"string_value": None,
"lang": None,
"speech": None,
"is_fallback_intent": None,
"webhook_for_slot_filling_used": None,
"webhook_used": None,
"intent_name": None,
"intent_id": None,
"score": None,
"action": None,
"resolved_query": None,
"source": None
}
for k,v in my_dict.items():
if(isinstance(v,dict)):
iterate_textpayload_multidimensional(v)
continue
if k in return_dict:
return_dict[k] = v
return return_dict
def iterate_textpayload(my_list):
res = []
for item in my_list:
my_list_item = item.replace('"', '')
if ':' in my_list_item:
res.append(map(str.strip, my_list_item.split(":", 1)))
return dict(res)
# function to get response body data from pub/sub message and build structure for BigQuery load
def parse_transform_response(data):
logging.info('--- START parse_transform_response Function ---')
pub_sub_data = json.loads(data)
fullpayload_dict = iterate_multidimensional(pub_sub_data)
# Clean textPlayload from Stackdriver - not a valid JSON object
text_payload = fullpayload_dict['textPayload']
return_merged_payload = None
if text_payload != None:
regex = re.compile(r'''[\S]+:(?:\s(?!\S+:)\S+)+''', re.VERBOSE)
matches = regex.findall(pub_sub_data["textPayload"])
iterate_textpayload_response = iterate_textpayload(matches)
textpayload_dict = iterate_textpayload_multidimensional(iterate_textpayload_response)
if textpayload_dict["error_type"] is not None:
textpayload_dict["error_type"] = textpayload_dict["error_type"].replace("\n", "").replace("}", "").strip()
return_merged_payload = dict(list(fullpayload_dict.items()) + list(textpayload_dict.items()))
if return_merged_payload is not None:
logging.info('--- END parse_transform_response Function ---')
logging.info(return_merged_payload)
return return_merged_payload
else:
logging.info('--- END parse_transform_response Function ---')
logging.info(fullpayload_dict)
return fullpayload_dict
def run(argv=None, save_main_session=True):
"""Build and run the pipeline."""
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--input_topic',
help=('Input PubSub topic of the form '
'"projects/<PROJECT>/topics/<TOPIC>".'))
group.add_argument(
'--input_subscription',
help=('Input PubSub subscription of the form '
'"projects/<PROJECT>/subscriptions/<SUBSCRIPTION>."'))
parser.add_argument('--output_bigquery', required=True,
help='Output BQ table to write results to '
'"PROJECT_ID:DATASET.TABLE"')
known_args, pipeline_args = parser.parse_known_args(argv)
pipeline_options = PipelineOptions(pipeline_args)
pipeline_options.view_as(SetupOptions).save_main_session = save_main_session
pipeline_options.view_as(StandardOptions).streaming = True
p = beam.Pipeline(options=pipeline_options)
# Read from PubSub into a PCollection.
if known_args.input_subscription:
messages = (p
| beam.io.ReadFromPubSub(
subscription=known_args.input_subscription)
.with_output_types(bytes))
else:
messages = (p
| beam.io.ReadFromPubSub(topic=known_args.input_topic)
.with_output_types(bytes))
decode_messages = messages | 'DecodePubSubMessages' >> beam.Map(lambda x: x.decode('utf-8'))
# Parse response body data from pub/sub message and build structure for BigQuery load
output = decode_messages | 'ParseTransformResponse' >> beam.Map(parse_transform_response)
# Write to BigQuery
bigquery_table_schema = {
"fields": [
{
"mode": "NULLABLE",
"name": "session_id",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "trace",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "caller_id",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "email",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "timestamp",
"type": "TIMESTAMP"
},
{
"mode": "NULLABLE",
"name": "receiveTimestamp",
"type": "TIMESTAMP"
},
{
"mode": "NULLABLE",
"name": "resolved_query",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "string_value",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "speech",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "is_fallback_intent",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "webhook_for_slot_filling_used",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "webhook_used",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "intent_name",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "intent_id",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "score",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "action",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "source",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "error_type",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "code",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "insertId",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "logName",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "lang",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "textPayload",
"type": "STRING"
}
]
}
output | 'WriteToBigQuery' >> beam.io.WriteToBigQuery(
known_args.output_bigquery,
schema=bigquery_table_schema,
create_disposition=beam.io.BigQueryDisposition.CREATE_IF_NEEDED,
write_disposition=beam.io.BigQueryDisposition.WRITE_APPEND)
p.run()
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
run()
# [END parsing Stackdriver logs from pubsub_to_bigquery]