[{"data":1,"prerenderedAt":66},["ShallowReactive",2],{"content-testing-python-starter-5-observability-in-testing":3},{"markdown":4,"frontMatterAttributes":5,"bodyRaw":12,"contributorPaths":13,"menuType":16,"contentName":17,"slug":18,"readingTime":19,"menu":20,"nextDocItem":59,"previousDocItem":48},"\u003Ch2>Fetch, tail, and filter Lambda function logs locally\u003C\u002Fh2>\n\u003Cp>To simplify troubleshooting, SAM CLI has a command called \u003Ccode>sam logs\u003C\u002Fcode>. The \u003Ccode>sam logs\u003C\u002Fcode> command lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several features to help you quickly find your bug.\u003C\u002Fp>\n\u003Cp>\u003Ccode>NOTE\u003C\u002Fcode>: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-bash\"> sam logs -n PythonTestDemo --stack-name apigw-lambda --tail\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>In a new terminal, send a \u003Ccode>curl\u003C\u002Fcode> command to the API Gateway endpoint and watch the log output.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-bash\"> curl &lt;API Gateway url&gt;\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>You can find more information and examples about filtering Lambda function logs in the \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-logging.html\">SAM CLI Documentation\u003C\u002Fa>.\u003C\u002Fp>\n\u003Ch2>Implement application tracing\u003C\u002Fh2>\n\u003Cp>You can use AWS X-Ray to track user requests as they travel through your entire application. With X-Ray, you can understand how your application and its underlying services are performing to identify and troubleshoot the root cause of performance issues and errors.\u003C\u002Fp>\n\u003Cp>We instrumented the \u003Ca href=\".\u002Fsrc\u002Fapp.py\">Lambda function\u003C\u002Fa> handler using AWS X-Ray. To view the X-Ray traces, open the console and find your Lambda function. Navigate to \u003Ccode>Monitor\u003C\u002Fcode> -&gt; \u003Ccode>Traces\u003C\u002Fcode> and you should see your X-Ray service map and traces.\u003C\u002Fp>\n\u003Cp>You may also navigate to the \u003Ca href=\"https:\u002F\u002Fconsole.aws.amazon.com\u002Fxray\u002Fhome\">X-Ray service\u003C\u002Fa> in the console to examine your traces in greater detail. In the console you should be able to find a service map that looks similar to the one below. \u003C\u002Fp>\n\u003Cp>\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Ftesting\u002Fpython-starter\u002Fassets\u002Fxray.png\" alt=\"X-Ray image\">\u003C\u002Fp>\n",{"title":6,"order":7,"callout":8},"Observability in Testing",5,{"title":9,"description":10,"link":11},"Check out other starter guides","Do you prefer to learn in another language? We also have starter guides in Java, TypeScript, and .NET.","https:\u002F\u002Fgithub.com\u002Faws-samples\u002Fserverless-test-samples\u002F","## Fetch, tail, and filter Lambda function logs locally\nTo simplify troubleshooting, SAM CLI has a command called `sam logs`. The `sam logs` command lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several features to help you quickly find your bug.\n\n`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.\n\n```bash\n sam logs -n PythonTestDemo --stack-name apigw-lambda --tail\n```\n\nIn a new terminal, send a `curl` command to the API Gateway endpoint and watch the log output.\n\n```bash\n curl \u003CAPI Gateway url>\n```\n\nYou can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https:\u002F\u002Fdocs.aws.amazon.com\u002Fserverless-application-model\u002Flatest\u002Fdeveloperguide\u002Fserverless-sam-cli-logging.html).\n\n## Implement application tracing\nYou can use AWS X-Ray to track user requests as they travel through your entire application. With X-Ray, you can understand how your application and its underlying services are performing to identify and troubleshoot the root cause of performance issues and errors.\n\nWe instrumented the [Lambda function](.\u002Fsrc\u002Fapp.py) handler using AWS X-Ray. To view the X-Ray traces, open the console and find your Lambda function. Navigate to `Monitor` -> `Traces` and you should see your X-Ray service map and traces.\n\nYou may also navigate to the [X-Ray service](https:\u002F\u002Fconsole.aws.amazon.com\u002Fxray\u002Fhome) in the console to examine your traces in greater detail. In the console you should be able to find a service map that looks similar to the one below. \n\n![X-Ray image](\u002Fassets\u002Fexternal\u002Ftesting\u002Fpython-starter\u002Fassets\u002Fxray.png)",[14,15],"content\u002Fcontributors\u002Fdan-fox.json","content\u002Fcontributors\u002Frohan-mehta.json","LIST","Python Test Samples Starter Project","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F5-observability-in-testing","2 min",[21],{"title":22,"content":23},"Testing Guide",[24,32,40,48,55,59],{"title":25,"order":26,"callout":27,"time":28,"path":29,"id":30,"link":31},"Introduction",1,{"title":9,"description":10,"link":11},"1 min","1-introduction","1-introduction.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F1-introduction",{"title":33,"order":34,"callout":35,"time":36,"path":37,"id":38,"link":39},"Project Setup",2,{"title":9,"description":10,"link":11},"3 min","2-project-setup","2-project-setup.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F2-project-setup",{"title":41,"order":42,"callout":43,"time":44,"path":45,"id":46,"link":47},"Local Testing",3,{"title":9,"description":10,"link":11},"4 min","3-local-testing","3-local-testing.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F3-local-testing",{"title":49,"order":50,"callout":51,"time":19,"path":52,"id":53,"link":54},"Testing in the Cloud",4,{"title":9,"description":10,"link":11},"4-testing-in-the-cloud","4-testing-in-the-cloud.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F4-testing-in-the-cloud",{"title":6,"order":7,"callout":56,"time":19,"path":57,"id":58,"link":18},{"title":9,"description":10,"link":11},"5-observability-in-testing","5-observability-in-testing.md",{"title":60,"order":61,"callout":62,"time":28,"path":63,"id":64,"link":65},"Cleanup",6,{"title":9,"description":10,"link":11},"6-cleanup","6-cleanup.md","\u002Fcontent\u002Ftesting\u002Fpython-starter\u002F6-cleanup",1792578875042]