-
Notifications
You must be signed in to change notification settings - Fork 350
/
Alerting in Google Cloud
95 lines (70 loc) · 2.22 KB
/
Alerting in Google Cloud
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
git clone --depth 1 https://github.com/GoogleCloudPlatform/training-data-analyst.git
cd ~/training-data-analyst/courses/design-process/deploying-apps-to-gcp
sudo pip install -r requirements.txt
echo "runtime: python39" > app.yaml
gcloud app create --region=$REGION
gcloud app deploy --version=one --quiet
cat > pubsub-channel.json <<EOF_END
{
"type": "pubsub",
"displayName": "",
"description": "",
"labels": {
"topic": "projects/$DEVSHELL_PROJECT_ID/topics/notificationTopic"
},
}
EOF_END
gcloud beta monitoring channels create --channel-content-from-file="pubsub-channel.json"
channel_info=$(gcloud beta monitoring channels list)
channel_id=$(echo "$channel_info" | grep -oP 'name: \K[^ ]+' | head -n 1)
cat > app-engine-error-percent-policy.json <<EOF_END
{
"displayName": "Hello too slow",
"userLabels": {},
"conditions": [
{
"displayName": "Response latency [MEAN] for 99th% over 8s",
"conditionThreshold": {
"filter": "resource.type = \"gae_app\" AND metric.type = \"appengine.googleapis.com/http/server/response_latencies\"",
"aggregations": [
{
"alignmentPeriod": "60s",
"crossSeriesReducer": "REDUCE_NONE",
"perSeriesAligner": "ALIGN_PERCENTILE_99"
}
],
"comparison": "COMPARISON_GT",
"duration": "0s",
"trigger": {
"count": 1
},
"thresholdValue": 8000
}
}
],
"alertStrategy": {
"autoClose": "604800s"
},
"combiner": "OR",
"enabled": true,
"notificationChannels": [
"$channel_id"
]
}
EOF_END
cd ~/training-data-analyst/courses/design-process/deploying-apps-to-gcp
gcloud alpha monitoring policies create --policy-from-file="app-engine-error-percent-policy.json"
cat > main.py <<EOF_END
from flask import Flask, render_template, request
import time
import random
import json
app = Flask(__name__)
@app.route("/")
def main():
model = {"title": "Hello GCP."}
time.sleep(10)
return render_template('index.html', model=model)
EOF_END
gcloud app deploy --version=two --quiet
while true; do curl -s https://$DEVSHELL_PROJECT_ID.appspot.com/ | grep -e "<title>" -e "error";sleep .$[( $RANDOM % 10 )]s;done