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