Skip to content

Example code for creating new dashboard with row #212

Description

@ykyuen

Hi all,

i follow the example code and try creating a new dashboard.

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/grafana-tools/sdk"
)

func main() {
	const GRAFANA_URL = "http://localhost:3000"
	const GRAFANA_API_KEY = "<GRAFANA_API_KEY>"

	// Initialize the grafana client
	grafanaClient, err := sdk.NewClient(GRAFANA_URL, GRAFANA_API_KEY, sdk.DefaultHTTPClient)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create a client: %s\n", err)
		os.Exit(1)
	}

	// Create dashboard
	board := sdk.NewBoard("Sample dashboard title")
	board.ID = 1
	board.Time.From = "now-30m"
	board.Time.To = "now"
	row1 := board.AddRow("Sample row title")
	row1.Add(sdk.NewGraph("Sample graph"))
	graph := sdk.NewGraph("Sample graph 2")
	target := sdk.Target{
		RefID:      "A",
		Datasource: "Sample Source 1",
		Expr:       "sample request 1"}
	graph.AddTarget(&target)
	row1.Add(graph)
	response, err := grafanaClient.SetDashboard(context.TODO() ,*board, sdk.SetDashboardParams{
		Overwrite: false,
	})
	if err != nil {
		fmt.Printf("error on uploading dashboard %s", board.Title)
	} else {
		fmt.Printf("dashboard URL: %v", GRAFANA_URL + *response.URL)
	}
}

The dashboard is successfully created but there is no row with name "Sample row title".
sc-2022-06-08-23-55-25

What i suppose to have...
sc-2022-06-09-00-02-09

What did i miss?

Thanks for developing this project.

Regards,
Kit

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions