Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: third_party/google-endpoints/google/api/control/metric_descriptor.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """metric_descriptor provides funcs for working with `MetricDescriptor` instance s.
16
17 :class:`KnownMetrics` is an :class:`enum.Enum` that defines the list of known
18 `MetricDescriptor` instances. It is a complex enumeration that includes various
19 attributes including
20
21 - the full metric name
22 - the kind of the metric
23 - the value type of the metric
24 - a func for updating :class:`Operation`s from a `ReportRequestInfo`
25
26 """
27
28 from __future__ import absolute_import
29
30
31 from enum import Enum
32 from . import distribution, metric_value, messages, MetricKind, ValueType
33
34
35 def _add_metric_value(name, value, an_op):
36 an_op.metricValueSets.append(
37 messages.MetricValueSet(metricName=name, metricValues=[value]))
38
39
40 def _add_int64_metric_value(name, value, an_op):
41 _add_metric_value(
42 name, metric_value.create(int64Value=value), an_op)
43
44
45 def _set_int64_metric_to_constant_1(name, dummy_info, op):
46 _add_int64_metric_value(name, 1, op)
47
48
49 def _set_int64_metric_to_constant_1_if_http_error(name, info, op):
50 if info.response_code >= 400:
51 _add_int64_metric_value(name, 1, op)
52
53
54 def _add_distribution_metric_value(name, value, an_op, distribution_args):
55 d = distribution.create_exponential(*distribution_args)
56 distribution.add_sample(value, d)
57 _add_metric_value(
58 name, metric_value.create(distributionValue=d), an_op)
59
60
61 _SIZE_DISTRIBUTION_ARGS = (8, 10.0, 1.0)
62
63
64 def _set_distribution_metric_to_request_size(name, info, an_op):
65 if info.request_size >= 0:
66 _add_distribution_metric_value(name, info.request_size, an_op,
67 _SIZE_DISTRIBUTION_ARGS)
68
69
70 def _set_distribution_metric_to_response_size(name, info, an_op):
71 if info.response_size >= 0:
72 _add_distribution_metric_value(name, info.response_size, an_op,
73 _SIZE_DISTRIBUTION_ARGS)
74
75
76 _TIME_DISTRIBUTION_ARGS = (8, 10.0, 1e-6)
77
78
79 def _set_distribution_metric_to_request_time(name, info, an_op):
80 if info.request_time:
81 _add_distribution_metric_value(name, info.request_time.total_seconds(),
82 an_op, _TIME_DISTRIBUTION_ARGS)
83
84
85 def _set_distribution_metric_to_backend_time(name, info, an_op):
86 if info.backend_time:
87 _add_distribution_metric_value(name, info.backend_time.total_seconds(),
88 an_op, _TIME_DISTRIBUTION_ARGS)
89
90
91 def _set_distribution_metric_to_overhead_time(name, info, an_op):
92 if info.overhead_time:
93 _add_distribution_metric_value(name, info.overhead_time.total_seconds(),
94 an_op, _TIME_DISTRIBUTION_ARGS)
95
96
97 class Mark(Enum):
98 """Enumerates the types of metric."""
99 PRODUCER = 1
100 CONSUMER = 2
101
102
103 class KnownMetrics(Enum):
104 """Enumerates the known metrics."""
105
106 CONSUMER_REQUEST_COUNT = (
107 'serviceruntime.googleapis.com/api/consumer/request_count',
108 MetricKind.DELTA,
109 ValueType.INT64,
110 _set_int64_metric_to_constant_1,
111 Mark.CONSUMER,
112 )
113 PRODUCER_REQUEST_COUNT = (
114 'serviceruntime.googleapis.com/api/producer/request_count',
115 MetricKind.DELTA,
116 ValueType.INT64,
117 _set_int64_metric_to_constant_1,
118 )
119 CONSUMER_REQUEST_SIZES = (
120 'serviceruntime.googleapis.com/api/consumer/request_sizes',
121 MetricKind.DELTA,
122 ValueType.DISTRIBUTION,
123 _set_distribution_metric_to_request_size,
124 Mark.CONSUMER,
125 )
126 PRODUCER_REQUEST_SIZES = (
127 'serviceruntime.googleapis.com/api/producer/request_sizes',
128 MetricKind.DELTA,
129 ValueType.DISTRIBUTION,
130 _set_distribution_metric_to_request_size,
131 )
132 CONSUMER_RESPONSE_SIZES = (
133 'serviceruntime.googleapis.com/api/consumer/response_sizes',
134 MetricKind.DELTA,
135 ValueType.DISTRIBUTION,
136 _set_distribution_metric_to_response_size,
137 Mark.CONSUMER,
138 )
139 PRODUCER_RESPONSE_SIZES = (
140 'serviceruntime.googleapis.com/api/producer/response_sizes',
141 MetricKind.DELTA,
142 ValueType.DISTRIBUTION,
143 _set_distribution_metric_to_response_size,
144 )
145 CONSUMER_ERROR_COUNT = (
146 'serviceruntime.googleapis.com/api/consumer/error_count',
147 MetricKind.DELTA,
148 ValueType.INT64,
149 _set_int64_metric_to_constant_1_if_http_error,
150 Mark.CONSUMER,
151 )
152 PRODUCER_ERROR_COUNT = (
153 'serviceruntime.googleapis.com/api/producer/error_count',
154 MetricKind.DELTA,
155 ValueType.INT64,
156 _set_int64_metric_to_constant_1_if_http_error,
157 )
158 CONSUMER_TOTAL_LATENCIES = (
159 'serviceruntime.googleapis.com/api/consumer/total_latencies',
160 MetricKind.DELTA,
161 ValueType.DISTRIBUTION,
162 _set_distribution_metric_to_request_time,
163 Mark.CONSUMER,
164 )
165 PRODUCER_TOTAL_LATENCIES = (
166 'serviceruntime.googleapis.com/api/producer/total_latencies',
167 MetricKind.DELTA,
168 ValueType.DISTRIBUTION,
169 _set_distribution_metric_to_request_time,
170 )
171 CONSUMER_BACKEND_LATENCIES = (
172 'serviceruntime.googleapis.com/api/consumer/backend_latencies',
173 MetricKind.DELTA,
174 ValueType.DISTRIBUTION,
175 _set_distribution_metric_to_backend_time,
176 Mark.CONSUMER,
177 )
178 PRODUCER_BACKEND_LATENCIES = (
179 'serviceruntime.googleapis.com/api/producer/backend_latencies',
180 MetricKind.DELTA,
181 ValueType.DISTRIBUTION,
182 _set_distribution_metric_to_backend_time,
183 )
184 CONSUMER_REQUEST_OVERHEAD_LATENCIES = (
185 'serviceruntime.googleapis.com/api/consumer/request_overhead_latencies',
186 MetricKind.DELTA,
187 ValueType.DISTRIBUTION,
188 _set_distribution_metric_to_overhead_time,
189 Mark.CONSUMER,
190 )
191 PRODUCER_REQUEST_OVERHEAD_LATENCIES = (
192 'serviceruntime.googleapis.com/api/producer/request_overhead_latencies',
193 MetricKind.DELTA,
194 ValueType.DISTRIBUTION,
195 _set_distribution_metric_to_overhead_time,
196 )
197
198 def __init__(self, metric_name, kind, value_type, update_op_func,
199 mark=Mark.PRODUCER):
200 """Constructor.
201
202 update_op_func is used to when updating an `Operation` from a
203 `ReportRequestInfo`.
204
205 Args:
206 metric_name (str): the name of the metric descriptor
207 kind (:class:`MetricKind`): the ``kind`` of the described metric
208 value_type (:class:`ValueType`): the `value type` of the described me tric
209 update_op_func (function): the func to update an operation
210
211 """
212 self.kind = kind
213 self.metric_name = metric_name
214 self.update_op_func = (self._consumer_metric(update_op_func)
215 if mark is Mark.CONSUMER else update_op_func)
216 self.value_type = value_type
217 self.mark = mark
218
219 def matches(self, desc):
220 """Determines if a given metric descriptor matches this enum instance
221
222 Args:
223 desc (:class:`google.api.gen.servicecontrol_v1_messages.MetricDescrip tor`): the
224 instance to test
225
226 Return:
227 `True` if desc is supported, otherwise `False`
228
229 """
230 return (self.metric_name == desc.name and
231 self.kind == desc.metricKind and
232 self.value_type == desc.valueType)
233
234 def do_operation_update(self, info, an_op):
235 """Updates an operation using the assigned update_op_func
236
237 Args:
238 info: (:class:`google.api.control.report_request.Info`): the
239 info instance to update
240 an_op: (:class:`google.api.control.report_request.Info`):
241 the info instance to update
242
243 Return:
244 `True` if desc is supported, otherwise `False`
245
246 """
247 self.update_op_func(self.metric_name, info, an_op)
248
249 def _consumer_metric(self, update_op_func):
250 def resulting_updater(metric_name, info, an_op):
251 if info.api_key_valid:
252 update_op_func(metric_name, info, an_op)
253
254 return resulting_updater
255
256 @classmethod
257 def is_supported(cls, desc):
258 """Determines if the given metric descriptor is supported.
259
260 Args:
261 desc (:class:`google.api.gen.servicecontrol_v1_messages.MetricDescrip tor`): the
262 metric descriptor to test
263
264 Return:
265 `True` if desc is supported, otherwise `False`
266
267 """
268 for m in cls:
269 if m.matches(desc):
270 return True
271 return False
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698