AWS::Synthetics的CDK构造库

aws-cdk.aws-synthetics的Python项目详细描述


Amazon CloudWatch综合构建库

---

cfn-resources: Stable

All classes with the Cfn prefix in this module (CFN Resources) are always stable and safe to use.

cdk-constructs: Developer Preview

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端点:

^{pr2}$

下面是导出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仪表盘:

UI Screenshot

金丝雀代码将在Synthetics代表您创建的lambda函数中执行。Lambda函数包含由Synthetics提供的自定义runtime。提供的运行时包括各种方便的工具,如Puppeteer和Chromium。默认运行时是syn-nodejs-2.0。在

要了解有关合成功能的更多信息,请查看docs。在

配置Canary脚本

要配置canary执行的脚本,请使用test属性。test属性接受一个可以由Test类静态方法初始化的Test实例。目前,唯一实现的方法是Test.custom(),它允许您携带自己的代码。将来还会增加其他方法。Test.custom()接受code和{}属性——这两个属性都是合成函数为您创建lambda函数所必需的。在

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() and code.fromBucket(), the canary resource requires the following folder structure:

canary/
├── nodejs/
   ├── node_modules/
        ├── <filename>.js

See 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。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Docker&SeleniumJava:无法在Docker容器上运行的chrome浏览器中上载图像/文件   在python中运行java命令   Java垃圾收集器异常行为   java java是否根据底层操作系统执行字节码级优化?   java是否可以休眠自定义查询返回映射而不是列表?   java Spring引导RabbitMQ接收器Jackson反序列化到POJO   apache flex在ActionScript3中创建对象相等“HashMap”作为java HashMap   java如何在Eclipse集成中切换JProfiler启动器   缓存JSP页面结果的java最佳实践?   java集成jaxb绑定文件,使用CXF生成基于WSDL的客户端   java为什么在上传操作结束之前,客户端没有检测到HttpServletResponse的PrintWriter内容?   java在接口内创建类和在类内创建接口有什么用   java向文件写入错误Android Studio   java合并多个RealmList并对结果列表排序?   谷歌API视觉java。lang.NoSuchMethodError   java如何使用逗号分别存储每个值,然后将它们存储到单独的数组中?