0.36.2 • Published 3 years ago

@myhelix-cdk/monitoring v0.36.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 years ago

@myhelix-cdk/monitoring

A Construct for adding cloudwatch monitoring and alerting to a lambda service.

How to use

import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as cwactions from '@aws-cdk/aws-cloudwatch-actions';

import * as monitoring from '@myhelix-cdk/monitoring';

// this will create a dashboard and sns topic which publishes to pagerduty
const monitors = new monitoring.Monitoring(this, 'monitoring', {
      lambdaFn: this.lambdaFn,
      urlPath: '/app/myservice/pagerduty/url',
    });


// this adds and alarm for lambdas attached to streams that haven't processed any items in the last hour
new cloudwatch.Alarm(this, 'High Lambda IteratorAge', {
  metric: monitors.iteratorAgeMetric,
  threshold: 3600000,
  period: cdk.Duration.hours(1),
  evaluationPeriods: 1,
  datapointsToAlarm: 1,
  treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
}).addAlarmAction(new cwactions.SnsAction(monitors.errorTopic));

add your own metrics

const monitors = new monitoring.Monitoring(this, 'monitoring', {
      lambdaFn: this.lambdaFn,
      urlPath: '/app/myservice/pagerduty/url',
    });

const api = new apigateway.LambdaRestApi(this, 'example', {
  restApiName: 'myApi',
  handler: someLambda,
  deployOptions: {
    tracingEnabled: true,
  },
});

const ApiErrors = new cloudwatch.Metric({
      metricName: '4XXError',
      namespace: 'AWS/ApiGateway',
      dimensions: {ApiId: api.httpApiId},
      unit: cloudwatch.Unit.COUNT,
      label: '4XX Errors',
      statistic: 'sum',
      period: cdk.Duration.seconds(900)
    });

const ApiErrorCount = new cloudwatch.Metric({
      metricName: 'Count',
      namespace: 'AWS/ApiGateway',
      dimensions: {ApiId: api.httpApiId},
      unit: cloudwatch.Unit.COUNT,
      label: '# Requests',
      statistic: 'sum',
      period: cdk.Duration.seconds(900)
    });

let apiGateway4xxErrorPercentage = new cloudwatch.MathExpression({
      expression: 'm1/m2*100',
      label: '% API Gateway 4xx Errors',
      usingMetrics: {
        m1: ApiErrors,
        m2: ApiErrorCount,
      },
      period: cdk.Duration.minutes(5)
    });

monitors.dashboard.addWidgets(
    monitors.buldGraphWidget('ApiGateway400Errors', [apiGateway4xxErrorPercentage])
    );