diff --git a/chalice/package.py b/chalice/package.py index ecdd0268e..fb55676e9 100644 --- a/chalice/package.py +++ b/chalice/package.py @@ -334,6 +334,9 @@ def _generate_restapi(self, resource, template): properties = resources['RestAPI']['Properties'] properties['MinimumCompressionSize'] = \ int(resource.minimum_compression) + if self._config.xray_enabled: + properties = resources['RestAPI']['Properties'] + properties['TracingEnabled'] = True handler_cfn_name = to_cfn_resource_name( resource.lambda_function.resource_name) diff --git a/docs/source/topics/middleware.rst b/docs/source/topics/middleware.rst index 90a8ea0de..465315bc1 100644 --- a/docs/source/topics/middleware.rst +++ b/docs/source/topics/middleware.rst @@ -271,15 +271,15 @@ Integrating with AWS Lambda Powertools -------------------------------------- `AWS Lambda Powertools -`__ is a suite of +`__ is a suite of utilities for AWS Lambda functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier. You can use Chalice middleware to easily integrate Lambda Powertools with your Chalice apps. In this example, we'll use the `Logger -`__ -and `Tracer `__ +`__ +and `Tracer `__ and convert them to Chalice middleware so they will be automatically applied to all Lambda functions in our application. diff --git a/tests/unit/test_package.py b/tests/unit/test_package.py index cd01e451d..e770344be 100644 --- a/tests/unit/test_package.py +++ b/tests/unit/test_package.py @@ -1335,6 +1335,17 @@ def test_can_generate_scheduled_event(self): }, } + def test_can_generate_rest_api_with_xray( + self, sample_app_with_auth): + config = Config.create(chalice_app=sample_app_with_auth, + project_dir='.', + api_gateway_stage='api', + xray=True + ) + template = self.generate_template(config) + resources = template['Resources'] + assert resources['RestAPI']['Properties']['TracingEnabled'] is True + def test_can_generate_rest_api_without_compression( self, sample_app_with_auth): config = Config.create(chalice_app=sample_app_with_auth, @@ -1372,6 +1383,7 @@ def test_can_generate_rest_api(self, sample_app_with_auth): assert resources['RestAPI']['Type'] == 'AWS::Serverless::Api' assert resources['RestAPI']['Properties']['MinimumCompressionSize'] \ == 100 + assert 'TracingEnabled' not in resources['RestAPI']['Properties'] # We should also create the auth lambda function. assert resources['Myauth']['Type'] == 'AWS::Serverless::Function' # Along with permission to invoke from API Gateway.