1
1
"""Encapsulate common rest operations between business services propagating trace headers if configured.
2
2
"""
3
+ import logging
4
+
3
5
import opentracing
4
6
import requests
5
- from flask import current_app
7
+ from flask import current_app , request
6
8
9
+ from pyms .constants import LOGGER_NAME
7
10
from pyms .flask .services .driver import DriverService
8
11
12
+ logger = logging .getLogger (LOGGER_NAME )
13
+
9
14
10
15
class Service (DriverService ):
11
16
service = "requests"
@@ -27,10 +32,10 @@ def insert_trace_headers(self, headers):
27
32
28
33
try :
29
34
# FLASK https://github.com/opentracing-contrib/python-flask
30
- span = self ._tracer .get_span ()
31
- self ._tracer ._tracer .inject (span , opentracing .Format .HTTP_HEADERS , headers )
35
+ span = self ._tracer .get_span (request )
36
+ self ._tracer .tracer .inject (span . context , opentracing .Format .HTTP_HEADERS , headers )
32
37
except Exception as ex :
33
- current_app . logger .warning ("Tracer error {}" .format (ex ))
38
+ logger .debug ("Tracer error {}" .format (ex ))
34
39
return headers
35
40
36
41
def _get_headers (self , headers ):
@@ -87,7 +92,7 @@ def get(self, url, path_params=None, params=None, headers=None, **kwargs):
87
92
:param params: (optional) Dictionary, list of tuples or bytes to send in the body of the :class:`Request` (as query
88
93
string parameters)
89
94
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
90
- :param \*\* kwargs: Optional arguments that ``request`` takes.
95
+ :param kwargs: Optional arguments that ``request`` takes.
91
96
:return: :class:`Response <Response>` object
92
97
:rtype: requests.Response
93
98
"""
@@ -109,7 +114,7 @@ def get_for_object(self, url, path_params=None, params=None, headers=None, **kwa
109
114
:param params: (optional) Dictionary, list of tuples or bytes to send in the body of the :class:`Request` (as query
110
115
string parameters)
111
116
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
112
- :param \*\* kwargs: Optional arguments that ``request`` takes.
117
+ :param kwargs: Optional arguments that ``request`` takes.
113
118
:return: :class:`Response <Response>` object
114
119
:rtype: requests.Response
115
120
"""
@@ -126,7 +131,7 @@ def post(self, url, path_params=None, data=None, json=None, headers=None, **kwar
126
131
:class:`Request`.
127
132
:param json: (optional) json data to send in the body of the :class:`Request`.
128
133
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
129
- :param \*\* kwargs: Optional arguments that ``request`` takes.
134
+ :param kwargs: Optional arguments that ``request`` takes.
130
135
:return: :class:`Response <Response>` object
131
136
:rtype: requests.Response
132
137
"""
@@ -149,7 +154,7 @@ def post_for_object(self, url, path_params=None, data=None, json=None, headers=N
149
154
:class:`Request`.
150
155
:param json: (optional) json data to send in the body of the :class:`Request`.
151
156
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
152
- :param \*\* kwargs: Optional arguments that ``request`` takes.
157
+ :param kwargs: Optional arguments that ``request`` takes.
153
158
:return: :class:`Response <Response>` object
154
159
:rtype: requests.Response
155
160
"""
@@ -166,7 +171,7 @@ def put(self, url, path_params=None, data=None, headers=None, **kwargs):
166
171
object to send in the body of the :class:`Request`.
167
172
:param json: (optional) json data to send in the body of the :class:`Request`.
168
173
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
169
- :param \*\* kwargs: Optional arguments that ``request`` takes.
174
+ :param kwargs: Optional arguments that ``request`` takes.
170
175
:return: :class:`Response <Response>` object
171
176
:rtype: requests.Response
172
177
"""
@@ -189,7 +194,7 @@ def put_for_object(self, url, path_params=None, data=None, headers=None, **kwarg
189
194
object to send in the body of the :class:`Request`.
190
195
:param json: (optional) json data to send in the body of the :class:`Request`.
191
196
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
192
- :param \*\* kwargs: Optional arguments that ``request`` takes.
197
+ :param kwargs: Optional arguments that ``request`` takes.
193
198
:return: :class:`Response <Response>` object
194
199
:rtype: requests.Response
195
200
"""
@@ -203,7 +208,7 @@ def delete(self, url, path_params=None, headers=None, **kwargs):
203
208
:param url: URL for the new :class:`Request` object. Could contain path parameters
204
209
:param path_params: (optional) Dictionary, list of tuples with path parameters values to compose url
205
210
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
206
- :param \*\* kwargs: Optional arguments that ``request`` takes.
211
+ :param kwargs: Optional arguments that ``request`` takes.
207
212
:return: :class:`Response <Response>` object
208
213
:rtype: requests.Response
209
214
"""
0 commit comments