Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 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 | """A class to log client-side javascript error reports. |
| 7 | |
| 8 | Updates frontend/js_errors ts_mon metric. |
| 9 | """ |
| 10 | from __future__ import print_function |
| 11 | from __future__ import division |
| 12 | from __future__ import absolute_import |
| 13 | |
| 14 | import json |
| 15 | import logging |
| 16 | |
| 17 | from framework import jsonfeed |
| 18 | |
| 19 | from infra_libs import ts_mon |
| 20 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 21 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 22 | class ClientMonitor(jsonfeed.FlaskJsonFeed): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 23 | """JSON feed to track client side js errors in ts_mon.""" |
| 24 | |
| 25 | js_errors = ts_mon.CounterMetric('frontend/js_errors', |
| 26 | 'Number of uncaught client-side JS errors.', |
| 27 | None) |
| 28 | |
| 29 | def HandleRequest(self, mr): |
| 30 | """Build up a dictionary of data values to use when rendering the page. |
| 31 | |
| 32 | Args: |
| 33 | mr: commonly used info parsed from the request. |
| 34 | |
| 35 | Returns: |
| 36 | Dict of values used by EZT for rendering the page. |
| 37 | """ |
| 38 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 39 | post_data = mr.request.values |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 40 | errors = post_data.get('errors') |
| 41 | try: |
| 42 | errors = json.loads(errors) |
| 43 | |
| 44 | total_errors = 0 |
| 45 | for error_key in errors: |
| 46 | total_errors += errors[error_key] |
| 47 | logging.error('client monitor report (%d): %s', total_errors, |
| 48 | post_data.get('errors')) |
| 49 | self.js_errors.increment_by(total_errors) |
| 50 | except Exception as e: |
| 51 | logging.error('Problem processing client monitor report: %r', e) |
| 52 | |
| 53 | return {} |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 54 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 55 | def PostClientMonitor(self, **kwargs): |
| 56 | return self.handler(**kwargs) |