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

[Bug] TestClient for fastapi cause the req.client None error #12733

Open
3 tasks done
CharlieSeastar opened this issue Oct 31, 2024 · 3 comments
Open
3 tasks done

[Bug] TestClient for fastapi cause the req.client None error #12733

CharlieSeastar opened this issue Oct 31, 2024 · 3 comments
Assignees
Labels
bug Something isn't working and you are sure it's a bug! python Python agent related

Comments

@CharlieSeastar
Copy link

CharlieSeastar commented Oct 31, 2024

Search before asking

  • I had searched in the issues and found no similar issues.

Apache SkyWalking Component

Python Agent (apache/skywalking-python)

What happened

sw-fastapi plugin fail to set span.peers when using Fastapi TestClient

What you expected to happen

Fastapi TestClient work properly

How to reproduce

Adding decorator @trace to a custom middleware in Fastapi, and use TestClient to invoke any router.
in custom_middleware.py

class RequestAroundMiddleware(BaseHTTPMiddleware):
    def __init__(self, app, dispatch=None) -> None:
        super().__init__(app=app, dispatch=dispatch)

    @trace("request_around")
    async def dispatch(self, request: Request, call_next):
        return await call_next(request)

in test_demo.py

from fastapi.testclient import TestClient
def test_read_main():
    from main import app
    client = TestClient(app)
    response = client.get("/health")
    assert response.status_code == 200

run pytest
then

async def create_span(self, method, scope, req, send, receive):
        carrier = Carrier()
    
        for item in carrier:
            if item.key.capitalize() in req.headers:
                item.val = req.headers[item.key.capitalize()]
    
        span = NoopSpan(NoopContext()) if config.ignore_http_method_check(method) \
            else get_context().new_entry_span(op=dict(scope)['path'], carrier=carrier, inherit=Component.General)
    
        with span:
            span.layer = Layer.Http
            span.component = Component.FastAPI
>           span.peer = f'{req.client.host}:{req.client.port}'
E           AttributeError: 'NoneType' object has no attribute 'host'

in skywalking/plugins/sw_fastapi.py line 59

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

@CharlieSeastar CharlieSeastar added the bug Something isn't working and you are sure it's a bug! label Oct 31, 2024
@wu-sheng wu-sheng added the python Python agent related label Oct 31, 2024
@wu-sheng wu-sheng added this to the Python - 1.2.0 milestone Oct 31, 2024
@kezhenxu94
Copy link
Member

I’m not sure why you want to trace a TestClient? I think TestClient is for test only and don’t have a peer as expected

@CharlieSeastar
Copy link
Author

CharlieSeastar commented Oct 31, 2024

I’m not sure why you want to trace a TestClient? I think TestClient is for test only and don’t have a peer as expected

Since this decorator is used in the actual code, it isn’t necessary to trace when running the FastAPI TestClient. However, it does impact the actual test results. I shouldn’t have to modify my code just to run tests

@kezhenxu94
Copy link
Member

OK make sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working and you are sure it's a bug! python Python agent related
Projects
None yet
Development

No branches or pull requests

3 participants