| OLD | NEW |
| 1 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 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 """Report ts_mon metrics for HTTP requests made by the `requests` library. | 5 """Report ts_mon metrics for HTTP requests made by the `requests` library. |
| 6 | 6 |
| 7 This module provides get(), post(), etc. methods that wrap the corresponding | 7 This module provides get(), post(), etc. methods that wrap the corresponding |
| 8 methods in the requests module. They take an additional first 'name' argument | 8 methods in the requests module. They take an additional first 'name' argument |
| 9 which is an identifier for the type of request being made. | 9 which is an identifier for the type of request being made. |
| 10 | 10 |
| 11 Example:: | 11 Example:: |
| 12 | 12 |
| 13 from infra_libs import instrumented_requests | 13 from infra_libs import instrumented_requests |
| 14 r = instrumented_requests.get('myapi', 'https://example.com/api') | 14 r = instrumented_requests.get('myapi', 'https://example.com/api') |
| 15 | 15 |
| 16 Alternatively you can add the hook manually:: | 16 Alternatively you can add the hook manually:: |
| 17 | 17 |
| 18 import requests | 18 import requests |
| 19 from infra_libs import instrumented_requests | 19 from infra_libs import instrumented_requests |
| 20 r = requests.get( | 20 r = requests.get( |
| 21 'https://example.com/api', | 21 'https://example.com/api', |
| 22 hooks={'response': instrumented_requests.instrumentation_hook('myapi')}) | 22 hooks={'response': instrumented_requests.instrumentation_hook('myapi')}) |
| 23 """ | 23 """ |
| 24 | 24 |
| 25 import functools | 25 import functools |
| 26 | 26 |
| 27 import requests | 27 import requests |
| 28 | 28 |
| 29 from infra_libs import http_metrics | 29 from infra_libs.ts_mon.common import http_metrics |
| 30 | 30 |
| 31 | 31 |
| 32 def instrumentation_hook(name): | 32 def instrumentation_hook(name): |
| 33 """Returns a hook function that records ts_mon metrics about the request. | 33 """Returns a hook function that records ts_mon metrics about the request. |
| 34 | 34 |
| 35 Usage:: | 35 Usage:: |
| 36 | 36 |
| 37 r = requests.get( | 37 r = requests.get( |
| 38 'https://example.com/api', | 38 'https://example.com/api', |
| 39 hooks={'response': instrumented_requests.instrumentation_hook('myapi')}) | 39 hooks={'response': instrumented_requests.instrumentation_hook('myapi')}) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 75 |
| 76 | 76 |
| 77 request = functools.partial(_wrap, 'request') | 77 request = functools.partial(_wrap, 'request') |
| 78 get = functools.partial(_wrap, 'get') | 78 get = functools.partial(_wrap, 'get') |
| 79 head = functools.partial(_wrap, 'head') | 79 head = functools.partial(_wrap, 'head') |
| 80 post = functools.partial(_wrap, 'post') | 80 post = functools.partial(_wrap, 'post') |
| 81 patch = functools.partial(_wrap, 'patch') | 81 patch = functools.partial(_wrap, 'patch') |
| 82 put = functools.partial(_wrap, 'put') | 82 put = functools.partial(_wrap, 'put') |
| 83 delete = functools.partial(_wrap, 'delete') | 83 delete = functools.partial(_wrap, 'delete') |
| 84 options = functools.partial(_wrap, 'options') | 84 options = functools.partial(_wrap, 'options') |
| OLD | NEW |