feat: add decorator factory to create your own middleware#17
Merged
heitorlessa merged 37 commits intodevelopfrom Apr 20, 2020
Merged
feat: add decorator factory to create your own middleware#17heitorlessa merged 37 commits intodevelopfrom
heitorlessa merged 37 commits intodevelopfrom
Conversation
Contributor
Author
|
Local perf tests to rule out major performance impact on multiple callables - Decorator only prints before handler and after. While Lambda will have less CPU it'll continue to be less than a microsecond, so it worth using this factory to refactor codebase to make it more contributor friendly (dev time is more expensive) No decorator 50000000 loops, best of 5: 6.88 nsec per loop Simpler decorator 50000000 loops, best of 5: 7.18 nsec per loop Decorator factory 50000000 loops, best of 5: 7.25 nsec per loop Simpler Stacked decorator 50000000 loops, best of 5: 7.05 nsec per loop Factory Stacked decorator 50000000 loops, best of 5: 7.05 nsec per loop
def call_handler_simpler(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print("SIMPLER - BEGIN")
response = func(*args, **kwargs)
print("SIMPLER - END")
return response
return wrapper
@lambda_handler_decorator
def call_handler_factory(handler, event, context):
print("[CALL HANDLER] BEGIN")
response = handler(event, context)
print("[CALL HANDLER] END")
return response
def lambda_handler(event, context):
a = {
"statusCode": 200,
"body": "Hello world"
}
print("-------------LAMBDA FN CALLED!-------------")
return a |
* develop: chore: clean up CI workflows fix: CI attempt 4 fix: CI attempt 3 fix: CI attempt 3 fix: CI attempt 2 feat: add docs to CI chore: fix github badge typo chore: pypi monthly download badge
nmoutschen
reviewed
Apr 20, 2020
b77dab7 to
fddc26b
Compare
heitorlessa
added a commit
that referenced
this pull request
Apr 21, 2020
* docs: add pypi badge * fix: add missing single_metric example; test var name * chore: pypi monthly download badge * chore: fix github badge typo * feat: add docs to CI * fix: CI attempt 2 * fix: CI attempt 3 * fix: CI attempt 3 * fix: CI attempt 4 * chore: clean up CI workflows * Decorator factory Feat: Create your own middleware (#17) * feat(utils): add decorator factory * improv: use partial to reduce complexity * improv: add error handling * chore: type hint * docs: include pypi downloads badge * feat: opt in to trace each middleware that runs * improv: add initial util tests * improv: test explicit and implicit trace_execution * improv: test decorator with params * chore: linting * docs: include utilities * improv: correct tests, dec_factory only for func * improv: make util name more explicit * improv: doc trace_execution, fix casting * docs: add limitations, improve syntax * docs: use new docs syntax * fix: remove middleware decorator from libs * feat: build docs in CI * chore: linting * fix: CI python-version type * chore: remove docs CI * chore: kick CI * chore: include build badge master branch * chore: refactor naming * fix: rearrange tracing tests * improv(tracer): toggle default auto patching * feat(tracer): retrieve registered class instance * fix(Makefile): make cov target more explicit * improv(Register): support multiple classes reg. * improv(Register): inject class methods correctly * docs: add how to reutilize Tracer * improv(tracer): test auto patch method * improv: address nicolas feedback * improv: update example to reflect middleware feat * fix: metric dimension in root blob * chore: version bump Co-authored-by: heitorlessa <lessa@amazon.co.uk> Co-authored-by: heitorlessa <lessa@amazon.co.uk>
heitorlessa
added a commit
that referenced
this pull request
May 16, 2020
* develop: (21 commits) bugfix: #32 Runtime Error for nested sync fns chore: renamed history to changelog dependabot bugfix: resolves #31 aiohttp lazy import chore: grammar issues improv: add project tenets Improv tracer - async support, patch, test coverage and X-Ray escape hatch (#29) Bugfix: "per second" metric units (#27) fix: #24 correct example test and docs chore: bump example to use 0.8.0 features Adopt logging best practices (#23) Decorator factory Feat: Create your own middleware (#17) chore: clean up CI workflows fix: CI attempt 4 fix: CI attempt 3 fix: CI attempt 3 fix: CI attempt 2 feat: add docs to CI chore: fix github badge typo chore: pypi monthly download badge fix: add missing single_metric example; test var name ...
heitorlessa
added a commit
that referenced
this pull request
May 29, 2020
* [Sync Master] 0.7.0 release (#22) * docs: add pypi badge * fix: add missing single_metric example; test var name * chore: pypi monthly download badge * chore: fix github badge typo * feat: add docs to CI * fix: CI attempt 2 * fix: CI attempt 3 * fix: CI attempt 3 * fix: CI attempt 4 * chore: clean up CI workflows * Decorator factory Feat: Create your own middleware (#17) * feat(utils): add decorator factory * improv: use partial to reduce complexity * improv: add error handling * chore: type hint * docs: include pypi downloads badge * feat: opt in to trace each middleware that runs * improv: add initial util tests * improv: test explicit and implicit trace_execution * improv: test decorator with params * chore: linting * docs: include utilities * improv: correct tests, dec_factory only for func * improv: make util name more explicit * improv: doc trace_execution, fix casting * docs: add limitations, improve syntax * docs: use new docs syntax * fix: remove middleware decorator from libs * feat: build docs in CI * chore: linting * fix: CI python-version type * chore: remove docs CI * chore: kick CI * chore: include build badge master branch * chore: refactor naming * fix: rearrange tracing tests * improv(tracer): toggle default auto patching * feat(tracer): retrieve registered class instance * fix(Makefile): make cov target more explicit * improv(Register): support multiple classes reg. * improv(Register): inject class methods correctly * docs: add how to reutilize Tracer * improv(tracer): test auto patch method * improv: address nicolas feedback * improv: update example to reflect middleware feat * fix: metric dimension in root blob * chore: version bump Co-authored-by: heitorlessa <lessa@amazon.co.uk> Co-authored-by: heitorlessa <lessa@amazon.co.uk> * feat: add algolia search for docs and api ref (#39) (#40) Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: revert makefile build-docs-api Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: metric_set reuse #43 Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: clear metrics Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: update serialize_metrics helper function to use MetricManager instead of Metrics * fix: clear metrics after lambda invocation #43 Signed-off-by: heitorlessa <lessa@amazon.co.uk> * improv: document metrics tests, remove redundants Signed-off-by: heitorlessa <lessa@amazon.co.uk> #43 * chore: linting Signed-off-by: heitorlessa <lessa@amazon.co.uk> * docs: add section to flush metrics manually Signed-off-by: heitorlessa <lessa@amazon.co.uk> * docs: include EMF Json object Signed-off-by: heitorlessa <lessa@amazon.co.uk> * chore: bump version 0.9.4 Signed-off-by: heitorlessa <lessa@amazon.co.uk> Co-authored-by: Tom McCarthy <thommcc@amazon.com>
heitorlessa
referenced
this pull request
in heitorlessa/aws-lambda-powertools-python
Jun 17, 2020
* feat(utils): add decorator factory * improv: use partial to reduce complexity * improv: add error handling * chore: type hint * docs: include pypi downloads badge * feat: opt in to trace each middleware that runs * improv: add initial util tests * improv: test explicit and implicit trace_execution * improv: test decorator with params * chore: linting * docs: include utilities * improv: correct tests, dec_factory only for func * improv: make util name more explicit * improv: doc trace_execution, fix casting * docs: add limitations, improve syntax * docs: use new docs syntax * fix: remove middleware decorator from libs * feat: build docs in CI * chore: linting * fix: CI python-version type * chore: remove docs CI * chore: kick CI * chore: include build badge master branch * chore: refactor naming * fix: rearrange tracing tests * improv(tracer): toggle default auto patching * feat(tracer): retrieve registered class instance * fix(Makefile): make cov target more explicit * improv(Register): support multiple classes reg. * improv(Register): inject class methods correctly * docs: add how to reutilize Tracer * improv(tracer): test auto patch method * improv: address nicolas feedback * improv: update example to reflect middleware feat * fix: metric dimension in root blob * chore: version bump Co-authored-by: heitorlessa <lessa@amazon.co.uk>
heitorlessa
referenced
this pull request
in heitorlessa/aws-lambda-powertools-python
Jun 17, 2020
* [Sync Master] 0.7.0 release (#22) * docs: add pypi badge * fix: add missing single_metric example; test var name * chore: pypi monthly download badge * chore: fix github badge typo * feat: add docs to CI * fix: CI attempt 2 * fix: CI attempt 3 * fix: CI attempt 3 * fix: CI attempt 4 * chore: clean up CI workflows * Decorator factory Feat: Create your own middleware (#17) * feat(utils): add decorator factory * improv: use partial to reduce complexity * improv: add error handling * chore: type hint * docs: include pypi downloads badge * feat: opt in to trace each middleware that runs * improv: add initial util tests * improv: test explicit and implicit trace_execution * improv: test decorator with params * chore: linting * docs: include utilities * improv: correct tests, dec_factory only for func * improv: make util name more explicit * improv: doc trace_execution, fix casting * docs: add limitations, improve syntax * docs: use new docs syntax * fix: remove middleware decorator from libs * feat: build docs in CI * chore: linting * fix: CI python-version type * chore: remove docs CI * chore: kick CI * chore: include build badge master branch * chore: refactor naming * fix: rearrange tracing tests * improv(tracer): toggle default auto patching * feat(tracer): retrieve registered class instance * fix(Makefile): make cov target more explicit * improv(Register): support multiple classes reg. * improv(Register): inject class methods correctly * docs: add how to reutilize Tracer * improv(tracer): test auto patch method * improv: address nicolas feedback * improv: update example to reflect middleware feat * fix: metric dimension in root blob * chore: version bump Co-authored-by: heitorlessa <lessa@amazon.co.uk> Co-authored-by: heitorlessa <lessa@amazon.co.uk> * feat: add algolia search for docs and api ref (#39) (#40) Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: revert makefile build-docs-api Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: metric_set reuse #43 Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: clear metrics Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: update serialize_metrics helper function to use MetricManager instead of Metrics * fix: clear metrics after lambda invocation #43 Signed-off-by: heitorlessa <lessa@amazon.co.uk> * improv: document metrics tests, remove redundants Signed-off-by: heitorlessa <lessa@amazon.co.uk> #43 * chore: linting Signed-off-by: heitorlessa <lessa@amazon.co.uk> * docs: add section to flush metrics manually Signed-off-by: heitorlessa <lessa@amazon.co.uk> * docs: include EMF Json object Signed-off-by: heitorlessa <lessa@amazon.co.uk> * chore: bump version 0.9.4 Signed-off-by: heitorlessa <lessa@amazon.co.uk> Co-authored-by: Tom McCarthy <thommcc@amazon.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #, if available: #20
Description of changes:
This is required for Serverless Airline project to move from the modified version of powertools, and create own middlewares to improve reusability as much as possible.
Instead of creating reusable methods, we're at the stage that makes more sense to give flexibility to create own middlewares as #9, so customers can create one middleware with many custom processing before, after and exception handling instead.
This gives flexibility to control before, after, and exception handling logic in the way you'd like. It also lay the foundations to allow us to create a base Trace provider so you can use any Tracer that follow our protocol.
One step closer from GA 🎉 - We'll tackle log level next!
User Experience
Create a middleware no params
Create a middleware with params
Trace execution of custom middleware - Creates sub-segments in X-Ray
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.