Tuesday, 27 July 2021

Python: fetching urllib3 request headers

We are injecting tracing information into request headers of all the http request calls in our API client library which is implemented based on urllib3

def _init_jaeger_tracer():
    '''Jaeger tracer initialization'''
    config = Config(
        config={
            'sampler': {
                'type': 'const',
                'param': 1,
            },
        },
        service_name="session"
        )
    return config.new_tracer()

class APIObject(rest.APIObject):
    '''Class for injecting traces into urllib3 request headers'''
    def __init__(self, configuration):
        print("RESTClientObject child class called####")
        self._tracer = None
        super().__init__(configuration)
        self._tracer = _init_jaeger_tracer()

    # pylint: disable=W0221
    def request(self, method, url, *args, **kwargs):
        lower_method = method.lower()
        with self._tracer.start_active_span('requests.{}'.format(lower_method)) as scope:
            span = scope.span
            span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_CLIENT)
            span.set_tag(tags.COMPONENT, 'request')
            span.set_tag(tags.HTTP_METHOD, lower_method)
            span.set_tag(tags.HTTP_URL, url)
            headers = kwargs.setdefault('headers', {})
            self._tracer.inject(span.context, Format.HTTP_HEADERS, headers)
    return r

After such implementation, urllib3 request headers look like this,

headers = {
    'Content-Type': 'application/json', 
    'User-Agent': 'API-Generator/1.0.0/python', 
    # **
    'uber-trace-id': '30cef3e816482516:1a4fed2c4863b2f6:0:1'
    # **
}

Now we have to obtain trace-id from the injected request header.

In python "requests" library we have "response.request.headers" attribute to return request headers

In urllib3 i could not find a way return request headers for further processing

Could any of you share some thoughts

Now from this request headers, we need to fetch the 'uber-trace-id' and its value for further building URL.



from Python: fetching urllib3 request headers

No comments:

Post a Comment