Comments (5)
same same for aiohttp?
import asyncio
from aiohttp import web
from aws_xray_sdk.ext.aiohttp.middleware import middleware
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.async_context import AsyncContext
from aws_xray_sdk.core import patch_all
patch_all()
# Configure X-Ray
xray_recorder.configure(
service='aiohttp_websocket_service',
context=AsyncContext(),
context_missing='RUNTIME_ERROR',
daemon_address='localhost:2000'
)
# @xray_recorder.capture_async('## websocket_handler')
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
segment = xray_recorder.begin_segment('websocket_connection')
print(f"segment id:{segment.trace_id}")
try:
async for msg in ws:
subsegment = xray_recorder.begin_segment('process_message')
if msg.type == web.WSMsgType.TEXT:
await ws.send_str(msg.data)
elif msg.type == web.WSMsgType.ERROR:
print(f'WebSocket connection closed with exception {ws.exception()}')
xray_recorder.end_segment()
except Exception as e:
print(e)
segment.add_exception(e)
xray_recorder.end_segment()
return ws
async def init_app():
app = web.Application(middlewares=[middleware])
app.add_routes([web.get('/ws', websocket_handler)])
return app
if __name__ == '__main__':
app = init_app()
web.run_app(app, port=8765)
from aws-xray-sdk-python.
doesnt even work with asyncio only:
import asyncio
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.async_context import AsyncContext
# Configure X-Ray
xray_recorder.configure(
service='asyncio_service',
context=AsyncContext(),
context_missing='RUNTIME_ERROR',
daemon_address='localhost:2000'
)
@xray_recorder.capture_async('## sub1')
async def sub1():
print("sub1")
# time.sleep(1)
@xray_recorder.capture_async('## main')
async def main():
print("main+")
await sub1()
print("main-")
if __name__ == '__main__':
xray_recorder.begin_segment('start')
asyncio.run(main())
print("end")
from aws-xray-sdk-python.
This is the trace:
Traceback (most recent call last):
File "/home/ec2-user/environment/projects/async/async.py", line 26, in
asyncio.run(main())
File "/usr/lib64/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib64/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/async_recorder.py", line 29, in call
return await self.recorder.record_subsegment_async(
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/async_recorder.py", line 75, in record_subsegment_async
subsegment = self.begin_subsegment(name, namespace)
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/recorder.py", line 313, in begin_subsegment
return self._begin_subsegment_helper(name, namespace)
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/recorder.py", line 287, in _begin_subsegment_helper
segment = self.current_segment()
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/recorder.py", line 271, in current_segment
entity = self.get_trace_entity()
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/recorder.py", line 411, in get_trace_entity
return self.context.get_trace_entity()
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/context.py", line 101, in get_trace_entity
return self.handle_context_missing()
File "/home/ec2-user/.local/lib/python3.9/site-packages/aws_xray_sdk/core/context.py", line 125, in handle_context_missing
raise SegmentNotFoundException(MISSING_SEGMENT_MSG)
aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open
Cloud9 with AmazonLinux2023
from aws-xray-sdk-python.
Hi @daTobiGit .
I think it is due to the async nature of these libraries that the parent segment is not present on the same task as the subsegment being created, and I think this is a known bug in the X-Ray SDK.
Since you are just starting out with using X-Ray, I would recommend you to check out the AWS Distro for OpenTelemetry. You can use the ADOT Python to automatically instrument the libraries in your Python application. The current version of the ADOT Python supports the following library instrumentations: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/release/v1.22.x-0.43bx/instrumentation/README.md
from aws-xray-sdk-python.
Hi @srprash,
thank you for your answer. i will try the other solution, but is there already a bugfix on the roadmap?
from aws-xray-sdk-python.
Related Issues (20)
- Very rare ReferenceError HOT 2
- Support for psycopg3 HOT 2
- Link a Textract async operation with downstream process HOT 1
- Current tox versions do not like how testenv.passenv is set
- aws_xray_sdk.core.exceptions.exceptions.SegmentNameMissingException: Segment name is required. HOT 1
- Using psycopg2 connection_factory throws exceptions HOT 4
- Documentation for patch_all HOT 2
- Sampling configuration should discuss DefaultSampler vs. LocalSampler HOT 1
- Flask middleware errors when an earlier Flask extension throws an exception in a before_request method HOT 1
- Patched DB cursor and template may record outside of XRayMiddleware HOT 2
- Segment not propgated when subsegment metadata is sufficiently long HOT 4
- sqlalchemy_core patch errors for unencoded special characters in db url HOT 1
- Error when using AsyncContext in python 3.11.5 HOT 3
- MongoDB AttributeError: 'NoneType' object has no attribute 'put_annotation'
- aws-xray-sdk-python compatibility with Python 3.12 HOT 9
- Fix docstring in Entity.close() HOT 1
- No supported for fastAPI, need to add middleware for fastapi HOT 1
- Incompatible with Pynamodb 6.x HOT 4
- Unable to get RDS or SQL tracing with psycopg2 plugin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-xray-sdk-python.