4

我们最近通过以下方式将 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

我不知道他的意思是什么以及如何摆脱它,我的谷歌尝试给出的相关结果并不多,我也尝试在本地运行 x 射线守护程序,但仍然遇到同样的问题:

./xray_mac -o -n ap-southeast-2
4

1 回答 1

5

当您的代码在启用了跟踪的 AWS Lambda 上运行时,Lambda 容器将生成一个表示整个函数调用的段。它还将上下文设置为环境变量,以便 SDK 可以将函数内创建的任何子段链接回父段。

如果您在本地运行相同的代码,SDK 仍会尝试为实际功能代码创建子段,但找不到任何上下文,从而引发您发布的错误。

要解决此问题,您需要设置一些环境变量,以确保 SDK 具有与在实际 Lambda 容器上运行的信息相同的信息。

  1. LAMBDA_TASK_ROOT通过设置您想要的任何值来确保 SDK 认为它在 Lambda 容器上运行(只有密钥的存在才重要)。您可以在此处查看源代码:https ://github.com/aws/aws-xray-sdk-python/blob/master/aws_xray_sdk/core/lambda_launcher.py
  2. 设置LAMBDA_TRACE_HEADER_KEY使函数具有跟踪上下文。该值必须是跟踪标头,您可以在此处查看更多详细信息:https ://docs.aws.amazon.com/lambda/latest/dg/lambda-x-ray.html

这种解决方法并不理想,因为它需要从用户端进行额外的代码更改。我们希望为本地测试 X-Ray 检测的 Lambda 函数提供更好的客户体验。您是否介意分享更多关于您如何进行本地测试以及您期望 X 射线跟踪如何在此类测试环境中工作的详细信息,以便我们可以更好地改进您的用例?

于 2018-06-11T19:00:55.893 回答