From cf5f6b11923a87417e1a5e5d8b20b996ed9471a7 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 14:52:07 +0300 Subject: [PATCH] Implement function to encode debug variables --- state/service.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/state/service.go b/state/service.go index 343a822..577046c 100644 --- a/state/service.go +++ b/state/service.go @@ -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()} +}