aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
2 lines (1 loc) • 13.6 kB
JavaScript
var __decorate=exports&&exports.__decorate||function(decorators,target,key,desc){var c=arguments.length,r=c<3?target:desc===null?desc=Object.getOwnPropertyDescriptor(target,key):desc,d;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")r=Reflect.decorate(decorators,target,key,desc);else for(var i=decorators.length-1;i>=0;i--)(d=decorators[i])&&(r=(c<3?d(r):c>3?d(target,key,r):d(target,key))||r);return c>3&&r&&Object.defineProperty(target,key,r),r},_a,_b,Alarm_1;Object.defineProperty(exports,"__esModule",{value:!0}),exports.AnomalyDetectionAlarm=exports.Alarm=exports.TreatMissingData=exports.ComparisonOperator=void 0;var jsiiDeprecationWarnings=()=>{var tmp=require("../../.warnings.jsii.js");return jsiiDeprecationWarnings=()=>tmp,tmp};const JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti");var alarm_base_1=()=>{var tmp=require("./alarm-base");return alarm_base_1=()=>tmp,tmp},cloudwatch_generated_1=()=>{var tmp=require("./cloudwatch.generated");return cloudwatch_generated_1=()=>tmp,tmp},metric_1=()=>{var tmp=require("./metric");return metric_1=()=>tmp,tmp},anomaly_detection_1=()=>{var tmp=require("./private/anomaly-detection");return anomaly_detection_1=()=>tmp,tmp},metric_util_1=()=>{var tmp=require("./private/metric-util");return metric_util_1=()=>tmp,tmp},object_1=()=>{var tmp=require("./private/object");return object_1=()=>tmp,tmp},rendering_1=()=>{var tmp=require("./private/rendering");return rendering_1=()=>tmp,tmp},statistic_1=()=>{var tmp=require("./private/statistic");return statistic_1=()=>tmp,tmp},core_1=()=>{var tmp=require("../../core");return core_1=()=>tmp,tmp},metadata_resource_1=()=>{var tmp=require("../../core/lib/metadata-resource");return metadata_resource_1=()=>tmp,tmp},prop_injectable_1=()=>{var tmp=require("../../core/lib/prop-injectable");return prop_injectable_1=()=>tmp,tmp},ComparisonOperator;(function(ComparisonOperator2){ComparisonOperator2.GREATER_THAN_OR_EQUAL_TO_THRESHOLD="GreaterThanOrEqualToThreshold",ComparisonOperator2.GREATER_THAN_THRESHOLD="GreaterThanThreshold",ComparisonOperator2.LESS_THAN_THRESHOLD="LessThanThreshold",ComparisonOperator2.LESS_THAN_OR_EQUAL_TO_THRESHOLD="LessThanOrEqualToThreshold",ComparisonOperator2.LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD="LessThanLowerOrGreaterThanUpperThreshold",ComparisonOperator2.GREATER_THAN_UPPER_THRESHOLD="GreaterThanUpperThreshold",ComparisonOperator2.LESS_THAN_LOWER_THRESHOLD="LessThanLowerThreshold"})(ComparisonOperator||(exports.ComparisonOperator=ComparisonOperator={}));const OPERATOR_SYMBOLS={GreaterThanOrEqualToThreshold:">=",GreaterThanThreshold:">",LessThanThreshold:"<",LessThanOrEqualToThreshold:"<="};var TreatMissingData;(function(TreatMissingData2){TreatMissingData2.BREACHING="breaching",TreatMissingData2.NOT_BREACHING="notBreaching",TreatMissingData2.IGNORE="ignore",TreatMissingData2.MISSING="missing"})(TreatMissingData||(exports.TreatMissingData=TreatMissingData={}));let Alarm=Alarm_1=class Alarm2 extends alarm_base_1().AlarmBase{static fromAlarmName(scope,id,alarmName){const stack=core_1().Stack.of(scope);return this.fromAlarmArn(scope,id,stack.formatArn({service:"cloudwatch",resource:"alarm",resourceName:alarmName,arnFormat:core_1().ArnFormat.COLON_RESOURCE_NAME}))}static fromAlarmArn(scope,id,alarmArn){class Import extends alarm_base_1().AlarmBase{constructor(){super(...arguments),this.alarmArn=alarmArn,this.alarmName=core_1().Stack.of(scope).splitArn(alarmArn,core_1().ArnFormat.COLON_RESOURCE_NAME).resourceName}}return new Import(scope,id)}constructor(scope,id,props){super(scope,id,{physicalName:props.alarmName});try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_AlarmProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,Alarm2),error}(0,metadata_resource_1().addConstructMetadata)(this,props);const comparisonOperator=props.comparisonOperator||ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,isAnomalyDetection=(0,anomaly_detection_1().isAnomalyDetectionOperator)(comparisonOperator);let threshold,thresholdMetricId;if(isAnomalyDetection){if(!isAnomalyDetectionMetric(props.metric))throw new(core_1()).ValidationError(`Anomaly detection operator ${comparisonOperator} requires an ANOMALY_DETECTION_BAND() metric. Use the construct AnomalyDetectionAlarm or wrap your metric in an ANOMALY_DETECTION_BAND expression.`,this);props.threshold!==void 0&&props.threshold!==Alarm_1.ANOMALY_DETECTION_NO_THRESHOLD&&core_1().Annotations.of(this).addWarningV2("aws-cdk-lib/aws-cloudwatch:thresholdIgnoredForAnomalyDetection","threshold is unused for anomaly detection alarms. Use 'Alarm.ANOMALY_DETECTION_NO_THRESHOLD' or use the AnomalyDetectionAlarm construct.")}else{if(isAnomalyDetectionMetric(props.metric))throw new(core_1()).ValidationError(`Fixed threshold operator ${props.comparisonOperator} can not be used with an ANOMALY_DETECTION_BAND() math expression; use an anomaly threshold operator instead.`,this);if(props.threshold===void 0)throw new(core_1()).ValidationError("threshold must be specified for standard alarms",this);threshold=props.threshold}const rendered=this.renderMetric(props.metric,isAnomalyDetection),metricProps=rendered.props;props.period&&(metricProps.period=props.period.toSeconds()),props.statistic&&Object.assign(metricProps,{statistic:renderIfSimpleStatistic(props.statistic),extendedStatistic:renderIfExtendedStatistic(props.statistic)}),isAnomalyDetection&&(thresholdMetricId=rendered.primaryId);const alarm=new(cloudwatch_generated_1()).CfnAlarm(this,"Resource",{alarmDescription:props.alarmDescription,alarmName:this.physicalName,comparisonOperator,threshold,thresholdMetricId,datapointsToAlarm:props.datapointsToAlarm,evaluateLowSampleCountPercentile:props.evaluateLowSampleCountPercentile,evaluationPeriods:props.evaluationPeriods,treatMissingData:props.treatMissingData,actionsEnabled:props.actionsEnabled,alarmActions:core_1().Lazy.list({produce:()=>this.alarmActionArns}),insufficientDataActions:core_1().Lazy.list({produce:()=>this.insufficientDataActionArns}),okActions:core_1().Lazy.list({produce:()=>this.okActionArns}),...metricProps});if(this.alarmArn=this.getResourceArnAttribute(alarm.attrArn,{service:"cloudwatch",resource:"alarm",resourceName:this.physicalName,arnFormat:core_1().ArnFormat.COLON_RESOURCE_NAME}),this.alarmName=this.getResourceNameAttribute(alarm.ref),this.metric=props.metric,isAnomalyDetection)this.annotation={value:Alarm_1.ANOMALY_DETECTION_NO_THRESHOLD,label:"Anomaly Detection Band",visible:!1};else{const datapoints=props.datapointsToAlarm||props.evaluationPeriods;this.annotation={label:`${props.metric} ${OPERATOR_SYMBOLS[comparisonOperator]} ${props.threshold} for ${datapoints} datapoints within ${describePeriod(props.evaluationPeriods*(0,metric_util_1().metricPeriod)(props.metric).toSeconds())}`,value:props.threshold}}for(const[i,message]of Object.entries(this.metric.warningsV2??{}))core_1().Annotations.of(this).addWarningV2(i,message)}toAnnotation(){return this.annotation}addAlarmAction(...actions){try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_IAlarmAction(actions)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addAlarmAction),error}this.alarmActionArns===void 0&&(this.alarmActionArns=[]),this.alarmActionArns.push(...actions.map(a=>this.validateActionArn(a.bind(this,this).alarmActionArn)))}validateActionArn(actionArn){if(/arn:aws[a-z0-9-]*:automate:[a-z|\d|-]+:ec2:[a-z]+/.test(actionArn)){const metricConfig=this.metric.toMetricConfig();if(metricConfig.metricStat?.dimensions?.length!=1||!metricConfig.metricStat?.dimensions?.some(dimension=>dimension.name==="InstanceId"))throw new(core_1()).ValidationError(`EC2 alarm actions requires an EC2 Per-Instance Metric. (${JSON.stringify(metricConfig)} does not have an 'InstanceId' dimension)`,this)}return actionArn}renderMetric(metric,doubleReturnData){const returnDataLevel=doubleReturnData?2:1,self=this;return(0,metric_util_1().dispatchMetric)(metric,{withStat(stat,conf){return self.validateMetricStat(stat,metric),conf.renderingProperties?.label==null&&!self.requiresAccountId(stat)?{props:(0,object_1().dropUndefined)({dimensions:stat.dimensions,namespace:stat.namespace,metricName:stat.metricName,period:stat.period?.toSeconds(),statistic:renderIfSimpleStatistic(stat.statistic),extendedStatistic:renderIfExtendedStatistic(stat.statistic),unit:stat.unitFilter}),primaryId:void 0}:{props:{metrics:[{metricStat:{metric:{metricName:stat.metricName,namespace:stat.namespace,dimensions:stat.dimensions},period:stat.period.toSeconds(),stat:stat.statistic,unit:stat.unitFilter},id:"m1",accountId:self.requiresAccountId(stat)?stat.account:void 0,label:conf.renderingProperties?.label,returnData:!0}]},primaryId:"m1"}},withExpression(){const mset=new(rendering_1()).MetricSet;mset.addTopLevel(!0,metric);let eid=0;function uniqueMetricId(){return`expr_${++eid}`}let primaryId;const props={metrics:mset.entries.map(entry=>{const id=entry.id||uniqueMetricId();entry.tag&&(primaryId=id);const returnData=entry.level<=returnDataLevel;return(0,metric_util_1().dispatchMetric)(entry.metric,{withStat(stat,conf){return self.validateMetricStat(stat,entry.metric),{metricStat:{metric:{metricName:stat.metricName,namespace:stat.namespace,dimensions:stat.dimensions},period:stat.period.toSeconds(),stat:stat.statistic,unit:stat.unitFilter},id,accountId:self.requiresAccountId(stat)?stat.account:void 0,label:conf.renderingProperties?.label,returnData}},withExpression(expr,conf){const hasSubmetrics=mathExprHasSubmetrics(expr);return hasSubmetrics&&assertSubmetricsCount(self,expr),self.validateMetricExpression(expr),{expression:expr.expression,id,label:conf.renderingProperties?.label,period:hasSubmetrics?void 0:expr.period,returnData}}})})};if(!primaryId)throw new(core_1()).AssumptionError("Expected at least one metric to be the primary");return{props,primaryId}}})}validateMetricStat(stat,metric){const stack=core_1().Stack.of(this);if(definitelyDifferent(stat.region,stack.region))throw new(core_1()).ValidationError(`Cannot create an Alarm in region '${stack.region}' based on metric '${metric}' in '${stat.region}'`,this)}validateMetricExpression(expr){if(expr.searchAccount!==void 0||expr.searchRegion!==void 0)throw new(core_1()).ValidationError("Cannot create an Alarm based on a MathExpression which specifies a searchAccount or searchRegion",this)}requiresAccountId(stat){const stackAccount=core_1().Stack.of(this).account;return stat.account===void 0?!1:stackAccount!==stat.account}};exports.Alarm=Alarm,_a=JSII_RTTI_SYMBOL_1,Alarm[_a]={fqn:"aws-cdk-lib.aws_cloudwatch.Alarm",version:"2.202.0"},Alarm.PROPERTY_INJECTION_ID="aws-cdk-lib.aws-cloudwatch.Alarm",Alarm.ANOMALY_DETECTION_NO_THRESHOLD=0,__decorate([(0,metadata_resource_1().MethodMetadata)()],Alarm.prototype,"toAnnotation",null),__decorate([(0,metadata_resource_1().MethodMetadata)()],Alarm.prototype,"addAlarmAction",null),exports.Alarm=Alarm=Alarm_1=__decorate([prop_injectable_1().propertyInjectable],Alarm);function isAnomalyDetectionMetric(metric){let isAnomalyDetection=!1;return(0,metric_util_1().dispatchMetric)(metric,{withStat(){isAnomalyDetection=!1},withExpression(expr){isAnomalyDetection=expr.expression.includes("ANOMALY_DETECTION_BAND")}}),isAnomalyDetection}let AnomalyDetectionAlarm=class AnomalyDetectionAlarm2 extends Alarm{constructor(scope,id,props){super(scope,id,{...props,comparisonOperator:props.comparisonOperator??ComparisonOperator.LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD,metric:metric_1().Metric.anomalyDetectionFor(props),threshold:Alarm.ANOMALY_DETECTION_NO_THRESHOLD});try{jsiiDeprecationWarnings().aws_cdk_lib_aws_cloudwatch_AnomalyDetectionAlarmProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,AnomalyDetectionAlarm2),error}if((0,metadata_resource_1().addConstructMetadata)(this,props),props.comparisonOperator&&!(0,anomaly_detection_1().isAnomalyDetectionOperator)(props.comparisonOperator))throw new(core_1()).ValidationError(`Must use one of the anomaly detection operators, got ${props.comparisonOperator}`,this)}};exports.AnomalyDetectionAlarm=AnomalyDetectionAlarm,_b=JSII_RTTI_SYMBOL_1,AnomalyDetectionAlarm[_b]={fqn:"aws-cdk-lib.aws_cloudwatch.AnomalyDetectionAlarm",version:"2.202.0"},AnomalyDetectionAlarm.PROPERTY_INJECTION_ID="aws-cdk-lib.aws-cloudwatch.AnomalyDetectionAlarm",exports.AnomalyDetectionAlarm=AnomalyDetectionAlarm=__decorate([prop_injectable_1().propertyInjectable],AnomalyDetectionAlarm);function definitelyDifferent(x,y){return x&&!core_1().Token.isUnresolved(y)&&x!==y}function describePeriod(seconds){return seconds===60?"1 minute":seconds===1?"1 second":seconds>60?seconds/60+" minutes":seconds+" seconds"}function renderIfSimpleStatistic(statistic){if(statistic===void 0)return;const parsed=(0,statistic_1().parseStatistic)(statistic);if(parsed.type==="simple")return(0,statistic_1().normalizeStatistic)(parsed)}function renderIfExtendedStatistic(statistic){if(statistic===void 0)return;const parsed=(0,statistic_1().parseStatistic)(statistic);if(parsed.type!=="simple")return parsed.type==="single"||parsed.type==="pair"?(0,statistic_1().normalizeStatistic)(parsed):parsed.statistic}function mathExprHasSubmetrics(expr){return Object.keys(expr.usingMetrics).length>0}function assertSubmetricsCount(scope,expr){if(Object.keys(expr.usingMetrics).length>10)throw new(core_1()).ValidationError("Alarms on math expressions cannot contain more than 10 individual metrics",scope)}
;