---
# Amazon CloudFront distribution metrics (namespace AWS/CloudFront).
#
# CloudFront is a global service: CloudWatch publishes its metrics ONLY in us-east-1,
# and every metric carries a constant Region="Global" dimension. supported_regions
# lets the rule compiler apply that restriction. The constant Region dimension is
# matched and queried but not emitted as a label (it carries no per-distribution
# identity).
version: v1
display_name: AWS CloudFront
namespace: AWS/CloudFront
supported_regions: [us-east-1]
query:
  period: 5m
instance:
  dimensions:
    - name: DistributionId
      label: distribution_id
    - name: Region
      constant: Global
metrics:
  - id: requests
    metric_name: Requests
    statistics: [sum]
    rate: true
  - id: bytes_downloaded
    metric_name: BytesDownloaded
    statistics: [sum]
    rate: true
  - id: bytes_uploaded
    metric_name: BytesUploaded
    statistics: [sum]
    rate: true
  - id: error_rate_total
    metric_name: TotalErrorRate
    statistics: [average]
  - id: error_rate_4xx
    metric_name: 4xxErrorRate
    statistics: [average]
  - id: error_rate_5xx
    metric_name: 5xxErrorRate
    statistics: [average]
  # ----------------------------------------------------------------------------------------
  # Opt-in metrics (disabled by default). Select them through rules[].metrics.
  # These are CloudFront "additional metrics"
  # that must be enabled per distribution in AWS (they return no data until then). All are
  # published at the {DistributionId, Region=Global} grain.
  # ----------------------------------------------------------------------------------------
  - id: cache_hit_rate
    disabled: true
    metric_name: CacheHitRate
    statistics: [average]
  - id: error_rate_401
    disabled: true
    metric_name: 401ErrorRate
    statistics: [average]
  - id: error_rate_403
    disabled: true
    metric_name: 403ErrorRate
    statistics: [average]
  - id: error_rate_404
    disabled: true
    metric_name: 404ErrorRate
    statistics: [average]
  - id: error_rate_502
    disabled: true
    metric_name: 502ErrorRate
    statistics: [average]
  - id: error_rate_503
    disabled: true
    metric_name: 503ErrorRate
    statistics: [average]
  - id: error_rate_504
    disabled: true
    metric_name: 504ErrorRate
    statistics: [average]
  - id: origin_latency                 # OriginLatency's only valid statistic is a percentile (ms gauge)
    disabled: true
    metric_name: OriginLatency
    statistics: [p50, p99]
template:
  family: CloudFront
  context_namespace: cloudfront
  chart_defaults:
    instances:
      by_labels: [account_id, region, distribution_id]
  charts:
    - id: aws_cloudwatch_cloudfront_requests
      context: requests
      title: CloudFront Requests
      family: Requests
      units: requests/s
      algorithm: absolute
      dimensions:
        - selector: requests_sum
          name: requests
    - id: aws_cloudwatch_cloudfront_traffic
      context: traffic
      title: CloudFront Traffic
      family: Traffic
      units: bytes/s
      algorithm: absolute
      dimensions:
        - selector: bytes_downloaded_sum
          name: downloaded
        - selector: bytes_uploaded_sum
          name: uploaded
    - id: aws_cloudwatch_cloudfront_error_rate
      context: error_rate
      title: CloudFront Error Rate
      family: Errors
      units: percentage
      algorithm: absolute
      dimensions:
        - selector: error_rate_total_average
          name: total
        - selector: error_rate_4xx_average
          name: 4xx
        - selector: error_rate_5xx_average
          name: 5xx
    # ----------------------------------------------------------------------------------------
    # Charts for the opt-in metrics above.
    # ----------------------------------------------------------------------------------------
    - id: aws_cloudwatch_cloudfront_cache_hit_rate
      context: cache_hit_rate
      title: CloudFront Cache Hit Rate
      family: Cache
      units: percentage
      algorithm: absolute
      dimensions:
        - selector: cache_hit_rate_average
          name: hit_rate
    - id: aws_cloudwatch_cloudfront_status_error_rate
      context: status_error_rate
      title: CloudFront Error Rate by Status Code
      family: Errors
      units: percentage
      algorithm: absolute
      dimensions:
        - selector: error_rate_401_average
          name: "401"
        - selector: error_rate_403_average
          name: "403"
        - selector: error_rate_404_average
          name: "404"
        - selector: error_rate_502_average
          name: "502"
        - selector: error_rate_503_average
          name: "503"
        - selector: error_rate_504_average
          name: "504"
    - id: aws_cloudwatch_cloudfront_origin_latency
      context: origin_latency
      title: CloudFront Origin Latency
      family: Latency
      units: milliseconds
      algorithm: absolute
      dimensions:
        - selector: origin_latency_p50
          name: p50
        - selector: origin_latency_p99
          name: p99
