Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitoring/resource_monitoring_dashboard.go:37 -> "panic: runtime error: index out of range [x] with length x" #16439

Comments

@liufuyang
Copy link

liufuyang commented Nov 3, 2023

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to the modular-magician user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to hashibot, a community member has claimed the issue already.

Terraform Version

Terraform v1.6.2
on darwin_arm64
+ provider registry.terraform.io/hashicorp/google v5.3.0
+ provider registry.terraform.io/hashicorp/google-beta v5.3.0
+ provider registry.terraform.io/hashicorp/null v3.2.1
+ provider registry.terraform.io/hashicorp/random v3.5.1

Affected Resource(s)

  • google_monitoring_dashboard

Terraform Configuration Files

resource "google_monitoring_dashboard" "service-dashboard" {
  dashboard_json = templatefile("${path.module}/monitoring.json", {})
}

I added some stuff (more metrics) in monitoring.json then it works, but when I removed some stuff later in it, the tf plan panics as below.

Debug Output

Panic Output

[terraform-plan-dev] Stack trace from the terraform-provider-google_v5.4.0_x5 plugin:
[terraform-plan-dev] 
[terraform-plan-dev] panic: runtime error: index out of range [23] with length 23
[terraform-plan-dev] 
[terraform-plan-dev] goroutine 980 [running]:
[terraform-plan-dev] github.com/hashicorp/terraform-provider-google/google/services/monitoring.removeComputedKeys(0x103d4ae60?, 0x14001011b00?)
[terraform-plan-dev] 	github.com/hashicorp/terraform-provider-google/google/services/monitoring/resource_monitoring_dashboard.go:37 +0x568
[terraform-plan-dev] github.com/hashicorp/terraform-provider-google/google/services/monitoring.removeComputedKeys(0x140019b6000?, 0x2d6e?)
[terraform-plan-dev] 	github.com/hashicorp/terraform-provider-google/google/services/monitoring/resource_monitoring_dashboard.go:30 +0x298
[terraform-plan-dev] github.com/hashicorp/terraform-provider-google/google/services/monitoring.monitoringDashboardDiffSuppress({0x14001fac9a8?, 0x10322b6f5?}, {0x1400198f500, 0x3211}, {0x140019a7000, 0x2d6e}, 0x14000cf0f80?)
[terraform-plan-dev] 	github.com/hashicorp/terraform-provider-google/google/services/monitoring/resource_monitoring_dashboard.go:57 +0xc0
[terraform-plan-dev] github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.schemaMap.diff(0x103216c08?, {0x1043215b8, 0x14000ffec00}, {0x10322b6f5, 0xe}, 0x14000ef7a40, 0x14000cf0f00, {0x104326f20?, 0x14000cf0f80}, 0x0)
[terraform-plan-dev] 	github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/schema.go:1144 +0x274
[terraform-plan-dev] github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.schemaMap.Diff(0x14000edb290, {0x1043215b8, 0x14000ffec00}, 0x14000959860, 0x14000fff530, 0x14000657b60, {0x1042c4720, 0x140020b6d00}, 0x0)
[terraform-plan-dev] 	github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/schema.go:679 +0x2a0
[terraform-plan-dev] github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).SimpleDiff(0x104322230?, {0x1043215b8?, 0x14000ffec00?}, 0x14000959860, 0x103d4ae60?, {0x1042c4720?, 0x140020b6d00?})
[terraform-plan-dev] 	github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/resource.go:890 +0x50
[terraform-plan-dev] github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).PlanResourceChange(0x14000f7fb78, {0x1043215b8?, 0x14000ffeae0?}, 0x14000ff9220)
[terraform-plan-dev] 	github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.0/helper/schema/grpc_provider.go:741 +0x838
[terraform-plan-dev] github.com/hashicorp/terraform-plugin-mux/tf5muxserver.muxServer.PlanResourceChange({0x14000f352f0, 0x14000f35350, {0x140015f5880, 0x2, 0x2}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...}, ...}, ...)
[terraform-plan-dev] 	github.com/hashicorp/terraform-plugin-mux@v0.8.0/tf5muxserver/mux_server_PlanResourceChange.go:27 +0xdc
[terraform-plan-dev] github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).PlanResourceChange(0x14000f79d60, {0x1043215b8?, 0x1400100ce10?}, 0x14001b11730)
[terraform-plan-dev] 	github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov5/tf5server/server.go:783 +0x3b8
[terraform-plan-dev] github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_PlanResourceChange_Handler({0x10423b520?, 0x14000f79d60}, {0x1043215b8, 0x1400100ce10}, 0x14001b116c0, 0x0)
[terraform-plan-dev] 	github.com/hashicorp/terraform-plugin-go@v0.14.3/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:367 +0x170
[terraform-plan-dev] google.golang.org/grpc.(*Server).processUnaryRPC(0x14001323680, {0x10432a5a0, 0x140009b01a0}, 0x1400177ac60, 0x140016abc20, 0x105aa9be8, 0x0)
[terraform-plan-dev] 	google.golang.org/grpc@v1.57.0/server.go:1360 +0xc88
[terraform-plan-dev] google.golang.org/grpc.(*Server).handleStream(0x14001323680, {0x10432a5a0, 0x140009b01a0}, 0x1400177ac60, 0x0)
[terraform-plan-dev] 	google.golang.org/grpc@v1.57.0/server.go:1737 +0x82c
[terraform-plan-dev] google.golang.org/grpc.(*Server).serveStreams.func1.1()
[terraform-plan-dev] 	google.golang.org/grpc@v1.57.0/server.go:982 +0x84
[terraform-plan-dev] created by google.golang.org/grpc.(*Server).serveStreams.func1
[terraform-plan-dev] 	google.golang.org/grpc@v1.57.0/server.go:980 +0x16c
[terraform-plan-dev] 
[terraform-plan-dev] Error: The terraform-provider-google_v5.4.0_x5 plugin crashed!
[terraform-plan-dev] 
[terraform-plan-dev] This is always indicative of a bug within the plugin. It would be immensely
[terraform-plan-dev] helpful if you could report the crash with the plugin's maintainers so that it
[terraform-plan-dev] can be fixed. The output above should help diagnose the issue.
[terraform-plan-dev] 

Expected Behavior

Actual Behavior

Steps to Reproduce

  1. setup a monitoring dashboard with some json input
  2. terraform apply
  3. then remove a few metrics from the json
  4. terraform apply
  5. probably sees this panic.

Important Factoids

References

  • #0000

b/309603218

@liufuyang liufuyang added the bug label Nov 3, 2023
@github-actions github-actions bot added the crash label Nov 3, 2023
@edwardmedia edwardmedia self-assigned this Nov 3, 2023
@liufuyang
Copy link
Author

liufuyang commented Nov 3, 2023

Nothing the tf apply also gives the same panic error.

I could resolve this manually by going to Google cloud console, open the created monitoring dashboard, manually delete the metrics that were intended to be removed from the json, then tf plan and apply can work.

So perhaps some code counted or removed stuff wrongly, causing the index out of bounds.

@edwardmedia
Copy link
Contributor

@liufuyang this could be a known issue. Take a look at #15676 to see if that make sense.

Unfortunately we don't have a lot of options to work around this, as it's a restriction at the SDK level

See hashicorp/terraform-plugin-sdk#1236

@liufuyang
Copy link
Author

liufuyang commented Nov 3, 2023

Thank you. Yeah I saw that one, but it was mentioned to be related to system version? I am on an arm64 machine and also using arm64 build tf. Also as I mentioned above, the error can be fixed by manually removing some metrics on GoogleCloud. Do you think it is related to that one but not a different case, or a bug in the monitoring provider code? 🤔

My stack trace points to this monitoring/resource_monitoring_dashboard.go:37 file.

The error is panic: runtime error: index out of range [23] with length 23

And it is NOT some strconv.ParseInt: parsing "1698107804964847": value out of range which looks like an OS arch issue.

@liufuyang
Copy link
Author

In my monitoring.json I also do have 23 tiles, matches onto the panic error message.

image

@edwardmedia
Copy link
Contributor

edwardmedia commented Nov 3, 2023

@liufuyang oh I see. Can you share your monitoring.json and repro steps?

@liufuyang
Copy link
Author

liufuyang commented Nov 3, 2023

Ah... my monitoring.json is huge and it has many customised metrics. But let me try draw a simpler one for you perhaps. Hold on...

monitoring.json before removing stuff

{
  "dashboardFilters": [],
  "displayName": "Dashboard",
  "labels": {},
  "mosaicLayout": {
    "columns": 48,
    "tiles": [
      {
        "height": 16,
        "widget": {
          "title": "VM Instance - CPU utilization [MEAN]",
          "xyChart": {
            "chartOptions": {
              "mode": "COLOR"
            },
            "dataSets": [
              {
                "minAlignmentPeriod": "60s",
                "plotType": "LINE",
                "targetAxis": "Y1",
                "timeSeriesQuery": {
                  "timeSeriesFilter": {
                    "aggregation": {
                      "alignmentPeriod": "60s",
                      "crossSeriesReducer": "REDUCE_MEAN",
                      "groupByFields": [],
                      "perSeriesAligner": "ALIGN_MEAN"
                    },
                    "filter": "metric.type=\"compute.googleapis.com/instance/cpu/utilization\" resource.type=\"gce_instance\""
                  }
                }
              }
            ],
            "thresholds": [],
            "yAxis": {
              "label": "",
              "scale": "LINEAR"
            }
          }
        },
        "width": 24
      },
      {
        "height": 16,
        "widget": {
          "title": "system.cpu.time [SUM]",
          "xyChart": {
            "chartOptions": {
              "mode": "COLOR"
            },
            "dataSets": [
              {
                "minAlignmentPeriod": "60s",
                "plotType": "LINE",
                "targetAxis": "Y1",
                "timeSeriesQuery": {
                  "timeSeriesFilter": {
                    "aggregation": {
                      "alignmentPeriod": "60s",
                      "crossSeriesReducer": "REDUCE_SUM",
                      "groupByFields": [],
                      "perSeriesAligner": "ALIGN_RATE"
                    },
                    "filter": "metric.type=\"workload.googleapis.com/system.cpu.time\" resource.type=\"generic_node\""
                  }
                }
              }
            ],
            "thresholds": [],
            "yAxis": {
              "label": "",
              "scale": "LINEAR"
            }
          }
        },
        "width": 24,
        "yPos": 16
      },
      {
        "height": 16,
        "widget": {
          "title": "system.memory.usage [MEAN]",
          "xyChart": {
            "chartOptions": {
              "mode": "COLOR"
            },
            "dataSets": [
              {
                "minAlignmentPeriod": "60s",
                "plotType": "LINE",
                "targetAxis": "Y1",
                "timeSeriesQuery": {
                  "timeSeriesFilter": {
                    "aggregation": {
                      "alignmentPeriod": "60s",
                      "crossSeriesReducer": "REDUCE_MEAN",
                      "groupByFields": [],
                      "perSeriesAligner": "ALIGN_MEAN"
                    },
                    "filter": "metric.type=\"workload.googleapis.com/system.memory.usage\" resource.type=\"generic_node\""
                  }
                }
              }
            ],
            "thresholds": [],
            "yAxis": {
              "label": "",
              "scale": "LINEAR"
            }
          }
        },
        "width": 24,
        "yPos": 32
      }
    ]
  }
}

@edwardmedia
Copy link
Contributor

edwardmedia commented Nov 3, 2023

@liufuyang I do see it is bug here. Your monitoring.json will help us to build tests to cover the use cases

https://github.com/hashicorp/terraform-provider-google/blob/main/google/services/monitoring/resource_monitoring_dashboard.go#L22

@edwardmedia edwardmedia assigned NickElliot and unassigned edwardmedia Nov 3, 2023
@liufuyang
Copy link
Author

liufuyang commented Nov 3, 2023

I build a simpler json for you above, I believe my current large json is nothing special but just more element.

So if you do tf apply with the json above, which has 3 tiles, then a dashboard will be created on GoogleCloud Monitoring and having 3 metrics. Then you try remove the last tile from the "tiles": [...] list in the json, and do another tf apply or plan, you should see the issue :)

Let me know whether this helps :)

@liufuyang
Copy link
Author

monitoring.json after removing last tile:

{
  "dashboardFilters": [],
  "displayName": "Dashboard",
  "labels": {},
  "mosaicLayout": {
    "columns": 48,
    "tiles": [
      {
        "height": 16,
        "widget": {
          "title": "VM Instance - CPU utilization [MEAN]",
          "xyChart": {
            "chartOptions": {
              "mode": "COLOR"
            },
            "dataSets": [
              {
                "minAlignmentPeriod": "60s",
                "plotType": "LINE",
                "targetAxis": "Y1",
                "timeSeriesQuery": {
                  "timeSeriesFilter": {
                    "aggregation": {
                      "alignmentPeriod": "60s",
                      "crossSeriesReducer": "REDUCE_MEAN",
                      "groupByFields": [],
                      "perSeriesAligner": "ALIGN_MEAN"
                    },
                    "filter": "metric.type=\"compute.googleapis.com/instance/cpu/utilization\" resource.type=\"gce_instance\""
                  }
                }
              }
            ],
            "thresholds": [],
            "yAxis": {
              "label": "",
              "scale": "LINEAR"
            }
          }
        },
        "width": 24
      },
      {
        "height": 16,
        "widget": {
          "title": "system.cpu.time [SUM]",
          "xyChart": {
            "chartOptions": {
              "mode": "COLOR"
            },
            "dataSets": [
              {
                "minAlignmentPeriod": "60s",
                "plotType": "LINE",
                "targetAxis": "Y1",
                "timeSeriesQuery": {
                  "timeSeriesFilter": {
                    "aggregation": {
                      "alignmentPeriod": "60s",
                      "crossSeriesReducer": "REDUCE_SUM",
                      "groupByFields": [],
                      "perSeriesAligner": "ALIGN_RATE"
                    },
                    "filter": "metric.type=\"workload.googleapis.com/system.cpu.time\" resource.type=\"generic_node\""
                  }
                }
              }
            ],
            "thresholds": [],
            "yAxis": {
              "label": "",
              "scale": "LINEAR"
            }
          }
        },
        "width": 24,
        "yPos": 16
      }
    ]
  }
}

@liufuyang
Copy link
Author

liufuyang commented Nov 3, 2023

Let me know it if helps or whether you could see the bug :)

By the way, the tf resource setup can be set as this:

resource "google_monitoring_dashboard" "service-dashboard" {
  dashboard_json = templatefile("${path.module}/monitoring.json", {})
}

@edwardmedia
Copy link
Contributor

Yeah, thanks for the info. Let me try to repro

@liufuyang
Copy link
Author

liufuyang commented Nov 3, 2023

Yep, I just tried with those simple json above and got the error again, this time it is:

panic: runtime error: index out of range [2] with length 2

This happens when there are 3 tiles/metrics on the Dashboard view:

The error can be resolved by manually removing the last (or perhaps any) metric from the console. (Remember to press the "Save" button on the top if you do so)

@edwardmedia
Copy link
Contributor

Yes, I can repro with your file. Thanks

@liufuyang liufuyang changed the title monitoring/resource_monitoring_dashboard.go:37 -> "panic: runtime error: index out of range [23] with length 23" monitoring/resource_monitoring_dashboard.go:37 -> "panic: runtime error: index out of range [x] with length x" Nov 3, 2023
@NickElliot NickElliot added service/monitoring-services forward/review In review; remove label to forward and removed forward/review In review; remove label to forward labels Nov 3, 2023
@NickElliot NickElliot removed their assignment Nov 3, 2023
@etruong42
Copy link

etruong42 commented Nov 29, 2023

The stacktrace points to code that was introduced at GoogleCloudPlatform/magic-modules#9065 and a comment in the internal bug tracking system also suggests this new code to be the issue. I have opened a PR to revert this change: GoogleCloudPlatform/magic-modules#9538

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.