Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ruokeqx
Copy link

@ruokeqx ruokeqx commented May 30, 2025

No description provided.

@ayushr2
Copy link
Collaborator

ayushr2 commented May 30, 2025

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.

@ruokeqx
Copy link
Author

ruokeqx commented May 30, 2025

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))
}

Copy link
Collaborator

@ayushr2 ayushr2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it makes sense, these are implementing the json.Marshaler interface.

@ayushr2
Copy link
Collaborator

ayushr2 commented May 30, 2025

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

@ruokeqx ruokeqx force-pushed the dev/MarshalJSON branch from 8450c8e to d53107c Compare May 30, 2025 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants