fix for #126, nginx plugin not catching net.SplitHostPort error

This commit is contained in:
Cameron Sparr
2015-08-20 11:26:49 -06:00
parent ecfdafab06
commit 532d953b5a
3 changed files with 36 additions and 12 deletions

View File

@@ -20,7 +20,7 @@ type Nginx struct {
var sampleConfig = `
# An array of Nginx stub_status URI to gather stats.
urls = ["localhost/status"]`
urls = ["http://localhost/status"]`
func (n *Nginx) SampleConfig() string {
return sampleConfig
@@ -125,8 +125,8 @@ func (n *Nginx) gatherUrl(addr *url.URL, acc plugins.Accumulator) error {
return err
}
host, _, _ := net.SplitHostPort(addr.Host)
tags := map[string]string{"server": host}
tags := getTags(addr)
acc.Add("active", active, tags)
acc.Add("accepts", accepts, tags)
acc.Add("handled", handled, tags)
@@ -138,6 +138,18 @@ func (n *Nginx) gatherUrl(addr *url.URL, acc plugins.Accumulator) error {
return nil
}
// Get tag(s) for the nginx plugin
func getTags(addr *url.URL) map[string]string {
h := addr.Host
var htag string
if host, _, err := net.SplitHostPort(h); err == nil {
htag = host
} else {
htag = h
}
return map[string]string{"server": htag}
}
func init() {
plugins.Add("nginx", func() plugins.Plugin {
return &Nginx{}

View File

@@ -14,12 +14,23 @@ import (
)
const sampleResponse = `
Active connections: 585
Active connections: 585
server accepts handled requests
85340 85340 35085
Reading: 4 Writing: 135 Waiting: 446
85340 85340 35085
Reading: 4 Writing: 135 Waiting: 446
`
// Verify that nginx tags are properly parsed based on the server
func TestNginxTags(t *testing.T) {
urls := []string{"http://localhost/endpoint", "http://localhost:80/endpoint"}
var addr *url.URL
for _, url1 := range urls {
addr, _ = url.Parse(url1)
tagMap := getTags(addr)
assert.Contains(t, tagMap["server"], "localhost")
}
}
func TestNginxGeneratesMetrics(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var rsp string