Skip to content

Commit

Permalink
[ADDED] Endpoint metadata to endpoint stats (#1233)
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar authored Mar 15, 2023
1 parent fa3b167 commit 19f305b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
28 changes: 16 additions & 12 deletions micro/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
}

Expand Down
10 changes: 8 additions & 2 deletions micro/test/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,9 @@ func TestServiceStats(t *testing.T) {
Version: "0.1.0",
APIURL: "http://someapi.com/v1",
Endpoint: &micro.EndpointConfig{
Subject: "test.func",
Handler: micro.HandlerFunc(handler),
Subject: "test.func",
Handler: micro.HandlerFunc(handler),
Metadata: map[string]string{"test": "value"},
Schema: &micro.Schema{
Request: "some_request",
Response: "some_response",
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 19f305b

Please sign in to comment.