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