Skip to main content

CDK Constructs for AWS CloudWatch

Project description

Amazon CloudWatch Construct Library

---

Stability: Stable


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:

# Example may have issues. See https://github.com/aws/jsii/issues/826
errors = fn.metric_errors()

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:

# Example may have issues. See https://github.com/aws/jsii/issues/826
minute_error_rate = fn.metric_errors(
    statistic="avg",
    period=Duration.minutes(1),
    label="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.

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:

# Example may have issues. See https://github.com/aws/jsii/issues/826
Alarm(self, "Alarm",
    metric=fn.metric_errors(),
    threshold=100,
    evaluation_periods=2
)

Alternatively, you can call metric.createAlarm():

# Example may have issues. See https://github.com/aws/jsii/issues/826
fn.metric_errors().create_alarm(self, "Alarm",
    threshold=100,
    evaluation_periods=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.

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.

Graph widget

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

# Example may have issues. See https://github.com/aws/jsii/issues/826
dashboard.add_widgets(GraphWidget(
    title="Executions vs error rate",

    left=[execution_count_metric],

    right=[error_count_metric.with(
        statistic="average",
        label="Error rate",
        color="00FF00"
    )]
))

Alarm widget

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

# Example may have issues. See https://github.com/aws/jsii/issues/826
dashboard.add_widgets(AlarmWidget(
    title="Errors",
    alarm=error_alarm
))

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):

# Example may have issues. See https://github.com/aws/jsii/issues/826
dashboard.add_widgets(SingleValueWidget(
    metrics=[visitor_count, purchase_count]
))

Text widget

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

# Example may have issues. See https://github.com/aws/jsii/issues/826
dashboard.add_widgets(TextWidget(
    markdown="# Key Performance Indicators"
))

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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-cloudwatch-1.16.2.tar.gz (128.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk.aws_cloudwatch-1.16.2-py3-none-any.whl (126.6 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-cloudwatch-1.16.2.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-cloudwatch-1.16.2.tar.gz
  • Upload date:
  • Size: 128.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-cloudwatch-1.16.2.tar.gz
Algorithm Hash digest
SHA256 672a5530a2eec43e6c70108405a9f5dd01771efaa7a469c01470a7a70ab8e5c4
MD5 97b84d2610ddf83ca173c92c024bd88f
BLAKE2b-256 6e38adee130eb6db6717d50edc421ac82dacf019054a8f0670c989915e0448d6

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_cloudwatch-1.16.2-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_cloudwatch-1.16.2-py3-none-any.whl
  • Upload date:
  • Size: 126.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_cloudwatch-1.16.2-py3-none-any.whl
Algorithm Hash digest
SHA256 35191fe35ff51800ec9e457f8fdc1bc737b16ec9ea376edc000bc06f96afceec
MD5 804822064099c20e2ab0e8780cb5307c
BLAKE2b-256 c0927acb4aa4945a4d10f74c955d3623013ee97f70aeea2d59c4ee2b4cb0d2a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page