Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use request.environ dict to populate zipkin tags #133

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions pyramid_zipkin/request_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]++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/[email protected]/-/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
Expand Down Expand Up @@ -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
51 changes: 51 additions & 0 deletions tests/request_helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Loading