From 901a499791d9df41b24cab9ecdfdfdc8691aebec Mon Sep 17 00:00:00 2001 From: Emmanuel Ojeah Date: Mon, 23 Sep 2024 15:33:03 +0100 Subject: [PATCH] Use request.environ dict to populate zipkin tags --- pyramid_zipkin/request_helper.py | 60 ++++++++++++++++++++++++-------- tests/request_helper_test.py | 51 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 14 deletions(-) diff --git a/pyramid_zipkin/request_helper.py b/pyramid_zipkin/request_helper.py index 73424d0..6bbdb00 100644 --- a/pyramid_zipkin/request_helper.py +++ b/pyramid_zipkin/request_helper.py @@ -167,31 +167,25 @@ def get_binary_annotations( :param response: the Pyramid response object :returns: binary annotation dict of {str: str} """ - + # use only @property of the request object + # https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/Pylons/webob$@1.8.7++file:%5Esrc/webob/request%5C.py$+@property&patternType=keyword&sm=0 annotations = { - 'http.request.method': request.method, - 'network.protocol.version': request.http_version, - 'url.path': request.path, - 'server.address': request.server_name, - 'server.port': str(request.server_port), - 'url.scheme': request.scheme, 'http.uri': request.path, 'http.uri.qs': request.path_qs, 'otel.library.name': __name__.split('.')[0], 'otel.library.version': __version__, } - if request.user_agent: - annotations['user_agent.original'] = request.user_agent - - if request.query_string: - annotations["url.query"] = request.query_string + if request.client_addr: + annotations['client.address'] = request.client_addr if request.matched_route: annotations['http.route'] = request.matched_route.pattern - if request.client_addr: - annotations['client.address'] = request.client_addr + # update attributes from request.environ object + # https://sourcegraph.com/github.com/Pylons/webob@1.8.7/-/blob/docs/index.txt?L63-68 + request_env = request.environ + _update_annotations_from_request_environ(request_env, annotations) if response: status_code = response.status_code @@ -219,3 +213,41 @@ def get_binary_annotations( settings['zipkin.set_extra_binary_annotations'](request, response) ) return annotations + + +def _update_annotations_from_request_environ( + environ: Dict, + annotations: Dict[str, Optional[str]] +) -> None: + method = environ.get('REQUEST_METHOD', '').strip() + if method: + annotations['http.request.method'] = method + + flavor = environ.get('SERVER_PROTOCOL', '') + if flavor: + annotations['network.protocol.version'] = flavor + + path = environ.get('PATH_INFO') + if path: + annotations['url.path'] = path + + host_name = environ.get('SERVER_NAME') + host_port = environ.get('SERVER_PORT') + + if host_name: + annotations['server.address'] = host_name + + if host_port: + annotations['server.port'] = str(host_port) + + url_scheme = environ.get('wsgi.url_scheme') + if url_scheme: + annotations['url.scheme'] = url_scheme + + user_agent = environ.get('HTTP_USER_AGENT') + if user_agent: + annotations['user_agent.original'] = user_agent + + query_string = environ.get('QUERY_STRING') + if query_string: + annotations['url.query'] = query_string diff --git a/tests/request_helper_test.py b/tests/request_helper_test.py index e616539..63e6c8e 100644 --- a/tests/request_helper_test.py +++ b/tests/request_helper_test.py @@ -181,3 +181,54 @@ def test_convert_signed_hex(): request_helper._convert_signed_hex('-0x3ab5151d76fb85e1') == 'c54aeae289047a1f' ) + + +def test_update_annotations_from_request_environ(): + annotation = {} + request_environ = { + 'REQUEST_METHOD': 'GET', + } + request_helper._update_annotations_from_request_environ( + request_environ, annotation + ) + assert annotation == { + 'http.request.method': 'GET', + } + request_environ = { + 'REQUEST_METHOD': 'POST', + 'PATH_INFO': '/foo', + 'QUERY_STRING': 'bar=baz', + 'SERVER_PROTOCOL': 'HTTP/1.1', + 'HTTP_USER_AGENT': 'test-agent', + } + annotation = {} + request_helper._update_annotations_from_request_environ( + request_environ, annotation + ) + assert annotation == { + 'http.request.method': 'POST', + 'url.path': '/foo', + 'network.protocol.version': 'HTTP/1.1', + 'user_agent.original': 'test-agent', + 'url.query': 'bar=baz', + } + request_environ = {} + annotation = {} + request_helper._update_annotations_from_request_environ( + request_environ, annotation + ) + assert annotation == {} + request_environ = { + 'SERVER_NAME': 'localhost', + 'SERVER_PORT': '8080', + 'wsgi.url_scheme': 'http', + } + annotation = {} + request_helper._update_annotations_from_request_environ( + request_environ, annotation + ) + assert annotation == { + 'server.address': 'localhost', + 'server.port': '8080', + 'url.scheme': 'http', + }