Skip to content

Commit

Permalink
Reduce HTTPJSON metrics allocations (#36282)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-gr authored Aug 21, 2023
1 parent eca0a0b commit 25514e7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Add setup option `--force-enable-module-filesets`, that will act as if all filesets have been enabled in a module during setup. {issue}30915[30915] {pull}99999[99999]
- Add setup option `--force-enable-module-filesets`, that will act as if all filesets have been enabled in a module during setup. {issue}30915[30915] {pull}36286[36286]
- [Azure] Add input metrics to the azure-eventhub input. {pull}35739[35739]
- Reduce HTTPJSON metrics allocations. {pull}36282[36282]

*Auditbeat*

Expand Down
14 changes: 8 additions & 6 deletions x-pack/filebeat/input/httpjson/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func TestMetrics(t *testing.T) {
},
baseConfig: map[string]interface{}{
"interval": time.Millisecond,
"request.method": http.MethodGet,
"request.method": http.MethodPost,
"request.body": map[string]interface{}{
"field": "value",
},
"response.split": map[string]interface{}{
"target": "body.items",
"transforms": []interface{}{
Expand Down Expand Up @@ -70,21 +73,20 @@ func TestMetrics(t *testing.T) {
},
assertMetrics: func(reg *monitoring.Registry) error {
checkHasValue := func(v interface{}) bool {
var c int64
switch t := v.(type) {
case int64:
c = t
return t > 0
case map[string]interface{}:
h := t["histogram"].(map[string]interface{})
c = h["count"].(int64)
return h["count"].(int64) > 0 && h["max"].(int64) > 0
}
return c > 0
return false
}

snapshot := monitoring.CollectStructSnapshot(reg, monitoring.Full, true)

for _, m := range []string{
"http_request_body_bytes", "http_request_get_total",
"http_request_body_bytes", "http_request_post_total",
"http_request_total", "http_response_2xx_total",
"http_response_body_bytes", "http_response_total",
"http_round_trip_time", "httpjson_interval_execution_time",
Expand Down
23 changes: 6 additions & 17 deletions x-pack/filebeat/input/internal/httplog/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,9 @@ func (rt *MetricsRoundTripper) RoundTrip(req *http.Request) (*http.Response, err

rt.monitorByMethod(req.Method)

var (
body []byte
err error
)

req.Body, body, err = copyBody(req.Body)
if err != nil {
rt.metrics.reqErrs.Add(1)
} else {
rt.metrics.reqsAccSize.Add(uint64(len(body)))
rt.metrics.reqsSize.Update(int64(len(body)))
if req.ContentLength >= 0 {
rt.metrics.reqsAccSize.Add(uint64(req.ContentLength))
rt.metrics.reqsSize.Update(req.ContentLength)
}

reqStart := time.Now()
Expand All @@ -355,12 +347,9 @@ func (rt *MetricsRoundTripper) RoundTrip(req *http.Request) (*http.Response, err

rt.monitorByStatusCode(resp.StatusCode)

resp.Body, body, err = copyBody(resp.Body)
if err != nil {
rt.metrics.respErrs.Add(1)
} else {
rt.metrics.respsAccSize.Add(uint64(len(body)))
rt.metrics.respsSize.Update(int64(len(body)))
if resp.ContentLength >= 0 {
rt.metrics.respsAccSize.Add(uint64(resp.ContentLength))
rt.metrics.respsSize.Update(resp.ContentLength)
}

return resp, err
Expand Down

0 comments on commit 25514e7

Please sign in to comment.