From 19f305b510bdd086cee3341bd5347c6dc3375467 Mon Sep 17 00:00:00 2001 From: "R.I.Pienaar" Date: Wed, 15 Mar 2023 12:07:00 +0100 Subject: [PATCH] [ADDED] Endpoint metadata to endpoint stats (#1233) Signed-off-by: R.I.Pienaar --- micro/service.go | 28 ++++++++++++++++------------ micro/test/service_test.go | 10 ++++++++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/micro/service.go b/micro/service.go index 79f0cc94e..ed151b893 100644 --- a/micro/service.go +++ b/micro/service.go @@ -108,14 +108,15 @@ type ( // EndpointStats contains stats for a specific endpoint. EndpointStats struct { - Name string `json:"name"` - Subject string `json:"subject"` - NumRequests int `json:"num_requests"` - NumErrors int `json:"num_errors"` - LastError string `json:"last_error"` - ProcessingTime time.Duration `json:"processing_time"` - AverageProcessingTime time.Duration `json:"average_processing_time"` - Data json.RawMessage `json:"data,omitempty"` + Name string `json:"name"` + Subject string `json:"subject"` + Metadata map[string]string `json:"metadata"` + NumRequests int `json:"num_requests"` + NumErrors int `json:"num_errors"` + LastError string `json:"last_error"` + ProcessingTime time.Duration `json:"processing_time"` + AverageProcessingTime time.Duration `json:"average_processing_time"` + Data json.RawMessage `json:"data,omitempty"` } // Ping is the response type for PING monitoring endpoint. @@ -465,8 +466,9 @@ func addEndpoint(s *service, name, subject string, handler Handler, schema *Sche endpoint.subscription = sub s.endpoints = append(s.endpoints, endpoint) endpoint.stats = EndpointStats{ - Name: name, - Subject: subject, + Name: name, + Subject: subject, + Metadata: endpoint.Metadata, } return nil } @@ -731,6 +733,7 @@ func (s *service) Stats() Stats { endpointStats := &EndpointStats{ Name: endpoint.stats.Name, Subject: endpoint.stats.Subject, + Metadata: endpoint.stats.Metadata, NumRequests: endpoint.stats.NumRequests, NumErrors: endpoint.stats.NumErrors, LastError: endpoint.stats.LastError, @@ -845,8 +848,9 @@ func (e *Endpoint) stop() error { func (e *Endpoint) reset() { e.stats = EndpointStats{ - Name: e.stats.Name, - Subject: e.stats.Subject, + Name: e.stats.Name, + Subject: e.stats.Subject, + Metadata: e.stats.Metadata, } } diff --git a/micro/test/service_test.go b/micro/test/service_test.go index d168fbed9..2dae0e1b0 100644 --- a/micro/test/service_test.go +++ b/micro/test/service_test.go @@ -1159,8 +1159,9 @@ func TestServiceStats(t *testing.T) { Version: "0.1.0", APIURL: "http://someapi.com/v1", Endpoint: µ.EndpointConfig{ - Subject: "test.func", - Handler: micro.HandlerFunc(handler), + Subject: "test.func", + Handler: micro.HandlerFunc(handler), + Metadata: map[string]string{"test": "value"}, Schema: µ.Schema{ Request: "some_request", Response: "some_response", @@ -1278,6 +1279,11 @@ func TestServiceStats(t *testing.T) { if stats.Type != micro.StatsResponseType { t.Errorf("Invalid response type; want: %s; got: %s", micro.StatsResponseType, stats.Type) } + if test.config.Endpoint != nil && test.config.Endpoint.Metadata != nil { + if !reflect.DeepEqual(test.config.Endpoint.Metadata, stats.Endpoints[0].Metadata) { + t.Errorf("invalid endpoint metadata: %v", stats.Endpoints[0].Metadata) + } + } if test.expectedStats != nil { var data map[string]interface{}