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:
Solved! Go to Solution.
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)
--
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
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)
--
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
User | Count |
---|---|
3 | |
2 | |
1 | |
1 | |
1 |