Skip to content

Commit

Permalink
Switch to using json.RawMessage for stats data
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpio committed Dec 20, 2022
1 parent d1e8c98 commit 63932a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion micro/example_test.go
Expand Up @@ -16,6 +16,7 @@ package micro
import (
"fmt"
"log"
"reflect"

"github.com/nats-io/nats.go"
)
Expand Down Expand Up @@ -198,7 +199,7 @@ func ExampleService_Reset() {
empty := Stats{
ServiceIdentity: srv.Info().ServiceIdentity,
}
if srv.Stats() != empty {
if !reflect.DeepEqual(srv.Stats(), empty) {
log.Fatal("Expected endpoint stats to be empty")
}
}
Expand Down
20 changes: 10 additions & 10 deletions micro/service.go
Expand Up @@ -69,13 +69,13 @@ type (
// It contains stats for a specific endpoint (either request handler or monitoring enpoints).
Stats struct {
ServiceIdentity
NumRequests int `json:"num_requests"`
NumErrors int `json:"num_errors"`
LastError error `json:"last_error"`
ProcessingTime time.Duration `json:"processing_time"`
AverageProcessingTime time.Duration `json:"average_processing_time"`
Started string `json:"started"`
Data interface{} `json:"data,omitempty"`
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"`
Started string `json:"started"`
Data json.RawMessage `json:"data,omitempty"`
}

// Ping is the response type for PING monitoring endpoint.
Expand All @@ -91,7 +91,7 @@ type (
// SchemaResp is the response value for SCHEMA requests.
SchemaResp struct {
ServiceIdentity
Schema
Schema Schema `json:"schema"`
}

// Schema can be used to configure a schema for a service.
Expand Down Expand Up @@ -469,7 +469,7 @@ func (s *service) reqHandler(req *Request) {

if err != nil {
s.stats.NumErrors++
s.stats.LastError = err
s.stats.LastError = err.Error()
}
s.m.Unlock()
}
Expand Down Expand Up @@ -529,7 +529,7 @@ func (s *service) Stats() Stats {
s.m.Lock()
defer s.m.Unlock()
if s.StatsHandler != nil {
s.stats.Data = s.StatsHandler(s.Endpoint)
s.stats.Data, _ = json.Marshal(s.StatsHandler(s.Endpoint))
}
info := s.Info()
return Stats{
Expand Down
10 changes: 7 additions & 3 deletions micro/service_test.go
Expand Up @@ -187,7 +187,7 @@ func TestServiceBasics(t *testing.T) {
ServiceIdentity: svcs[0].Info().ServiceIdentity,
}

if svcs[0].Stats() != emptyStats {
if !reflect.DeepEqual(svcs[0].Stats(), emptyStats) {
t.Fatalf("Expected empty stats after reset; got: %+v", svcs[0].Stats())
}

Expand Down Expand Up @@ -866,8 +866,12 @@ func TestServiceStats(t *testing.T) {
t.Errorf("Unexpected num_requests; want: 1; got: %d", stats.NumErrors)
}
if test.expectedStats != nil {
if val, ok := stats.Data.(map[string]interface{}); !ok || !reflect.DeepEqual(val, test.expectedStats) {
t.Fatalf("Invalid data from stats handler; want: %v; got: %v", test.expectedStats, val)
var data map[string]interface{}
if err := json.Unmarshal(stats.Data, &data); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !reflect.DeepEqual(data, test.expectedStats) {
t.Fatalf("Invalid data from stats handler; want: %v; got: %v", test.expectedStats, data)
}
}
})
Expand Down

0 comments on commit 63932a2

Please sign in to comment.