Skip to content

Commit d88c0d0

Browse files
committed
debug nstat
Signed-off-by: bwplotka <bwplotka@gmail.com>
1 parent 097f9ea commit d88c0d0

3 files changed

Lines changed: 428 additions & 0 deletions

File tree

examples/debug_netstat/main.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// A debugging script using client_golang process collector code to reproduce and inspect
15+
// process_network_receive_bytes_total and process_network_transmit_bytes_total metrics,
16+
// specifically helping debug why network bytes may evaluate to 0 on macOS (darwin).
17+
package main
18+
19+
import (
20+
"fmt"
21+
"io"
22+
"log"
23+
"net"
24+
"net/http"
25+
"os"
26+
"runtime"
27+
"time"
28+
29+
"github.com/prometheus/client_golang/prometheus"
30+
"github.com/prometheus/client_golang/prometheus/collectors"
31+
)
32+
33+
func main() {
34+
pid := os.Getpid()
35+
fmt.Printf("=== Process Collector Network Bytes Debugger ===\n")
36+
fmt.Printf("OS: %s, Architecture: %s, PID: %d\n\n", runtime.GOOS, runtime.GOARCH, pid)
37+
38+
// 1. Initial collection using client_golang ProcessCollector
39+
reg := prometheus.NewRegistry()
40+
collector := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
41+
reg.MustRegister(collector)
42+
43+
fmt.Println("--- 1. Initial Gathering from client_golang ProcessCollector ---")
44+
gatherAndPrintNetworkMetrics(reg)
45+
46+
// 2. Generate active HTTP and TCP network traffic
47+
fmt.Println("\n--- 2. Generating Active Network Traffic ---")
48+
bytesSent, bytesRecv := generateNetworkTraffic()
49+
fmt.Printf("Generated traffic: ~%d bytes sent, ~%d bytes received\n", bytesSent, bytesRecv)
50+
51+
// 3. Gathering from client_golang ProcessCollector after traffic
52+
fmt.Println("\n--- 3. Post-Traffic Gathering from client_golang ProcessCollector ---")
53+
gatherAndPrintNetworkMetrics(reg)
54+
55+
// 4. Low-level platform diagnostics
56+
runPlatformDiagnostics(pid)
57+
}
58+
59+
func gatherAndPrintNetworkMetrics(reg *prometheus.Registry) {
60+
mfs, err := reg.Gather()
61+
if err != nil {
62+
log.Fatalf("Gather failed: %v", err)
63+
}
64+
65+
foundMetrics := false
66+
for _, mf := range mfs {
67+
name := mf.GetName()
68+
if name == "process_network_receive_bytes_total" || name == "process_network_transmit_bytes_total" {
69+
foundMetrics = true
70+
for _, m := range mf.GetMetric() {
71+
if m.Counter != nil {
72+
fmt.Printf(" %s: %.0f bytes\n", name, m.Counter.GetValue())
73+
}
74+
}
75+
}
76+
}
77+
if !foundMetrics {
78+
fmt.Println(" (network metrics not exposed on this platform)")
79+
}
80+
}
81+
82+
func generateNetworkTraffic() (int, int) {
83+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84+
w.WriteHeader(http.StatusOK)
85+
w.Write([]byte("DEBUG_NETWORK_BYTES_RESPONSE"))
86+
})
87+
listener, err := net.Listen("tcp", "127.0.0.1:0")
88+
if err != nil {
89+
log.Printf("Failed to listen on local interface: %v", err)
90+
return 0, 0
91+
}
92+
defer listener.Close()
93+
94+
server := &http.Server{Handler: handler}
95+
go server.Serve(listener)
96+
defer server.Close()
97+
98+
var sent, recv int
99+
for i := 0; i < 10; i++ {
100+
resp, err := http.Get("http://" + listener.Addr().String())
101+
if err != nil {
102+
log.Printf("HTTP request error: %v", err)
103+
continue
104+
}
105+
body, _ := io.ReadAll(resp.Body)
106+
resp.Body.Close()
107+
recv += len(body)
108+
sent += 50 // approximate request size
109+
}
110+
time.Sleep(100 * time.Millisecond)
111+
return sent, recv
112+
}

0 commit comments

Comments
 (0)