Skip to content

Commit 1b48b71

Browse files
authored
Expand Buffer Metrics
Expand metrics emitted by low latency buffer
1 parent 388c3b7 commit 1b48b71

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

spectator/writer/lowlatency_buffer.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package writer
22

33
import (
44
"fmt"
5-
"github.com/Netflix/spectator-go/v2/spectator/logger"
65
"runtime"
76
"sync"
87
"sync/atomic"
98
"time"
9+
10+
"github.com/Netflix/spectator-go/v2/spectator/logger"
1011
)
1112

1213
// chunkSize is set to 60KB, to ensure each message fits in the socket buffer (64KB), with some room
@@ -26,28 +27,32 @@ const separator = "\n"
2627
// is less than the impact of the front and back buffer design, but it is still important for
2728
// throughput reasons.
2829
type bufferShard struct {
29-
data [][]byte // Array of chunkSize chunks of spectatord protocol lines, stored as bytes
30-
chunkIndex int // Index of the chunk available for writes
31-
overflows int // Count the buffer overflows, which correspond to data drops, for reporting metrics
32-
mu sync.Mutex
30+
data [][]byte // Array of chunkSize chunks of spectatord protocol lines, stored as bytes
31+
chunkIndex int // Index of the chunk available for writes
32+
overflows int // Count the buffer overflows, which correspond to data drops, for reporting metrics
33+
overflowBytes int64 // Count the bytes that were dropped, for reporting metrics
34+
mu sync.Mutex
3335
}
3436

3537
// getChunkIndexForLine returns the chunkIndex that should be used for storing the line, or -1, if there is
3638
// an overflow and the line cannot be stored in the bufferShard.
3739
func (b *bufferShard) getChunkIndexForLine(line []byte) int {
40+
totalWriteLength := len(line)
41+
3842
// All chunks are full for the shard, drop the data
3943
if b.chunkIndex >= len(b.data) {
4044
b.overflows++
45+
b.overflowBytes += int64(totalWriteLength)
4146
return -1
4247
}
4348

4449
// This should not happen, drop the data. The maximum length of a well-formed protocol line is 3.8KB.
4550
if len(line) > chunkSize {
4651
b.overflows++
52+
b.overflowBytes += int64(totalWriteLength)
4753
return -1
4854
}
4955

50-
totalWriteLength := len(line)
5156
if len(b.data[b.chunkIndex]) > 0 {
5257
// Chunk has data, so account for the separator character
5358
totalWriteLength++
@@ -61,6 +66,7 @@ func (b *bufferShard) getChunkIndexForLine(line []byte) int {
6166
// Out of space in the shard, drop the data
6267
if b.chunkIndex == len(b.data) {
6368
b.overflows++
69+
b.overflowBytes += int64(totalWriteLength)
6470
return -1
6571
}
6672

@@ -190,6 +196,8 @@ func (llb *LowLatencyBuffer) flushLoop() {
190196

191197
// swapAndFlush swaps the front and back buffers and flushes the deactivated buffers
192198
func (llb *LowLatencyBuffer) swapAndFlush() {
199+
start := time.Now()
200+
193201
// Swap the buffer sets, so one can be drained, while the other accepts application writes
194202
old := llb.useFrontBuffers.Load()
195203
llb.useFrontBuffers.CompareAndSwap(old, !old)
@@ -219,6 +227,8 @@ func (llb *LowLatencyBuffer) swapAndFlush() {
219227
if pctUsage > 0 {
220228
llb.writer.WriteString(fmt.Sprintf("g,1:spectator-go.lowLatencyBuffer.pctUsage,bufferSet=%s:%f", bufferSet, pctUsage))
221229
}
230+
231+
llb.writer.WriteString(fmt.Sprintf("t:spectator-go.lowLatencyBuffer.flushTime,bufferSet=%s:%f", bufferSet, time.Since(start).Seconds()))
222232
}
223233

224234
// flushBufferShard flushes a single bufferShard to the socket, iterating through all chunks
@@ -244,7 +254,9 @@ func (llb *LowLatencyBuffer) flushBufferShard(buffer *bufferShard, bufferSet str
244254
// record status metrics and reset shard statistics
245255
if buffer.overflows > 0 {
246256
llb.writer.WriteString(fmt.Sprintf("c:spectator-go.lowLatencyBuffer.overflows,bufferSet=%s:%d", bufferSet, buffer.overflows))
257+
llb.writer.WriteString(fmt.Sprintf("d:spectator-go.lowLatencyBuffer.overflowBytes,bufferSet=%s:%d", bufferSet, buffer.overflowBytes))
247258
buffer.overflows = 0
259+
buffer.overflowBytes = 0
248260
}
249261
buffer.chunkIndex = 0
250262
return bytesWritten

0 commit comments

Comments
 (0)