Skip to content

Commit 79ff882

Browse files
committed
Add packet capture debug stats
Also remove potentially sensitive memstat and cmdargs from /debug/vars endopint
1 parent c01be50 commit 79ff882

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

capture/capture.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package capture
33
import (
44
"context"
55
"errors"
6+
"expvar"
67
"fmt"
78
"io"
89
"log"
@@ -22,9 +23,20 @@ import (
2223
"github.com/google/gopacket/pcap"
2324
)
2425

26+
var stats *expvar.Map
27+
28+
func init() {
29+
stats = expvar.NewMap("raw")
30+
stats.Init()
31+
}
32+
2533
// PacketHandler is a function that is used to handle packets
2634
type PacketHandler func(*tcp.Packet)
2735

36+
type PcapStatProvider interface {
37+
Stats() (*pcap.Stats, error)
38+
}
39+
2840
// PcapOptions options that can be set on a pcap capture handle,
2941
// these options take effect on inactive pcap handles
3042
type PcapOptions struct {
@@ -339,10 +351,19 @@ func (l *Listener) read(handler PacketHandler) {
339351
}
340352
}
341353

354+
timer := time.NewTicker(1 * time.Second)
355+
342356
for {
343357
select {
344358
case <-l.quit:
345359
return
360+
case <-timer.C:
361+
if h, ok := hndl.handler.(PcapStatProvider); ok {
362+
s, _ := h.Stats()
363+
stats.Add("packets_received", int64(s.PacketsReceived))
364+
stats.Add("packets_dropped", int64(s.PacketsDropped))
365+
stats.Add("packets_if_dropped", int64(s.PacketsIfDropped))
366+
}
346367
default:
347368
data, ci, err := hndl.handler.ReadPacketData()
348369
if err == nil {

gor.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
package main
44

55
import (
6-
_ "expvar"
6+
"expvar"
77
"flag"
8+
"fmt"
89
"log"
910
"net/http"
1011
"net/http/httputil"
11-
_ "net/http/pprof"
12+
httppptof "net/http/pprof"
1213
"os"
1314
"os/signal"
1415
"runtime"
@@ -22,6 +23,35 @@ var (
2223
memprofile = flag.String("memprofile", "", "write memory profile to this file")
2324
)
2425

26+
func init() {
27+
var defaultServeMux http.ServeMux
28+
http.DefaultServeMux = &defaultServeMux
29+
30+
http.HandleFunc("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
31+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
32+
fmt.Fprintf(w, "{\n")
33+
first := true
34+
expvar.Do(func(kv expvar.KeyValue) {
35+
if kv.Key == "memstats" || kv.Key == "cmdline" {
36+
return
37+
}
38+
39+
if !first {
40+
fmt.Fprintf(w, ",\n")
41+
}
42+
first = false
43+
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
44+
})
45+
fmt.Fprintf(w, "\n}\n")
46+
})
47+
48+
http.HandleFunc("/debug/pprof/", httppptof.Index)
49+
http.HandleFunc("/debug/pprof/cmdline", httppptof.Cmdline)
50+
http.HandleFunc("/debug/pprof/profile", httppptof.Profile)
51+
http.HandleFunc("/debug/pprof/symbol", httppptof.Symbol)
52+
http.HandleFunc("/debug/pprof/trace", httppptof.Trace)
53+
}
54+
2555
func loggingMiddleware(addr string, next http.Handler) http.Handler {
2656
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2757
if r.URL.Path == "/loop" {

0 commit comments

Comments
 (0)