-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Client request tracing. Enable passing variables from context to TracingConfig #2754
Comments
Yeps. IMHO the glue to pass through information between third libraries can be done without coupling that glue to the import aiocontext
from aiohttp import web, TraceConfig, ClientSession
@web.middleware
async def aws_xray_header(request, handler):
if "X-AWS-Xray-header" in request['headers']:
aiocontext.set("aws_xray_segment", request.headers['X-AWS-Xray-header'])
return await handler(request)
async def on_request_start(session, trace_config_ctx, params):
aws_xray_segment = aiocontext.get("aws_xray_segment", default=None)
if aws_xray_segment:
trace_config_ctx.start = session.loop.time()
params.headers['X-AWS-Xray-header'] = aws_xray_segment
async def on_request_end(session, trace_config_ctx, params):
aws_xray_segment = aiocontext.get("aws_xray_segment", default=None)
if aws_xray_segment:
await send_timming(aws_xray_segment, session.loop.time() - trace_config_ctx.start)
trace_config = TraceConfig()
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
async def index(request):
async with ClientSession(trace_configs=[trace_config]) as client:
data = await client.get('http://python.org')
return web.Response(text=data)
app = web.Application(loop=loop, middlewares=[aws_xray_header])
app.router.add_get('/', index) The previous snippet uses I would say that this code is able to do the same as you proposed, but without coupling the pattern within the ClientSession code. Indeed you could rewrite your code to something like that: import aiocontext
class my_context:
def __init__(self, name, value):
self.name = name
self.value = value
def __enter__(self):
aiocontext.set('name', self.name)
aiocontext.set('value', self.value)
def __exit__(self):
aiocontext.pop('name')
aiocontext.pop('value')
async def view(....)
with my_context('balancing_group_id', 'group.id'):
async with self.client.get(url) as resp:
... So, or at least the original ideas of the current implementation in terms of how the developer pass information:
|
Proper task local implementation is impossible without Python 3.7 or custom task factory (the state should be cloned on forking a new task. |
@pfreixes you are right, I can see now that I can pass the task context into the event handlers implementation. I can even make the handler
@asvetlov with all do respect I disagree with this point of view. I personally like tools which are small, one-purpose and you can put them together in a way that the authors haven't thought about. Like |
We can return to contextvar usage question after Python 3.7 release. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs. |
Long story short
(this is discussion moved from #2685)
I would like to be able to do the following:
By allowing this I can pass it context-specific variables into the
TraceConfig
instance. This would allow me to save them in a way that will later allow to filter them by the specified params.@pfreixes has pointed out, that
ClientSession
allows passing intrace_request_ctx
through the interface of get/post/put/delete method.My opinion is that it is not sufficient, because will not play nice with the third-party libraries, because high-level methods they expose would have to be update to funnel
trace_request_ctx
.Implementation idea (pseudo-code, not tested)
The text was updated successfully, but these errors were encountered: