Skip to content

Commit

Permalink
Support all NSQ versions (#6)
Browse files Browse the repository at this point in the history
* support nsq < 1.x & nsq 1.x

* test both nsq 0.3.8 & 1.0.0

* start nsqlookupd & nsqd in installation scripts
  • Loading branch information
adamweiner authored Oct 26, 2017
1 parent e43a1c9 commit 8b0f22a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
25 changes: 12 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ notifications:
go:
- 1.9.x
- master
before_script:
- cd /tmp
- curl -L https://s3.amazonaws.com/bitly-downloads/nsq/nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz -o nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz
- tar -zxvf nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz
- sudo cp -R nsq-1.0.0-compat.linux-amd64.go1.8/bin/. /usr/local/bin
- nsqlookupd &
- nsqd -lookupd-tcp-address localhost:4160 &
- sleep 1
- echo 'test' | to_nsq -nsqd-tcp-address localhost:4150 -topic test -rate 2
- cd $HOME/gopath/src/github.com/adamweiner/nsqd-prometheus-exporter
script:
# build nsqd-prometheus-exporter
- make
# test with NSQ 0.3.8
- ./test/install_nsq_0.3.8.sh
- ./nsqd-prometheus-exporter &
- sleep 1
- ./test/test_depth_metric.sh
# clean up
- sudo killall -v nsqlookupd nsqd nsqd-prometheus-exporter
- sudo rm -rfv /usr/local/bin/*nsq* /tmp/*{nsq,diskqueue}*
# test with NSQ 1.0.0
- ./test/install_nsq_1.0.0.sh
- ./nsqd-prometheus-exporter &
- sleep 1
- curl -s localhost:30000/metrics > metrics.out
- cat metrics.out
- cat metrics.out | grep 'nsqd_depth{channel="",paused="false",topic="test",type="topic"} 1'
- ./test/test_depth_metric.sh
24 changes: 23 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ package main

import (
"encoding/json"
"io/ioutil"
"net/http"
)

type statsResponse struct {
StatusCode int `json:"status_code"`
StatusText string `json:"status_text"`
Data stats `json:"data"`
}

type stats struct {
Version string `json:"version"`
Health string `json:"health"`
Expand Down Expand Up @@ -90,9 +97,24 @@ func getNsqdStats(nsqdURL string) (*stats, error) {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

// nsq <= 0.3.8 uses statsResponse
// if a status code != 0 exists, assume the unmarshal is successful
var sr statsResponse
if err = json.Unmarshal(body, &sr); err != nil {
return nil, err
}
if sr.StatusCode != 0 {
return &sr.Data, nil
}

// nsq 1.x drops statsResponse for just stats
var s stats
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
if err = json.Unmarshal(body, &s); err != nil {
return nil, err
}
return &s, nil
Expand Down
16 changes: 16 additions & 0 deletions test/install_nsq_0.3.8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -ex -o pipefail

# Install NSQ 0.3.8
cd /tmp
curl -L https://s3.amazonaws.com/bitly-downloads/nsq/nsq-0.3.8.linux-amd64.go1.6.2.tar.gz -o nsq-0.3.8.linux-amd64.go1.6.2.tar.gz
tar -zxvf nsq-0.3.8.linux-amd64.go1.6.2.tar.gz
sudo cp -R nsq-0.3.8.linux-amd64.go1.6.2/bin/. /usr/local/bin

# Start nsqlookupd & nsqd
nsqlookupd &
nsqd -lookupd-tcp-address localhost:4160 &

# Emit test message
sleep 1
echo 'test' | to_nsq -nsqd-tcp-address localhost:4150 -topic test
16 changes: 16 additions & 0 deletions test/install_nsq_1.0.0.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -ex -o pipefail

# Install NSQ 1.0.0
cd /tmp
curl -L https://s3.amazonaws.com/bitly-downloads/nsq/nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz -o nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz
tar -zxvf nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz
sudo cp -R nsq-1.0.0-compat.linux-amd64.go1.8/bin/. /usr/local/bin

# Start nsqlookupd & nsqd
nsqlookupd &
nsqd -lookupd-tcp-address localhost:4160 &

# Emit test message
sleep 1
echo 'test' | to_nsq -nsqd-tcp-address localhost:4150 -topic test -rate 2
7 changes: 7 additions & 0 deletions test/test_depth_metric.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -ex -o pipefail

# Test for expected metric
curl -s localhost:30000/metrics > metrics.out
cat metrics.out
cat metrics.out | grep 'nsqd_depth{channel="",paused="false",topic="test",type="topic"} 1'

0 comments on commit 8b0f22a

Please sign in to comment.