Skip to content

Commit cb6b7d4

Browse files
chez-shanpukaworu
authored andcommitted
hubble: count ring-buffer lost events on forward reads only
The `hubble_ring_buffer` lost events metric was incremented whenever a reader reached the ring buffer boundary. However, reaching the boundary does not always mean events were actually lost. Hitting the boundary in backward scans is an expected stop condition rather than a real loss. This commit moves the metric count from the producer (ring.getLostEvent) to the consumer's forward-reading paths (RingReader.Next/NextFollow). The metric is now counted only when a consumer actually catches a lost event. Normal ring scans no longer count the metric when they expectedly reach that boundary. Fixes: cilium#46584 Signed-off-by: Tomoki Sugiura <tomoki-sugiura@cybozu.co.jp>
1 parent dbd7ebc commit cb6b7d4

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

pkg/hubble/container/ring.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"io"
10-
"strings"
1110
"sync/atomic"
1211
"unsafe"
1312

@@ -16,7 +15,6 @@ import (
1615
flowpb "github.com/cilium/cilium/api/v1/flow"
1716
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
1817
"github.com/cilium/cilium/pkg/hubble/math"
19-
"github.com/cilium/cilium/pkg/hubble/metrics"
2018
"github.com/cilium/cilium/pkg/lock"
2119
"github.com/cilium/cilium/pkg/time"
2220
)
@@ -217,7 +215,6 @@ func (r *Ring) OldestWrite() uint64 {
217215
}
218216

219217
func getLostEvent() *v1.Event {
220-
metrics.LostEvents.WithLabelValues(strings.ToLower(flowpb.LostEventSource_HUBBLE_RING_BUFFER.String())).Inc()
221218
now := time.Now().UTC()
222219
return &v1.Event{
223220
Timestamp: &timestamppb.Timestamp{

pkg/hubble/container/ring_reader.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ package container
55

66
import (
77
"context"
8+
"strings"
89
"sync"
910

11+
flowpb "github.com/cilium/cilium/api/v1/flow"
1012
v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
13+
"github.com/cilium/cilium/pkg/hubble/metrics"
1114
"github.com/cilium/cilium/pkg/lock"
1215
)
1316

@@ -67,6 +70,10 @@ func (r *RingReader) Next() (*v1.Event, error) {
6770
return nil, err
6871
}
6972
r.idx++
73+
if lost, ok := e.Event.(*flowpb.LostEvent); ok && lost.Source == flowpb.LostEventSource_HUBBLE_RING_BUFFER {
74+
// count hubble_ring_buffer lost events
75+
metrics.LostEvents.WithLabelValues(strings.ToLower(flowpb.LostEventSource_HUBBLE_RING_BUFFER.String())).Inc()
76+
}
7077
return e, nil
7178
}
7279

@@ -115,6 +122,10 @@ func (r *RingReader) NextFollow(ctx context.Context) *v1.Event {
115122
// increment idx so that future calls to the ring reader will
116123
// continue reading from were we stopped.
117124
r.idx++
125+
if lost, ok := e.Event.(*flowpb.LostEvent); ok && lost.Source == flowpb.LostEventSource_HUBBLE_RING_BUFFER {
126+
// count hubble_ring_buffer lost events
127+
metrics.LostEvents.WithLabelValues(strings.ToLower(flowpb.LostEventSource_HUBBLE_RING_BUFFER.String())).Inc()
128+
}
118129
return e
119130
case <-ctx.Done():
120131
return nil

0 commit comments

Comments
 (0)