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 | """Monitoring ts_mon custom to monorail.""" |
| 6 | |
| 7 | from infra_libs import ts_mon |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 8 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 9 | from framework import framework_helpers |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 10 | from framework import logger |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 11 | import settings |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def GetCommonFields(status, name, is_robot=False): |
| 15 | # type: (int, str, bool=False) -> Dict[str, Union[int, str, bool]] |
| 16 | return { |
| 17 | 'status': status, |
| 18 | 'name': name, |
| 19 | 'is_robot': is_robot, |
| 20 | } |
| 21 | |
| 22 | |
| 23 | API_REQUESTS_COUNT = ts_mon.CounterMetric( |
| 24 | 'monorail/api_requests', |
| 25 | 'Number of requests to Monorail APIs', |
| 26 | [ts_mon.StringField('client_id'), |
| 27 | ts_mon.StringField('client_email'), |
| 28 | ts_mon.StringField('version')]) |
| 29 | |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 30 | |
| 31 | def IncrementAPIRequestsCount( |
| 32 | version, client_id, client_email=None, handler='none'): |
| 33 | # type: (str, str, Optional[str], Optional[str]) -> None |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 34 | """Increment the request count in ts_mon.""" |
| 35 | if not client_email: |
| 36 | client_email = 'anonymous' |
| 37 | elif not framework_helpers.IsServiceAccount(client_email): |
| 38 | # Avoid value explosion and protect PII info |
| 39 | client_email = 'user@email.com' |
| 40 | |
| 41 | fields = { |
| 42 | 'client_id': client_id, |
| 43 | 'client_email': client_email, |
| 44 | 'version': version |
| 45 | } |
| 46 | API_REQUESTS_COUNT.increment_by(1, fields) |
| 47 | |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 48 | if not settings.unit_test_mode: |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 49 | logger.log( |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 50 | { |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 51 | 'log_type': 'IncrementAPIRequestsCount', |
Adrià Vilanova Martínez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 52 | 'client_id': client_id, |
| 53 | 'client_email': client_email, |
| 54 | 'requests_count': str(API_REQUESTS_COUNT.get(fields)), |
| 55 | 'endpoint': handler |
| 56 | }) |
| 57 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 58 | |
| 59 | # 90% of durations are in the range 11-1873ms. Growth factor 10^0.06 puts that |
| 60 | # range into 37 buckets. Max finite bucket value is 12 minutes. |
| 61 | DURATION_BUCKETER = ts_mon.GeometricBucketer(10**0.06) |
| 62 | |
| 63 | # 90% of sizes are in the range 0.17-217014 bytes. Growth factor 10^0.1 puts |
| 64 | # that range into 54 buckets. Max finite bucket value is 6.3GB. |
| 65 | SIZE_BUCKETER = ts_mon.GeometricBucketer(10**0.1) |
| 66 | |
| 67 | # TODO(https://crbug.com/monorail/9281): Differentiate internal/external calls. |
| 68 | SERVER_DURATIONS = ts_mon.CumulativeDistributionMetric( |
| 69 | 'monorail/server_durations', |
| 70 | 'Time elapsed between receiving a request and sending a' |
| 71 | ' response (including parsing) in milliseconds.', [ |
| 72 | ts_mon.IntegerField('status'), |
| 73 | ts_mon.StringField('name'), |
| 74 | ts_mon.BooleanField('is_robot'), |
| 75 | ], |
| 76 | bucketer=DURATION_BUCKETER) |
| 77 | |
| 78 | |
| 79 | def AddServerDurations(elapsed_ms, fields): |
| 80 | # type: (int, Dict[str, Union[int, bool]]) -> None |
| 81 | SERVER_DURATIONS.add(elapsed_ms, fields=fields) |
| 82 | |
| 83 | |
| 84 | SERVER_RESPONSE_STATUS = ts_mon.CounterMetric( |
| 85 | 'monorail/server_response_status', |
| 86 | 'Number of responses sent by HTTP status code.', [ |
| 87 | ts_mon.IntegerField('status'), |
| 88 | ts_mon.StringField('name'), |
| 89 | ts_mon.BooleanField('is_robot'), |
| 90 | ]) |
| 91 | |
| 92 | |
| 93 | def IncrementServerResponseStatusCount(fields): |
| 94 | # type: (Dict[str, Union[int, bool]]) -> None |
| 95 | SERVER_RESPONSE_STATUS.increment(fields=fields) |
| 96 | |
| 97 | |
| 98 | SERVER_REQUEST_BYTES = ts_mon.CumulativeDistributionMetric( |
| 99 | 'monorail/server_request_bytes', |
| 100 | 'Bytes received per http request (body only).', [ |
| 101 | ts_mon.IntegerField('status'), |
| 102 | ts_mon.StringField('name'), |
| 103 | ts_mon.BooleanField('is_robot'), |
| 104 | ], |
| 105 | bucketer=SIZE_BUCKETER) |
| 106 | |
| 107 | |
| 108 | def AddServerRequesteBytes(request_length, fields): |
| 109 | # type: (int, Dict[str, Union[int, bool]]) -> None |
| 110 | SERVER_REQUEST_BYTES.add(request_length, fields=fields) |
| 111 | |
| 112 | |
| 113 | SERVER_RESPONSE_BYTES = ts_mon.CumulativeDistributionMetric( |
| 114 | 'monorail/server_response_bytes', |
| 115 | 'Bytes sent per http request (content only).', [ |
| 116 | ts_mon.IntegerField('status'), |
| 117 | ts_mon.StringField('name'), |
| 118 | ts_mon.BooleanField('is_robot'), |
| 119 | ], |
| 120 | bucketer=SIZE_BUCKETER) |
| 121 | |
| 122 | |
| 123 | def AddServerResponseBytes(response_length, fields): |
| 124 | SERVER_RESPONSE_BYTES.add(response_length, fields=fields) |