-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix #190] Add httpjson tags support #275
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ type Service struct { | |
Name string | ||
Servers []string | ||
Method string | ||
Tags []string | ||
Parameters map[string]string | ||
} | ||
|
||
|
@@ -61,6 +62,12 @@ var sampleConfig = ` | |
# HTTP method to use (case-sensitive) | ||
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) | ||
[httpjson.services.parameters] | ||
event_type = "cpu_spike" | ||
|
@@ -126,7 +133,7 @@ func (h *HttpJson) gatherServer(acc plugins.Accumulator, service Service, server | |
return err | ||
} | ||
|
||
var jsonOut interface{} | ||
var jsonOut map[string]interface{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this break the entire plugin if there is a non-string key in the top-level of the JSON? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, response can contain JSON array. We should have a test case for this) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this plugin wasn't aware of array JSON. I think this should go to another PR (along with support for string values, do you know why the plugin supports only floats, btw?). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @palkan supporting strings could become very complicated, there tends to be a lot of metadata in JSON that users probably don't want. Also storing strings as InfluxDB values has bad compression and isn't very useful. |
||
if err = json.Unmarshal([]byte(resp), &jsonOut); err != nil { | ||
return errors.New("Error decoding JSON response") | ||
} | ||
|
@@ -135,6 +142,14 @@ func (h *HttpJson) gatherServer(acc plugins.Accumulator, service Service, server | |
"server": serverURL, | ||
} | ||
|
||
for _, tag := range service.Tags { | ||
switch v := jsonOut[tag].(type) { | ||
case string: | ||
tags[tag] = v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation, should all be tabs in Go |
||
} | ||
delete(jsonOut, tag) | ||
} | ||
|
||
processResponse(acc, service.Name, tags, jsonOut) | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment this by default