Skip to content

Commit

Permalink
refactor: make buffer prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
w-h-a committed Oct 14, 2024
1 parent d905c1b commit 8b33a00
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
6 changes: 3 additions & 3 deletions telemetry/log/memory/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (l *memoryLog) Read(opts ...log.ReadOption) ([]log.Record, error) {
if options.Count > 0 {
entries = l.buffer.Get(options.Count)
} else {
entries = l.buffer.Get(l.buffer.Size)
entries = l.buffer.Get(l.buffer.Options().Size)
}

records := []log.Record{}
Expand Down Expand Up @@ -63,9 +63,9 @@ func NewLog(opts ...log.LogOption) log.Log {
}

if s, ok := GetSizeFromContext(options.Context); ok && s > 0 {
l.buffer = memoryutils.NewBuffer(s)
l.buffer = memoryutils.NewBuffer(memoryutils.BufferWithSize(s))
} else {
l.buffer = memoryutils.NewBuffer(1024)
l.buffer = memoryutils.NewBuffer()
}

return l
Expand Down
2 changes: 1 addition & 1 deletion telemetry/log/memory/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestLogger(t *testing.T) {
service := "service.namespace"

logger := NewLog(log.LogWithPrefix(service), log.LogWithLevel(log.LevelDebug), LogWithSize(size))
require.Equal(t, size, logger.(*memoryLog).buffer.Size)
require.Equal(t, size, logger.(*memoryLog).buffer.Options().Size)

log.SetLogger(logger)

Expand Down
6 changes: 3 additions & 3 deletions telemetry/trace/memory/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (t *memoryTrace) Read(opts ...trace.ReadOption) ([]*trace.Span, error) {
if options.Count > 0 {
entries = t.buffer.Get(options.Count)
} else {
entries = t.buffer.Get(t.buffer.Size)
entries = t.buffer.Get(t.buffer.Options().Size)
}

spans := []*trace.Span{}
Expand Down Expand Up @@ -95,9 +95,9 @@ func NewTrace(opts ...trace.TraceOption) trace.Trace {
}

if s, ok := GetSizeFromContext(options.Context); ok && s > 0 {
t.buffer = memoryutils.NewBuffer(s)
t.buffer = memoryutils.NewBuffer(memoryutils.BufferWithSize(s))
} else {
t.buffer = memoryutils.NewBuffer(1024)
t.buffer = memoryutils.NewBuffer()
}

return t
Expand Down
2 changes: 1 addition & 1 deletion telemetry/tracev2/memory/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (t *memoryTrace) Read(opts ...tracev2.ReadOption) ([]*tracev2.SpanData, err
if options.Count > 0 {
entries = t.buffer.Get(options.Count)
} else {
entries = t.buffer.Get(t.buffer.Size)
entries = t.buffer.Get(t.buffer.Options().Size)
}

spans := []*tracev2.SpanData{}
Expand Down
1 change: 0 additions & 1 deletion telemetry/tracev2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
const (
traceParentKey = "traceparent"
spanParentKey = "spanparent"
spanKey = "span"
)

func ContextWithTraceParent(ctx context.Context, traceparent [16]byte) (context.Context, error) {
Expand Down
18 changes: 14 additions & 4 deletions utils/memoryutils/memory_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"time"
)

var (
defaultSize = 1024
)

type Buffer struct {
Size int
options BufferOptions

Check failure on line 13 in utils/memoryutils/memory_utils.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: BufferOptions
mtx sync.RWMutex
entries []*Entry
}
Expand All @@ -16,6 +20,10 @@ type Entry struct {
Timestamp time.Time
}

func (m *Buffer) Options() BufferOptions {

Check failure on line 23 in utils/memoryutils/memory_utils.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: BufferOptions
return m.options
}

func (m *Buffer) Put(v interface{}) {
m.mtx.Lock()
defer m.mtx.Unlock()
Expand All @@ -31,7 +39,7 @@ func (m *Buffer) Put(v interface{}) {

// if the length of the entries is greater than the
// specified size, then trim down the buffer by 1
if len(m.entries) > m.Size {
if len(m.entries) > m.options.Size {
m.entries = m.entries[1:]
}
}
Expand All @@ -51,9 +59,11 @@ func (m *Buffer) Get(n int) []*Entry {
return m.entries[delta:]
}

func NewBuffer(size int) *Buffer {
func NewBuffer(opts ...BufferOption) *Buffer {

Check failure on line 62 in utils/memoryutils/memory_utils.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: BufferOption
options := NewBufferOptions(opts...)

Check failure on line 63 in utils/memoryutils/memory_utils.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: NewBufferOptions

b := &Buffer{
Size: size,
options: options,
mtx: sync.RWMutex{},
entries: []*Entry{},
}
Expand Down
4 changes: 2 additions & 2 deletions utils/memoryutils/memory_utils.test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestMemoryBuffer(t *testing.T) {
b := NewBuffer(10)
b := NewBuffer(BufferWithSize(10))

Check failure on line 10 in utils/memoryutils/memory_utils.test.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: BufferWithSize

b.Put("foo")

Expand All @@ -16,7 +16,7 @@ func TestMemoryBuffer(t *testing.T) {
val := entries[0].Value.(string)
require.Equal(t, "foo", val)

b = NewBuffer(10)
b = NewBuffer(BufferWithSize(10))

Check failure on line 19 in utils/memoryutils/memory_utils.test.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: BufferWithSize

for i := 0; i < 10; i++ {
b.Put(i)
Expand Down

0 comments on commit 8b33a00

Please sign in to comment.