graphite

package module
v0.0.0-...-72ae6e7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2019 License: MIT Imports: 8 Imported by: 0

README

About

Client for graphite web api. Full documentation with other examples could be found on godoc.

Example

	package main
	
	import (
		"github.com/lorehov/graphite-api-client"
		"log"
		"fmt"
	)

	func main() {
		client, err := NewFromString("https://my-graphite.tld")
		if err != nil {
			log.Fatalf("Couldn't parse url")
		}

		res, err := client.QueryRender(
			RenderRequest{
				From: time.Unix(1468339853, 0),
				Until: time.Unix(1568339853, 0),
				MaxDataPoints: 1000,
				Targets: []string{"main"},
			},
		)

		if err != nil {
			log.Fatalf("Error %s while performing query %s", err.Error(), err.Query)
		}
		
		for _, series := range res {
			fmt.Printf("Processing target %s", series.Target)
			for _, dp := range series.Datapoints {
				fmt.Printf("Datapoint %+v", dp)
			}
		}
	}

Documentation

Overview

Package graphite provides client to graphite-web-api (http://graphite-api.readthedocs.io/en/latest/api.html).

Index

Examples

Constants

This section is empty.

Variables

View Source
var WrongUrlError = errors.New("Wrong url")

Functions

This section is empty.

Types

type Client

type Client struct {
	Url    url.URL
	Client *http.Client
}

Client is client to `graphite web api` (http://graphite-api.readthedocs.io/en/latest/api.html). You can either instantiate it manually by providing `url` and `client` or use a `NewFromString` shortcut.

func NewFromString

func NewFromString(urlString string) (*Client, error)

NewFromString is a convenient function for constructing `graphite.Client` from url string. For example 'https://my-graphite.tld'. NewFromString could return either `graphite.Client` instance or `WrongUrlError` error.

Example
client, _ := NewFromString("http://my-graphite.tld")
fmt.Printf(client.Url.Host)
Output:

my-graphite.tld

func (*Client) ExpandMetrics

func (c *Client) ExpandMetrics(r ExpandMetricRequest) (ExpandResult, error)

FindMetrics perform request to /metrics/expand API: http://graphite-api.readthedocs.io/en/latest/api.html#metrics-expand It returns slice of Metric if all is OK or RequestError if things goes wrong.

Example
ts := createExpandMetricsTestServer()
defer ts.Close()

client, _ := NewFromString(ts.URL)
res, _ := client.ExpandMetrics(
	ExpandMetricRequest{
		Query: "cluster.*",
	},
)
fmt.Printf("%+v", res)
Output:

{Results:[cluster.one cluster.two]}

func (*Client) FindMetrics

func (c *Client) FindMetrics(r FindMetricRequest) ([]Metric, error)

FindMetrics perform request to /metrics/find API: http://graphite-api.readthedocs.io/en/latest/api.html#metrics-find It returns slice of Metric if all is OK or RequestError if things goes wrong.

Example
ts := createFindMetricsTestServer()
defer ts.Close()

client, _ := NewFromString(ts.URL)
res, _ := client.FindMetrics(
	FindMetricRequest{
		Query: "cluster.*",
	},
)
fmt.Printf("%+v", res)
Output:

[{Id:cluster.one Text:one Expandable:1 Leaf:0 AllowChildren:1} {Id:cluster.two Text:two Expandable:1 Leaf:0 AllowChildren:1}]

func (*Client) QueryRender

func (c *Client) QueryRender(r RenderRequest) ([]Series, error)

QueryRender performs query to graphite `/render/` api. Normally it should return `[]graphite.Series`, but if things go wrong it will return `graphite.RequestError` error.

Example
ts := createTestServer()
defer ts.Close()

client, _ := NewFromString(ts.URL)
res, _ := client.QueryRender(
	RenderRequest{
		Targets: []string{"main"},
	},
)
fmt.Printf(strconv.FormatInt(res[0].Datapoints[0].Timestamp.Unix(), 10))
Output:

1468339853

type DataPoint

type DataPoint struct {
	Value     float64
	Timestamp time.Time
}

DataPoint describes concrete point of time series.

type ExpandMetricRequest

type ExpandMetricRequest struct {
	Query       string
	GroupByExpr bool
	LeavesOnly  bool
}

type ExpandResult

type ExpandResult struct {
	Results []string
}

Results is a list of metric ids.

type FindMetricRequest

type FindMetricRequest struct {
	From      time.Time
	Until     time.Time
	Query     string
	Wildcards bool
}

FindMetricRequest is struct describing request to /metrics/find api.

type Metric

type Metric struct {
	Id            string
	Text          string
	Expandable    int
	Leaf          int
	AllowChildren int
}

type RenderRequest

type RenderRequest struct {
	From          time.Time
	Until         time.Time
	MaxDataPoints int
	Targets       []string
}

RenderRequest is struct, describing request to graphite `/render/` api. No fields are required. If field has zero value it'll be just skipped in request. RenderRequest.Targets are slice of strings, were every entry is a path identifying one or several metrics, optionally with functions acting on those metrics.

Warning. While wildcards could be used in Targets one should use them with caution, as using of the simple target like "main.cluster.*.cpu.*" could result in hundreds of series with megabytes of data inside.

type RequestError

type RequestError struct {
	Query string
	Type  string
}

RequestError is special error, which not only implements an error interface, but also provides access to `Query` field, containing original query which cause an error.

func (RequestError) Error

func (e RequestError) Error() string
Example
err := RequestError{
	Type: "Error type",
}
fmt.Printf(err.Error())
Output:

Error type

type Series

type Series struct {
	Target     string
	Datapoints []DataPoint
}

Series describes time series data for given target.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL