ACloudGuru / serverless-plugin-aws-alerts

A Serverless Framework plugin that creates CloudWatch alarms for functions.
MIT License
531 stars 149 forks source link

support for custom metrics #163

Closed nalbion closed 3 years ago

nalbion commented 3 years ago

This is a Feature Proposal

Description

My application is using aws-embedded-metrics to create custom CloudWatch metrics:

            const metrics = createMetricsLogger();
            metrics.setNamespace(namespace);
            metrics.putDimensions({ OperationName: request.operationName });
            if (context.user) {
                metrics.setProperty("User", context.user.id);
            }

I can manually create a CloudWatch alarm that will correctly monitor my custom metric. When I click "Edit" and view the source tab it looks like:

{
    "metrics": [
        [ "MyNamespace", "Duration", "OperationName", "GetCapabilities", "ServiceName", "my_service-prod-my_function", "LogGroup", "my_service-prod-my_function", "ServiceType", "AWS::Lambda::Function", { "stat": "Maximum" } ]
    ],
    "view": "timeSeries",
    "stacked": false,
    "period": 86400,
    "region": "ap-southeast-2"
}

I've tried to reproduce this alarm using serverless-plugin-aws-alerts:

custom:
  alerts:
    definitions:
      myCustomAlarm
        namespace: MyNamespace
        metric: Duration
        threshold: 1000
        statistic: Maximum
        period: 60
        evaluationPeriods: 1
        comparisonOperator: GreaterThanThreshold
        omitDefaultDimension: true
        nameTemplate: Operation-GetCapabilities-Duration-Alarm
        dimension:
          - name: OperationName
            value: GetCapabilities

Unfortunately, the generated metrics list does not contain the extra fields as my manually-generated alarm:

{
    "metrics": [
        [ "MyNamespace", "Duration", { "stat": "Maximum" } ]
    ],
    "view": "timeSeries",
    "stacked": false,
    "period": 60,
    "region": "ap-southeast-2"
}

Additional Data

nalbion commented 3 years ago

I managed to get this working:

custom:
  alerts:
    definitions:
      authExceptions:
        namespace: Authorization
        metric: Count
        threshold: 1
        statistic: Maximum
        period: 60
        evaluationPeriods: 1
        comparisonOperator: GreaterThanThreshold
        omitDefaultDimension: true
        dimensions:
          - Name: ServiceName
            Value: ${self:service}-${opt:stage, self:custom.stage}-authorizer
          - Name: LogGroup
            Value: ${self:service}-${opt:stage, self:custom.stage}-authorizer
          - Name: ServiceType
            Value: AWS::Lambda::Function
          - Name: ResponseType
            Value: Error