-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ring-buffer reporter to libbeat (#28750)
* Add a ring-buffer reporter to libbeat Add a ring buffer reporter that when enabled will store configured namespaces in a buffer to allow operators to view recent metrics history. Defaults are to gather the stats namespace every 10s for 10m. Must be explicitly enabled, along with monitoring, and the HTTP endpoint. The buffer endpoint is intended to be used for diagnostics reporting. * Add basic benchmarks * Review feddback
- Loading branch information
1 parent
1c68693
commit 6769d47
Showing
9 changed files
with
519 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
// the buffer should be thread-safe. | ||
type ringBuffer struct { | ||
mu sync.Mutex | ||
entries []interface{} | ||
i int | ||
full bool | ||
} | ||
|
||
// 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]...) | ||
} |
Oops, something went wrong.