| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import copy | 5 import copy |
| 6 import datetime | 6 import datetime |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import time | 10 import time |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 shared.INTERNAL_CALLBACK_NAME, _internal_callback) | 164 shared.INTERNAL_CALLBACK_NAME, _internal_callback) |
| 165 | 165 |
| 166 logging.info('Initialized ts_mon with service_name=%s, job_name=%s, ' | 166 logging.info('Initialized ts_mon with service_name=%s, job_name=%s, ' |
| 167 'hostname=%s', service_name, job_name, hostname) | 167 'hostname=%s', service_name, job_name, hostname) |
| 168 | 168 |
| 169 | 169 |
| 170 def _instrumented_dispatcher(dispatcher, request, response, time_fn=time.time): | 170 def _instrumented_dispatcher(dispatcher, request, response, time_fn=time.time): |
| 171 start_time = time_fn() | 171 start_time = time_fn() |
| 172 response_status = 0 | 172 response_status = 0 |
| 173 interface.state.store.initialize_context() | 173 interface.state.store.initialize_context() |
| 174 flush_thread = threading.Thread(target=flush_metrics_if_needed) |
| 175 flush_thread.start() |
| 174 try: | 176 try: |
| 175 ret = dispatcher(request, response) | 177 ret = dispatcher(request, response) |
| 176 except webapp2.HTTPException as ex: | 178 except webapp2.HTTPException as ex: |
| 177 response_status = ex.code | 179 response_status = ex.code |
| 178 raise | 180 raise |
| 179 except Exception: | 181 except Exception: |
| 180 response_status = 500 | 182 response_status = 500 |
| 181 raise | 183 raise |
| 182 else: | 184 else: |
| 183 if isinstance(ret, webapp2.Response): | 185 if isinstance(ret, webapp2.Response): |
| 184 response = ret | 186 response = ret |
| 185 response_status = response.status_int | 187 response_status = response.status_int |
| 186 finally: | 188 finally: |
| 189 flush_thread.join() |
| 187 elapsed_ms = int((time_fn() - start_time) * 1000) | 190 elapsed_ms = int((time_fn() - start_time) * 1000) |
| 188 | 191 |
| 189 fields = {'status': response_status, 'name': '', 'is_robot': False} | 192 fields = {'status': response_status, 'name': '', 'is_robot': False} |
| 190 if request.route is not None: | 193 if request.route is not None: |
| 191 # Use the route template regex, not the request path, to prevent an | 194 # Use the route template regex, not the request path, to prevent an |
| 192 # explosion in possible field values. | 195 # explosion in possible field values. |
| 193 fields['name'] = request.route.template | 196 fields['name'] = request.route.template |
| 194 if request.user_agent is not None: | 197 if request.user_agent is not None: |
| 195 # We must not log user agents, but we can store whether or not the | 198 # We must not log user agents, but we can store whether or not the |
| 196 # user agent string indicates that the requester was a Google bot. | 199 # user agent string indicates that the requester was a Google bot. |
| 197 fields['is_robot'] = ( | 200 fields['is_robot'] = ( |
| 198 'GoogleBot' in request.user_agent or | 201 'GoogleBot' in request.user_agent or |
| 199 'GoogleSecurityScanner' in request.user_agent) | 202 'GoogleSecurityScanner' in request.user_agent) |
| 200 | 203 |
| 201 http_metrics.server_durations.add(elapsed_ms, fields=fields) | 204 http_metrics.server_durations.add(elapsed_ms, fields=fields) |
| 202 http_metrics.server_response_status.increment(fields=fields) | 205 http_metrics.server_response_status.increment(fields=fields) |
| 203 if request.content_length is not None: | 206 if request.content_length is not None: |
| 204 http_metrics.server_request_bytes.add(request.content_length, | 207 http_metrics.server_request_bytes.add(request.content_length, |
| 205 fields=fields) | 208 fields=fields) |
| 206 if response.content_length is not None: # pragma: no cover | 209 if response.content_length is not None: # pragma: no cover |
| 207 http_metrics.server_response_bytes.add(response.content_length, | 210 http_metrics.server_response_bytes.add(response.content_length, |
| 208 fields=fields) | 211 fields=fields) |
| 209 flush_metrics_if_needed() | |
| 210 | |
| 211 return ret | 212 return ret |
| 212 | 213 |
| 213 | 214 |
| 214 def instrument_wsgi_application(app, time_fn=time.time): | 215 def instrument_wsgi_application(app, time_fn=time.time): |
| 215 # Don't instrument the same router twice. | 216 # Don't instrument the same router twice. |
| 216 if hasattr(app.router, '__instrumented_by_ts_mon'): | 217 if hasattr(app.router, '__instrumented_by_ts_mon'): |
| 217 return | 218 return |
| 218 | 219 |
| 219 old_dispatcher = app.router.dispatch | 220 old_dispatcher = app.router.dispatch |
| 220 | 221 |
| 221 def dispatch(router, request, response): | 222 def dispatch(router, request, response): |
| 222 return _instrumented_dispatcher(old_dispatcher, request, response, | 223 return _instrumented_dispatcher(old_dispatcher, request, response, |
| 223 time_fn=time_fn) | 224 time_fn=time_fn) |
| 224 | 225 |
| 225 app.router.set_dispatcher(dispatch) | 226 app.router.set_dispatcher(dispatch) |
| 226 app.router.__instrumented_by_ts_mon = True | 227 app.router.__instrumented_by_ts_mon = True |
| 227 | 228 |
| 228 | 229 |
| 229 def reset_for_unittest(): | 230 def reset_for_unittest(): |
| 230 shared.reset_for_unittest() | 231 shared.reset_for_unittest() |
| 231 interface.reset_for_unittest() | 232 interface.reset_for_unittest() |
| OLD | NEW |