From 395bbdf81fb70c9d4cd8050dcd1b6acbb38b8d12 Mon Sep 17 00:00:00 2001 From: Christian Galsterer Date: Sun, 18 Dec 2016 09:35:46 +0100 Subject: [PATCH] Feature: * [Output format of response body is now defined via output_format parameter](https://github.com/christiangalsterer/httpbeat/issues/8). Default is 'string' Bugfixes * [Missing es2x template](https://github.com/christiangalsterer/httpbeat/issues/13) * [Correct parsing of large numbers in JSON output](https://github.com/christiangalsterer/httpbeat/issues/12) --- README.md | 9 ++++++++ beater/poller.go | 44 +++++++++++++++++++++++++++---------- config/config.go | 2 ++ docs/configuration.asciidoc | 14 +++++++++++- etc/httpbeat.full..yml | 7 ++++++ etc/httpbeat.yml | 18 ++++++++++----- main.go | 2 +- 7 files changed, 77 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e012b89..435743d 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,15 @@ This will fetch and create all images required for the build process. The hole p # Releases +3.0.0 (2016-12-XX) + +Feature release containing the following changes: +* [Output format of response body is now defined via output_format parameter](https://github.com/christiangalsterer/httpbeat/issues/8). Default is 'string' + +Bugfix release containing the following changes: +* [Missing es2x template](https://github.com/christiangalsterer/httpbeat/issues/13) +* [Correct parsing of large numbers in JSON output](https://github.com/christiangalsterer/httpbeat/issues/12) + 2.0.0 (2016-11-26) Feature release containing the following changes: diff --git a/beater/poller.go b/beater/poller.go index 2f226ce..8cf28e9 100644 --- a/beater/poller.go +++ b/beater/poller.go @@ -74,6 +74,18 @@ func (p *Poller) runOneTime() error { return fmt.Errorf("Unsupported HTTP method %g", method) } + outputFormat := p.config.OutputFormat + + switch outputFormat { + case "": + outputFormat = config.DefaultOutputFormat + case "string": + case "json": + break + default: + return fmt.Errorf("Unsupported output format %g", outputFormat) + } + // set timeout if p.config.Timeout != nil { p.request.Timeout(time.Duration(*p.config.Timeout) * time.Second) @@ -137,21 +149,31 @@ func (p *Poller) runOneTime() error { } var jsonBody map[string]interface{} - if json.Unmarshal([]byte(body), &jsonBody) != nil { - jsonBody = nil - } else { - if p.config.JsonDotMode == "unflatten" { - jsonBody = unflat(jsonBody).(map[string]interface{}) - } else if p.config.JsonDotMode == "replace" { - jsonBody = replaceDots(jsonBody).(map[string]interface{}) - } - } responseEvent := Response{ StatusCode: resp.StatusCode, Headers: p.GetResponseHeader(resp), - Body: body, - JsonBody: jsonBody, + } + + if outputFormat == "string" { + responseEvent.Body = body; + } else { + if outputFormat == "json" { + decoder := json.NewDecoder(strings.NewReader(body)) + decoder.UseNumber() + errs := decoder.Decode(&jsonBody) + if errs != nil { + jsonBody = nil + logp.Err("An error occurred while marshalling response to JSON: %w", errs) + } else { + if p.config.JsonDotMode == "unflatten" { + jsonBody = unflat(jsonBody).(map[string]interface{}) + } else if p.config.JsonDotMode == "replace" { + jsonBody = replaceDots(jsonBody).(map[string]interface{}) + } + } + responseEvent.JsonBody = jsonBody; + } } event := HttpEvent{ diff --git a/config/config.go b/config/config.go index a691586..ddd315a 100644 --- a/config/config.go +++ b/config/config.go @@ -10,6 +10,7 @@ const ( DefaultCron string = "@every 1m" DefaultTimeout time.Duration = 60 * time.Second DefaultDocumentType string = "httpbeat" + DefaultOutputFormat string = "string" ) type HttpbeatConfig struct { @@ -28,6 +29,7 @@ type UrlConfig struct { DocumentType string `config:"document_type"` Fields map[string]string `config:"fields"` SSL *outputs.TLSConfig + OutputFormat string `config:"output_format"` JsonDotMode string `config:"json_dot_mode"` } diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index 2c14451..2831b4a 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -95,7 +95,7 @@ httpbeat: ------------------------------------------------------------------------------------- -A complete example can be found in link:../etc/httpbeat.yml[etc/httpbeat.yml] +A complete example can be found in link:../etc/httpbeat.full.yml[etc/httpbeat.full.yml] ==== Options @@ -206,6 +206,18 @@ document. The default value is `httpbeat`. document_type: httpbeat ------------------------------------------------------------------------------------- +===== output_format +Optional output format for the response body. +Possible options are: + * string + * json +Default output format is 'string' + +[source,yaml] +------------------------------------------------------------------------------------- +output_format: string +------------------------------------------------------------------------------------- + ===== json_dot_mode Optional convertion of dots in keys in JSON response body. By default is off. Possible options are: diff --git a/etc/httpbeat.full..yml b/etc/httpbeat.full..yml index 35860ef..e19d2ea 100644 --- a/etc/httpbeat.full..yml +++ b/etc/httpbeat.full..yml @@ -76,6 +76,13 @@ httpbeat: # in. Default: httpbeat document_type: + # Optional output format for the response body. + # Possible options are: + # * string + # * json + # Default output format is 'string' + output_format: json + # Optional additional headers to send to the endpoint headers: diff --git a/etc/httpbeat.yml b/etc/httpbeat.yml index ad4d906..46ae91e 100644 --- a/etc/httpbeat.yml +++ b/etc/httpbeat.yml @@ -35,15 +35,22 @@ httpbeat: # in. Default: httpbeat document_type: jolokia + # Optional output format for the response body. + # Possible options are: + # * string + # * json + # Default output format is 'string' + output_format: json + # Optional additional headers to send to the endpoint headers: Accept: application/json - Foo: bar + Number: 121221333347454675 + # Optional additional fields. These field can be freely picked # to add additional information - fields: - host: test + test: 1 - # Optional cron expression, defines when to poll the URL endpoint. # Default is every 1 minute. @@ -80,12 +87,11 @@ httpbeat: # Optional additional headers to send to the endpoint headers: Accept: application/json - Foo: bar + # Optional additional fields. These field can be freely picked # to add additional information - fields: - host: test2 + test: 2 #================================ Outputs ===================================== diff --git a/main.go b/main.go index 6f3ffb7..d9d9dc1 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( "os" ) -var version = "2.0.0" +var version = "3.0.0-SNAPSHOT" var name = "httpbeat" func main() {