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

Add Serverless Framework Support #127

Merged
merged 6 commits into from
Feb 26, 2019
Merged

Conversation

chanchiem
Copy link
Contributor

Added support for the Serverless Framework. This is often used by applications that utilize AWS Lambda as the compute and API Gateway as the endpoint which invokes AWS Lambda. Specifically, works for Flask and Django deployments as well as through the tool Zappa.

Removes the "FacadeSegments cannot be mutated" exception error and generates a subsegment whenever segments are created.

Issue #, if available:
#2

Description of changes:
A new "Serverless" context is created to give the abstraction of Segments being the top level entities but is then converted to a subsegment upon transmission to the data plane. These segments are called MimicSegments and their only purposes is to be treated as a Segment all the way until transmission via the Recorder. All MimicSegments are parented by FacadeSegments.

Currently supports Flask and Django as middlewares; this has been confirmed to be natively working with Zappa if the application is running under Flask/Django.

The middlewares Flask and Django automatically use the ServerlessContext when it is present in a Lambda environment.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

aws_xray_sdk/core/models/mimic_segment.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/models/mimic_segment.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/models/mimic_segment.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/recorder.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/serverless_context.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/serverless_context.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/serverless_context.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/serverless_context.py Outdated Show resolved Hide resolved
chanchiem added a commit to chanchiem/aws-xray-sdk-python that referenced this pull request Feb 15, 2019
* Renamed ServerlessContext to ServerlessLambdaContext to be more precise about its purpose.
* Instead of explicitly using Context class's put_subsegment/put_segment methods, use LambdaContext's
grandparent class to store. LambdaContext's methods are really only used to acquire the FacadeSegment.
* Parameters when initializing the mimic segment are now required.
* Unit tests added to test these changes.
@chanchiem
Copy link
Contributor Author

Revision 2 Added:

  • Renamed ServerlessContext to ServerlessLambdaContext to be more precise about its purpose.
  • Instead of explicitly using Context class's put_subsegment/put_segment methods, call LambdaContext's grandparent class to store segments for code conciseness. LambdaContext's methods are really only used to acquire the FacadeSegment.
  • Parameters when initializing the mimic segment are now required.
  • Unit tests added to test these changes.

    * Serverless architecture in this case includes one that utilizes
      Lambda and API Gateway.
    * A new "Serverless" context is created to give the abstraction of
      Segments being the toplevel entities but is then converted to a
      subsegment upon transmission to the data plane.
      These segments are called MimicSegments.  All generated
      segments have a parent segment that is the FacadeSegment.
    * Currently supports Flask and Django as middlewares; this has been
      confirmed to be natively working with Zappa if the application is
      running under Flask/Django.
* Renamed ServerlessContext to ServerlessLambdaContext to be more precise about its purpose.
* Instead of explicitly using Context class's put_subsegment/put_segment methods, use LambdaContext's
grandparent class to store. LambdaContext's methods are really only used to acquire the FacadeSegment.
* Parameters when initializing the mimic segment are now required.
* Unit tests added to test these changes.
aws_xray_sdk/core/models/mimic_segment.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/recorder.py Outdated Show resolved Hide resolved
aws_xray_sdk/core/serverless_lambda_context.py Outdated Show resolved Hide resolved
* Mimic Segments now perform no-op on segment-only methods.
* Added comments to address this contract.
* Unit tests to ensure segment-only methods don't add data
to the mimic segment. Serialization methods are also tested
to ensure this.
@chanchiem
Copy link
Contributor Author

Revision 3 added

  • Mimic Segments now perform no-op on segment-only methods to ensure that no breaking changes will be sent to the data plane.
  • Added comments to docs to address this contract.
  • Unit tests to ensure segment-only methods don't add data to the mimic segment. Serialization methods are also tested to ensure this.

* Revised design; no more serverless context.
* Subsegment generated when the middleware adds request
* Unit tests to ensure that these subsegments are added on top of the facade segment
@chanchiem
Copy link
Contributor Author

Revision 4 Added:

  • Revised design; no more serverless context; simpler design that is changed at the higher level middleware calls.
  • get/save_origin_trace_header moved from segment to entity so subsegments can also propagate the header information. This is automatically deleted upon transmission and is only used to get request contexts in middlewares before and after requests.
  • Subsegment generated when the middleware intercepts incoming request
  • Unit tests to ensure that these subsegments are added on top of the facade segment

* Comment change added for save_origin_trace_header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants