Skip to content
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

expvar: expvarHandler should use json.Marshal to encode vars #1684

Closed
stevvooe opened this issue Apr 10, 2011 · 8 comments
Closed

expvar: expvarHandler should use json.Marshal to encode vars #1684

stevvooe opened this issue Apr 10, 2011 · 8 comments

Comments

@stevvooe
Copy link

Create a simple http server setup with expvar:

import (
    _ "expvar"
    "http"
    "log"
)

func main() {
    err := http.ListenAndServe(":8888", nil)
    if err != nil {
        log.Fatal(err)
    }
}

The output returned, however, are keys with json strings that need to be decoded from
json independently. This makes no sense. Take the case of passing the output of a curl
call through a json formatter:

$ curl -s http://localhost:8888/debug/vars | python -m json.tool
{
    "cmdline": "[\"./t\"]", 
    "memstats": "{\n\t\"Alloc\": 666160,\n\t\"TotalAlloc\": 1115928,\n\t\"Sys\": 5110008,\n\t\"Lookups\": 185,\n\t\"Mallocs\": 14971,\n\t\"Frees\": 12158,\n\t\"HeapAlloc\": 657968,\n\t\"HeapSys\": 2097152,\n\t\"HeapIdle\": 0,\n\t\"HeapInuse\": 929792,\n\t\"HeapObjects\": 2583,\n\t\"StackInuse\": 20480,\n\t\"StackSys\": 131072,\n\t\"MSpanInuse\": 7272,\n\t\"MSpanSys\": 131072,\n\t\"MCacheInuse\": 3024,\n\t\"MCacheSys\": 131072,\n\t\"BuckHashSys\": 1439992,\n\t\"NextGC\": 1197808,\n\t\"PauseTotalNs\": 1195000,\n\t\"PauseNs\": [\n\t\t114000,\n\t\t230000,\n\t\t851000,\n\t\t0,\n\t\t0,\n\t\t0,\n\t\t0,\n\t\t0,\n\t\t0,\n\t\t0,\n\t\t0,\n\t\t0,\n\t\t0
....

Complete garbage, albiet valid.

I am using the latest weekly.

The expvar package is a great idea, but may need to updated a bit.
@robpike
Copy link
Contributor

robpike commented Apr 12, 2011

Comment 1:

Owner changed to @dsymonds.

Status changed to Accepted.

@stevvooe
Copy link
Author

Comment 2:

I wrote a possible fix for this, but there would be API changes, and a reliance on the
current api of the json module.
Let me know if there is interest.

@dsymonds
Copy link
Contributor

Comment 3:

I think it's just that cmdline and memstats are using StringFunc instead of an as-yet
unimplemented ValueFunc that uses raw JSON.

Status changed to Started.

@rsc
Copy link
Contributor

rsc commented Apr 13, 2011

Comment 4:

I agree that everything should be JSON now.
There was some confusion about the design but I think we've resolved that.

@stevvooe
Copy link
Author

Comment 5:

I can take this bug and submit a patch, but their would likely be an api change. The api
would be simplified to the following functions and remove the automatic registration of
the handler:
// Publish the variable to name.
func Publish(name string, v interface{})
// Get returns the value registered under name
func Get(name string) interface{}
// Published returns a slice of the currently published keys
func Published() []string
// Register the default vars object with the default http server. Place this in your 
// package or commands's init function to register the handler automatically. This 
// function is guarded by a sync.Once object.
func Register()
Published does present a possible race condition, so that may need to be changed. The
structure that implements the above functions, called Exporter, would be exposed in case
one wants to mount the handler at a different location or has some specific use case.
This structure would implement http.Handler and json.Marshaler.

@rsc
Copy link
Contributor

rsc commented Apr 13, 2011

Comment 6:

The status is Started.
I think David is planning to send out a CL soon.

@stevvooe
Copy link
Author

Comment 7:

What's the right venue to propose a change like this (comment 5)? It seems like expvar
has a duplication of what's already available in the reflect package.

@dsymonds
Copy link
Contributor

Comment 8:

This issue was closed by revision 57d0c26.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants