-
Notifications
You must be signed in to change notification settings - Fork 480
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
Limit log and trace telemetry #470
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e554443
Limit log and trace telemetry
JamesNK 914fa47
Fix
JamesNK 671f161
Fix
JamesNK 20c752e
Clean up and read configuration
JamesNK a306f20
Fix
JamesNK 583b410
Update
JamesNK 5d1c1c5
TODO
JamesNK 591f88a
Comment
JamesNK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Aspire.Dashboard.Otlp.Storage; | ||
|
||
/// <summary> | ||
/// The circular buffer starts with an empty list and grows to a maximum size. | ||
/// When the buffer is full, adding or inserting a new item removes the first item in the buffer. | ||
/// </summary> | ||
internal sealed class CircularBuffer<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable | ||
{ | ||
// Internal for testing. | ||
internal readonly List<T> _buffer; | ||
internal int _start; | ||
internal int _end; | ||
|
||
public CircularBuffer(int capacity) | ||
{ | ||
if (capacity < 1) | ||
{ | ||
throw new ArgumentException("Circular buffer must have a capacity greater than 0.", nameof(capacity)); | ||
} | ||
|
||
_buffer = new List<T>(); | ||
Capacity = capacity; | ||
_start = 0; | ||
_end = 0; | ||
} | ||
|
||
public int Capacity { get; } | ||
|
||
public bool IsFull => Count == Capacity; | ||
|
||
public bool IsEmpty => Count == 0; | ||
|
||
public int Count => _buffer.Count; | ||
|
||
public bool IsReadOnly { get; } | ||
|
||
public bool IsFixedSize { get; } = true; | ||
|
||
public object SyncRoot { get; } = new object(); | ||
|
||
public bool IsSynchronized { get; } | ||
|
||
public int IndexOf(T item) | ||
{ | ||
for (var index = 0; index < Count; ++index) | ||
{ | ||
if (Equals(this[index], item)) | ||
{ | ||
return index; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public void Insert(int index, T item) | ||
{ | ||
// TODO: There are a lot of branches in this method. Look into simplifying it. | ||
if (index == Count) | ||
{ | ||
Add(item); | ||
return; | ||
} | ||
|
||
ValidateIndexInRange(index); | ||
|
||
if (IsFull) | ||
{ | ||
if (index == 0) | ||
{ | ||
// When full, the item inserted at 0 is always the "last" in the buffer and is removed. | ||
return; | ||
} | ||
|
||
var internalIndex = InternalIndex(index); | ||
|
||
var data = CollectionsMarshal.AsSpan(_buffer); | ||
|
||
// Shift data to make remove for insert. | ||
if (internalIndex == 0) | ||
{ | ||
data.Slice(0, _end).CopyTo(data.Slice(1)); | ||
} | ||
else if (internalIndex > _end) | ||
{ | ||
// Data is shifted forward so save the last item to copy to the front. | ||
var overflowItem = data[data.Length - 1]; | ||
|
||
var shiftLength = data.Length - internalIndex - 1; | ||
data.Slice(internalIndex, shiftLength).CopyTo(data.Slice(internalIndex + 1)); | ||
if (shiftLength > 0 || internalIndex == _buffer.Count - 1) | ||
{ | ||
data.Slice(0, _end).CopyTo(data.Slice(1)); | ||
} | ||
data[0] = overflowItem; | ||
} | ||
else | ||
{ | ||
data.Slice(internalIndex, data.Length - internalIndex - 1).CopyTo(data.Slice(internalIndex + 1)); | ||
} | ||
|
||
// Set the actual item. | ||
data[internalIndex] = item; | ||
|
||
Increment(ref _end); | ||
_start = _end; | ||
} | ||
else | ||
{ | ||
var internalIndex = index + _start; | ||
if (internalIndex > _buffer.Count) | ||
{ | ||
internalIndex = internalIndex % _buffer.Count; | ||
} | ||
|
||
_buffer.Insert(internalIndex, item); | ||
if (internalIndex < _end) | ||
{ | ||
Increment(ref _end); | ||
if (_end != _buffer.Count) | ||
{ | ||
_start = _end; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void RemoveAt(int index) | ||
{ | ||
ValidateIndexInRange(index); | ||
|
||
var internalIndex = InternalIndex(index); | ||
_buffer.RemoveAt(internalIndex); | ||
if (internalIndex < _end) | ||
{ | ||
Decrement(ref _end); | ||
_start = _end; | ||
} | ||
} | ||
|
||
private void ValidateIndexInRange(int index) | ||
{ | ||
if (index >= Count) | ||
{ | ||
throw new InvalidOperationException($"Cannot access index {index}. Buffer size is {Count}"); | ||
} | ||
} | ||
|
||
public bool Remove(T item) => throw new NotImplementedException(); | ||
|
||
public T this[int index] | ||
{ | ||
get | ||
{ | ||
ValidateIndexInRange(index); | ||
return _buffer[InternalIndex(index)]; | ||
} | ||
set | ||
{ | ||
ValidateIndexInRange(index); | ||
_buffer[InternalIndex(index)] = value; | ||
} | ||
} | ||
|
||
public void Add(T item) | ||
{ | ||
if (IsFull) | ||
{ | ||
_buffer[_end] = item; | ||
Increment(ref _end); | ||
_start = _end; | ||
} | ||
else | ||
{ | ||
_buffer.Insert(_end, item); | ||
Increment(ref _end); | ||
if (_end != _buffer.Count) | ||
{ | ||
_start = _end; | ||
} | ||
} | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_start = 0; | ||
_end = 0; | ||
_buffer.Clear(); | ||
} | ||
|
||
public bool Contains(T item) => IndexOf(item) != -1; | ||
|
||
public void CopyTo(T[] array, int arrayIndex) | ||
{ | ||
if (array.Length - arrayIndex < Count) | ||
{ | ||
throw new ArgumentException("Array does not contain enough space for items"); | ||
} | ||
|
||
for (var index = 0; index < Count; ++index) | ||
{ | ||
array[index + arrayIndex] = this[index]; | ||
} | ||
} | ||
|
||
public T[] ToArray() | ||
{ | ||
if (IsEmpty) | ||
{ | ||
return Array.Empty<T>(); | ||
} | ||
|
||
var array = new T[Count]; | ||
for (var index = 0; index < Count; ++index) | ||
{ | ||
array[index] = this[index]; | ||
} | ||
|
||
return array; | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
for (var i = 0; i < Count; ++i) | ||
{ | ||
yield return this[i]; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
private int InternalIndex(int index) | ||
{ | ||
return (_start + index) % _buffer.Count; | ||
} | ||
|
||
private void Increment(ref int index) | ||
{ | ||
if (++index < Capacity) | ||
{ | ||
return; | ||
} | ||
|
||
index = 0; | ||
} | ||
|
||
private void Decrement(ref int index) | ||
{ | ||
if (index <= 0) | ||
{ | ||
index = Capacity - 1; | ||
} | ||
|
||
--index; | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@stephentoub Can/Should we use ConcurrentQueueSegment?
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.
Items are inserted with an order and can arrive out of order, so inserting at a position is required.
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.
Got it.