[Fix #190] Add httpjson tags support

This commit is contained in:
palkan 2015-10-16 17:10:08 +03:00 committed by Cameron Sparr
parent 869483617b
commit e9356c893b
2 changed files with 55 additions and 3 deletions

View File

@ -22,6 +22,7 @@ type Service struct {
Name string Name string
Servers []string Servers []string
Method string Method string
Tags []string
Parameters map[string]string Parameters map[string]string
} }
@ -61,6 +62,12 @@ var sampleConfig = `
# HTTP method to use (case-sensitive) # HTTP method to use (case-sensitive)
method = "GET" method = "GET"
# List of tag names to extract from server response
tags = [
"my_tag_1",
"my_tag_2"
]
# HTTP parameters (all values must be strings) # HTTP parameters (all values must be strings)
[httpjson.services.parameters] [httpjson.services.parameters]
event_type = "cpu_spike" event_type = "cpu_spike"
@ -126,7 +133,7 @@ func (h *HttpJson) gatherServer(acc plugins.Accumulator, service Service, server
return err return err
} }
var jsonOut interface{} var jsonOut map[string]interface{}
if err = json.Unmarshal([]byte(resp), &jsonOut); err != nil { if err = json.Unmarshal([]byte(resp), &jsonOut); err != nil {
return errors.New("Error decoding JSON response") return errors.New("Error decoding JSON response")
} }
@ -135,6 +142,14 @@ func (h *HttpJson) gatherServer(acc plugins.Accumulator, service Service, server
"server": serverURL, "server": serverURL,
} }
for _, tag := range service.Tags {
switch v := jsonOut[tag].(type) {
case string:
tags[tag] = v
}
delete(jsonOut, tag)
}
processResponse(acc, service.Name, tags, jsonOut) processResponse(acc, service.Name, tags, jsonOut)
return nil return nil
} }

View File

@ -28,6 +28,14 @@ const validJSON = `
} }
}` }`
const validJSONTags = `
{
"value": 15,
"role": "master",
"build": "123"
}`
const invalidJSON = "I don't think this is JSON" const invalidJSON = "I don't think this is JSON"
const empty = "" const empty = ""
@ -87,8 +95,8 @@ func genMockHttpJson(response string, statusCode int) *HttpJson {
}, },
Service{ Service{
Servers: []string{ Servers: []string{
"http://server1.example.com/metrics/", "http://server3.example.com/metrics/",
"http://server2.example.com/metrics/", "http://server4.example.com/metrics/",
}, },
Name: "other_webapp", Name: "other_webapp",
Method: "POST", Method: "POST",
@ -96,6 +104,10 @@ func genMockHttpJson(response string, statusCode int) *HttpJson {
"httpParam1": "12", "httpParam1": "12",
"httpParam2": "the second parameter", "httpParam2": "the second parameter",
}, },
Tags: []string{
"role",
"build",
},
}, },
}, },
} }
@ -185,3 +197,28 @@ func TestHttpJsonEmptyResponse(t *testing.T) {
assert.Equal(t, len(strings.Split(err.Error(), "\n")), 4) assert.Equal(t, len(strings.Split(err.Error(), "\n")), 4)
assert.Equal(t, 0, len(acc.Points)) assert.Equal(t, 0, len(acc.Points))
} }
// Test that the proper values are ignored or collected
func TestHttpJson200Tags(t *testing.T) {
httpjson := genMockHttpJson(validJSONTags, 200)
var acc testutil.Accumulator
err := httpjson.Gather(&acc)
require.NoError(t, err)
assert.Equal(t, 4, len(acc.Points))
for _, service := range httpjson.Services {
if service.Name == "other_webapp" {
for _, srv := range service.Servers {
require.NoError(t,
acc.ValidateTaggedValue(
fmt.Sprintf("%s_value", service.Name),
15.0,
map[string]string{"server": srv, "role": "master", "build": "123"},
),
)
}
}
}
}