Error Creating GCP Dashboard with Python

I'm quite familiar with gcp, but new with Python. I'm trying to create a simple GCP custom dashboard with python, but am seeing the error below.

 

Traceback (most recent call last):
File "C:\Users\ibrah\OneDrive\Documents\Training\Automated Dashboard with Python\main.py", line 56, in <module>
create_dashboard(project_id, dashboard_json)
File "C:\Users\ibrah\OneDrive\Documents\Training\Automated Dashboard with Python\main.py", line 7, in create_dashboard
dashboard = monitoring_dashboard_v1.types.Dashboard(**dashboard_json)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ibrah\AppData\Local\Programs\Python\Python311\Lib\site-packages\proto\message.py", line 576, in __init__
raise ValueError(
ValueError: Unknown field for Dashboard: displayName




My python code is as follows:

from google.cloud import monitoring_dashboard_v1

def create_dashboard(project_id, dashboard_json😞
    client = monitoring_dashboard_v1.DashboardsServiceClient()
    project_name = f"projects/{project_id}"
    # Assuming dashboard_json is a dictionary that follows the structure required by the API
    dashboard = monitoring_dashboard_v1.types.Dashboard(**dashboard_json)

    response = client.create_dashboard(parent=project_name, body=dashboard)
    print("Dashboard created: ", response.name)
    return response

# Example usage
project_id = 'staticwebsitegcs'  # replace with your GCP project ID
dashboard_json = {
"displayName": "Dashboard_Test4",
  "mosaicLayout": {
    "columns": 48,
    "tiles": [
      {
        "width": 24,
        "height": 16,
        "widget": {
          "title": "Log_Count",
          "xyChart": {
            "dataSets": [
              {
                "timeSeriesQuery": {
                  "timeSeriesFilter": {
                    "filter": "metric.type=\"logging.googleapis.com/user/niis-main-qal1-gce-instance-logs\" resource.type=\"gce_instance\"",
                    "aggregation": {
                      "alignmentPeriod": "60s",
                      "perSeriesAligner": "ALIGN_COUNT",
                      "crossSeriesReducer": "REDUCE_COUNT"
                    }
                  }
                },
                "plotType": "LINE",
                "minAlignmentPeriod": "60s",
                "targetAxis": "Y1"
              }
            ],
            "yAxis": {
              "scale": "LINEAR"
            },
            "chartOptions": {
              "mode": "COLOR"
            }
          }
        }
      }
    ]
  }
}

create_dashboard(project_id, dashboard_json)



Solved Solved
5 2 170
2 ACCEPTED SOLUTIONS

Hello @ibrahiminui  ,Welcome on Google Cloud Community.

before you execute python code:

pip3 install google-cloud-monitoring-dashboards

Fixed code: 

from google.cloud import monitoring_dashboard_v1

def create_dashboard(project_id, dashboard_json):
    client = monitoring_dashboard_v1.DashboardsServiceClient()
    project_name = f"projects/{project_id}"
    # Assuming dashboard_json is a dictionary that follows the structure required by the API
    dashboard = monitoring_dashboard_v1.types.Dashboard(
        display_name=dashboard_json["display_name"],
        mosaic_layout=dashboard_json["mosaic_layout"]
    )

    response = client.create_dashboard(parent=project_name, dashboard=dashboard)
    print("Dashboard created: ", response.name)
    return response

# Example usage
project_id = 'webapp-wordpress'  # replace with your GCP project ID
dashboard_json = {
    "display_name": "Dashboard_Test4",
    "mosaic_layout": {
        "columns": 48,
        "tiles": [
            {
                "width": 24,
                "height": 16,
                "widget": {
                    "title": "Log_Count",
                    "xy_chart": {
                        "data_sets": [
                            {
                                "time_series_query": {
                                    "time_series_filter": {
                                        "filter": "metric.type=\"logging.googleapis.com/user/niis-main-qal1-gce-instance-logs\" resource.type=\"gce_instance\"",
                                        "aggregation": {
                                            "alignment_period": "60s",
                                            "per_series_aligner": "ALIGN_COUNT",
                                            "cross_series_reducer": "REDUCE_COUNT"
                                        }
                                    }
                                },
                                "plot_type": "LINE",
                                "min_alignment_period": "60s",
                                "target_axis": "Y1"
                            }
                        ],
                        "y_axis": {
                            "scale": "LINEAR"
                        },
                        "chart_options": {
                            "mode": "COLOR"
                        }
                    }
                }
            }
        ]
    }
}

create_dashboard(project_id, dashboard_json)

DamianS_0-1714627043987.png

--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost

View solution in original post

Thanks @DamianS . This worked. Where can I see the dashboard field equivalents?

Based on what I've seen online, we have is displayName,  mosaicLayout,  xyChart, dataSets, etc. But seems the API is expecting those fields in a different way. 

Do you have a documentation where I can see the appropriate fields? Thanks

View solution in original post

2 REPLIES 2

Hello @ibrahiminui  ,Welcome on Google Cloud Community.

before you execute python code:

pip3 install google-cloud-monitoring-dashboards

Fixed code: 

from google.cloud import monitoring_dashboard_v1

def create_dashboard(project_id, dashboard_json):
    client = monitoring_dashboard_v1.DashboardsServiceClient()
    project_name = f"projects/{project_id}"
    # Assuming dashboard_json is a dictionary that follows the structure required by the API
    dashboard = monitoring_dashboard_v1.types.Dashboard(
        display_name=dashboard_json["display_name"],
        mosaic_layout=dashboard_json["mosaic_layout"]
    )

    response = client.create_dashboard(parent=project_name, dashboard=dashboard)
    print("Dashboard created: ", response.name)
    return response

# Example usage
project_id = 'webapp-wordpress'  # replace with your GCP project ID
dashboard_json = {
    "display_name": "Dashboard_Test4",
    "mosaic_layout": {
        "columns": 48,
        "tiles": [
            {
                "width": 24,
                "height": 16,
                "widget": {
                    "title": "Log_Count",
                    "xy_chart": {
                        "data_sets": [
                            {
                                "time_series_query": {
                                    "time_series_filter": {
                                        "filter": "metric.type=\"logging.googleapis.com/user/niis-main-qal1-gce-instance-logs\" resource.type=\"gce_instance\"",
                                        "aggregation": {
                                            "alignment_period": "60s",
                                            "per_series_aligner": "ALIGN_COUNT",
                                            "cross_series_reducer": "REDUCE_COUNT"
                                        }
                                    }
                                },
                                "plot_type": "LINE",
                                "min_alignment_period": "60s",
                                "target_axis": "Y1"
                            }
                        ],
                        "y_axis": {
                            "scale": "LINEAR"
                        },
                        "chart_options": {
                            "mode": "COLOR"
                        }
                    }
                }
            }
        ]
    }
}

create_dashboard(project_id, dashboard_json)

DamianS_0-1714627043987.png

--
cheers,
DamianS
LinkedIn medium.com Cloudskillsboost

Thanks @DamianS . This worked. Where can I see the dashboard field equivalents?

Based on what I've seen online, we have is displayName,  mosaicLayout,  xyChart, dataSets, etc. But seems the API is expecting those fields in a different way. 

Do you have a documentation where I can see the appropriate fields? Thanks

Top Labels in this Space