AWS::Synthetics的CDK构造库
aws-cdk.aws-synthetics的Python项目详细描述
Amazon CloudWatch综合构建库
---All classes with the
Cfn
prefix in this module (CFN Resources) are always stable and safe to use.
The APIs of higher level constructs in this module are in developer preview before they become stable. We will only make breaking changes to address unforeseen API issues. Therefore, these APIs are not subject to Semantic Versioning, and breaking changes will be announced in release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
Amazon CloudWatch Synthetics允许您通过生成synthetic流量来监视应用程序。流量由canary生成:一个按计划运行的可配置脚本。您将canary脚本配置为遵循与用户相同的路由和执行相同的操作,这样即使在应用程序上没有任何流量的情况下,也可以继续验证用户体验。在
金丝雀
为了演示如何使用金丝雀,假设您的应用程序定义了以下端点:
% curl "https://api.example.com/user/books/topbook/"The Hitchhikers Guide to the Galaxy
下面的代码定义了一个金丝雀,它将每5分钟命中一次books/topbook
端点:
下面是导出index.js
函数的index.js
文件的示例:
varsynthetics=require('Synthetics');constlog=require('SyntheticsLogger');constpageLoadBlueprint=asyncfunction(){// INSERT URL hereconstURL="https://api.example.com/user/books/topbook/";letpage=awaitsynthetics.getPage();constresponse=awaitpage.goto(URL,{waitUntil:'domcontentloaded',timeout:30000});//Wait for page to render.//Increase or decrease wait time based on endpoint being monitored.awaitpage.waitFor(15000);// This will take a screenshot that will be included in test output artifactsawaitsynthetics.takeScreenshot('loaded','loaded');letpageTitle=awaitpage.title();log.info('Page title: '+pageTitle);if(response.status()!==200){throw"Failed to load page!";}};exports.handler=async()=>{returnawaitpageLoadBlueprint();};
Note: The function must be called
handler
.
金丝雀将自动生成CloudWatch仪表盘:
金丝雀代码将在Synthetics代表您创建的lambda函数中执行。Lambda函数包含由Synthetics提供的自定义runtime。提供的运行时包括各种方便的工具,如Puppeteer和Chromium。默认运行时是syn-nodejs-2.0
。在
要了解有关合成功能的更多信息,请查看docs。在
配置Canary脚本
要配置canary执行的脚本,请使用test
属性。test
属性接受一个可以由Test
类静态方法初始化的Test
实例。目前,唯一实现的方法是Test.custom()
,它允许您携带自己的代码。将来还会增加其他方法。Test.custom()
接受code
和{
synthetics.Code
类公开静态方法来绑定代码构件:
code.fromInline(code)
-指定一个内联脚本。在code.fromAsset(path)
-在本地文件系统中指定一个.zip文件或一个目录,该文件将在部署时压缩并上载到S3。目录结构见上述注释。在code.fromBucket(bucket, key[, objectVersion])
-指定一个包含运行时代码的.zip文件的S3对象。目录结构见上述注释。在
使用Code
类静态初始值设定项:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826# To supply the code inline:canary=Canary(self,"MyCanary",test=Test.custom(code=synthetics.Code.from_inline("/* Synthetics handler code */"),handler="index.handler"),runtime=synthetics.Runtime.SYNTHETICS_NODEJS_2_0)# To supply the code from your local filesystem:canary=Canary(self,"MyCanary",test=Test.custom(code=synthetics.Code.from_asset(path.join(__dirname,"canary")),handler="index.handler"),runtime=synthetics.Runtime.SYNTHETICS_NODEJS_2_0)# To supply the code from a S3 bucket:canary=Canary(self,"MyCanary",test=Test.custom(code=synthetics.Code.from_bucket(bucket,"canary.zip"),handler="index.handler"),runtime=synthetics.Runtime.SYNTHETICS_NODEJS_2_0)
Note: For
code.fromAsset()
andcode.fromBucket()
, the canary resource requires the following folder structure:canary/ ├── nodejs/ ├── node_modules/ ├── <filename>.jsSee Synthetics docs.
报警
您可以在金丝雀度量上配置CloudWatch警报。指标由CloudWatch自动发出,可通过以下API访问:
canary.metricSuccessPercent()
-在给定时间内成功运行的金丝雀的百分比canary.metricDuration()
-每次金丝雀运行所需的时间,以秒为单位。在canary.metricFailed()
-给定时间内失败的canary运行次数
创建跟踪金丝雀指标的警报:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826cloudwatch.Alarm(self,"CanaryAlarm",metric=canary.metric_success_percent(),evaluation_periods=2,threshold=90,comparison_operator=cloudwatch.ComparisonOperator.LESS_THAN_THRESHOLD)
未来工作
- 将蓝图添加到测试类#9613。在
- 项目
标签: