github.com/moleculer-go/moleculer@v0.3.3/metrics/metrics_test.go (about) 1 package metrics 2 3 import ( 4 "errors" 5 6 "github.com/moleculer-go/moleculer" 7 "github.com/moleculer-go/moleculer/context" 8 "github.com/moleculer-go/moleculer/payload" 9 "github.com/moleculer-go/moleculer/test" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Metrics", func() { 15 16 It("metricEnd() should record endTime, duration and fire span.finish event", func() { 17 var eventPayload moleculer.Payload 18 delegates := test.DelegatesWithIdAndConfig("nodex", moleculer.Config{}) 19 delegates.EmitEvent = func(context moleculer.BrokerContext) { 20 if context.EventName() == "metrics.trace.span.finish" { 21 eventPayload = context.Payload() 22 } else if context.EventName() == "metrics.trace.span.start" { 23 eventPayload = nil 24 } else { 25 Fail("Invalid event name") 26 } 27 } 28 delegates.ServiceForAction = func(string) []*moleculer.ServiceSchema { 29 return []*moleculer.ServiceSchema{&moleculer.ServiceSchema{ 30 Name: "math", 31 Version: "2", 32 }} 33 } 34 actionContext := context.BrokerContext(delegates).ChildActionContext("math.add", payload.New(nil)) 35 result := payload.New(errors.New("some error")) 36 37 //calling metricEnd without calling metricStart should not 38 //emit the event, since there is not startTime in the context 39 metricEnd(actionContext, result) 40 Expect(eventPayload).Should(BeNil()) 41 42 metricStart(actionContext) 43 44 metricEnd(actionContext, result) 45 Expect(eventPayload).ShouldNot(BeNil()) 46 Expect(eventPayload.Exists()).Should(BeTrue()) 47 48 Expect(eventPayload.Get("error").Exists()).Should(BeTrue()) 49 Expect(eventPayload.Get("error").Get("message").String()).Should(Equal("some error")) 50 51 Expect(eventPayload.Get("duration").Exists()).Should(BeTrue()) 52 Expect(eventPayload.Get("endTime").Exists()).Should(BeTrue()) 53 54 Expect(eventPayload.Get("id").Exists()).Should(BeTrue()) 55 Expect(eventPayload.Get("level").Exists()).Should(BeTrue()) 56 Expect(eventPayload.Get("level").Int()).Should(Equal(2)) 57 Expect(eventPayload.Get("action").Exists()).Should(BeTrue()) 58 Expect(eventPayload.Get("action").Get("name").String()).Should(Equal("math.add")) 59 60 Expect(eventPayload.Get("service").Exists()).Should(BeTrue()) 61 Expect(eventPayload.Get("service").Get("name").String()).Should(Equal("math")) 62 Expect(eventPayload.Get("service").Get("version").String()).Should(Equal("2")) 63 64 Expect(eventPayload.Get("meta").Exists()).Should(BeTrue()) 65 Expect(eventPayload.Get("meta").Get("duration").Exists()).Should(Equal(true)) 66 Expect(eventPayload.Get("meta").Get("duration").Int()).Should(Equal(0)) 67 Expect(eventPayload.Get("meta").Get("startTime").Exists()).Should(BeTrue()) 68 Expect(eventPayload.Get("nodeID").String()).Should(Equal("nodex")) 69 Expect(eventPayload.Get("remoteCall").Bool()).Should(Equal(true)) 70 Expect(eventPayload.Get("callerNodeID").String()).Should(Equal("")) 71 72 }) 73 74 It("metricStart() should record startTime and fire span.start event", func() { 75 var eventPayload moleculer.Payload 76 delegates := test.DelegatesWithIdAndConfig("nodex", moleculer.Config{}) 77 delegates.EmitEvent = func(context moleculer.BrokerContext) { 78 Expect(context.EventName()).Should(Equal("metrics.trace.span.start")) 79 eventPayload = context.Payload() 80 } 81 delegates.ServiceForAction = func(string) []*moleculer.ServiceSchema { 82 return []*moleculer.ServiceSchema{&moleculer.ServiceSchema{ 83 Name: "math", 84 Version: "2", 85 }} 86 } 87 actionContext := context.BrokerContext(delegates).ChildActionContext("math.add", payload.New(nil)) 88 metricStart(actionContext) 89 Expect(eventPayload).ShouldNot(BeNil()) 90 Expect(eventPayload.Exists()).Should(BeTrue()) 91 Expect(eventPayload.Get("id").Exists()).Should(BeTrue()) 92 Expect(eventPayload.Get("level").Exists()).Should(BeTrue()) 93 Expect(eventPayload.Get("level").Int()).Should(Equal(2)) 94 Expect(eventPayload.Get("action").Exists()).Should(BeTrue()) 95 Expect(eventPayload.Get("action").Get("name").String()).Should(Equal("math.add")) 96 97 Expect(eventPayload.Get("service").Exists()).Should(BeTrue()) 98 Expect(eventPayload.Get("service").Get("name").String()).Should(Equal("math")) 99 Expect(eventPayload.Get("service").Get("version").String()).Should(Equal("2")) 100 101 Expect(eventPayload.Get("meta").Exists()).Should(BeTrue()) 102 Expect(eventPayload.Get("meta").Get("duration").Exists()).Should(Equal(true)) 103 Expect(eventPayload.Get("meta").Get("duration").Int()).Should(Equal(0)) 104 Expect(eventPayload.Get("meta").Get("startTime").Exists()).Should(BeTrue()) 105 Expect(eventPayload.Get("nodeID").String()).Should(Equal("nodex")) 106 Expect(eventPayload.Get("remoteCall").Bool()).Should(Equal(true)) 107 Expect(eventPayload.Get("callerNodeID").String()).Should(Equal("")) 108 109 }) 110 111 It("createShouldMetric() should be false", func() { 112 113 shouldMetric := createShouldMetric(moleculer.Config{}) 114 115 brokerContext := context.BrokerContext(test.DelegatesWithIdAndConfig("x", moleculer.Config{})) 116 Expect(shouldMetric(brokerContext)).Should(BeFalse()) 117 118 brokerContext = context.BrokerContext(test.DelegatesWithIdAndConfig("x", moleculer.Config{ 119 Metrics: false, 120 })) 121 actionContext := brokerContext.ChildActionContext("a", payload.New(nil)) 122 Expect(shouldMetric(actionContext)).Should(BeFalse()) 123 }) 124 125 It("createShouldMetric() should be true", func() { 126 config := moleculer.Config{ 127 Metrics: true, 128 MetricsRate: 1, 129 } 130 shouldMetric := createShouldMetric(config) 131 context := context.BrokerContext(test.DelegatesWithIdAndConfig("x", config)) 132 actionContext := context.ChildActionContext("a", payload.New(nil)) 133 Expect(shouldMetric(actionContext)).Should(BeTrue()) 134 }) 135 136 It("createShouldMetric() should be true on for half of the requests", func() { 137 138 config := moleculer.Config{ 139 Metrics: true, 140 MetricsRate: .5, 141 } 142 shouldMetric := createShouldMetric(config) 143 144 brokerContext := context.BrokerContext(test.DelegatesWithIdAndConfig("x", config)) 145 actionContext := brokerContext.ChildActionContext("a", payload.New(nil)) 146 Expect(shouldMetric(actionContext)).Should(BeFalse()) 147 Expect(shouldMetric(actionContext)).Should(BeTrue()) 148 Expect(shouldMetric(actionContext)).Should(BeFalse()) 149 Expect(shouldMetric(actionContext)).Should(BeTrue()) 150 }) 151 152 It("createShouldMetric() should be true 1/10 of the requests", func() { 153 154 config := moleculer.Config{ 155 Metrics: true, 156 MetricsRate: .1, 157 } 158 shouldMetric := createShouldMetric(config) 159 160 brokerContext := context.BrokerContext(test.DelegatesWithIdAndConfig("x", config)) 161 actionContext := brokerContext.ChildActionContext("a", payload.New(nil)) 162 Expect(shouldMetric(actionContext)).Should(BeFalse()) 163 Expect(shouldMetric(actionContext)).Should(BeFalse()) 164 Expect(shouldMetric(actionContext)).Should(BeFalse()) 165 Expect(shouldMetric(actionContext)).Should(BeFalse()) 166 Expect(shouldMetric(actionContext)).Should(BeFalse()) 167 Expect(shouldMetric(actionContext)).Should(BeFalse()) 168 Expect(shouldMetric(actionContext)).Should(BeFalse()) 169 Expect(shouldMetric(actionContext)).Should(BeFalse()) 170 Expect(shouldMetric(actionContext)).Should(BeFalse()) 171 Expect(shouldMetric(actionContext)).Should(BeTrue()) 172 Expect(shouldMetric(actionContext)).Should(BeFalse()) 173 Expect(shouldMetric(actionContext)).Should(BeFalse()) 174 }) 175 176 })