Package list golang-github-go-kit-kit / 0ff90aa
adding graphite/Dialer JP Robinson 5 years ago
2 changed file(s) with 30 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
2727 type Emitter struct {
2828 prefix string
2929
30 addr string
31 tcp bool
32 conn net.Conn
33 start sync.Once
34 stop chan bool
30 network, addr string
31 conn net.Conn
32 dialer Dialer
33 start sync.Once
34 stop chan bool
3535
3636 mtx sync.Mutex
3737 counters []*counter
4343
4444 // NewEmitter will return an Emitter that will prefix all
4545 // metrics names with the given prefix. Once started, it will attempt to create
46 // a TCP or a UDP connection with the given address and periodically post
46 // a connection with the given network and address via `net.Dial` and periodically post
4747 // metrics to the connection in the Graphite plaintext protocol.
48 // If the provided `tcp` parameter is false, a UDP connection will be used.
49 func NewEmitter(addr string, tcp bool, metricsPrefix string, logger log.Logger) *Emitter {
48 func NewEmitter(network, addr string, metricsPrefix string, logger log.Logger) *Emitter {
49 return NewEmitterDial(network, addr, net.Dial, metricsPrefix, logger)
50 }
51
52 // NewEmitter will return an Emitter that will prefix all
53 // metrics names with the given prefix. Once started, it will attempt to create
54 // a connection with the given network and address via the given Dialer and periodically post
55 // metrics to the connection in the Graphite plaintext protocol.
56 func NewEmitterDial(network, addr string, dialer Dialer, metricsPrefix string, logger log.Logger) *Emitter {
5057 return &Emitter{
51 addr: addr,
52 tcp: tcp,
53 stop: make(chan bool),
54 prefix: metricsPrefix,
55 logger: logger,
58 network: network,
59 addr: addr,
60 dialer: net.Dial,
61 stop: make(chan bool),
62 prefix: metricsPrefix,
63 logger: logger,
5664 }
5765 }
5866
117125 }
118126
119127 func (e *Emitter) dial() error {
120 if e.tcp {
121 tAddr, err := net.ResolveTCPAddr("tcp", e.addr)
122 if err != nil {
123 return err
124 }
125 e.conn, err = net.DialTCP("tcp", nil, tAddr)
126 if err != nil {
127 return err
128 }
129 } else {
130 uAddr, err := net.ResolveUDPAddr("udp", e.addr)
131 if err != nil {
132 return err
133 }
134 e.conn, err = net.DialUDP("udp", nil, uAddr)
135 if err != nil {
136 return err
137 }
138 }
139 return nil
140 }
128 var err error
129 e.conn, err = e.dialer(e.network, e.addr)
130 return err
131 }
132
133 type Dialer func(network, addr string) (net.Conn, error)
141134
142135 // Start will kick off a background goroutine to
143136 // call Flush once every interval.
1111
1212 func TestHistogramQuantiles(t *testing.T) {
1313 prefix := "prefix"
14 e := NewEmitter("", true, prefix, nil)
14 e := NewEmitter("", "", prefix, nil)
1515 var (
1616 name = "test_histogram_quantiles"
1717 quantiles = []int{50, 90, 95, 99}
3535 prefix = "prefix"
3636 name = "m"
3737 value = 123
38 e = NewEmitter("", true, prefix, nil)
38 e = NewEmitter("", "", prefix, nil)
3939 b bytes.Buffer
4040 )
4141 e.NewCounter(name).With(metrics.Field{Key: "ignored", Value: "field"}).Add(uint64(value))
5353 name = "xyz"
5454 value = 54321
5555 delta = 12345
56 e = NewEmitter("", true, prefix, nil)
56 e = NewEmitter("", "", prefix, nil)
5757 b bytes.Buffer
5858 g = e.NewGauge(name).With(metrics.Field{Key: "ignored", Value: "field"})
5959 )