Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #24 from eranb/patch-1
Browse files Browse the repository at this point in the history
UDP fix
  • Loading branch information
marpaia committed Dec 31, 2017
2 parents c474c9b + 42b90fe commit 134b9af
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions graphite.go
Expand Up @@ -46,7 +46,19 @@ func (graphite *Graphite) Connect() error {
graphite.Timeout = defaultTimeout * time.Second
}

conn, err := net.DialTimeout(graphite.Protocol, address, graphite.Timeout)
var err error
var conn net.Conn

if graphite.Protocol == "udp" {
udpAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return err
}
conn, err = net.DialUDP(graphite.Protocol, nil, udpAddr)
} else {
conn, err = net.DialTimeout(graphite.Protocol, address, graphite.Timeout)
}

if err != nil {
return err
}
Expand Down Expand Up @@ -104,8 +116,7 @@ func (graphite *Graphite) sendMetrics(metrics []Metric) error {
metric_name = metric.Name
}
if graphite.Protocol == "udp" {
bufString := bytes.NewBufferString(fmt.Sprintf("%s %s %d\n", metric_name, metric.Value, metric.Timestamp))
graphite.conn.Write(bufString.Bytes())
fmt.Fprintf(graphite.conn, "%s %s %d\n", metric_name, metric.Value, metric.Timestamp)
continue
}
buf.WriteString(fmt.Sprintf("%s %s %d\n", metric_name, metric.Value, metric.Timestamp))
Expand Down

0 comments on commit 134b9af

Please sign in to comment.