-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.py
455 lines (357 loc) · 10.9 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from urllib import request
from goblet import Goblet, jsonify, Response, goblet_entrypoint
from goblet.alerts.alert_conditions import (
MetricCondition,
LogMatchCondition,
CustomMetricCondition,
UptimeCondition,
PubSubDLQCondition
)
from goblet.alerts.alerts import BackendAlert, UptimeAlert, PubSubDLQAlert
from goblet.handlers.routes import CORSConfig
import asyncio
import logging
app = Goblet(function_name="goblet-example")
app.log.setLevel(logging.INFO) # configure goblet logger level
goblet_entrypoint(app)
from typing import List
from marshmallow import Schema, fields
# Example http trigger
@app.http()
def main_http(request):
return jsonify(request.json)
# Example http trigger that contains header
@app.http(headers={"X-Github-Event"})
def main_header(request):
return jsonify(request.json)
# Example http triggers that matches header
@app.http(headers={"X-Github-Event": "issue"})
def main(request):
return jsonify(request.json)
# Path param
@app.route("/home/{test}")
def home(test):
return jsonify(test)
# Example query args
@app.route("/home")
def query_args():
request = app.current_request
q = request.args.get("q")
return jsonify(q)
# POST request
@app.route("/home", methods=["POST"])
def post():
request = app.current_request
return jsonify(request.json)
# Typed Path Param
@app.route("/home/{name}/{id}", methods=["GET"])
def namer(name: str, id: int):
return f"{name}: {id}"
class Point(Schema):
lat = fields.Int()
lng = fields.Int()
# Custom schema types
@app.route("/points")
def points() -> List[Point]:
point = Point().load({"lat": 0, "lng": 0})
return [point]
# Custom Marshmallow Fields
from marshmallow_enum import EnumField
from enum import Enum
def enum_to_properties(self, field, **kwargs):
"""
Add an OpenAPI extension for marshmallow_enum.EnumField instances
"""
if isinstance(field, EnumField):
return {"type": "string", "enum": [m.name for m in field.enum]}
return {}
app.handlers["route"].marshmallow_attribute_function = enum_to_properties
class StopLight(Enum):
green = 1
yellow = 2
red = 3
class TrafficStop(Schema):
light_color = EnumField(StopLight)
@app.route("/traffic")
def traffic() -> TrafficStop:
return TrafficStop().dump({"light_color": StopLight.green})
# Returns follow openapi spec
# definitions:
# TrafficStop:
# type: object
# properties:
# light_color:
# type: string
# enum:
# - green
# - yellow
# - red
# Enum paramter
@app.route("/{color}")
def prim_enum(color: StopLight):
return StopLight(color)
# Pydantic Typing
from pydantic import BaseModel
class NestedModel(BaseModel):
text: str
class PydanticModel(BaseModel):
id: int
nested: NestedModel
# Request Body Typing
@app.route("/pydantic", request_body=PydanticModel)
def traffic() -> PydanticModel:
return jsonify(PydanticModel().dict)
# Custom Backend
@app.route("/custom_backend", backend="https://www.CLOUDRUN_URL.com/home")
def custom_backend():
return
# Method Security
@app.route("/method_security", security=[{"your_custom_auth_id": []}])
def method_security():
return
# Custom responses and request_types
@app.route(
"/custom",
request_body={
"application/json": {"schema": {"type": "array", "items": {"type": "string"}}}
},
responses={"400": {"description": "400"}},
)
def custom():
request = app.current_request
assert request.data["string1", "string2"]
return
# Example response object
@app.route("/response")
def response():
return Response(
{"failed": 400}, headers={"Content-Type": "application/json"}, status_code=400
)
# Example CORS
@app.route('/custom_cors', cors=CORSConfig(allow_origin='localhost', allow_methods=["GET"], extra_headers={"X-TEST":"X-HEADER-VALUE"}))
def custom_cors():
return jsonify('localhost is allowed with GET method')
# Scheduled job
@app.schedule("5 * * * *")
def scheduled_job():
return jsonify("success")
# Scheduled job with custom headers, method, and body
@app.schedule(
"5 * * * *",
httpMethod="POST",
headers={"X-Custom": "header"},
body="BASE64 ENCODED STRING",
)
def scheduled_job():
headers = app.current_request.headers
body = app.current_request.body
method = app.current_request.method
return jsonify("success")
# Pubsub Subscription
@app.pubsub_subscription("test")
def test_subscription(data):
app.log.info(data)
return
# Pubsub topic with matching message attributes
@app.pubsub_subscription("test", attributes={"key": "value"})
def pubsub_attributes(data):
app.log.info(data)
return
# create a pubsub subscription instead of pubsub triggered function
@app.pubsub_subscription("test", use_subscription=True)
def pubsub_subscription_use_subscription(data):
return
# create a pubsub subscription instead of pubsub triggered function and add filter
@app.pubsub_subscription(
"test",
use_subscription=True,
filter='attributes.name = "com" AND -attributes:"iana.org/language_tag"',
)
def pubsub_subscription_filter(data):
return
# Pubsub Subscription with DLQ and alert
# Triggered by pubsub topic. Simulates failure to trigger DLQ
@app.pubsub_subscription(
"goblet-created-test-topic",
dlq=True,
dlq_alerts=[
PubSubDLQAlert(
"pubsubdlq",
conditions=[
PubSubDLQCondition(
"pubsublq-condition"
)
],
)
]
)
def failed_subscription(data):
raise Exception("Simulating failure")
# Create a pubsub topic
app.pubsub_topic(
"test-topic"
)
# Example Storage trigger on the create/finalize event
@app.storage("BUCKET_NAME", "finalize")
def storage(event):
app.log.info(event)
return
# Example before request
@app.before_request()
def add_db(request):
app.g.db = "db"
return request
# Example after request
@app.after_request()
def add_header(response):
response.headers["X-Custom"] = "custom header"
return response
# Example eventarc pubsub topic
@app.eventarc(topic="test")
def pubsub(data):
app.log.info("pubsub")
return
# Example eventarc direct event
@app.eventarc(
event_filters=[
{"attribute": "type", "value": "google.cloud.storage.object.v1.finalized"},
{"attribute": "bucket", "value": "BUCKET"},
],
region="us-east1",
)
def bucket(data):
app.log.info("bucket_post")
return
# Example eventarc audit log
@app.eventarc(
event_filters=[
{"attribute": "type", "value": "google.cloud.audit.log.v1.written"},
{"attribute": "methodName", "value": "storage.objects.get"},
{"attribute": "serviceName", "value": "storage.googleapis.com"},
],
region="us-central1",
)
def bucket_get(data):
app.log.info("bucket_get")
return
# Example Cloudrun Job with schedule
@app.job("job1", schedule="* * * * *")
def job1_task1(id):
app.log.info(f"job...{id}")
return "200"
# Example Cloudrun Job with additional task
@app.job("job1", task_id=1)
def job1_task2(id):
app.log.info(f"different task for job...{id}")
return "200"
# Example BQ Remote Function
# Called in BQ with the following sql: SELECT `PROJECT.DATASET.math_example_multiply(x,y,z)` from DATASET.table
@app.bqremotefunction(dataset_id="DATASET")
def multiply(x: int, y: int, z: int) -> int:
w = x * y * z
return w
# Totally contrived example of an async function (a real one would use aiohttp or similar)
async def async_multiply(x: int, y: int) -> int:
w = x * y
return w
# Example BQ Remote Function with vectorized function
# For network-bound BQ Remote Functions, this approach using async will yield significantly better performance
@app.bqremotefunction(dataset_id="blogs",vectorize_func=True)
def function_test(x: List[int], y: List[int]) -> List[int]:
results = [asyncio.run(async_multiply(elem_x, elem_y)) for elem_x, elem_y in zip(x, y)]
return results
# Example Redis Instance
app.redis("redis-test")
# Example VPC Connector
app.vpcconnector("vpc-conn-test")
# Example Metric Alert for the cloudfunction metric execution_count with a threshold of 10
metric_alert = BackendAlert(
"metric",
conditions=[
MetricCondition(
"test",
metric="cloudfunctions.googleapis.com/function/execution_count",
value=10
)
],
)
app.alert(metric_alert)
# Example Metric Alert for the cloudfunction metric execution_times with a custom aggregation
metric_alert_2 = BackendAlert(
"metric",
conditions=[
MetricCondition(
"test",
metric="cloudfunctions.googleapis.com/function/execution_times",
value=1000,
aggregations=[
{
"alignmentPeriod": "300s",
"crossSeriesReducer": "REDUCE_NONE",
"perSeriesAligner": "ALIGN_PERCENTILE_50",
}
],
)
],
)
app.alert(metric_alert_2)
# Example Log Match metric that will trigger an incendent off of any Error logs
log_alert = BackendAlert(
"error",
conditions=[LogMatchCondition("error", "severity>=ERROR")],
)
app.alert(log_alert)
# Example Metric Alert that creates a custom metric for severe errors with http code in the 500's and creates an alert with a threshold of 10
custom_alert = BackendAlert(
"custom",
conditions=[
CustomMetricCondition(
"custom",
metric_filter="severity=(ERROR OR CRITICAL OR ALERT OR EMERGENCY) httpRequest.status=(500 OR 501 OR 502 OR 503 OR 504)",
value=10,
)
],
)
app.alert(custom_alert)
# Example CloudTask Queue + CloudTask HTTP Target
client = app.cloudtaskqueue("queue", config={
"rateLimits": {
"maxDispatchesPerSecond": 500,
"maxBurstSize": 100,
"maxConcurrentDispatches": 1000
},
"retryConfig": {
"maxAttempts": 10,
"minBackoff": "0.100s",
"maxBackoff": "3600s",
"maxDoublings": 16
}
})
# Cloudtask HTTP Target
@app.cloudtasktarget(name="target")
def my_target_handler(request):
''' handle request '''
return {}
# Enqueue a message using the CloudTask Queue client
@app.route("/enqueue", methods=["GET"])
def enqueue():
payload = {"message": {"title": "enqueue"}}
client.enqueue(target="target", payload=payload)
return {}
# Example of handling the GobletRouteNotFoundError with a custom response
@app.errorhandler("GobletRouteNotFoundError")
def handle_missing_route(error):
return Response("Custom Error", status_code=404)
# Example of handling ValueError.
@app.errorhandler("ValueError")
def return_error_string(error):
return Response(str(error), status_code=200)
# Example uptime check
@app.uptime(timeout="30s")
def uptime_check():
return "success"
# Example uptime check with alert
@app.uptime(timeout="30s",alerts=[UptimeAlert("uptime", conditions=[UptimeCondition("uptime")])])
def uptime_check_with_alert():
app.log.info("success")
return "success"