gvisor icon indicating copy to clipboard operation
gvisor copied to clipboard

pkg/tcpip: impl json.Marshaler/Unmarshaler for StatCounter to Marshal/Unmarshal *Stats

Open ruokeqx opened this issue 8 months ago • 3 comments

ruokeqx avatar May 30 '25 16:05 ruokeqx

This doesn't have any usages in gVisor codebase. If you are using the netstack package, maybe just define this in the client code? StatCounter is an exported type.

ayushr2 avatar May 30 '25 17:05 ayushr2

This doesn't have any usages in gVisor codebase. If you are using the netstack package, maybe just define this in the client code? StatCounter is an exported type.

Hi.

Yes, I am indeed using the netstack package. I'm writing simple code like the following and want it to output JSON with the fields of Stats as keys and the corresponding values of StatCounter as values. However, the count filed of StatCounter is not exported and will generate empty JSON object {}. I cannot extend the implementation of StatCounter outside of the tcpip package.

Of course, i can write code like https://github.com/xjasonlyu/tun2socks/blob/main/restapi/netstats.go#L63-L96, but simple code is prefered.

Do you have any suggestions?

package main

import (
	"encoding/json"
	"fmt"
	"gvisor.dev/gvisor/pkg/tcpip/stack"
)

func main() {
	s := stack.New(stack.Options{})
	b, err := json.Marshal(s.Stats().TCP)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b))
}

ruokeqx avatar May 30 '25 19:05 ruokeqx

Please squash your commits, all commits are applied to the master branch after merge.

ayushr2 avatar May 30 '25 20:05 ayushr2