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