-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add a ring-buffer reporter to libbeat #28750
Changes from 3 commits
2a7f38e
a3ec1d8
6006ffc
6c8797f
4d74030
b0947cc
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 |
---|---|---|
|
@@ -184,3 +184,39 @@ func simpleMux() *http.ServeMux { | |
}) | ||
return mux | ||
} | ||
|
||
func TestAttachHandler(t *testing.T) { | ||
url := "http://localhost:0" | ||
|
||
cfg := common.MustNewConfigFrom(map[string]interface{}{ | ||
"host": url, | ||
}) | ||
|
||
s, err := New(nil, simpleMux(), cfg) | ||
require.NoError(t, err) | ||
go s.Start() | ||
defer s.Stop() | ||
|
||
h := &testHandler{} | ||
|
||
err = s.AttachHandler("/test", h) | ||
require.NoError(t, err) | ||
|
||
r, err := http.Get("http://" + s.l.Addr().String() + "/test") | ||
require.NoError(t, err) | ||
defer r.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
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. s/ioutil/io/ |
||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "test!", string(body)) | ||
|
||
err = s.AttachHandler("/test", h) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
type testHandler struct{} | ||
|
||
func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "test!") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package monitoring | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
) | ||
|
||
func TestIsBufferEnabled(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]interface{} | ||
expect bool | ||
}{{ | ||
name: "enabled", | ||
input: map[string]interface{}{ | ||
"enabled": true, | ||
}, | ||
expect: true, | ||
}, { | ||
name: "disabled", | ||
input: map[string]interface{}{ | ||
"enabled": false, | ||
}, | ||
expect: false, | ||
}, { | ||
name: "missing", | ||
input: map[string]interface{}{ | ||
"size": 10, | ||
}, | ||
expect: false, | ||
}, { | ||
name: "nil", | ||
input: nil, | ||
expect: false, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cfg, err := common.NewConfigFrom(tt.input) | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.expect, IsBufferEnabled(cfg)) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||||||
// Licensed to Elasticsearch B.V. under one or more contributor | ||||||||
// license agreements. See the NOTICE file distributed with | ||||||||
// this work for additional information regarding copyright | ||||||||
// ownership. Elasticsearch B.V. licenses this file to you under | ||||||||
// the Apache License, Version 2.0 (the "License"); you may | ||||||||
// not use this file except in compliance with the License. | ||||||||
// You may obtain a copy of the License at | ||||||||
// | ||||||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||||||
// | ||||||||
// Unless required by applicable law or agreed to in writing, | ||||||||
// software distributed under the License is distributed on an | ||||||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||
// KIND, either express or implied. See the License for the | ||||||||
// specific language governing permissions and limitations | ||||||||
// under the License. | ||||||||
|
||||||||
package buffer | ||||||||
|
||||||||
import "sync" | ||||||||
|
||||||||
// ringBuffer is a buffer with a fixed number of items that can be tracked. | ||||||||
// | ||||||||
// we assume that the size of the buffer is greater than one. | ||||||||
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.
Suggested change
|
||||||||
// the buffer should be thread-safe. | ||||||||
type ringBuffer struct { | ||||||||
entries []interface{} | ||||||||
i int | ||||||||
full bool | ||||||||
mu sync.Mutex | ||||||||
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. Conventionally mutexes are placed above the field they protect. |
||||||||
} | ||||||||
|
||||||||
// newBuffer returns a reference to a new ringBuffer with set size. | ||||||||
func newBuffer(size int) *ringBuffer { | ||||||||
return &ringBuffer{ | ||||||||
entries: make([]interface{}, size), | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// add will add the passed entry to the buffer. | ||||||||
func (r *ringBuffer) add(entry interface{}) { | ||||||||
r.mu.Lock() | ||||||||
defer r.mu.Unlock() | ||||||||
r.entries[r.i] = entry | ||||||||
r.i = (r.i + 1) % len(r.entries) | ||||||||
if r.i == 0 { | ||||||||
r.full = true | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// getAll returns all entries in the buffer in order | ||||||||
func (r *ringBuffer) getAll() []interface{} { | ||||||||
r.mu.Lock() | ||||||||
defer r.mu.Unlock() | ||||||||
if r.i == 0 && !r.full { | ||||||||
return []interface{}{} | ||||||||
} | ||||||||
if !r.full { | ||||||||
return r.entries[:r.i] | ||||||||
} | ||||||||
if r.full && r.i == 0 { | ||||||||
return r.entries | ||||||||
} | ||||||||
return append(r.entries[r.i:], r.entries[:r.i]...) | ||||||||
} |
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.
Needs "errors" added to imports.