Skip to content

Commit 3e54ff4

Browse files
committed
add metrics
1 parent 32f4e9d commit 3e54ff4

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

interface.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ type Interface struct {
9393
messageMetrics *MessageMetrics
9494
cachedPacketMetrics *cachedPacketMetrics
9595

96+
listenInN int
97+
listenOutN int
98+
99+
listenInMetric metrics.Histogram
100+
listenOutMetric metrics.Histogram
101+
96102
l *logrus.Logger
97103
}
98104

@@ -197,6 +203,8 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
197203

198204
l: c.l,
199205
}
206+
ifce.listenInMetric = metrics.GetOrRegisterHistogram("vhost.listenIn.n", nil, metrics.NewExpDecaySample(1028, 0.015))
207+
ifce.listenOutMetric = metrics.GetOrRegisterHistogram("vhost.listenOut.n", nil, metrics.NewExpDecaySample(1028, 0.015))
200208

201209
ifce.tryPromoteEvery.Store(c.tryPromoteEvery)
202210
ifce.reQueryEvery.Store(c.reQueryEvery)
@@ -296,8 +304,13 @@ func (f *Interface) listenOut(q int) {
296304
}
297305
}
298306
}
307+
n := len(toSend)
308+
if f.l.Level == logrus.DebugLevel {
309+
f.listenOutMetric.Update(int64(n))
310+
}
311+
f.listenOutN = n
299312
//toSend = toSend[:toSendCount]
300-
for i := 0; i < len(toSend); i += batch {
313+
for i := 0; i < n; i += batch {
301314
x := min(len(toSend[i:]), batch)
302315
toSendThisTime := toSend[i : i+x]
303316
_, err := f.readers[q].WriteMany(toSendThisTime, q)
@@ -330,6 +343,10 @@ func (f *Interface) listenIn(reader overlay.TunDev, queueNum int) {
330343

331344
for {
332345
n, err := reader.ReadMany(packets, queueNum)
346+
if f.l.Level == logrus.DebugLevel {
347+
f.listenInMetric.Update(int64(n))
348+
}
349+
f.listenInN = n
333350
//todo!!
334351
if err != nil {
335352
if errors.Is(err, os.ErrClosed) && f.closed.Load() {
@@ -488,6 +505,11 @@ func (f *Interface) emitStats(ctx context.Context, i time.Duration) {
488505
} else {
489506
certMaxVersion.Update(int64(certState.v1Cert.Version()))
490507
}
508+
if f.l.Level != logrus.DebugLevel {
509+
f.listenInMetric.Update(int64(f.listenInN))
510+
f.listenOutMetric.Update(int64(f.listenOutN))
511+
}
512+
491513
}
492514
}
493515
}

0 commit comments

Comments
 (0)