Implement function to encode debug variables

This commit is contained in:
Evgeniy Kulikov 2020-02-10 14:52:07 +03:00
parent bf5cd8b740
commit cf5f6b1192
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2

View file

@ -1,7 +1,10 @@
package state
import (
"bytes"
"encoding/json"
"expvar"
"fmt"
"github.com/golang/protobuf/proto"
"github.com/prometheus/client_golang/prometheus"
@ -61,3 +64,25 @@ func EncodeConfig(v *viper.Viper) (*DumpResponse, error) {
return &DumpResponse{Config: data}, nil
}
// EncodeVariables encodes debug variables into DumpVarsResponse message.
// Variables encoded into JSON and stored as slice of bytes.
func EncodeVariables() *DumpVarsResponse {
buf := new(bytes.Buffer)
buf.WriteString("{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if !first {
buf.WriteString(",\n")
}
first = false
_, _ = fmt.Fprintf(buf, "%q: %s", kv.Key, kv.Value)
})
buf.WriteString("\n}\n")
return &DumpVarsResponse{Variables: buf.Bytes()}
}