Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """ts_mon JavaScript proxy handler.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | from framework import authdata |
| 12 | from framework import sql |
| 13 | from framework import xsrf |
| 14 | |
| 15 | from gae_ts_mon.handlers import TSMonJSHandler |
| 16 | |
| 17 | from google.appengine.api import users |
| 18 | |
| 19 | from infra_libs import ts_mon |
| 20 | |
| 21 | |
| 22 | STANDARD_FIELDS = [ |
| 23 | ts_mon.StringField('client_id'), |
| 24 | ts_mon.StringField('host_name'), |
| 25 | ts_mon.BooleanField('document_visible'), |
| 26 | ] |
| 27 | |
| 28 | |
| 29 | # User action metrics. |
| 30 | ISSUE_CREATE_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric( |
| 31 | 'monorail/frontend/issue_create_latency', ( |
| 32 | 'Latency between Issue Entry form submission and page load of ' |
| 33 | 'the subsequent issue page.' |
| 34 | ), field_spec=STANDARD_FIELDS, |
| 35 | units=ts_mon.MetricsDataUnits.MILLISECONDS) |
| 36 | ISSUE_UPDATE_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric( |
| 37 | 'monorail/frontend/issue_update_latency', ( |
| 38 | 'Latency between Issue Update form submission and page load of ' |
| 39 | 'the subsequent issue page.' |
| 40 | ), field_spec=STANDARD_FIELDS, |
| 41 | units=ts_mon.MetricsDataUnits.MILLISECONDS) |
| 42 | AUTOCOMPLETE_POPULATE_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric( |
| 43 | 'monorail/frontend/autocomplete_populate_latency', ( |
| 44 | 'Latency between page load and autocomplete options loading.' |
| 45 | ), field_spec=STANDARD_FIELDS, |
| 46 | units=ts_mon.MetricsDataUnits.MILLISECONDS) |
| 47 | CHARTS_SWITCH_DATE_RANGE_METRIC = ts_mon.CounterMetric( |
| 48 | 'monorail/frontend/charts/switch_date_range', ( |
| 49 | 'Number of times user clicks frequency button.' |
| 50 | ), field_spec=STANDARD_FIELDS + [ts_mon.IntegerField('date_range')]) |
| 51 | |
| 52 | # Page load metrics. |
| 53 | ISSUE_COMMENTS_LOAD_EXTRA_FIELDS = [ |
| 54 | ts_mon.StringField('template_name'), |
| 55 | ts_mon.BooleanField('full_app_load'), |
| 56 | ] |
| 57 | ISSUE_COMMENTS_LOAD_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric( |
| 58 | 'monorail/frontend/issue_comments_load_latency', ( |
| 59 | 'Time from navigation or click to issue comments loaded.' |
| 60 | ), field_spec=STANDARD_FIELDS + ISSUE_COMMENTS_LOAD_EXTRA_FIELDS, |
| 61 | units=ts_mon.MetricsDataUnits.MILLISECONDS) |
| 62 | DOM_CONTENT_LOADED_EXTRA_FIELDS = [ |
| 63 | ts_mon.StringField('template_name')] |
| 64 | DOM_CONTENT_LOADED_METRIC = ts_mon.CumulativeDistributionMetric( |
| 65 | 'frontend/dom_content_loaded', ( |
| 66 | 'domContentLoaded performance timing.' |
| 67 | ), field_spec=STANDARD_FIELDS + DOM_CONTENT_LOADED_EXTRA_FIELDS, |
| 68 | units=ts_mon.MetricsDataUnits.MILLISECONDS) |
| 69 | |
| 70 | |
| 71 | ISSUE_LIST_LOAD_EXTRA_FIELDS = [ |
| 72 | ts_mon.StringField('template_name'), |
| 73 | ts_mon.BooleanField('full_app_load'), |
| 74 | ] |
| 75 | ISSUE_LIST_LOAD_LATENCY_METRIC = ts_mon.CumulativeDistributionMetric( |
| 76 | 'monorail/frontend/issue_list_load_latency', ( |
| 77 | 'Time from navigation or click to search issues list loaded.' |
| 78 | ), field_spec=STANDARD_FIELDS + ISSUE_LIST_LOAD_EXTRA_FIELDS, |
| 79 | units=ts_mon.MetricsDataUnits.MILLISECONDS) |
| 80 | |
| 81 | |
| 82 | class MonorailTSMonJSHandler(TSMonJSHandler): |
| 83 | |
| 84 | def __init__(self, request=None, response=None): |
| 85 | super(MonorailTSMonJSHandler, self).__init__(request, response) |
| 86 | self.register_metrics([ |
| 87 | ISSUE_CREATE_LATENCY_METRIC, |
| 88 | ISSUE_UPDATE_LATENCY_METRIC, |
| 89 | AUTOCOMPLETE_POPULATE_LATENCY_METRIC, |
| 90 | CHARTS_SWITCH_DATE_RANGE_METRIC, |
| 91 | ISSUE_COMMENTS_LOAD_LATENCY_METRIC, |
| 92 | DOM_CONTENT_LOADED_METRIC, |
| 93 | ISSUE_LIST_LOAD_LATENCY_METRIC]) |
| 94 | |
| 95 | def xsrf_is_valid(self, body): |
| 96 | """This method expects the body dictionary to include two fields: |
| 97 | `token` and `user_id`. |
| 98 | """ |
| 99 | cnxn = sql.MonorailConnection() |
| 100 | token = body.get('token') |
| 101 | user = users.get_current_user() |
| 102 | email = user.email() if user else None |
| 103 | |
| 104 | services = self.app.config.get('services') |
| 105 | auth = authdata.AuthData.FromEmail(cnxn, email, services, autocreate=False) |
| 106 | try: |
| 107 | xsrf.ValidateToken(token, auth.user_id, xsrf.XHR_SERVLET_PATH) |
| 108 | return True |
| 109 | except xsrf.TokenIncorrect: |
| 110 | return False |