0
0
GCPcloud~5 mins

Metrics and dashboards in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run applications or services, you want to know how well they are working. Metrics and dashboards help you see important numbers and trends in one place so you can fix problems quickly.
When you want to watch how much CPU or memory your app uses over time
When you need to check if your website is responding fast or slow
When you want to get alerts if something goes wrong with your cloud resources
When you want to share performance data with your team in an easy-to-understand view
When you want to compare usage patterns between different time periods
Config File - dashboard.json
dashboard.json
{
  "displayName": "My App Metrics Dashboard",
  "gridLayout": {
    "columns": 2,
    "widgets": [
      {
        "title": "CPU Usage",
        "xyChart": {
          "dataSets": [
            {
              "timeSeriesQuery": {
                "timeSeriesFilter": {
                  "filter": "metric.type=\"compute.googleapis.com/instance/cpu/utilization\"",
                  "aggregation": {
                    "alignmentPeriod": "60s",
                    "perSeriesAligner": "ALIGN_MEAN"
                  }
                }
              }
            }
          ],
          "timeshiftDuration": "0s",
          "yAxis": {
            "label": "CPU Utilization",
            "scale": "LINEAR"
          }
        }
      },
      {
        "title": "Memory Usage",
        "xyChart": {
          "dataSets": [
            {
              "timeSeriesQuery": {
                "timeSeriesFilter": {
                  "filter": "metric.type=\"agent.googleapis.com/memory/usage\"",
                  "aggregation": {
                    "alignmentPeriod": "60s",
                    "perSeriesAligner": "ALIGN_MEAN"
                  }
                }
              }
            }
          ],
          "timeshiftDuration": "0s",
          "yAxis": {
            "label": "Memory Usage",
            "scale": "LINEAR"
          }
        }
      }
    ]
  }
}

This JSON file defines a Google Cloud Monitoring dashboard named "My App Metrics Dashboard".

It has a grid layout with 2 columns and 2 widgets.

The first widget shows CPU usage over time using the metric compute.googleapis.com/instance/cpu/utilization.

The second widget shows memory usage over time using the metric agent.googleapis.com/memory/usage.

Both charts show average values aligned every 60 seconds.

Commands
This command creates a new dashboard in Google Cloud Monitoring using the JSON configuration file. It sets up the widgets to show CPU and memory usage metrics.
Terminal
gcloud monitoring dashboards create --config-from-file=dashboard.json
Expected OutputExpected
Created dashboard: projects/my-project/dashboards/abc12345-6789-0123-4567-89abcdef0123
--config-from-file - Specifies the JSON file that contains the dashboard configuration
This command lists all dashboards in your Google Cloud project so you can verify that your new dashboard was created.
Terminal
gcloud monitoring dashboards list
Expected OutputExpected
NAME DISPLAY NAME projects/my-project/dashboards/abc12345-6789-0123-4567-89abcdef0123 My App Metrics Dashboard
This command shows detailed information about the dashboard you created, including its widgets and metrics.
Terminal
gcloud monitoring dashboards describe abc12345-6789-0123-4567-89abcdef0123
Expected OutputExpected
name: projects/my-project/dashboards/abc12345-6789-0123-4567-89abcdef0123 displayName: My App Metrics Dashboard gridLayout: columns: 2 widgets: - title: CPU Usage xyChart: dataSets: - timeSeriesQuery: timeSeriesFilter: filter: 'metric.type="compute.googleapis.com/instance/cpu/utilization"' aggregation: alignmentPeriod: 60s perSeriesAligner: ALIGN_MEAN yAxis: label: CPU Utilization scale: LINEAR - title: Memory Usage xyChart: dataSets: - timeSeriesQuery: timeSeriesFilter: filter: 'metric.type="agent.googleapis.com/memory/usage"' aggregation: alignmentPeriod: 60s perSeriesAligner: ALIGN_MEAN yAxis: label: Memory Usage scale: LINEAR
Key Concept

If you remember nothing else from this pattern, remember: dashboards collect and show important metrics visually so you can quickly understand your system's health.

Common Mistakes
Using incorrect metric type names in the dashboard JSON
The dashboard will not show any data because it cannot find the metrics.
Always use exact metric type names from Google Cloud Monitoring documentation or metric explorer.
Not specifying the alignment period in aggregation
Metrics may appear noisy or inconsistent because data points are not aligned properly.
Set a reasonable alignment period like 60 seconds to smooth data for better visualization.
Forgetting to verify dashboard creation with 'gcloud monitoring dashboards list'
You might think the dashboard was created but it was not, leading to confusion.
Always list dashboards after creation to confirm success.
Summary
Create a dashboard JSON file defining widgets and metrics to monitor.
Use 'gcloud monitoring dashboards create' to deploy the dashboard to Google Cloud.
Verify the dashboard exists and inspect its details with 'gcloud monitoring dashboards list' and 'describe'.