graphigo

package module
v1.0.0-...-56c4b5b Latest Latest
Warning

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

Go to latest
Published: May 9, 2015 License: MIT Imports: 5 Imported by: 0

README

Graphigo

Build Status GoDoc

A simple go client for the graphite monitoring tool.

Installation

Use go get to install graphigo:

go get github.com/fgrosse/graphigo

No additional dependencies are required.

Documentation

A generated documentation is available at godoc.org

Usage

The following usage example is basically a copy of tests/usage_example.go for your convenience.

package tests

import "github.com/fgrosse/graphigo"

func usageExample() {
    client := graphigo.NewClient("graphite.your.org:2003")

    // set a custom timeout (seconds) for the graphite connection
    // if timeout = 0 then the graphigo.DefaultTimeout = 5 seconds is used
    // Setting Timeout to -1 disables the timeout
    client.Timeout = 0

    // set a custom prefix for all recorded metrics of this client (optional)
    client.Prefix = "foo.bar.baz"

	if err := client.Connect(); err != nil {
		panic(err)
	}

	// close the TCP connection properly if you don't need it anymore
	defer client.Disconnect()

	// capture and send values using a single line
	client.SendValue("hello.graphite.world", 42)

	// capture a metric and send it any time later
	metric := graphigo.NewMetric("test", 3.14) // you can use any type as value
	defer client.Send(metric)

	// create a whole bunch of metrics and send them all with one TCP call
	metrics := []graphigo.Metric{
		graphigo.NewMetric("foo", 1),
		graphigo.NewMetric("bar", 1.23),
		graphigo.NewMetric("baz", "456"),
	}
	client.SendAll(metrics)

	// of course this all works in once line and still reads nicely
	client.SendAll([]graphigo.Metric{
		graphigo.NewMetric("shut", 1),
		graphigo.NewMetric("up", 2),
		graphigo.NewMetric("and", 3),
		graphigo.NewMetric("take", 4),
		graphigo.NewMetric("my", 5),
		graphigo.NewMetric("money", 6),
	})
}

There is also the NullClient implementation which implements the GraphiteClient interface but does not actually send any data to graphite.

package tests

import "github.com/fgrosse/graphigo"

// Note that we do return the interface so we can also set up the concrete implementation in this function
func setupGraphiteClient(address string, enabled bool) graphigo.GraphiteClient {
    if enabled {
	    client := graphigo.NewClient(address)
        client.Prefix = "foo.bar.baz"
        return client
    } else {
        return graphigo.NewNullClient()
    }
}

Running the tests

Graphigo uses the awesome ginkgo framework for its tests. You can execute the tests running:

ginkgo tests

If you prefer to use go test directly you can either switch into the ./tests directory and run it there or run the following from the repository root directory:

go test ./tests

Contributing

Any contributions are always welcome (use pull requests). Please keep in mind that I might not always be able to respond immediately but I usually try to react within the week ☺.

Documentation

Overview

Package graphigo provides a simple go client for the graphite monitoring tool. See http://graphite.readthedocs.org/en/latest/overview.html

Index

Constants

View Source
const (
	// DefaultTimeout is the timeout that is applied when connecting to a graphite server
	// if no explicit timeout has been configured.
	DefaultTimeout = 5

	// TimeoutDisabled is used to disable the client timeout entirely
	TimeoutDisabled = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Graphigo

type Graphigo struct {
	Address string
	Timeout time.Duration
	Prefix  string
	// contains filtered or unexported fields
}

Graphigo is a simple implementation of the GraphiteClient interface You can get new instances using graphigo.NewClient(address)

If you want to configure a global name prefix for all recorded values you can access the globally available Prefix field.

To set a timeout on the underlying connection to graphite, set the Timeout field of this struct.

func NewClient

func NewClient(address string) *Graphigo

NewClient creates a new instance of a graphite client. Use the address:port notation to specify the port.

func (*Graphigo) Connect

func (g *Graphigo) Connect() (err error)

Connect attempts to establish the connection to the graphite server. This will return an error if a TCP connection can not or has already been established.

func (*Graphigo) Disconnect

func (g *Graphigo) Disconnect() error

Disconnect closes the underlying connection to graphite.

func (*Graphigo) InjectConnection

func (g *Graphigo) InjectConnection(connection io.WriteCloser)

InjectConnection can be used to inject your own implementation of the connection to graphite. The connection is reset by any call to Disconnect().

func (*Graphigo) Send

func (g *Graphigo) Send(metric Metric) error

Send sends a graphigo.Metric to graphite. This can be used to send a metric which has been recorded earlier.

Use SendValue if you want to create and send a metric in one step. Use SendAll if you want to send multiple metrics at once. This will return an error if the client has not yet been connected or the metric name is empty.

func (*Graphigo) SendAll

func (g *Graphigo) SendAll(metrics []Metric) (err error)

SendAll sends multiple graphigo.Metric to graphite. This can be used to send multiple metrics that have been recorded earlier.

Use Send if you want to send a single metric. This will return an error if the client has not yet been connected or if any of the metrics has an empty name.

func (*Graphigo) SendValue

func (g *Graphigo) SendValue(name string, value interface{}) error

SendValue creates a new graphigo.Metric with the metric timestamp set to now and sends it to graphite.

Use Send(metric) if you want to split metric recording and sending. This will return an error if the client has not yet been connected or the metric name is empty.

type GraphiteClient

type GraphiteClient interface {
	Connect() error
	Disconnect() error

	SendValue(name string, value interface{}) error
	Send(Metric) error
	SendAll([]Metric) error
}

GraphiteClient is the interface for a graphigo graphite client. Use it for dependency injection and the good of humanity

type Metric

type Metric struct {
	Name      string
	Value     interface{}
	Timestamp time.Time
}

Metric holds all information that is send to graphite. The value can be any go type but in practice graphite will probably only accept numeric values.

func NewMetric

func NewMetric(name string, value interface{}) Metric

NewMetric creates a new metric

func (Metric) String

func (m Metric) String() string

String returns a textual representation of a metric (used for debugging)

func (Metric) UnixTimestamp

func (m Metric) UnixTimestamp() int64

UnixTimestamp returns the the number of seconds elapsed since January 1, 1970 UTC.

type NullClient

type NullClient struct{}

NullClient implements the GraphiteClient but does not perform any actions

func NewNullClient

func NewNullClient() *NullClient

func (*NullClient) Connect

func (c *NullClient) Connect() error

func (*NullClient) Disconnect

func (c *NullClient) Disconnect() error

func (*NullClient) Send

func (c *NullClient) Send(Metric) error

func (*NullClient) SendAll

func (c *NullClient) SendAll([]Metric) error

func (*NullClient) SendValue

func (c *NullClient) SendValue(_ string, _ interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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