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第三方库


热门话题
java何时可以运行。toString()是否有可能返回重复的字符串?   使用REST进行Java应用程序登录验证?   java测试onErrorResume()Spring Webflux   java设置一个单元格样式,使数字显示为百分比ApachePOI   java仅替换regex az09   java将字符串附加到文件   java Hibernate:如何在集合中查找对象   当独立客户端为Web服务实例化代理时,java WebSphere会生成ClassNotFoundException   java简单算法。我做不好   java我的代码有什么问题?我想用Android制作一个“cardflip”动画   java如何模拟Springbean及其自动连接的参数?   java在Android中将arraylist对象的某些参数显示到列表视图中   java setOnclickListener(此)错误   java自动连接未按类型连接bean   java如何禁止在Viewpager上滑动?   java代码检查每个if语句吗?   java NIO选择器OP_READ和OP_WRITE,关于处理它们的一些问题   java如何在不锁定文件的情况下获取文件大小   Oculus Rift的Java API?   java是一种选择。仍然需要fork来设置bootClasspath