Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds endpoint metadata to endpoint stats #1233

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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