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

Tornado attributes #706

Merged
merged 8 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,8 @@ def _get_attributes_from_request(request):
SpanAttributes.HTTP_TARGET: request.path,
}

if request.host:
attrs[SpanAttributes.HTTP_HOST] = request.host

if request.remote_ip:
attrs[SpanAttributes.NET_PEER_IP] = request.remote_ip
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here for future contributors describing why we assign these to different attributes.

attrs[SpanAttributes.HTTP_CLIENT_IP] = request.remote_ip

return extract_attributes_from_object(
request, _traced_request_attrs, attrs
Expand All @@ -252,6 +249,11 @@ def _get_operation_name(handler, request):
return f"{class_name}.{request.method.lower()}"


def _get_full_handler_name(handler):
klass = type(handler)
return f"{klass.__module__}.{klass.__qualname__}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this work fine for dynamically generated handlers within a function? I know that is not very common but sounds like an edge case. Any concerns regarding that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe __qualname__ would do the right thing with closures, see the examples in PEP 3155



def _start_span(tracer, handler, start_time) -> _TraceContext:
token = context.attach(extract(handler.request.headers))

Expand All @@ -264,6 +266,7 @@ def _start_span(tracer, handler, start_time) -> _TraceContext:
attributes = _get_attributes_from_request(handler.request)
for key, value in attributes.items():
span.set_attribute(key, value)
span.set_attribute("handler", _get_full_handler_name(handler))
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved

activation = trace.use_span(span, end_on_exit=True)
activation.__enter__() # pylint: disable=E1101
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ def _test_http_method_call(self, method):
SpanAttributes.HTTP_HOST: "127.0.0.1:"
+ str(self.get_http_port()),
SpanAttributes.HTTP_TARGET: "/",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.HTTP_CLIENT_IP: "127.0.0.1",
SpanAttributes.HTTP_STATUS_CODE: 201,
"handler": "tests.tornado_test_app.MainHandler",
},
)

Expand Down Expand Up @@ -220,7 +221,7 @@ def _test_async_handler(self, url, handler_name):
SpanAttributes.HTTP_HOST: "127.0.0.1:"
+ str(self.get_http_port()),
SpanAttributes.HTTP_TARGET: url,
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.HTTP_CLIENT_IP: "127.0.0.1",
SpanAttributes.HTTP_STATUS_CODE: 201,
},
)
Expand Down Expand Up @@ -258,8 +259,9 @@ def test_500(self):
SpanAttributes.HTTP_HOST: "127.0.0.1:"
+ str(self.get_http_port()),
SpanAttributes.HTTP_TARGET: "/error",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.HTTP_CLIENT_IP: "127.0.0.1",
SpanAttributes.HTTP_STATUS_CODE: 500,
"handler": "tests.tornado_test_app.BadHandler",
},
)

Expand Down Expand Up @@ -292,8 +294,9 @@ def test_404(self):
SpanAttributes.HTTP_HOST: "127.0.0.1:"
+ str(self.get_http_port()),
SpanAttributes.HTTP_TARGET: "/missing-url",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.HTTP_CLIENT_IP: "127.0.0.1",
SpanAttributes.HTTP_STATUS_CODE: 404,
"handler": "tornado.web.ErrorHandler",
},
)

Expand Down Expand Up @@ -336,8 +339,9 @@ def test_dynamic_handler(self):
SpanAttributes.HTTP_HOST: "127.0.0.1:"
+ str(self.get_http_port()),
SpanAttributes.HTTP_TARGET: "/dyna",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.HTTP_CLIENT_IP: "127.0.0.1",
SpanAttributes.HTTP_STATUS_CODE: 202,
"handler": "tests.tornado_test_app.DynamicHandler",
},
)

Expand Down Expand Up @@ -377,8 +381,9 @@ def test_handler_on_finish(self):
SpanAttributes.HTTP_HOST: "127.0.0.1:"
+ str(self.get_http_port()),
SpanAttributes.HTTP_TARGET: "/on_finish",
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.HTTP_CLIENT_IP: "127.0.0.1",
SpanAttributes.HTTP_STATUS_CODE: 200,
"handler": "tests.tornado_test_app.FinishedHandler",
},
)

Expand Down