Permalink
Browse files

Add unified factory method for creating graphite

Add tests for factory method
  • Loading branch information...
1 parent bea0c29 commit 5bc23c31ae20a0d777c287953382939b3bb8f6e0 @stgleb stgleb committed Aug 30, 2016
Showing with 85 additions and 22 deletions.
  1. +24 −22 graphite.go
  2. +61 −0 graphite_test.go
View
@@ -127,42 +127,44 @@ func (graphite *Graphite) SimpleSend(stat string, value string) error {
// NewGraphite is a factory method that's used to create a new Graphite
func NewGraphite(host string, port int) (*Graphite, error) {
- Graphite := &Graphite{Host: host, Port: port, Protocol: "tcp"}
- err := Graphite.Connect()
- if err != nil {
- return nil, err
- }
-
- return Graphite, nil
+ return GraphiteFactory("tcp", host, port, "")
}
// NewGraphiteWithMetricPrefix is a factory method that's used to create a new Graphite with a metric prefix
func NewGraphiteWithMetricPrefix(host string, port int, prefix string) (*Graphite, error) {
- Graphite := &Graphite{Host: host, Port: port, Protocol: "tcp", Prefix: prefix}
- err := Graphite.Connect()
- if err != nil {
- return nil, err
- }
-
- return Graphite, nil
+ return GraphiteFactory("tcp", host, port, prefix)
}
// When a UDP connection to Graphite is required
func NewGraphiteUDP(host string, port int) (*Graphite, error) {
- Graphite := &Graphite{Host: host, Port: port, Protocol: "udp"}
- err := Graphite.Connect()
- if err != nil {
- return nil, err
- }
-
- return Graphite, nil
+ return GraphiteFactory("udp", host, port, "")
}
// 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}
+ graphiteNop, _ := GraphiteFactory("nop", host, port, "")
return graphiteNop
}
+
+func GraphiteFactory(protocol string, host string, port int, prefix string) (*Graphite, error) {
+ var graphite *Graphite
+
+ switch protocol {
+ case "tcp":
+ graphite = &Graphite{Host: host, Port: port, Protocol: "tcp", Prefix: prefix}
+ case "udp":
+ graphite = &Graphite{Host: host, Port: port, Protocol: "udp"}
+ case "nop":
+ graphite = &Graphite{Host: host, Port: port, nop: true}
+ }
+
+ err := graphite.Connect()
+ if err != nil {
+ return nil, err
+ }
+
+ return graphite, nil
+}
View
@@ -1,10 +1,16 @@
package graphite
import (
+ "fmt"
"net"
+ "strings"
"testing"
)
+const TCP = "tcp"
+const UDP = "udp"
+const NOP = "nop"
+
// Change these to be your own graphite server if you so please
var graphiteHost = "carbon.hostedgraphite.com"
var graphitePort = 2003
@@ -43,6 +49,61 @@ func TestNewGraphiteUDP(t *testing.T) {
}
}
+func TestGraphiteFactoryTCP(t *testing.T) {
+ gr, err := GraphiteFactory(TCP, graphiteHost, graphitePort, "")
+
+ if err != nil {
+ t.Error(err)
+ }
+
+ if _, ok := gr.conn.(*net.TCPConn); !ok {
+ t.Error("GraphiteHost.conn is not a TCP connection")
+ }
+}
+
+func TestGraphiteFactoryTCPWithPrefix(t *testing.T) {
+ prefix := "test"
+ gr, err := GraphiteFactory(TCP, graphiteHost, graphitePort, prefix)
+
+ if err != nil {
+ t.Error(err)
+ }
+
+ if _, ok := gr.conn.(*net.TCPConn); !ok {
+ t.Error("GraphiteHost.conn is not a TCP connection")
+ }
+
+ if !strings.EqualFold(prefix, gr.Prefix) {
+ t.Error(fmt.Sprintf("Wrong prefix is set expected %s actual %s",
+ prefix,
+ gr.Prefix))
+ }
+}
+
+func TestGraphiteFactoryUDP(t *testing.T) {
+ gr, err := GraphiteFactory(UDP, graphiteHost, graphitePort, "")
+
+ if err != nil {
+ t.Error(err)
+ }
+
+ if _, ok := gr.conn.(*net.UDPConn); !ok {
+ t.Error("GraphiteHost.conn is not a UDP connection")
+ }
+}
+
+func TestGraphiteFactoryNop(t *testing.T) {
+ gr, err := GraphiteFactory(NOP, graphiteHost, graphitePort, "")
+
+ if err != nil {
+ t.Error(err)
+ }
+
+ if !gr.IsNop() {
+ t.Error("GraphiteHost is not NOP")
+ }
+}
+
// Uncomment the following method to test sending an actual metric to graphite
//
//func TestSendMetric(t *testing.T) {

0 comments on commit 5bc23c3

Please sign in to comment.