-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add event watcher prometheus metrics and a new tctl top tab to visualize them
- Loading branch information
1 parent
b58ad48
commit fb0ab2b
Showing
6 changed files
with
484 additions
and
77 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2021 Gravitational, Inc. | ||
Licensed 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 utils | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/gravitational/trace" | ||
) | ||
|
||
// CircularBuffer implements an in-memory circular buffer of predefined size | ||
type CircularBuffer struct { | ||
sync.Mutex | ||
buf []float64 | ||
start int | ||
end int | ||
size int | ||
} | ||
|
||
// NewCircularBuffer returns a new instance of a circular buffer that will hold | ||
// size elements before it rotates | ||
func NewCircularBuffer(size int) (*CircularBuffer, error) { | ||
if size <= 0 { | ||
return nil, trace.BadParameter("circular buffer size should be > 0") | ||
} | ||
buf := &CircularBuffer{ | ||
buf: make([]float64, size), | ||
start: -1, | ||
end: -1, | ||
size: 0, | ||
} | ||
return buf, nil | ||
} | ||
|
||
// Data returns the most recent n elements in the correct order | ||
func (t *CircularBuffer) Data(n int) []float64 { | ||
t.Lock() | ||
defer t.Unlock() | ||
|
||
if n <= 0 || t.size == 0 { | ||
return nil | ||
} | ||
|
||
// skip first N items so that the most recent are always provided | ||
start := t.start | ||
if n < t.size { | ||
start = (t.start + (t.size - n)) % len(t.buf) | ||
} | ||
|
||
if start <= t.end { | ||
return t.buf[start : t.end+1] | ||
} | ||
|
||
return append(t.buf[start:], t.buf[:t.end+1]...) | ||
} | ||
|
||
// Add pushes a new item onto the buffer | ||
func (t *CircularBuffer) Add(d float64) { | ||
t.Lock() | ||
defer t.Unlock() | ||
|
||
if t.size == 0 { | ||
t.start = 0 | ||
t.end = 0 | ||
t.size = 1 | ||
} else if t.size < len(t.buf) { | ||
t.end++ | ||
t.size++ | ||
} else { | ||
t.end = t.start | ||
t.start = (t.start + 1) % len(t.buf) | ||
} | ||
|
||
t.buf[t.end] = d | ||
} |
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,66 @@ | ||
/* | ||
Copyright 2021 Gravitational, Inc. | ||
Licensed 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 utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewCircularBuffer(t *testing.T) { | ||
buff, err := NewCircularBuffer(-1) | ||
require.Error(t, err) | ||
require.Nil(t, buff) | ||
|
||
buff, err = NewCircularBuffer(5) | ||
require.NoError(t, err) | ||
require.NotNil(t, buff) | ||
require.Len(t, buff.buf, 5) | ||
} | ||
|
||
func TestCircularBuffer_Data(t *testing.T) { | ||
buff, err := NewCircularBuffer(5) | ||
require.NoError(t, err) | ||
|
||
expectData := func(expected []float64) { | ||
for i := 0; i < 15; i++ { | ||
e := expected | ||
if i <= len(expected) { | ||
e = expected[len(expected)-i:] | ||
} | ||
require.Empty(t, cmp.Diff(e, buff.Data(i), cmpopts.EquateEmpty()), "i = %v", i) | ||
} | ||
} | ||
|
||
expectData(nil) | ||
|
||
buff.Add(1) | ||
expectData([]float64{1}) | ||
|
||
buff.Add(2) | ||
buff.Add(3) | ||
buff.Add(4) | ||
expectData([]float64{1, 2, 3, 4}) | ||
|
||
buff.Add(5) | ||
buff.Add(6) | ||
buff.Add(7) | ||
expectData([]float64{3, 4, 5, 6, 7}) | ||
} |
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
Oops, something went wrong.