-
Notifications
You must be signed in to change notification settings - Fork 361
/
Creating and Alerting on Logs-based Metrics
136 lines (99 loc) · 3.23 KB
/
Creating and Alerting on Logs-based Metrics
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
export ZONE=
gcloud auth list
gcloud config set compute/zone $ZONE
gcloud config set project $DEVSHELL_PROJECT_ID
export PROJECT_ID=$(gcloud info --format='value(config.project)')
gcloud container clusters create gmp-cluster --num-nodes=1 --zone $ZONE
gcloud logging metrics create stopped-vm --log-filter='resource.type="gce_instance" protoPayload.methodName="v1.compute.instances.stop"' --description="Metric for stopped VMs"
cat > Hi.json <<EOF_CP
{
"type": "pubsub",
"displayName": "btecky",
"description": "btecky",
"labels": {
"topic": "projects/$DEVSHELL_PROJECT_ID/topics/notificationTopic"
}
}
EOF_CP
gcloud beta monitoring channels create --channel-content-from-file=Hi.json
email_channel=$(gcloud beta monitoring channels list)
channel_id=$(echo "$email_channel" | grep -oP 'name: \K[^ ]+' | head -n 1)
cat > stopped-vm-cp-policy.json <<EOF_CP
{
"displayName": "stopped vm",
"documentation": {
"content": "Documentation content for the stopped vm alert policy",
"mime_type": "text/markdown"
},
"userLabels": {},
"conditions": [
{
"displayName": "Log match condition",
"conditionMatchedLog": {
"filter": "resource.type=\"gce_instance\" protoPayload.methodName=\"v1.compute.instances.stop\""
}
}
],
"alertStrategy": {
"notificationRateLimit": {
"period": "300s"
},
"autoClose": "3600s"
},
"combiner": "OR",
"enabled": true,
"notificationChannels": [
"$channel_id"
]
}
EOF_CP
gcloud alpha monitoring policies create --policy-from-file=stopped-vm-cp-policy.json
export ZONE2=us-central1-a
gcloud compute instances stop instance1 --zone=$ZONE2 --quiet
sleep 45
gcloud container clusters list
gcloud container clusters get-credentials gmp-cluster
kubectl create ns gmp-test
kubectl -n gmp-test apply -f https://storage.googleapis.com/spls/gsp091/gmp_flask_deployment.yaml
kubectl -n gmp-test apply -f https://storage.googleapis.com/spls/gsp091/gmp_flask_service.yaml
kubectl get services -n gmp-test
gcloud logging metrics create hello-app-error \
--description="Metric for hello-app errors" \
--log-filter='severity=ERROR
resource.labels.container_name="hello-app"
textPayload: "ERROR: 404 Error page not found"'
cat > btecky.json <<'EOF_CP'
{
"displayName": "log based metric alert",
"userLabels": {},
"conditions": [
{
"displayName": "New condition",
"conditionThreshold": {
"filter": 'metric.type="logging.googleapis.com/user/hello-app-error" AND resource.type="global"',
"aggregations": [
{
"alignmentPeriod": "120s",
"crossSeriesReducer": "REDUCE_SUM",
"perSeriesAligner": "ALIGN_DELTA"
}
],
"comparison": "COMPARISON_GT",
"duration": "60s",
"trigger": {
"count": 1
}
}
}
],
"alertStrategy": {
"autoClose": "604800s"
},
"combiner": "OR",
"enabled": true,
"notificationChannels": [],
"severity": "SEVERITY_UNSPECIFIED"
}
EOF_CP
gcloud alpha monitoring policies create --policy-from-file=btecky.json
timeout 120 bash -c -- 'while true; do curl $(kubectl get services -n gmp-test -o jsonpath='{.items[*].status.loadBalancer.ingress[0].ip}')/error; sleep $((RANDOM % 4)) ; done'