blob: fd10684d75b0b32db300574e5d33a7f56ca1430d [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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
8Updates frontend/js_errors ts_mon metric.
9"""
10from __future__ import print_function
11from __future__ import division
12from __future__ import absolute_import
13
14import json
15import logging
16
17from framework import jsonfeed
18
19from infra_libs import ts_mon
20
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020021
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020022class ClientMonitor(jsonfeed.FlaskJsonFeed):
Copybara854996b2021-09-07 19:36:02 +000023 """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ínez9f9ade52022-10-10 23:20:11 +020039 post_data = mr.request.values
Copybara854996b2021-09-07 19:36:02 +000040 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ínezde942802022-07-15 14:06:55 +020054
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020055 def PostClientMonitor(self, **kwargs):
56 return self.handler(**kwargs)