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

Disappearing constant variables after getting and setting the same dashboard #173

Open
MinusNull opened this issue Oct 18, 2021 · 4 comments

Comments

@MinusNull
Copy link

I use grafana v8.0.2

My dashboard has following json model:

... "templating": { "list": [ { "datasource": null, "description": null, "error": null, "hide": 2, "includeAll": false, "label": "", "multi": false, "name": "period", "query": "1d", "refresh": false, "regex": "", "skipUrlSync": false, "type": "constant" } ] }, ...

After that code my dashboard has constant variables lost

board, properties, err := client.GetDashboardByUID(context.Background(), grafanaDashboardUid)
if err != nil {
	log.Fatal(err)
}

_, err = client.SetDashboard(context.Background(), board, sdk.SetDashboardParams{
	FolderID: properties.FolderID,
	Overwrite:  true,
})
if err != nil {
	log.Fatal(err)
}

Json model:
"templating": { "list": [] },

But if i use raw functions everything is fine

boardJson, properties, err := client.GetRawDashboardByUID(context.Background(), grafanaDashboardUid)
if err != nil {
	log.Fatal(err)
}

_, err = client.SetRawDashboardWithParam(context.Background(), sdk.RawBoardRequest{
	Dashboard:  boardJson,
	Parameters: sdk.SetDashboardParams{
		FolderID:   properties.FolderID,
		Overwrite:  true,
	},
})
if err != nil {
	log.Fatal(err)
}

I've found out that Options field of TemplateVar struct should have omitempty tag not to cause disappearing constant variables.
https://github.com/grafana-tools/sdk/blob/master/board.go#L88

Without omitempty tag Grafana sees that my constant variable has options json field and doesn't apply dashboard correctly.

But why Grafana has such behavior i don't know.

@GiedriusS
Copy link
Collaborator

GiedriusS commented Oct 18, 2021

I'm not sure when I'll be able to get around to this but if you have some time then maybe you could open up a PR with this change + some integration tests that would execute this path? I wonder if this is reproducible with older versions

@MinusNull
Copy link
Author

Ok, i'll try to do it with the latest Grafana image v8.2.1.

@MinusNull
Copy link
Author

I've tried to write an integration test but it requires using Selenium that looks like overkill. Without Selenium i have not found a way to ensure that my code change fixes the trouble i met.

I'll try to explain my trouble by screenshots:

  1. Options field doesn't have tag omitempty

a) I create new dashboard with only one constant variable:
image

b) There is my dashboard after calling GetDashboardByUID and SetDashboard
image
image
image

  1. Options field has tag omitempty

a) I create new dashboard with only one constant variable:
image

b) There is my dashboard after calling GetDashboardByUID and SetDashboard
image
image
image

@krya-kryak
Copy link
Collaborator

Can confirm, this is reproducible. This seems like a grafana quirk we should just work around now (add omitempty), leaving e2e tests for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants