Merge pull request #51 from nspcc-dev/add-method-to-state-service-to-dump-debug-variables

Add method to state service to dump debug variables
This commit is contained in:
Evgeniy Kulikov 2020-02-10 15:25:09 +03:00 committed by GitHub
commit 49926d59ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 112 additions and 1 deletions

View file

@ -14,7 +14,6 @@ deps:
@go mod vendor
@echo "${B}${G}=> Cleanup old files ${R}"
@find . -type f -name '*.pb.go' -not -path './vendor/*' -exec rm {} \;
@find . -type f -name '*.proto' -not -path './vendor/*' -not -name '*_test.proto' -exec rm {} \;
@echo "${B}${G}=> NeoFS Proto files ${R}"
@ -49,6 +48,9 @@ docgen: deps
# Regenerate proto files:
protoc: deps
@echo "${B}${G}=> Cleanup old files ${R}"
@find . -type f -name '*.pb.go' -not -path './vendor/*' -exec rm {} \;
@echo "${B}${G}=> Install specific version for gogo-proto ${R}"
@go list -f '{{.Path}}/...@{{.Version}}' -m github.com/gogo/protobuf | xargs go get -v
@echo "${B}${G}=> Install specific version for protobuf lib ${R}"
@ -60,3 +62,5 @@ protoc: deps
--proto_path=.:./vendor:/usr/local/include \
--gofast_out=plugins=grpc,paths=source_relative:. $$f; \
done
update: docgen protoc

View file

@ -10,6 +10,8 @@
- Messages
- [DumpRequest](#state.DumpRequest)
- [DumpResponse](#state.DumpResponse)
- [DumpVarsRequest](#state.DumpVarsRequest)
- [DumpVarsResponse](#state.DumpVarsResponse)
- [HealthRequest](#state.HealthRequest)
- [HealthResponse](#state.HealthResponse)
- [MetricsRequest](#state.MetricsRequest)
@ -39,6 +41,7 @@ rpc Netmap(NetmapRequest) returns (.bootstrap.SpreadMap);
rpc Metrics(MetricsRequest) returns (MetricsResponse);
rpc HealthCheck(HealthRequest) returns (HealthResponse);
rpc DumpConfig(DumpRequest) returns (DumpResponse);
rpc DumpVars(DumpVarsRequest) returns (DumpVarsResponse);
```
@ -73,6 +76,15 @@ The request should be signed.
| Name | Input | Output |
| ---- | ----- | ------ |
| DumpConfig | [DumpRequest](#state.DumpRequest) | [DumpResponse](#state.DumpResponse) |
#### Method DumpVars
DumpVars returns debug variables for the current node.
To permit access, used server config options.
The request should be signed.
| Name | Input | Output |
| ---- | ----- | ------ |
| DumpVars | [DumpVarsRequest](#state.DumpVarsRequest) | [DumpVarsResponse](#state.DumpVarsResponse) |
<!-- end services -->
@ -100,6 +112,30 @@ Config stored in JSON encoded into slice of bytes.
| Config | [bytes](#bytes) | | |
<a name="state.DumpVarsRequest"></a>
### Message DumpVarsRequest
DumpVarsRequest message to fetch current server debug variables.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| Meta | [service.RequestMetaHeader](#service.RequestMetaHeader) | | RequestMetaHeader contains information about request meta headers (should be embedded into message) |
| Verify | [service.RequestVerificationHeader](#service.RequestVerificationHeader) | | RequestVerificationHeader is a set of signatures of every NeoFS Node that processed request (should be embedded into message) |
<a name="state.DumpVarsResponse"></a>
### Message DumpVarsResponse
DumpVarsResponse message contains current server debug variables.
Variables stored in JSON encoded into slice of bytes.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| Variables | [bytes](#bytes) | | |
<a name="state.HealthRequest"></a>
### Message HealthRequest

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

Binary file not shown.

View file

@ -23,6 +23,10 @@ service Status {
// To permit access, used server config options.
// The request should be signed.
rpc DumpConfig(DumpRequest) returns (DumpResponse);
// DumpVars returns debug variables for the current node.
// To permit access, used server config options.
// The request should be signed.
rpc DumpVars(DumpVarsRequest) returns (DumpVarsResponse);
}
// NetmapRequest message to request current node netmap
@ -77,3 +81,17 @@ message DumpRequest {
message DumpResponse {
bytes Config = 1;
}
// DumpVarsRequest message to fetch current server debug variables.
message DumpVarsRequest {
// RequestMetaHeader contains information about request meta headers (should be embedded into message)
service.RequestMetaHeader Meta = 98 [(gogoproto.embed) = true, (gogoproto.nullable) = false];
// RequestVerificationHeader is a set of signatures of every NeoFS Node that processed request (should be embedded into message)
service.RequestVerificationHeader Verify = 99 [(gogoproto.embed) = true, (gogoproto.nullable) = false];
}
// DumpVarsResponse message contains current server debug variables.
// Variables stored in JSON encoded into slice of bytes.
message DumpVarsResponse {
bytes Variables = 1;
}

28
state/service_test.go Normal file
View file

@ -0,0 +1,28 @@
package state
import (
"encoding/json"
"expvar"
"testing"
"github.com/stretchr/testify/require"
)
func TestEncodeVariables(t *testing.T) {
dump := make(map[string]interface{})
expvar.NewString("test1").Set("test1")
expvar.NewString("test2").Set("test2")
res := EncodeVariables()
require.NoError(t, json.Unmarshal(res.Variables, &dump))
require.NotEmpty(t, dump)
// dump should contains keys `test1` and `test2`
require.Contains(t, dump, "test1")
require.Equal(t, "test1", dump["test1"])
require.Contains(t, dump, "test2")
require.Equal(t, "test2", dump["test2"])
}