Skip to content
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

Segment assumed started under lambda results in exception #137

Closed
IlyaSukhanov opened this issue Feb 27, 2019 · 3 comments
Closed

Segment assumed started under lambda results in exception #137

IlyaSukhanov opened this issue Feb 27, 2019 · 3 comments

Comments

@IlyaSukhanov
Copy link
Contributor

This is a regression from #127

[ERROR]	cannot find the current segment/subsegment, please make sure you have a segment open
[WARNING]	No segment found, cannot begin subsegment foo.app.
[ERROR]		Exception on / [GET]
Traceback (most recent call last):
File "/var/task/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/var/task/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/var/task/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/var/task/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/var/task/flask/_compat.py", line 35, in reraise
raise value
File "/var/task/flask/app.py", line 1811, in full_dispatch_request
rv = self.preprocess_request()
File "/var/task/flask/app.py", line 2087, in preprocess_request
rv = func()
File "/var/task/aws_xray_sdk/ext/flask/middleware.py", line 57, in _before_request
segment.save_origin_trace_header(xray_header)
AttributeError: 'NoneType' object has no attribute 'save_origin_trace_header'

For context this is under lambda execution of zappa/flask and aws-xray-sdk-python is initiated as follows:

from aws_xray_sdk.core import xray_recorder, patch_all
from aws_xray_sdk.core.lambda_launcher import check_in_lambda
from aws_xray_sdk.core.context import Context
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
[...snip...]
  if check_in_lambda():
      xray_recorder.configure(service=__name__, sampling=False, context=Context())
      XRayMiddleware(app, xray_recorder)
      patch_all()

Its obvious that this is related to application not being able to start segment (not entirely clear to me why that is happening), however if segment is not found for what ever reason expectation is that application starts (with warning) rather than crash with exception.

Assumption that if code is under lambda it must already have segment created is not entirely accurate?

@chanchiem
Copy link
Contributor

chanchiem commented Feb 27, 2019

Hey,

Thank you very much for letting us know about the issue you are having.

This new release added official support for the Serverless model. Prior to this, a common work around for customers to use middlewares in Lambda was to override the recorder to use the default context, which is what Context() is, and to not automatically use the LambdaContext.

This update changed the way middlewares behave in Lambda directly. They automatically create subsegments now instead of segments. So it is no longer necessary to override the recorder's Context.

So to fix your problem, all you have to do is remove the parameter context=Context() from your recorder configuration.

@chanchiem
Copy link
Contributor

This does indicate an even deeper bug: The middleware should only opt to begin subsegments rather than segments when the LambdaContext is used. That way, middlewares would not suddenly change behaviors when custom contexts are used--even if it's being used in a lambda environment. #138

@IlyaSukhanov
Copy link
Contributor Author

Awesome, removing context=Context() solved the problem. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants