sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Cannot unmarshal object into Go struct

Open mpostument opened this issue 4 years ago • 10 comments
trafficstars

Hello, have an issue on GetDashboardByUID method. On some dashboards getting errors such as

2021/03/05 13:27:41 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-compute-resources-workload
2021/03/05 13:27:42 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/kubernetes-networking-namespace-pods
2021/03/05 13:27:42 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/kubernetes-networking-pod
2021/03/05 13:27:43 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/loki-quick-search

And this

2021/03/05 15:48:17 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/auth
2021/03/05 15:48:20 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-cluster
2021/03/05 15:48:30 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/production

Additional details here

mpostument avatar Mar 05 '21 13:03 mpostument

Thanks for your report! Will try looking into it by using your gist. In the mean time help is welcome :hugs:

GiedriusS avatar Mar 10 '21 14:03 GiedriusS

For at least the TemplateVar.templating.list.query issue, I have fixed this in a private fork -- the problem is that prometheus based queries have a different schema, and afaict there is no way to tell what the schema is unless you know the type of the datasource (which you could only do by querying grafana during unmarshalling). I ended up writing something like the following:

func (tv *TemplateVar) UnmarshalJSON(b []byte) (err error) {
	var def TemplateVarDefault
	var prom TemplateVarPrometheus

	if err = json.Unmarshal(b, &def); err == nil {
		tv.TemplateVarDefault = &def
	} else if err = json.Unmarshal(b, &prom); err == nil {
		tv.TemplateVarPrometheus = &prom
	} else {
		return fmt.Errorf("unhandled schema")
	}
	return
}

Unfortunately there are a lot of places in the grafana json that require this type of approach, e.g. the Target struct which changes form in incompatible ways depending on the type of query. I'd be happy to help with some PRs, but the problem is fixes like above are breaking changes to the sdk, so I would want to get agreement on the general approach.

lukehunter-cs avatar Mar 11 '21 20:03 lukehunter-cs

Hello. Any updates? This issue blocks us.

Noksa avatar Mar 13 '21 13:03 Noksa

@GiedriusS this issue is still present after #151.

2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-networking-workload
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-persistent-volumes
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-pods
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-statefulsets
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/loki-quick-search

Can it be reopened?

mpostument avatar Sep 20 '21 11:09 mpostument

of type string for db/loki-quick-search

Isn't it interface{} now? @mpostument are you using the newest version? Could you please help me out with a test case for this?

GiedriusS avatar Sep 21 '21 19:09 GiedriusS

Last tried on version v0.0.0-20210621184808-90d328319afc Example of the dashboard where it failing, also save this error on few other providers, I think on elastic search data source

mpostument avatar Sep 21 '21 19:09 mpostument

Same issue, hard to debug

chenlujjj avatar Nov 30 '21 12:11 chenlujjj

Hey everyone! I was wondering what the state on this one is, as it blocks to sync dashboards easily.

telmich avatar Feb 05 '22 15:02 telmich

Looks like the cloudwatch datasource also triggers this behavior.

2022/02/14 16:22:25 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-billing
2022/02/14 16:22:25 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-cloudwatch-browser
2022/02/14 16:22:25 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-ebs
2022/02/14 16:22:26 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type string for db/aws-elasticsearch
2022/02/14 16:22:26 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-rds
2022/02/14 16:22:26 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-route-53
2022/02/14 16:22:27 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-s3
2022/02/14 16:22:27 unmarshal board: json: cannot unmarshal object into Go struct field Row.rows.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-vpn

Looks like it's a different root cause. This issue is tracking the datasource can be a string or a map in the JSON exports. My report seems to be a different element on the Panels, unfortunately it's not as obvious as I'd like

dekimsey avatar Feb 14 '22 22:02 dekimsey

Same here, even try the latest commit 2ff95a73e54ee3d32624e26e8880cefe7e28c8a3

json: cannot unmarshal object into Go struct field Board.panels of type string

titaneric avatar Mar 17 '22 11:03 titaneric