Skip to content

Commit

Permalink
Add user to any instrument-decorated function if available
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Nov 4, 2024
1 parent 33b1002 commit 8608caf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions services/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def span_decorator(func):

@wraps(func)
def wrap_with_span(*args, **kwargs):
bound_args = func_signature.bind(*args, **kwargs).arguments
if "request" in bound_args:
user = bound_args["request"].user
attributes_dict["user"] = user.username if user else ""

if func_attributes is not None:
bound_args = func_signature.bind(*args, **kwargs).arguments
for attribute, parameter_name in func_attributes.items():
Expand Down
27 changes: 21 additions & 6 deletions tests/integration/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest
from django.conf import settings

from opentelemetry import trace

from tests import factories
Expand Down Expand Up @@ -71,10 +70,26 @@ def test_middleware_user_trace(airlock_client, responses):
tracer = trace.get_tracer("test")
with tracer.start_as_current_span("mock_django_span"):
response = airlock_client.get("/workspaces/view/workspace/")

assert response.status_code == 200

mock_django_span_attributes, = [
span.attributes for span in get_trace() if span.name == "mock_django_span"
]
assert mock_django_span_attributes == {"workspace": "workspace", "user": user.username}
traces = {span.name: span.attributes for span in get_trace()}
assert traces["mock_django_span"] == {
"workspace": "workspace",
"user": user.username,
}
assert traces["workspace_view"] == {"workspace": "workspace", "user": user.username}


@pytest.mark.django_db
def test_middleware_user_trace_with_no_user(airlock_client, responses):
# In tests the current span in the middleware is a NonRecordingSpan,
# so call the endpoint inside another span so we can assert that the
# user attribute is added during the middleware
tracer = trace.get_tracer("test")
with tracer.start_as_current_span("mock_django_span"):
response = airlock_client.get("/login/")

assert response.status_code == 200
traces = {span.name: span.attributes for span in get_trace()}
assert traces["mock_django_span"] == {"user": ""}
2 changes: 1 addition & 1 deletion tests/integration/views/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,7 @@ def test_request_view_tracing_with_request_attribute(
airlock_client.get(url)
traces = get_trace()
last_trace = traces[-1]
assert last_trace.attributes == {"release_request": release_request.id}
assert last_trace.attributes == {"release_request": release_request.id, "user": login_as}


def test_group_edit_success(airlock_client):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/views/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,4 +1157,4 @@ def test_workspace_view_tracing_with_workspace_attribute(
airlock_client.get(urlpath)
traces = get_trace()
last_trace = traces[-1]
assert last_trace.attributes == {"workspace": "test-workspace"}
assert last_trace.attributes == {"workspace": "test-workspace", "user": airlock_client.user.username}

0 comments on commit 8608caf

Please sign in to comment.