Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 1 | # Copyright 2018 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """ts_mon JavaScript proxy handler.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | from framework import authdata |
| 11 | from framework import sql |
| 12 | from framework import xsrf |
| 13 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 14 | from gae_ts_mon.flask_handlers import TSMonJSFlaskHandler |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 15 | |
| 16 | from google.appengine.api import users |
| 17 | |
| 18 | from infra_libs import ts_mon |
| 19 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 20 | import flask |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 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 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 82 | class MonorailTSMonJSHandler(TSMonJSFlaskHandler): |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 83 | |
| 84 | def __init__(self, services=None): |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 85 | super(MonorailTSMonJSHandler, self).__init__( |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 86 | flask=flask, services=services) |
| 87 | self.register_metrics( |
| 88 | [ |
| 89 | ISSUE_CREATE_LATENCY_METRIC, ISSUE_UPDATE_LATENCY_METRIC, |
| 90 | AUTOCOMPLETE_POPULATE_LATENCY_METRIC, |
| 91 | CHARTS_SWITCH_DATE_RANGE_METRIC, ISSUE_COMMENTS_LOAD_LATENCY_METRIC, |
| 92 | DOM_CONTENT_LOADED_METRIC, ISSUE_LIST_LOAD_LATENCY_METRIC |
| 93 | ]) |
| 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.service |
| 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 |
| 111 | |
| 112 | def PostMonorailTSMonJSHandler(self): |
| 113 | return self.post() |