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 | |
| 22 | # TODO: convert to FlaskJsonFeed while convert to Flask |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 23 | class ClientMonitor(jsonfeed.JsonFeed): |
| 24 | """JSON feed to track client side js errors in ts_mon.""" |
| 25 | |
| 26 | js_errors = ts_mon.CounterMetric('frontend/js_errors', |
| 27 | 'Number of uncaught client-side JS errors.', |
| 28 | None) |
| 29 | |
| 30 | def HandleRequest(self, mr): |
| 31 | """Build up a dictionary of data values to use when rendering the page. |
| 32 | |
| 33 | Args: |
| 34 | mr: commonly used info parsed from the request. |
| 35 | |
| 36 | Returns: |
| 37 | Dict of values used by EZT for rendering the page. |
| 38 | """ |
| 39 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 40 | # TODO: uncomment while convert to flask |
| 41 | # post_data = mr.request.values |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 42 | post_data = mr.request.POST |
| 43 | errors = post_data.get('errors') |
| 44 | try: |
| 45 | errors = json.loads(errors) |
| 46 | |
| 47 | total_errors = 0 |
| 48 | for error_key in errors: |
| 49 | total_errors += errors[error_key] |
| 50 | logging.error('client monitor report (%d): %s', total_errors, |
| 51 | post_data.get('errors')) |
| 52 | self.js_errors.increment_by(total_errors) |
| 53 | except Exception as e: |
| 54 | logging.error('Problem processing client monitor report: %r', e) |
| 55 | |
| 56 | return {} |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame^] | 57 | |
| 58 | # def GetClientMonitor(self, **kwargs): |
| 59 | # return self.handler(**kwargs) |
| 60 | |
| 61 | # def PostClientMonitor(self, **kwargs): |
| 62 | # return self.handler(**kwargs) |