AWS X-Ray ERROR:aws_xray_sdk.core.context:cannot find the current segment/subsegment
AWS X-Ray ERROR:aws_xray_sdk.core.context:cannot find the current segment/subsegment
我们最近通过以下方式将 X-Ray 添加到我们的代码中:
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all
patch_all()
虽然这 运行 在 AWS Lambda 上很好,但是在调用 ElasticSearch 期间尝试在本地 运行 时,我们遇到以下异常:
ERROR:aws_xray_sdk.core.context:cannot find the current segment/subsegment, please make sure you have a segment open
queryCustomers - DEBUG - Caught exception for <function search_customer at 0x10bfcf0d0>
Traceback (most recent call last):
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/chalice/app.py", line 659, in _get_view_function_response
response = view_function(**function_args)
File "/Users/jameslin/projects/test-project/src/app.py", line 57, in search_customer
return query[0:size].execute().to_dict()['hits']['hits']
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch_dsl/search.py", line 639, in execute
**self._params
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
return func(*args, params=params, **kwargs)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 632, in search
doc_type, '_search'), params=params, body=body)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/transport.py", line 312, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/connection/http_requests.py", line 71, in perform_request
prepared_request = self.session.prepare_request(request)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/ext/requests/patch.py", line 38, in _inject_header
inject_trace_header(headers, xray_recorder.current_subsegment())
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 251, in current_subsegment
entity = self.get_trace_entity()
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 316, in get_trace_entity
return self.context.get_trace_entity()
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 93, in get_trace_entity
return self.handle_context_missing()
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 118, 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
我不知道他的意思以及如何摆脱它,我的 google 尝试没有给出很多相关结果,我也尝试 运行 在本地安装 X 射线守护程序,但仍然遇到同样的问题:
./xray_mac -o -n ap-southeast-2
当您的代码在启用跟踪的 AWS Lambda 上 运行ning 时,Lambda 容器将生成一个表示整个函数调用的段。它还将上下文设置为环境变量,因此 SDK 可以 link 在函数内创建的任何子分段返回到 parent 分段。
如果您 运行 在本地使用相同的代码,SDK 仍会尝试为实际功能代码创建子段,但找不到任何上下文,因此会抛出您发布的错误。
要解决此问题,您需要设置一些环境变量以确保 SDK 具有与 运行在实际 Lambda 容器上相同的信息。
- 通过将
LAMBDA_TASK_ROOT
设置为您想要的任何值(只有密钥的存在很重要),确保 SDK 认为它在 Lambda 容器上 运行ning。您可以在此处查看源代码:https://github.com/aws/aws-xray-sdk-python/blob/master/aws_xray_sdk/core/lambda_launcher.py
- 设置
LAMBDA_TRACE_HEADER_KEY
以便函数具有跟踪上下文。该值必须是跟踪 header,您可以在此处查看更多详细信息:https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
此解决方法并不理想,因为它需要用户端进行额外的代码更改。我们希望为在本地测试 X-Ray 检测的 Lambda 函数提供更好的客户体验。您介意分享有关您如何进行本地测试以及您希望 X-Ray 跟踪在此类测试环境中如何工作的更多详细信息,以便我们可以更好地改进您的用例吗?
我们最近通过以下方式将 X-Ray 添加到我们的代码中:
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all
patch_all()
虽然这 运行 在 AWS Lambda 上很好,但是在调用 ElasticSearch 期间尝试在本地 运行 时,我们遇到以下异常:
ERROR:aws_xray_sdk.core.context:cannot find the current segment/subsegment, please make sure you have a segment open
queryCustomers - DEBUG - Caught exception for <function search_customer at 0x10bfcf0d0>
Traceback (most recent call last):
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/chalice/app.py", line 659, in _get_view_function_response
response = view_function(**function_args)
File "/Users/jameslin/projects/test-project/src/app.py", line 57, in search_customer
return query[0:size].execute().to_dict()['hits']['hits']
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch_dsl/search.py", line 639, in execute
**self._params
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
return func(*args, params=params, **kwargs)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 632, in search
doc_type, '_search'), params=params, body=body)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/transport.py", line 312, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/elasticsearch/connection/http_requests.py", line 71, in perform_request
prepared_request = self.session.prepare_request(request)
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/ext/requests/patch.py", line 38, in _inject_header
inject_trace_header(headers, xray_recorder.current_subsegment())
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 251, in current_subsegment
entity = self.get_trace_entity()
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 316, in get_trace_entity
return self.context.get_trace_entity()
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 93, in get_trace_entity
return self.handle_context_missing()
File "/Users/jameslin/virtualenvs/test-project/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 118, 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
我不知道他的意思以及如何摆脱它,我的 google 尝试没有给出很多相关结果,我也尝试 运行 在本地安装 X 射线守护程序,但仍然遇到同样的问题:
./xray_mac -o -n ap-southeast-2
当您的代码在启用跟踪的 AWS Lambda 上 运行ning 时,Lambda 容器将生成一个表示整个函数调用的段。它还将上下文设置为环境变量,因此 SDK 可以 link 在函数内创建的任何子分段返回到 parent 分段。
如果您 运行 在本地使用相同的代码,SDK 仍会尝试为实际功能代码创建子段,但找不到任何上下文,因此会抛出您发布的错误。
要解决此问题,您需要设置一些环境变量以确保 SDK 具有与 运行在实际 Lambda 容器上相同的信息。
- 通过将
LAMBDA_TASK_ROOT
设置为您想要的任何值(只有密钥的存在很重要),确保 SDK 认为它在 Lambda 容器上 运行ning。您可以在此处查看源代码:https://github.com/aws/aws-xray-sdk-python/blob/master/aws_xray_sdk/core/lambda_launcher.py - 设置
LAMBDA_TRACE_HEADER_KEY
以便函数具有跟踪上下文。该值必须是跟踪 header,您可以在此处查看更多详细信息:https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html
此解决方法并不理想,因为它需要用户端进行额外的代码更改。我们希望为在本地测试 X-Ray 检测的 Lambda 函数提供更好的客户体验。您介意分享有关您如何进行本地测试以及您希望 X-Ray 跟踪在此类测试环境中如何工作的更多详细信息,以便我们可以更好地改进您的用例吗?