From ba4aa98cc2cb5fe3286d1132fb052649c9e79f1e Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 14:49:39 +0300 Subject: [PATCH 1/8] Refactoring Makefile --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 43f4642..a20243e 100644 --- a/Makefile +++ b/Makefile @@ -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 From e12a7887cbbc6f139dbb3c423c16788471d77d11 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 14:50:08 +0300 Subject: [PATCH 2/8] Update documentation --- docs/state.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/state.md b/docs/state.md index 7950455..2b3de4a 100644 --- a/docs/state.md +++ b/docs/state.md @@ -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) | @@ -100,6 +112,30 @@ Config stored in JSON encoded into slice of bytes. | Config | [bytes](#bytes) | | | + + +### 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) | + + + + +### 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) | | | + + ### Message HealthRequest From bf5cd8b74088542932d4fcd5d7a5d7cba4e7d875 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 14:51:24 +0300 Subject: [PATCH 3/8] Update service.proto file and generate go-code --- state/service.pb.go | Bin 50143 -> 62235 bytes state/service.proto | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/state/service.pb.go b/state/service.pb.go index f1e08dee3d96eb0efc29f6b4f5a26363b71d5dce..b55f3c12ecf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_EJ%VJ94^)06p{|TJWr_L=my9pr0uzag#<*bCSaHbD@y;8 zn-!R^P`3f7>vs3oyRQ{ubL|#d%#q;iTMdk>5ry1CSA#2+;3;+2sbI+j;N)|_!NlPT zBu3oDy}&Y$ws@5_6VhsV;%zxW7kdZ545b#{qIM!#v%|=sxmHsnlGC%mD*g9X4c=@s zHU1;?4m{`XP)N#kRsay34bTSspHlN~4T{0yFc4@kH2cB?me>=&?9Ce<%%+`$*>g=2 zfP~GgfbHY~4-5B=*v!~Zgww&cmUF^^v&Xp^cFt8Gwvg1T6_JQDX+u1dRH;6`H~3Mh zX^|vN$(S${yR$dTIg_-p8+NeTjYm1gwd$DA)Lvx|^_WxI0t+NSF__hs zFVk-GfJ5sq&)$+~N11cLx$Yf4TX?zJb0iWVs}9=5>j-4Tf#s)?vUrLE8~tv|OyXv} z!H>EQFzL|ic=o4YfQ-cy`<=6u!aRSCaP54w<{PrNVmbkDpLj7&lfIHx1!x8O=M*?# zjpsN^47S_hH2cAucc z{|5uH^O_!M?nuLal^>;4MqQqCkn2B?T-j0l`}Dh-w5!+j1P9$p>h0mtmf?)E$JvZs z89;AdokM4P<0eP8gYN8^-SN}o&mSJc2YMIR5$c X`Rm6oUp{-~c=q-8?;ZP}-@W`VN;Ka6 diff --git a/state/service.proto b/state/service.proto index 312d465..dc5efe4 100644 --- a/state/service.proto +++ b/state/service.proto @@ -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; +} From cf5f6b11923a87417e1a5e5d8b20b996ed9471a7 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 14:52:07 +0300 Subject: [PATCH 4/8] 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()} +} From fc91a5b2323759442472a86a33c38b6d2155c1ef Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 14:52:49 +0300 Subject: [PATCH 5/8] Add tests for EncodeVariables --- state/service_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 state/service_test.go diff --git a/state/service_test.go b/state/service_test.go new file mode 100644 index 0000000..2c089ad --- /dev/null +++ b/state/service_test.go @@ -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"]) +} From 02e82156ba46a828f00ca098b74a78e1298d3186 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 15:27:24 +0300 Subject: [PATCH 6/8] Add tests for EncodeConfig --- state/service_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/state/service_test.go b/state/service_test.go index 2c089ad..21dc0bc 100644 --- a/state/service_test.go +++ b/state/service_test.go @@ -5,6 +5,7 @@ import ( "expvar" "testing" + "github.com/spf13/viper" "github.com/stretchr/testify/require" ) @@ -26,3 +27,23 @@ func TestEncodeVariables(t *testing.T) { require.Contains(t, dump, "test2") require.Equal(t, "test2", dump["test2"]) } + +func TestEncodeConfig(t *testing.T) { + v := viper.New() + v.Set("test1", "test1") + v.Set("test2", "test2") + + res, err := EncodeConfig(v) + require.NoError(t, err) + + dump := make(map[string]interface{}) + require.NoError(t, json.Unmarshal(res.Config, &dump)) + + require.NotEmpty(t, dump) + + require.Contains(t, dump, "test1") + require.Equal(t, dump["test1"], "test1") + + require.Contains(t, dump, "test2") + require.Equal(t, dump["test2"], "test2") +} From 1deea42f5ac0dd2b3ae681eedbc6f5b570de1039 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 15:35:23 +0300 Subject: [PATCH 7/8] Add tests for EncodeMetrics and DecodeMetrics --- state/service_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/state/service_test.go b/state/service_test.go index 21dc0bc..d545cb0 100644 --- a/state/service_test.go +++ b/state/service_test.go @@ -1,14 +1,31 @@ package state import ( + "bytes" "encoding/json" "expvar" "testing" + "github.com/prometheus/client_golang/prometheus" "github.com/spf13/viper" "github.com/stretchr/testify/require" ) +type testCollector struct { + testA *prometheus.Desc + testB *prometheus.Desc +} + +func (c *testCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- c.testA + ch <- c.testB +} + +func (c *testCollector) Collect(ch chan<- prometheus.Metric) { + ch <- prometheus.MustNewConstMetric(c.testA, prometheus.GaugeValue, 1, "label_1") + ch <- prometheus.MustNewConstMetric(c.testB, prometheus.GaugeValue, 2, "label_2") +} + func TestEncodeVariables(t *testing.T) { dump := make(map[string]interface{}) @@ -47,3 +64,55 @@ func TestEncodeConfig(t *testing.T) { require.Contains(t, dump, "test2") require.Equal(t, dump["test2"], "test2") } + +func TestEncodeAndDecodeMetrics(t *testing.T) { + registry := prometheus.NewRegistry() + + collector := &testCollector{ + testA: prometheus.NewDesc("test1", "test1", []string{"test1"}, prometheus.Labels{"label_1": "test1"}), + testB: prometheus.NewDesc("test2", "test2", []string{"test2"}, prometheus.Labels{"label_2": "test2"}), + } + + require.NoError(t, registry.Register(collector)) + + gather, err := registry.Gather() + require.NoError(t, err) + + res, err := EncodeMetrics(registry) + require.NoError(t, err) + + metrics, err := DecodeMetrics(res) + require.NoError(t, err) + + require.Len(t, metrics, len(gather)) + + { // Check that JSON encoded metrics are equal: + expect := new(bytes.Buffer) + actual := new(bytes.Buffer) + + require.NoError(t, json.NewEncoder(expect).Encode(gather)) + require.NoError(t, json.NewEncoder(actual).Encode(metrics)) + + require.Equal(t, expect.Bytes(), actual.Bytes()) + } + + { // Deep comparison of metrics: + for i := range metrics { + require.Equal(t, gather[i].Help, metrics[i].Help) + require.Equal(t, gather[i].Name, metrics[i].Name) + require.Equal(t, gather[i].Type, metrics[i].Type) + + require.Len(t, metrics[i].Metric, len(gather[i].Metric)) + + for j := range metrics[i].Metric { + require.Equal(t, gather[i].Metric[j].Gauge, metrics[i].Metric[j].Gauge) + require.Len(t, metrics[i].Metric[j].Label, len(gather[i].Metric[j].Label)) + + for k := range metrics[i].Metric[j].Label { + require.Equal(t, gather[i].Metric[j].Label[k].Name, metrics[i].Metric[j].Label[k].Name) + require.Equal(t, gather[i].Metric[j].Label[k].Value, metrics[i].Metric[j].Label[k].Value) + } + } + } + } +} From d5bf805a0280553bb69d3274cb8de2d47da47141 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Mon, 10 Feb 2020 17:00:40 +0300 Subject: [PATCH 8/8] CHANGELOG --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 992ba8d..124d349 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ # Changelog This is the changelog for NeoFS Proto +## [0.3.2] - 2020-02-10 + +### Added +- gRPC method DumpVars to State service +- add method `EncodeVariables` to encode debug variables to JSON (slice of bytes) +- increase test coverage for state package + +### Updated +- state proto file +- documentation for state service and messages + ## [0.3.1] - 2020-02-07 ### Fixed - bug with `tz.Concat` @@ -177,3 +188,4 @@ Initial public release [0.2.14]: https://github.com/nspcc-dev/neofs-api/compare/v0.2.13...v0.2.14 [0.3.0]: https://github.com/nspcc-dev/neofs-api/compare/v0.2.14...v0.3.0 [0.3.1]: https://github.com/nspcc-dev/neofs-api/compare/v0.3.0...v0.3.1 +[0.3.2]: https://github.com/nspcc-dev/neofs-api/compare/v0.3.1...v0.3.2