resource "datadog_dashboard" "aws_rds_aurora_sma_api_dashboard" {
  description = <<-EOT
      This dashboard brings together metrics from Aurora's MySQL-compatible database engine with resource metrics from the AWS infrastructure for a detailed view of your RDS instances. Further reading on Aurora monitoring:

      - [Datadog's guide to key metrics for Amazon Aurora](https://www.datadoghq.com/blog/monitoring-amazon-aurora-performance-metrics/)

      - [How to collect metrics from Amazon Aurora](https://www.datadoghq.com/blog/how-to-collect-aurora-metrics/)

      - [How to monitor Aurora using Datadog](https://www.datadoghq.com/blog/monitor-aurora-using-datadog/)

      - [Datadog's RDS integration docs](https://docs.datadoghq.com/integrations/amazon_rds/)

      Clone this template dashboard to make changes and add your own graph widgets.
        EOT
  layout_type = "free"
  title       = "AWS RDS (Aurora) - SMA API"
  url         = "/dashboard/i5a-s5q-6yc/aws-rds-aurora---sma-api"
  template_variable {
    defaults = ["*"]
    name     = "region"
    prefix   = "region"
  }
  template_variable {
    defaults = ["*"]
    name     = "zone"
    prefix   = "availability-zone"
  }
  template_variable {
    defaults = ["*"]
    name     = "dbinstanceidentifier"
    prefix   = "dbinstanceidentifier"
  }
  template_variable {
    defaults = ["stage-delphi-main"]
    name     = "cluster"
    prefix   = "dbclusteridentifier"
  }
  template_variable_preset {
    name = "stage"
  }
  widget {
    query_value_definition {
      autoscale   = true
      live_span   = "1h"
      precision   = 2
      text_align  = "center"
      title       = "Queries"
      title_align = "center"
      title_size  = "16"
      request {
        aggregator = "last"
        q          = "sum:mysql.performance.queries{$region,$zone,$dbinstanceidentifier,$cluster}"
      }
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 25
      x               = 80
      y               = 0
    }
  }
  widget {
    image_definition {
      has_background = false
      has_border     = false
      sizing         = "center"
      url            = "/static/images/saas_logos/bot/amazon_rds.png"
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 25
      x               = 0
      y               = 0
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "[Connection](https://www.datadoghq.com/blog/monitoring-amazon-aurora-performance-metrics/#connection-metrics) & [Replication](https://www.datadoghq.com/blog/monitoring-amazon-aurora-performance-metrics/#read-replica-metrics)"
      font_size        = "18"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "right"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 41
      is_column_break = false
      width           = 12
      x               = 134
      y               = 20
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Replication lag"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.aurora_replica_lag{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "warm"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 79
      x               = 147
      y               = 41
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "[Disk I/O](https://www.datadoghq.com/blog/monitoring-amazon-aurora-performance-metrics/#resource-utilization)"
      font_size        = "18"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "right"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 41
      is_column_break = false
      width           = 12
      x               = 0
      y               = 63
    }
  }
  widget {
    image_definition {
      has_background = false
      has_border     = false
      sizing         = "fit"
      url            = "/static/images/saas_logos/bot/aurora.png"
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 25
      x               = 27
      y               = 0
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "[Query volume](https://www.datadoghq.com/blog/monitoring-amazon-aurora-performance-metrics/#query-throughput)"
      font_size        = "18"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "right"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 41
      is_column_break = false
      width           = 12
      x               = 0
      y               = 20
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "SELECT queries per second"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:mysql.performance.com_select{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}+avg:mysql.performance.qcache_hits{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "green"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 53
      y               = 20
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "DML (INSERT/UPDATE/DELETE) queries per second"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:mysql.performance.com_insert{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}+avg:mysql.performance.com_update{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}+avg:mysql.performance.com_delete{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "purple"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 93
      y               = 20
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Slow queries"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "bars"
        on_right_yaxis = false
        q              = "avg:mysql.performance.slow_queries{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "warm"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 13
      y               = 41
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Write latency per I/O"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.diskio.writeLatency{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "purple"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 93
      y               = 84
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Threads connected"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:mysql.performance.threads_connected{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "grey"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 147
      y               = 20
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Network in"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.network_receive_throughput{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}.as_rate()"
        style {
          palette = "purple"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 147
      y               = 84
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Network out"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.network_transmit_throughput{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}.as_rate()"
        style {
          palette = "green"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 187
      y               = 84
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "CPU"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.cpuutilization{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "warm"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 147
      y               = 63
    }
  }
  widget {
    query_value_definition {
      autoscale   = true
      live_span   = "1h"
      precision   = 0
      text_align  = "center"
      title       = "DB connections"
      title_align = "center"
      title_size  = "16"
      request {
        aggregator = "last"
        q          = "sum:aws.rds.database_connections{$region,$zone,$dbinstanceidentifier,$cluster}"
      }
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 25
      x               = 107
      y               = 0
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Threads running"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:mysql.performance.threads_running{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "grey"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 187
      y               = 20
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Queries per second"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:mysql.performance.queries{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "orange"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 13
      y               = 20
    }
  }
  widget {
    note_definition {
      background_color = "blue"
      content          = "Overall"
      font_size        = "16"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "bottom"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 5
      is_column_break = false
      width           = 39
      x               = 13
      y               = 14
    }
  }
  widget {
    note_definition {
      background_color = "blue"
      content          = "Reads"
      font_size        = "16"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "bottom"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 5
      is_column_break = false
      width           = 39
      x               = 53
      y               = 14
    }
  }
  widget {
    note_definition {
      background_color = "blue"
      content          = "Writes"
      font_size        = "16"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "bottom"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 5
      is_column_break = false
      width           = 39
      x               = 93
      y               = 14
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Read IOPS"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.diskio.readIOsPS{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}.as_count()"
        style {
          palette = "green"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 53
      y               = 63
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Write IOPS"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.diskio.writeIOsPS{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "purple"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 93
      y               = 63
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Disk queue depth"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.diskio.diskQueueDepth{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "blue"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 13
      y               = 84
    }
  }
  widget {
    note_definition {
      background_color = "gray"
      content          = "[AWS resource metrics](https://www.datadoghq.com/blog/monitoring-amazon-aurora-performance-metrics/#resource-utilization)"
      font_size        = "18"
      has_padding      = false
      show_tick        = true
      text_align       = "center"
      tick_edge        = "right"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 41
      is_column_break = false
      width           = 12
      x               = 134
      y               = 63
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "RAM available"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.freeable_memory{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "cool"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 187
      y               = 63
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "SELECT latency"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.select_latency{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          line_type  = "solid"
          line_width = "normal"
          palette    = "green"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 53
      y               = 41
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "DML (INSERT/UPDATE/DELETE) latency"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.dmllatency{$region,$zone,$dbinstanceidentifier,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "purple"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 93
      y               = 41
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Read latency per I/O"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.diskio.readLatency{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}"
        style {
          palette = "green"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 53
      y               = 84
    }
  }
  widget {
    timeseries_definition {
      legend_size = "0"
      live_span   = "4h"
      show_legend = false
      title       = "Total IOPS per Cluster"
      title_align = "left"
      title_size  = "16"
      request {
        display_type   = "line"
        on_right_yaxis = false
        q              = "avg:aws.rds.diskio.writeIOsPS{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}.as_count()+avg:aws.rds.diskio.readIOsPS{$region,$zone,$dbinstanceidentifier,engine:aurora,$cluster} by {dbinstanceidentifier}.as_count()"
        style {
          palette = "blue"
        }
      }
    }
    widget_layout {
      height          = 20
      is_column_break = false
      width           = 39
      x               = 13
      y               = 63
    }
  }
  widget {
    query_value_definition {
      autoscale   = false
      live_span   = "1h"
      precision   = 2
      text_align  = "center"
      title       = "SELECT latency"
      title_align = "center"
      title_size  = "16"
      request {
        aggregator = "last"
        q          = "avg:aws.rds.select_latency{$region,$zone,$dbinstanceidentifier,$cluster}"
      }
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 25
      x               = 134
      y               = 0
    }
  }
  widget {
    query_value_definition {
      autoscale   = false
      live_span   = "1h"
      precision   = 2
      text_align  = "center"
      title       = "DML latency"
      title_align = "center"
      title_size  = "16"
      request {
        aggregator = "last"
        q          = "avg:aws.rds.dmllatency{$region,$zone,$dbinstanceidentifier,$cluster}"
      }
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 25
      x               = 161
      y               = 0
    }
  }
  widget {
    note_definition {
      background_color = "yellow"
      content          = "NOTE: This dashboard displays metrics both from AWS CloudWatch and from Aurora's MySQL-compatible database engine. To see all the metrics, you will need to [enable the MySQL integration](https://docs.datadoghq.com/integrations/amazon_rds/)."
      font_size        = "14"
      has_padding      = false
      show_tick        = false
      text_align       = "left"
      tick_edge        = "left"
      tick_pos         = "50%"
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 39
      x               = 187
      y               = 0
    }
  }
  widget {
    check_status_definition {
      check     = "aws.status"
      grouping  = "cluster"
      live_span = "30m"
      tags = [
        "service:rds",
      ]
      title       = "RDS service status"
      title_align = "center"
      title_size  = "13"
    }
    widget_layout {
      height          = 12
      is_column_break = false
      width           = 24
      x               = 54
      y               = 0
    }
  }
}
