Skip to content

Commit

Permalink
Merge pull request #2 from messagebird/udp-disconnect-bug
Browse files Browse the repository at this point in the history
Don't return, but continue, on a UDP error
  • Loading branch information
marcelcorso authored Nov 10, 2016
2 parents f63e065 + ee50800 commit aaa49b6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: go

go:
- 1.4
- 1.5
- 1.6
- 1.7
- tip

script: make test
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ container: release_linux
test:
@echo "* Running tests"
@$(GO) test $(PROJECT)/...
@echo

@echo "* Checking code with golint"
@golint src/$(PROJECT)/...
@echo

@echo "* Checking code with go vet"
@$(GO) vet $(PROJECT)/...

clean:
rm -fr bin pkg vendor/pkg
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# Pushprom

[![Build Status](https://travis-ci.org/messagebird/pushprom.svg?branch=master)](https://travis-ci.org/messagebird/pushprom)

Pushprom is a proxy (HTTP/UDP) to the [Prometheus](https://prometheus.io/) Go client.

Prometheus doesn't offer a PHP client and PHP clients are hard to implement because they would need to keep track of state and PHP setups generally don't encourage that. That's why we built Pushprom.
Expand Down
5 changes: 2 additions & 3 deletions src/pushprom/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func listenUDP() {
n, _, err := serverConn.ReadFromUDP(buf)
if err != nil {
log.Println("Error: ", err)
return
continue
}
udpPacketCount.Inc()
if *debug {
Expand All @@ -34,13 +34,12 @@ func listenUDP() {
delta, err := NewDelta(bytes.NewBuffer(buf[0:n]))
if err != nil {
log.Println("Error: ", err)
return
continue
}

err = delta.Apply()
if err != nil {
log.Println("Error: ", err)
}

}
}
72 changes: 66 additions & 6 deletions src/pushprom/udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,46 @@ package main

import (
"encoding/json"
"log"
"net"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestUDP(t *testing.T) {
var conn *net.UDPConn

func TestMain(m *testing.M) {
*udpListenAddress = ":3007"

go listenUDP()

// wait for it to "boot"
time.Sleep(time.Millisecond * 1000)

serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1"+*udpListenAddress)
assert.Nil(t, err)
if err != nil {
log.Fatal("Could not resolve server UPD address")
}

localAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
assert.Nil(t, err)
if err != nil {
log.Fatal("Could not resolve local UDP address")
}

conn, err := net.DialUDP("udp", localAddr, serverAddr)
assert.Nil(t, err)
conn, err = net.DialUDP("udp", localAddr, serverAddr)
if err != nil {
log.Fatal("Could not establish UDP connection")
}

defer conn.Close()

os.Exit(m.Run())
}

func TestUDP(t *testing.T) {
delta := &Delta{
Type: GAUGE,
Method: "set",
Expand All @@ -38,7 +52,7 @@ func TestUDP(t *testing.T) {

buf, _ := json.Marshal(delta)

_, err = conn.Write(buf)
_, err := conn.Write(buf)
assert.Nil(t, err)

time.Sleep(time.Millisecond * 500)
Expand All @@ -49,3 +63,49 @@ func TestUDP(t *testing.T) {
assert.Equal(t, delta.Value, result)
}
}

func TestIncorrectJson(t *testing.T) {
// First, let's write the correct value
oldDelta := &Delta{
Type: GAUGE,
Method: "set",
Name: "tree_width",
Help: "the width in meters of the tree",
Value: 2.2,
}
buf, _ := json.Marshal(oldDelta)

_, err := conn.Write(buf)
assert.Nil(t, err)

oldMetrics := fetchMetrics(t)
oldResult, err := readMetric(oldMetrics, oldDelta.Name, oldDelta.Labels)
if assert.Nil(t, err) {
assert.Equal(t, oldDelta.Value, oldResult)
}

// Now, let's write the incorrect value
buf = []byte("hello")

_, err = conn.Write(buf)
assert.Nil(t, err)

// Last, let's write a new value
newDelta := &Delta{
Type: GAUGE,
Method: "set",
Name: "tree_width",
Help: "the width in meters of the tree",
Value: 2.5,
}
buf, _ = json.Marshal(newDelta)

_, err = conn.Write(buf)
assert.Nil(t, err)

newMetrics := fetchMetrics(t)
newResult, err := readMetric(newMetrics, newDelta.Name, newDelta.Labels)
if assert.Nil(t, err) {
assert.Equal(t, newDelta.Value, newResult)
}
}

0 comments on commit aaa49b6

Please sign in to comment.