Skip to content
This repository has been archived by the owner on Nov 29, 2019. It is now read-only.

spoof/go-grafana

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-grafana GoDoc Go Report Card Build Status

go-grafana is a Go client library for Grafana API

Installation

go get -u github.com/spoof/go-grafana

Usage

package main

import (
	"context"
	"log"
	"net/url"

	"github.com/spoof/go-grafana/client"
	"github.com/spoof/go-grafana/grafana"
	"github.com/spoof/go-grafana/grafana/panel"
	"github.com/spoof/go-grafana/grafana/query"
)

func main() {
	const token = "<token>"
	url, _ := url.Parse("http://localhost:3000/")
	client := client.NewClient(url, token, nil)
	ctx := context.Background()

	// Create dashboard
	d := grafana.NewDashboard("Title Demo")
	d.Tags.Set("tag1", "tag2")

	// Add row
	row := grafana.NewRow()
	row.Title = "Row"
	row.ShowTitle = true
 
 	// Add Graph panel
	p := panel.NewGraph()
	p.GeneralOptions().Height = "250px"
	p.GeneralOptions().Span = 2
	p.XAxis.Show = true
	p.YAxes.Left.Format = "short"
	p.YAxes.Left.Show = true

	// Graph panel with query to Prometheus datasource
	q := query.NewPrometheus("Prometheus")
	q.Expression = `up{job="job"}`
	q.Interval = "1"
	q.LegendFormat = "{{ instance }}"

	queries := p.Queries()
	*queries = []panel.Query{q}

	row.Panels = append(row.Panels, p)
	d.Rows = append(d.Rows, row)

	overwrite := true
	if err := client.Dashboards.Save(ctx, d, overwrite); err != nil {
		log.Fatalf("Error while saving dashboard %s", err)
	}

}

Current Status

Project is under active development. It's not ready for production use due to high risk of API changes. Please checkout out TODO for more information what's done and what's going to be done.

License

This library is distributed under the Apache 2.0 license found in the LICENSE file.