<jupyter_start><jupyter_text>Analyzing Rate Limit Exceeded eventsUse this notebook to dig into Rate Limit Exceeded events on Monorail.<jupyter_code>import gcp
import gcp.bigquery as bq

context = gcp.Context.default()
print 'The current project is %s' % context.project_id

# Set the date to analyze here:
date = 20160514
%%sql --module by_ip
SELECT
  protoPayload.ip as ip,
  COUNT(protoPayload.requestId) AS num
FROM
  [logs.appengine_googleapis_com_request_log_$date]
WHERE
  protoPayload.moduleId is null # == "default", otherwise you get backend queries too.
  AND
  protoPayload.line.logMessage LIKE "Rate Limit Exceeded%"
GROUP BY
  ip
ORDER BY
  num DESC
LIMIT
  100;
%%sql --module by_ip_class
SELECT
  REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\.]*\.){0}([^\.]*)\.?') AS a,
  REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\.]*\.){1}([^\.]*)\.?') AS b,
  REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\.]*\.){2}([^\.]*)\.?') AS c,
  REGEXP_EXTRACT(protoPayload.ip,r'^(?:[^\.]*\.){3}([^\.]*)\.?') AS d,
  COUNT(protoPayload.requestId) AS num
FROM
  [logs.appengine_googleapis_com_request_log_$date]
WHERE
  protoPayload.moduleId is null # == "default", otherwise you get backend queries too.
  AND
  protoPayload.line.logMessage LIKE "Rate Limit Exceeded%"
GROUP BY
  a,
  b,
  c,
  d
ORDER BY
  num DESC
LIMIT
  100;
%%sql --module by_country
SELECT
  protoPayload.line.logMessage as line,
  COUNT(DISTINCT protoPayload.ip) as ip_count,
  COUNT(protoPayload.requestId) AS req_count
FROM
  FLATTEN ([logs.appengine_googleapis_com_request_log_$date], protoPayload.line)
WHERE
  protoPayload.moduleId is null # == "default", otherwise you get backend queries too.
  AND
  protoPayload.line.logMessage LIKE "Rate Limit Exceeded%"
  AND
  REGEXP_MATCH(protoPayload.line.logMessage, 'X-AppEngine-Country')
GROUP BY
  line
ORDER BY
  req_count DESC
LIMIT
  100;
%%sql --module by_resource
SELECT
  protoPayload.resource as resource,
  COUNT(protoPayload.requestId) AS req_count
FROM
  [logs.appengine_googleapis_com_request_log_$date]
WHERE
  protoPayload.moduleId is null # == "default", otherwise you get backend queries too.
  AND
  protoPayload.line.logMessage LIKE "Rate Limit Exceeded%"
GROUP BY
  resource
ORDER BY
  req_count DESC
LIMIT
  100;<jupyter_output><empty_output><jupyter_text>Requests by IP<jupyter_code>df = bq.Query(by_ip, date=date).to_dataframe()
df.head(20)
if len(df):
  df.plot()<jupyter_output><empty_output><jupyter_text>Requests by IP Class<jupyter_code>df = bq.Query(by_ip_class,  date=date).to_dataframe()
df.head(20)
if len(df):
  df.plot()<jupyter_output><empty_output><jupyter_text>Requests by Country Code<jupyter_code>df = bq.Query(by_country, date=date).to_dataframe()
df.head(20)
if len(df):
  df.plot()<jupyter_output><empty_output><jupyter_text>Requests by Requested Resource<jupyter_code>df = bq.Query(by_resource, date=date).to_dataframe()
df.head(20)
if len(df):
  df.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13233287858117326, 'fraction_numerical': 0.010914051841746248, 'mean_word_length': 5.348484848484849, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '365272', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1070, 'n_words': 246, 'path': 'appengine/monorail/tools/datalab/ratelimiting.ipynb', 'repo_name': 'allaparthi/monorail', 'size': '2932'}