package
1.204.0-devpreview
Repository: https://github.com/aws/aws-cdk-go.git
Documentation: pkg.go.dev

# README

Amazon CloudWatch Construct Library

Metric objects

Metric objects represent a metric that is emitted by AWS services or your own application, such as CPUUsage, FailureCount or Bandwidth.

Metric objects can be constructed directly or are exposed by resources as attributes. Resources that expose metrics will have functions that look like metricXxx() which will return a Metric object, initialized with defaults that make sense.

For example, lambda.Function objects have the fn.metricErrors() method, which represents the amount of errors reported by that Lambda function:

var fn function


errors := fn.metricErrors()

Metric objects can be account and region aware. You can specify account and region as properties of the metric, or use the metric.attachTo(Construct) method. metric.attachTo() will automatically copy the region and account fields of the Construct, which can come from anywhere in the Construct tree.

You can also instantiate Metric objects to reference any published metric that's not exposed using a convenience method on the CDK construct. For example:

hostedZone := route53.NewHostedZone(this, jsii.String("MyHostedZone"), &HostedZoneProps{
	ZoneName: jsii.String("example.org"),
})
metric := cloudwatch.NewMetric(&MetricProps{
	Namespace: jsii.String("AWS/Route53"),
	MetricName: jsii.String("DNSQueries"),
	DimensionsMap: map[string]*string{
		"HostedZoneId": hostedZone.hostedZoneId,
	},
})

Instantiating a new Metric object

If you want to reference a metric that is not yet exposed by an existing construct, you can instantiate a Metric object to represent it. For example:

metric := cloudwatch.NewMetric(&MetricProps{
	Namespace: jsii.String("MyNamespace"),
	MetricName: jsii.String("MyMetric"),
	DimensionsMap: map[string]*string{
		"ProcessingStep": jsii.String("Download"),
	},
})

Metric Math

Math expressions are supported by instantiating the MathExpression class. For example, a math expression that sums two other metrics looks like this:

var fn function


allProblems := cloudwatch.NewMathExpression(&MathExpressionProps{
	Expression: jsii.String("errors + throttles"),
	UsingMetrics: map[string]iMetric{
		"errors": fn.metricErrors(),
		"faults": fn.metricThrottles(),
	},
})

You can use MathExpression objects like any other metric, including using them in other math expressions:

var fn function
var allProblems mathExpression


problemPercentage := cloudwatch.NewMathExpression(&MathExpressionProps{
	Expression: jsii.String("(problems / invocations) * 100"),
	UsingMetrics: map[string]iMetric{
		"problems": allProblems,
		"invocations": fn.metricInvocations(),
	},
})

Search Expressions

Math expressions also support search expressions. For example, the following search expression returns all CPUUtilization metrics that it finds, with the graph showing the Average statistic with an aggregation period of 5 minutes:

cpuUtilization := cloudwatch.NewMathExpression(&MathExpressionProps{
	Expression: jsii.String("SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)"),

	// Specifying '' as the label suppresses the default behavior
	// of using the expression as metric label. This is especially appropriate
	// when using expressions that return multiple time series (like SEARCH()
	// or METRICS()), to show the labels of the retrieved metrics only.
	Label: jsii.String(""),
})

Cross-account and cross-region search expressions are also supported. Use the searchAccount and searchRegion properties to specify the account and/or region to evaluate the search expression against.

Aggregation

To graph or alarm on metrics you must aggregate them first, using a function like Average or a percentile function like P99. By default, most Metric objects returned by CDK libraries will be configured as Average over 300 seconds (5 minutes). The exception is if the metric represents a count of discrete events, such as failures. In that case, the Metric object will be configured as Sum over 300 seconds, i.e. it represents the number of times that event occurred over the time period.

If you want to change the default aggregation of the Metric object (for example, the function or the period), you can do so by passing additional parameters to the metric function call:

var fn function


minuteErrorRate := fn.metricErrors(&MetricOptions{
	Statistic: jsii.String("avg"),
	Period: awscdk.Duration_Minutes(jsii.Number(1)),
	Label: jsii.String("Lambda failure rate"),
})

This function also allows changing the metric label or color (which will be useful when embedding them in graphs, see below).

Rates versus Sums

The reason for using Sum to count discrete events is that some events are emitted as either 0 or 1 (for example Errors for a Lambda) and some are only emitted as 1 (for example NumberOfMessagesPublished for an SNS topic).

In case 0-metrics are emitted, it makes sense to take the Average of this metric: the result will be the fraction of errors over all executions.

If 0-metrics are not emitted, the Average will always be equal to 1, and not be very useful.

In order to simplify the mental model of Metric objects, we default to aggregating using Sum, which will be the same for both metrics types. If you happen to know the Metric you want to alarm on makes sense as a rate (Average) you can always choose to change the statistic.

Labels

Metric labels are displayed in the legend of graphs that include the metrics.

You can use dynamic labels to show summary information about the displayed time series in the legend. For example, if you use:

var fn function


minuteErrorRate := fn.metricErrors(&MetricOptions{
	Statistic: jsii.String("sum"),
	Period: awscdk.Duration_Hours(jsii.Number(1)),

	// Show the maximum hourly error count in the legend
	Label: jsii.String("[max: ${MAX}] Lambda failure rate"),
})

As the metric label, the maximum value in the visible range will be shown next to the time series name in the graph's legend.

If the metric is a math expression producing more than one time series, the maximum will be individually calculated and shown for each time series produce by the math expression.

Alarms

Alarms can be created on metrics in one of two ways. Either create an Alarm object, passing the Metric object to set the alarm on:

var fn function


cloudwatch.NewAlarm(this, jsii.String("Alarm"), &AlarmProps{
	Metric: fn.metricErrors(),
	Threshold: jsii.Number(100),
	EvaluationPeriods: jsii.Number(2),
})

Alternatively, you can call metric.createAlarm():

var fn function


fn.metricErrors().CreateAlarm(this, jsii.String("Alarm"), &CreateAlarmOptions{
	Threshold: jsii.Number(100),
	EvaluationPeriods: jsii.Number(2),
})

The most important properties to set while creating an Alarms are:

  • threshold: the value to compare the metric against.
  • comparisonOperator: the comparison operation to use, defaults to metric >= threshold.
  • evaluationPeriods: how many consecutive periods the metric has to be breaching the the threshold for the alarm to trigger.

To create a cross-account alarm, make sure you have enabled cross-account functionality in CloudWatch. Then, set the account property in the Metric object either manually or via the metric.attachTo() method.

Alarm Actions

To add actions to an alarm, use the integration classes from the @aws-cdk/aws-cloudwatch-actions package. For example, to post a message to an SNS topic when an alarm breaches, do the following:

import cw_actions "github.com/aws/aws-cdk-go/awscdk"
var alarm alarm


topic := sns.NewTopic(this, jsii.String("Topic"))
alarm.AddAlarmAction(cw_actions.NewSnsAction(topic))

Notification formats

Alarms can be created in one of two "formats":

  • With "top-level parameters" (these are the classic style of CloudWatch Alarms).
  • With a list of metrics specifications (these are the modern style of CloudWatch Alarms).

For backwards compatibility, CDK will try to create classic, top-level CloudWatch alarms as much as possible, unless you are using features that cannot be expressed in that format. Features that require the new-style alarm format are:

  • Metric math
  • Cross-account metrics
  • Labels

The difference between these two does not impact the functionality of the alarm in any way, except that the format of the notifications the Alarm generates is different between them. This affects both the notifications sent out over SNS, as well as the EventBridge events generated by this Alarm. If you are writing code to consume these notifications, be sure to handle both formats.

Composite Alarms

Composite Alarms can be created from existing Alarm resources.

var alarm1 alarm
var alarm2 alarm
var alarm3 alarm
var alarm4 alarm


alarmRule := cloudwatch.AlarmRule_AnyOf(cloudwatch.AlarmRule_AllOf(cloudwatch.AlarmRule_AnyOf(alarm1, cloudwatch.AlarmRule_FromAlarm(alarm2, cloudwatch.AlarmState_OK), alarm3), cloudwatch.AlarmRule_Not(cloudwatch.AlarmRule_FromAlarm(alarm4, cloudwatch.AlarmState_INSUFFICIENT_DATA))), cloudwatch.AlarmRule_FromBoolean(jsii.Boolean(false)))

cloudwatch.NewCompositeAlarm(this, jsii.String("MyAwesomeCompositeAlarm"), &CompositeAlarmProps{
	AlarmRule: AlarmRule,
})

A note on units

In CloudWatch, Metrics datums are emitted with units, such as seconds or bytes. When Metric objects are given a unit attribute, it will be used to filter the stream of metric datums for datums emitted using the same unit attribute.

In particular, the unit field is not used to rescale datums or alarm threshold values (for example, it cannot be used to specify an alarm threshold in Megabytes if the metric stream is being emitted as bytes).

You almost certainly don't want to specify the unit property when creating Metric objects (which will retrieve all datums regardless of their unit), unless you have very specific requirements. Note that in any case, CloudWatch only supports filtering by unit for Alarms, not in Dashboard graphs.

Please see the following GitHub issue for a discussion on real unit calculations in CDK: https://github.com/aws/aws-cdk/issues/5595

Dashboards

Dashboards are set of Widgets stored server-side which can be accessed quickly from the AWS console. Available widgets are graphs of a metric over time, the current value of a metric, or a static piece of Markdown which explains what the graphs mean.

The following widgets are available:

  • GraphWidget -- shows any number of metrics on both the left and right vertical axes.
  • AlarmWidget -- shows the graph and alarm line for a single alarm.
  • SingleValueWidget -- shows the current value of a set of metrics.
  • TextWidget -- shows some static Markdown.
  • AlarmStatusWidget -- shows the status of your alarms in a grid view.

Graph widget

A graph widget can display any number of metrics on either the left or right vertical axis:

var dashboard dashboard
var executionCountMetric metric
var errorCountMetric metric


dashboard.AddWidgets(cloudwatch.NewGraphWidget(&GraphWidgetProps{
	Title: jsii.String("Executions vs error rate"),

	Left: []iMetric{
		executionCountMetric,
	},

	Right: []*iMetric{
		errorCountMetric.With(&MetricOptions{
			Statistic: jsii.String("average"),
			Label: jsii.String("Error rate"),
			Color: cloudwatch.Color_GREEN(),
		}),
	},
}))

Using the methods addLeftMetric() and addRightMetric() you can add metrics to a graph widget later on.

Graph widgets can also display annotations attached to the left or the right y-axis.

var dashboard dashboard


dashboard.AddWidgets(cloudwatch.NewGraphWidget(&GraphWidgetProps{
	// ...

	LeftAnnotations: []horizontalAnnotation{
		&horizontalAnnotation{
			Value: jsii.Number(1800),
			Label: awscdk.Duration_Minutes(jsii.Number(30)).ToHumanString(),
			Color: cloudwatch.Color_RED(),
		},
		&horizontalAnnotation{
			Value: jsii.Number(3600),
			Label: jsii.String("1 hour"),
			Color: jsii.String("#2ca02c"),
		},
	},
}))

The graph legend can be adjusted from the default position at bottom of the widget.

var dashboard dashboard


dashboard.AddWidgets(cloudwatch.NewGraphWidget(&GraphWidgetProps{
	// ...

	LegendPosition: cloudwatch.LegendPosition_RIGHT,
}))

The graph can publish live data within the last minute that has not been fully aggregated.

var dashboard dashboard


dashboard.AddWidgets(cloudwatch.NewGraphWidget(&GraphWidgetProps{
	// ...

	LiveData: jsii.Boolean(true),
}))

The graph view can be changed from default 'timeSeries' to 'bar' or 'pie'.

var dashboard dashboard


dashboard.AddWidgets(cloudwatch.NewGraphWidget(&GraphWidgetProps{
	// ...

	View: cloudwatch.GraphWidgetView_BAR,
}))

Alarm widget

An alarm widget shows the graph and the alarm line of a single alarm:

var dashboard dashboard
var errorAlarm alarm


dashboard.AddWidgets(cloudwatch.NewAlarmWidget(&AlarmWidgetProps{
	Title: jsii.String("Errors"),
	Alarm: errorAlarm,
}))

Single value widget

A single-value widget shows the latest value of a set of metrics (as opposed to a graph of the value over time):

var dashboard dashboard
var visitorCount metric
var purchaseCount metric


dashboard.AddWidgets(cloudwatch.NewSingleValueWidget(&SingleValueWidgetProps{
	Metrics: []iMetric{
		visitorCount,
		purchaseCount,
	},
}))

Show as many digits as can fit, before rounding.

var dashboard dashboard


dashboard.AddWidgets(cloudwatch.NewSingleValueWidget(&SingleValueWidgetProps{
	Metrics: []iMetric{
	},

	FullPrecision: jsii.Boolean(true),
}))

Text widget

A text widget shows an arbitrary piece of MarkDown. Use this to add explanations to your dashboard:

var dashboard dashboard


dashboard.AddWidgets(cloudwatch.NewTextWidget(&TextWidgetProps{
	Markdown: jsii.String("# Key Performance Indicators"),
}))

Alarm Status widget

An alarm status widget displays instantly the status of any type of alarms and gives the ability to aggregate one or more alarms together in a small surface.

var dashboard dashboard
var errorAlarm alarm


dashboard.AddWidgets(
cloudwatch.NewAlarmStatusWidget(&AlarmStatusWidgetProps{
	Alarms: []iAlarm{
		errorAlarm,
	},
}))

An alarm status widget only showing firing alarms, sorted by state and timestamp:

var dashboard dashboard
var errorAlarm alarm


dashboard.AddWidgets(cloudwatch.NewAlarmStatusWidget(&AlarmStatusWidgetProps{
	Title: jsii.String("Errors"),
	Alarms: []iAlarm{
		errorAlarm,
	},
	SortBy: cloudwatch.AlarmStatusWidgetSortBy_STATE_UPDATED_TIMESTAMP,
	States: []alarmState{
		cloudwatch.*alarmState_ALARM,
	},
}))

Query results widget

A LogQueryWidget shows the results of a query from Logs Insights:

var dashboard dashboard


dashboard.AddWidgets(cloudwatch.NewLogQueryWidget(&LogQueryWidgetProps{
	LogGroupNames: []*string{
		jsii.String("my-log-group"),
	},
	View: cloudwatch.LogQueryVisualizationType_TABLE,
	// The lines will be automatically combined using '\n|'.
	QueryLines: []*string{
		jsii.String("fields @message"),
		jsii.String("filter @message like /Error/"),
	},
}))

Custom widget

A CustomWidget shows the result of an AWS Lambda function:

var dashboard dashboard


// Import or create a lambda function
fn := lambda.Function_FromFunctionArn(dashboard, jsii.String("Function"), jsii.String("arn:aws:lambda:us-east-1:123456789012:function:MyFn"))

dashboard.AddWidgets(cloudwatch.NewCustomWidget(&CustomWidgetProps{
	FunctionArn: fn.FunctionArn,
	Title: jsii.String("My lambda baked widget"),
}))

You can learn more about custom widgets in the Amazon Cloudwatch User Guide.

Dashboard Layout

The widgets on a dashboard are visually laid out in a grid that is 24 columns wide. Normally you specify X and Y coordinates for the widgets on a Dashboard, but because this is inconvenient to do manually, the library contains a simple layout system to help you lay out your dashboards the way you want them to.

Widgets have a width and height property, and they will be automatically laid out either horizontally or vertically stacked to fill out the available space.

Widgets are added to a Dashboard by calling add(widget1, widget2, ...). Widgets given in the same call will be laid out horizontally. Widgets given in different calls will be laid out vertically. To make more complex layouts, you can use the following widgets to pack widgets together in different ways:

  • Column: stack two or more widgets vertically.
  • Row: lay out two or more widgets horizontally.
  • Spacer: take up empty space