Skip to content

Commit

Permalink
Feature:
Browse files Browse the repository at this point in the history
* [Output format of response body is now defined via output_format parameter](#8). Default is 'string'

Bugfixes
* [Missing es2x template](#13)
* [Correct parsing of large numbers in JSON output](#12)
  • Loading branch information
christiangalsterer committed Dec 18, 2016
1 parent 2cd049d commit 395bbdf
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 19 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 33 additions & 11 deletions beater/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
DefaultCron string = "@every 1m"
DefaultTimeout time.Duration = 60 * time.Second
DefaultDocumentType string = "httpbeat"
DefaultOutputFormat string = "string"
)

type HttpbeatConfig struct {
Expand All @@ -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"`
}

Expand Down
14 changes: 13 additions & 1 deletion docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions etc/httpbeat.full..yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
18 changes: 12 additions & 6 deletions etc/httpbeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 =====================================

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
)

var version = "2.0.0"
var version = "3.0.0-SNAPSHOT"
var name = "httpbeat"

func main() {
Expand Down

0 comments on commit 395bbdf

Please sign in to comment.