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

[FEATURE] Support for aiohttp Trace Config in AsyncHttpConnection for Enhanced Tracing with X-Ray #681

Open
angelcabo opened this issue Feb 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@angelcabo
Copy link

angelcabo commented Feb 27, 2024

Is your feature request related to a problem?

I'm encountering difficulties integrating X-Ray tracing with aiohttp requests made through the AsyncHttpConnection class. The core of the issue is that AsyncHttpConnection does not currently seem to allow passing trace_config information to the aiohttp.ClientSession, which is necessary for implementing detailed tracing with AWS X-Ray in asynchronous environments.

What solution would you like?

Would it be possible for AsyncHttpConnection to either support passing trace_configs as part of its initialization parameters or take an option to initialize the trace hooks? Apologies if this is already supported somehow and I missed it. If that's the case, let me know a way I can hook this up without sub-classing AsyncHttpConnection myself.

What alternatives have you considered?

As a workaround, I subclassed AsyncHttpConnection to manually create an aiohttp.ClientSession with the required trace_configs parameter. While this approach works, it requires additional maintenance and familiarity with the internals of both OpenSearch and aiohttp.

from aws_xray_sdk.ext.aiohttp.client import (
        aws_xray_trace_config,  # pragma: no cover
    )

class CustomAsyncConnection(AsyncHttpConnection):
    async def _create_aiohttp_session(self) -> Any:
        if self.loop is None:
            self.loop = get_running_loop()
        self.session = aiohttp.ClientSession(
            headers=self.headers,
            skip_auto_headers=("accept", "accept-encoding"),
            auto_decompress=True,
            loop=self.loop,
            cookie_jar=aiohttp.DummyCookieJar(),
            response_class=OpenSearchClientResponse,
            connector=aiohttp.TCPConnector(limit=self._limit, use_dns_cache=True, ssl=self._ssl_context),
            trace_configs=[aws_xray_trace_config()],
        )
    test_client = AsyncOpenSearch(
        hosts=[settings.SEARCH_CLUSTER_URL],
        connection_class=CustomAsyncConnection,
    )
    info = await test_client.info()
    print(info)
    await test_client.close()
@angelcabo angelcabo added enhancement New feature or request untriaged Need triage labels Feb 27, 2024
@dblock
Copy link
Member

dblock commented Feb 27, 2024

Seems reasonable. There's also a related issue on wanting to introduce metrics, #678. Feel free to contribute!

@dblock dblock removed the untriaged Need triage label Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants