Skip to content

Commit 395bbdf

Browse files
Feature:
* [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)
1 parent 2cd049d commit 395bbdf

File tree

7 files changed

+77
-19
lines changed

7 files changed

+77
-19
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ This will fetch and create all images required for the build process. The hole p
117117

118118
# Releases
119119

120+
3.0.0 (2016-12-XX)
121+
122+
Feature release containing the following changes:
123+
* [Output format of response body is now defined via output_format parameter](https://github.com/christiangalsterer/httpbeat/issues/8). Default is 'string'
124+
125+
Bugfix release containing the following changes:
126+
* [Missing es2x template](https://github.com/christiangalsterer/httpbeat/issues/13)
127+
* [Correct parsing of large numbers in JSON output](https://github.com/christiangalsterer/httpbeat/issues/12)
128+
120129
2.0.0 (2016-11-26)
121130

122131
Feature release containing the following changes:

beater/poller.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ func (p *Poller) runOneTime() error {
7474
return fmt.Errorf("Unsupported HTTP method %g", method)
7575
}
7676

77+
outputFormat := p.config.OutputFormat
78+
79+
switch outputFormat {
80+
case "":
81+
outputFormat = config.DefaultOutputFormat
82+
case "string":
83+
case "json":
84+
break
85+
default:
86+
return fmt.Errorf("Unsupported output format %g", outputFormat)
87+
}
88+
7789
// set timeout
7890
if p.config.Timeout != nil {
7991
p.request.Timeout(time.Duration(*p.config.Timeout) * time.Second)
@@ -137,21 +149,31 @@ func (p *Poller) runOneTime() error {
137149
}
138150

139151
var jsonBody map[string]interface{}
140-
if json.Unmarshal([]byte(body), &jsonBody) != nil {
141-
jsonBody = nil
142-
} else {
143-
if p.config.JsonDotMode == "unflatten" {
144-
jsonBody = unflat(jsonBody).(map[string]interface{})
145-
} else if p.config.JsonDotMode == "replace" {
146-
jsonBody = replaceDots(jsonBody).(map[string]interface{})
147-
}
148-
}
149152

150153
responseEvent := Response{
151154
StatusCode: resp.StatusCode,
152155
Headers: p.GetResponseHeader(resp),
153-
Body: body,
154-
JsonBody: jsonBody,
156+
}
157+
158+
if outputFormat == "string" {
159+
responseEvent.Body = body;
160+
} else {
161+
if outputFormat == "json" {
162+
decoder := json.NewDecoder(strings.NewReader(body))
163+
decoder.UseNumber()
164+
errs := decoder.Decode(&jsonBody)
165+
if errs != nil {
166+
jsonBody = nil
167+
logp.Err("An error occurred while marshalling response to JSON: %w", errs)
168+
} else {
169+
if p.config.JsonDotMode == "unflatten" {
170+
jsonBody = unflat(jsonBody).(map[string]interface{})
171+
} else if p.config.JsonDotMode == "replace" {
172+
jsonBody = replaceDots(jsonBody).(map[string]interface{})
173+
}
174+
}
175+
responseEvent.JsonBody = jsonBody;
176+
}
155177
}
156178

157179
event := HttpEvent{

config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
DefaultCron string = "@every 1m"
1111
DefaultTimeout time.Duration = 60 * time.Second
1212
DefaultDocumentType string = "httpbeat"
13+
DefaultOutputFormat string = "string"
1314
)
1415

1516
type HttpbeatConfig struct {
@@ -28,6 +29,7 @@ type UrlConfig struct {
2829
DocumentType string `config:"document_type"`
2930
Fields map[string]string `config:"fields"`
3031
SSL *outputs.TLSConfig
32+
OutputFormat string `config:"output_format"`
3133
JsonDotMode string `config:"json_dot_mode"`
3234
}
3335

docs/configuration.asciidoc

+13-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ httpbeat:
9595
9696
-------------------------------------------------------------------------------------
9797

98-
A complete example can be found in link:../etc/httpbeat.yml[etc/httpbeat.yml]
98+
A complete example can be found in link:../etc/httpbeat.full.yml[etc/httpbeat.full.yml]
9999

100100
==== Options
101101

@@ -206,6 +206,18 @@ document. The default value is `httpbeat`.
206206
document_type: httpbeat
207207
-------------------------------------------------------------------------------------
208208

209+
===== output_format
210+
Optional output format for the response body.
211+
Possible options are:
212+
* string
213+
* json
214+
Default output format is 'string'
215+
216+
[source,yaml]
217+
-------------------------------------------------------------------------------------
218+
output_format: string
219+
-------------------------------------------------------------------------------------
220+
209221
===== json_dot_mode
210222

211223
Optional convertion of dots in keys in JSON response body. By default is off. Possible options are:

etc/httpbeat.full..yml

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ httpbeat:
7676
# in. Default: httpbeat
7777
document_type:
7878

79+
# Optional output format for the response body.
80+
# Possible options are:
81+
# * string
82+
# * json
83+
# Default output format is 'string'
84+
output_format: json
85+
7986
# Optional additional headers to send to the endpoint
8087
headers:
8188

etc/httpbeat.yml

+12-6
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,22 @@ httpbeat:
3535
# in. Default: httpbeat
3636
document_type: jolokia
3737

38+
# Optional output format for the response body.
39+
# Possible options are:
40+
# * string
41+
# * json
42+
# Default output format is 'string'
43+
output_format: json
44+
3845
# Optional additional headers to send to the endpoint
3946
headers:
4047
Accept: application/json
41-
Foo: bar
48+
Number: 121221333347454675
49+
4250
# Optional additional fields. These field can be freely picked
4351
# to add additional information
44-
4552
fields:
46-
host: test
53+
test: 1
4754
-
4855
# Optional cron expression, defines when to poll the URL endpoint.
4956
# Default is every 1 minute.
@@ -80,12 +87,11 @@ httpbeat:
8087
# Optional additional headers to send to the endpoint
8188
headers:
8289
Accept: application/json
83-
Foo: bar
90+
8491
# Optional additional fields. These field can be freely picked
8592
# to add additional information
86-
8793
fields:
88-
host: test2
94+
test: 2
8995

9096
#================================ Outputs =====================================
9197

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77
)
88

9-
var version = "2.0.0"
9+
var version = "3.0.0-SNAPSHOT"
1010
var name = "httpbeat"
1111

1212
func main() {

0 commit comments

Comments
 (0)