blob: c7062f9d1b0f4494faf5b8c59cda5e673a2ea62c [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Monitoring ts_mon custom to monorail."""
6
7from infra_libs import ts_mon
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +02008
Copybara854996b2021-09-07 19:36:02 +00009from framework import framework_helpers
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020010from framework import logger
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020011import settings
Copybara854996b2021-09-07 19:36:02 +000012
13
14def 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
23API_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ínezac4a6442022-05-15 19:05:13 +020030
31def IncrementAPIRequestsCount(
32 version, client_id, client_email=None, handler='none'):
33 # type: (str, str, Optional[str], Optional[str]) -> None
Copybara854996b2021-09-07 19:36:02 +000034 """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ínezac4a6442022-05-15 19:05:13 +020048 if not settings.unit_test_mode:
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020049 logger.log(
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020050 {
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020051 'log_type': 'IncrementAPIRequestsCount',
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020052 'client_id': client_id,
53 'client_email': client_email,
54 'requests_count': str(API_REQUESTS_COUNT.get(fields)),
55 'endpoint': handler
56 })
57
Copybara854996b2021-09-07 19:36:02 +000058
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.
61DURATION_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.
65SIZE_BUCKETER = ts_mon.GeometricBucketer(10**0.1)
66
67# TODO(https://crbug.com/monorail/9281): Differentiate internal/external calls.
68SERVER_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
79def AddServerDurations(elapsed_ms, fields):
80 # type: (int, Dict[str, Union[int, bool]]) -> None
81 SERVER_DURATIONS.add(elapsed_ms, fields=fields)
82
83
84SERVER_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
93def IncrementServerResponseStatusCount(fields):
94 # type: (Dict[str, Union[int, bool]]) -> None
95 SERVER_RESPONSE_STATUS.increment(fields=fields)
96
97
98SERVER_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
108def AddServerRequesteBytes(request_length, fields):
109 # type: (int, Dict[str, Union[int, bool]]) -> None
110 SERVER_REQUEST_BYTES.add(request_length, fields=fields)
111
112
113SERVER_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
123def AddServerResponseBytes(response_length, fields):
124 SERVER_RESPONSE_BYTES.add(response_length, fields=fields)