Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 886db4351bb95fc8a7fba00a8cf2b99bd431e840 marpaia committed Nov 9, 2013
Showing with 246 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +57 −0 README.md
  3. +35 −0 doc.go
  4. +117 −0 graphite.go
  5. +34 −0 graphite_test.go
@@ -0,0 +1,3 @@
+*.test
+*.swp
+.DS_Store
@@ -0,0 +1,57 @@
+graphite-golang
+===============
+
+This is a lightweight Graphite API client written in Go.
+
+## Installation
+
+Use `go-get` to install graphite-golang
+```
+go get github.com/marpaia/graphite-golang
+```
+
+## External dependencies
+
+This project has no external dependencies other than the Go standard library.
+
+## Documentation
+
+Like most every other Golang project, this projects documentation can be found
+on godoc at [godoc.org/github.com/marpaia/graphite-golang](http://godoc.org/github.com/marpaia/graphite-golang).
+
+## Examples
+
+```go
+package mylib
+
+import (
+ "github.com/marpaia/graphite-golang"
+ "log"
+)
+
+func init() {
+
+ // load your configuration file / mechanism
+ config := newConfig()
+
+ // try to connect a graphite server
+ if config.GraphiteEnabled {
+ Graphite, err = graphite.NewGraphite(config.Graphite.Host, config.Graphite.Port)
+ } else {
+ Graphite = graphite.NewGraphiteNop(config.Graphite.Host, config.Graphite.Port)
+ }
+ // if you couldn't connect to graphite, use a nop
+ if err != nil {
+ Graphite = graphite.NewGraphiteNop(config.Graphite.Host, config.Graphite.Port)
+ }
+
+ log.Printf("Loaded Graphite connection: %#v", Graphite)
+ Graphite.Increment("stats.graphite_loaded")
+}
+
+func doWork() {
+ // this will work just fine, regardless of if you're working with a graphite
+ // nop or not
+ Graphite.Increment("stats.doing_work")
+}
+```
@@ -0,0 +1,35 @@
+// Example of using the graphiteNop feature in action:
+//
+// package mylib
+//
+// import (
+// "github.com/marpaia/graphite-golang"
+// "log"
+// )
+//
+// func init() {
+//
+// // load your configuration file / mechanism
+// config := newConfig()
+//
+// // try to connect a graphite server
+// if config.GraphiteEnabled {
+// Graphite, err = graphite.NewGraphite(config.Graphite.Host, config.Graphite.Port)
+// } else {
+// Graphite = graphite.NewGraphiteNop(config.Graphite.Host, config.Graphite.Port)
+// }
+// // if you couldn't connect to graphite, use a nop
+// if err != nil {
+// Graphite = graphite.NewGraphiteNop(config.Graphite.Host, config.Graphite.Port)
+// }
+//
+// log.Printf("Loaded Graphite connection: %#v", Graphite)
+// Graphite.Increment("stats.graphite_loaded")
+// }
+//
+// func doWork() {
+// // this will work just fine, regardless of if you're working with a graphite
+// // nop or not
+// Graphite.Increment("stats.doing_work")
+// }
+package graphite
@@ -0,0 +1,117 @@
+package graphite
+
+import (
+ "bytes"
+ "fmt"
+ "log"
+ "net"
+ "time"
+)
+
+// Graphite is a struct that defines the relevant properties of a graphite
+// connection
+type Graphite struct {
+ Host string
+ Port int
+ Timeout time.Duration
+ conn net.Conn
+ nop bool
+}
+
+// Metric is a struct that defines the relevant properties of a graphite metric
+type Metric struct {
+ Name string
+ Value string
+ Timestamp int64
+}
+
+// defaultTimeout is the default number of seconds that we're willing to wait
+// before forcing the connection establishment to fail
+const defaultTimeout = 5
+
+// IsNop is a getter for *graphite.Graphite.nop
+func (graphite *Graphite) IsNop() bool {
+ if graphite.nop {
+ return true
+ } else {
+ return false
+ }
+}
+
+// Given a Graphite struct, Connect populates the Graphite.conn field with an
+// appropriate TCP connection
+func (graphite *Graphite) Connect() error {
+ if !graphite.IsNop() {
+ address := fmt.Sprintf("%s:%d", graphite.Host, graphite.Port)
+
+ if graphite.Timeout == 0 {
+ graphite.Timeout = defaultTimeout * time.Second
+ }
+
+ conn, err := net.DialTimeout("tcp", address, graphite.Timeout)
+ if err != nil {
+ return err
+ }
+
+ graphite.conn = conn
+ }
+
+ return nil
+}
+
+// Given a Metric struct, the SendMetric method sends the supplied metric to the
+// Graphite connection that the method is called upon
+func (graphite *Graphite) SendMetric(metric Metric) {
+ if metric.Timestamp == 0 {
+ metric.Timestamp = time.Now().Unix()
+ }
+
+ graphite.sendMetric(metric)
+}
+
+// The Increment method increments an arbitrary metric on the remote graphite host
+func (graphite *Graphite) Increment(stat string) error {
+ metric := Metric{Name: stat, Value: "1", Timestamp: time.Now().Unix()}
+ err := graphite.sendMetric(metric)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+// sendMetric is an internal function that is used to write to the TCP
+// connection in order to communicate a metric to the remote Graphite host
+func (graphite *Graphite) sendMetric(metric Metric) error {
+ if !graphite.IsNop() {
+ buf := bytes.NewBufferString(fmt.Sprintf("%s %s %d\n", metric.Name, metric.Value, metric.Timestamp))
+ _, err := graphite.conn.Write(buf.Bytes())
+ if err != nil {
+ return err
+ }
+ } else {
+ log.Printf("Graphite: %s %s %d\n", metric.Name, metric.Value, metric.Timestamp)
+ }
+
+ return nil
+}
+
+// NewGraphiteHost is a factory method that's used to create a new Graphite
+// connection given a hostname and a port number
+func NewGraphite(host string, port int) (*Graphite, error) {
+ Graphite := &Graphite{Host: host, Port: port}
+ err := Graphite.Connect()
+ if err != nil {
+ return nil, err
+ }
+
+ return Graphite, nil
+}
+
+// NewGraphiteNop is a factory method that returns a Graphite struct but will
+// not actually try to send any packets to a remote host and, instead, will just
+// log. This is useful if you want to use Graphite in a project but don't want
+// to make Graphite a requirement for the project.
+func NewGraphiteNop(host string, port int) *Graphite {
+ graphiteNop := &Graphite{Host: host, Port: port, nop: true}
+ return graphiteNop
+}
@@ -0,0 +1,34 @@
+package graphite
+
+import (
+ "net"
+ "testing"
+)
+
+// Change these to be your own graphite server if you so please
+var graphiteHost = "carbon.hostedgraphite.com"
+var graphitePort = 2003
+
+func TestNewGraphite(t *testing.T) {
+ gh, err := NewGraphite(graphiteHost, graphitePort)
+ if err != nil {
+ t.Error(err)
+ }
+
+ if _, ok := gh.conn.(*net.TCPConn); !ok {
+ t.Error("GraphiteHost.conn is not a TCP connection")
+ }
+}
+
+// Uncomment the following method to test sending an actual metric to graphite
+//
+//func TestSendMetric(t *testing.T) {
+// gh, err := NewGraphite(graphiteHost, graphitePort)
+// if err != nil {
+// t.Error(err)
+// }
+// err = gh.Increment("stats.test.metric11")
+// if err != nil {
+// t.Error(err)
+// }
+//}

0 comments on commit 886db43

Please sign in to comment.