最近,我们将X射线添加到代码中,方法是:
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch_all
patch_all()虽然这在AWS上运行良好,但是在调用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发布于 2018-06-11 19:00:55
当您的代码在启用跟踪的AWS Lambda上运行时,Lambda容器将生成一个表示整个函数调用的段。它还将上下文设置为环境变量,以便SDK可以将函数中创建的任何子段链接回父段。
如果您在本地运行相同的代码,SDK仍然尝试为实际的函数代码创建子段,但是它找不到任何上下文,从而抛出您发布的错误。
要解决这个问题,您需要设置一些环境变量,以确保SDK具有与实际Lambda容器上运行的信息相同的信息。
LAMBDA_TASK_ROOT (只有关键问题的存在)。您可以在这里看到源代码:launcher.pyLAMBDA_TRACE_HEADER_KEY,以便该函数具有跟踪上下文。该值必须是跟踪头,您可以在这里看到更多详细信息:https://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html这种解决方法并不理想,因为它需要用户方进行额外的代码更改。我们希望提供更好的客户经验,以测试X射线仪器的Lambda功能在当地.您介意分享更多关于您是如何进行本地测试的细节吗?以及您希望X射线跟踪在这样的测试环境中如何工作,这样我们就可以更好地改进您的用例吗?
https://stackoverflow.com/questions/50320335
复制相似问题