blob: 9f29fc7792335d9641d8cb63d6db5bb0ee7cd0ab [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Analyzing Rate Limit Exceeded events\n",
8 "\n",
9 "Use this notebook to dig into Rate Limit Exceeded events on Monorail."
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": null,
15 "metadata": {
16 "collapsed": false
17 },
18 "outputs": [],
19 "source": [
20 "import gcp\n",
21 "import gcp.bigquery as bq\n",
22 "\n",
23 "context = gcp.Context.default()\n",
24 "print 'The current project is %s' % context.project_id\n",
25 "\n",
26 "# Set the date to analyze here:\n",
27 "date = 20160514"
28 ]
29 },
30 {
31 "cell_type": "code",
32 "execution_count": null,
33 "metadata": {
34 "collapsed": false
35 },
36 "outputs": [],
37 "source": [
38 "%%sql --module by_ip\n",
39 "SELECT\n",
40 " protoPayload.ip as ip,\n",
41 " COUNT(protoPayload.requestId) AS num\n",
42 "FROM\n",
43 " [logs.appengine_googleapis_com_request_log_$date]\n",
44 "WHERE\n",
45 " protoPayload.moduleId is null # == \"default\", otherwise you get backend queries too.\n",
46 " AND\n",
47 " protoPayload.line.logMessage LIKE \"Rate Limit Exceeded%\"\n",
48 "GROUP BY\n",
49 " ip\n",
50 "ORDER BY\n",
51 " num DESC\n",
52 "LIMIT\n",
53 " 100;"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": null,
59 "metadata": {
60 "collapsed": true
61 },
62 "outputs": [],
63 "source": [
64 "%%sql --module by_ip_class\n",
65 "SELECT\n",
66 " REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\\.]*\\.){0}([^\\.]*)\\.?') AS a,\n",
67 " REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\\.]*\\.){1}([^\\.]*)\\.?') AS b,\n",
68 " REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\\.]*\\.){2}([^\\.]*)\\.?') AS c,\n",
69 " REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\\.]*\\.){3}([^\\.]*)\\.?') AS d,\n",
70 " COUNT(protoPayload.requestId) AS num\n",
71 "FROM\n",
72 " [logs.appengine_googleapis_com_request_log_$date]\n",
73 "WHERE\n",
74 " protoPayload.moduleId is null # == \"default\", otherwise you get backend queries too.\n",
75 " AND\n",
76 " protoPayload.line.logMessage LIKE \"Rate Limit Exceeded%\"\n",
77 "GROUP BY\n",
78 " a,\n",
79 " b,\n",
80 " c,\n",
81 " d\n",
82 "ORDER BY\n",
83 " num DESC\n",
84 "LIMIT\n",
85 " 100;"
86 ]
87 },
88 {
89 "cell_type": "code",
90 "execution_count": null,
91 "metadata": {
92 "collapsed": true
93 },
94 "outputs": [],
95 "source": [
96 "%%sql --module by_country\n",
97 "SELECT\n",
98 " protoPayload.line.logMessage as line,\n",
99 " COUNT(DISTINCT protoPayload.ip) as ip_count,\n",
100 " COUNT(protoPayload.requestId) AS req_count\n",
101 "FROM\n",
102 " FLATTEN ([logs.appengine_googleapis_com_request_log_$date], protoPayload.line)\n",
103 "WHERE\n",
104 " protoPayload.moduleId is null # == \"default\", otherwise you get backend queries too.\n",
105 " AND\n",
106 " protoPayload.line.logMessage LIKE \"Rate Limit Exceeded%\"\n",
107 " AND\n",
108 " REGEXP_MATCH(protoPayload.line.logMessage, 'X-AppEngine-Country')\n",
109 "GROUP BY\n",
110 " line\n",
111 "ORDER BY\n",
112 " req_count DESC\n",
113 "LIMIT\n",
114 " 100;"
115 ]
116 },
117 {
118 "cell_type": "code",
119 "execution_count": null,
120 "metadata": {
121 "collapsed": true
122 },
123 "outputs": [],
124 "source": [
125 "%%sql --module by_resource\n",
126 "SELECT\n",
127 " protoPayload.resource as resource,\n",
128 " COUNT(protoPayload.requestId) AS req_count\n",
129 "FROM\n",
130 " [logs.appengine_googleapis_com_request_log_$date]\n",
131 "WHERE\n",
132 " protoPayload.moduleId is null # == \"default\", otherwise you get backend queries too.\n",
133 " AND\n",
134 " protoPayload.line.logMessage LIKE \"Rate Limit Exceeded%\"\n",
135 "GROUP BY\n",
136 " resource\n",
137 "ORDER BY\n",
138 " req_count DESC\n",
139 "LIMIT\n",
140 " 100;"
141 ]
142 },
143 {
144 "cell_type": "markdown",
145 "metadata": {},
146 "source": [
147 "## Requests by IP"
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": null,
153 "metadata": {
154 "collapsed": false
155 },
156 "outputs": [],
157 "source": [
158 "df = bq.Query(by_ip, date=date).to_dataframe()\n",
159 "df.head(20)"
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {
166 "collapsed": false
167 },
168 "outputs": [],
169 "source": [
170 "if len(df):\n",
171 " df.plot()"
172 ]
173 },
174 {
175 "cell_type": "markdown",
176 "metadata": {},
177 "source": [
178 "## Requests by IP Class"
179 ]
180 },
181 {
182 "cell_type": "code",
183 "execution_count": null,
184 "metadata": {
185 "collapsed": false
186 },
187 "outputs": [],
188 "source": [
189 "df = bq.Query(by_ip_class, date=date).to_dataframe()\n",
190 "df.head(20)"
191 ]
192 },
193 {
194 "cell_type": "code",
195 "execution_count": null,
196 "metadata": {
197 "collapsed": false
198 },
199 "outputs": [],
200 "source": [
201 "if len(df):\n",
202 " df.plot()"
203 ]
204 },
205 {
206 "cell_type": "markdown",
207 "metadata": {},
208 "source": [
209 "## Requests by Country Code"
210 ]
211 },
212 {
213 "cell_type": "code",
214 "execution_count": null,
215 "metadata": {
216 "collapsed": false
217 },
218 "outputs": [],
219 "source": [
220 "df = bq.Query(by_country, date=date).to_dataframe()\n",
221 "df.head(20)"
222 ]
223 },
224 {
225 "cell_type": "code",
226 "execution_count": null,
227 "metadata": {
228 "collapsed": false
229 },
230 "outputs": [],
231 "source": [
232 "if len(df):\n",
233 " df.plot()"
234 ]
235 },
236 {
237 "cell_type": "markdown",
238 "metadata": {},
239 "source": [
240 "## Requests by Requested Resource"
241 ]
242 },
243 {
244 "cell_type": "code",
245 "execution_count": null,
246 "metadata": {
247 "collapsed": false
248 },
249 "outputs": [],
250 "source": [
251 "df = bq.Query(by_resource, date=date).to_dataframe()\n",
252 "df.head(20)"
253 ]
254 },
255 {
256 "cell_type": "code",
257 "execution_count": null,
258 "metadata": {
259 "collapsed": false
260 },
261 "outputs": [],
262 "source": [
263 "if len(df):\n",
264 " df.plot()"
265 ]
266 },
267 {
268 "cell_type": "code",
269 "execution_count": null,
270 "metadata": {
271 "collapsed": true
272 },
273 "outputs": [],
274 "source": []
275 }
276 ],
277 "metadata": {
278 "kernelspec": {
279 "display_name": "Python 2",
280 "language": "python",
281 "name": "python2"
282 },
283 "language_info": {
284 "codemirror_mode": {
285 "name": "ipython",
286 "version": 2
287 },
288 "file_extension": ".py",
289 "mimetype": "text/x-python",
290 "name": "python",
291 "nbconvert_exporter": "python",
292 "pygments_lexer": "ipython2",
293 "version": "2.7.9"
294 }
295 },
296 "nbformat": 4,
297 "nbformat_minor": 0
298}