1515package metrics
1616
1717import (
18- "encoding/json "
18+ "os "
1919 "testing"
2020
2121 "github.com/edgexfoundry/go-mod-core-contracts/v4/common"
@@ -97,6 +97,168 @@ func TestMessageBusReporter_Report(t *testing.T) {
9797 Tags : nil ,
9898 }
9999
100+ expectedTags := []dtos.MetricTag {
101+ {
102+ Name : serviceNameTagKey ,
103+ Value : expectedServiceName ,
104+ },
105+ }
106+ intValue := int64 (50 )
107+ expectedCounterMetric , err := dtos .NewMetric (expectedMetricName ,
108+ []dtos.MetricField {
109+ {
110+ Name : counterCountName ,
111+ Value : intValue ,
112+ }},
113+ expectedTags )
114+ require .NoError (t , err )
115+
116+ reg := gometrics .NewRegistry ()
117+
118+ counter := gometrics .NewCounter ()
119+ counter .Inc (intValue )
120+
121+ disabledCounter := gometrics .NewCounter ()
122+ disabledCounter .Inc (intValue )
123+ err = reg .Register (unexpectedMetricName , disabledCounter )
124+ require .NoError (t , err )
125+
126+ gauge := gometrics .NewGauge ()
127+ gauge .Update (intValue )
128+ expectedGaugeMetric := expectedCounterMetric
129+ expectedGaugeMetric .Fields = []dtos.MetricField {
130+ {
131+ Name : gaugeValueName ,
132+ Value : intValue ,
133+ }}
134+
135+ floatValue := 50.55
136+ expectedGaugeFloat64Metric := expectedCounterMetric
137+ expectedGaugeFloat64Metric .Fields = []dtos.MetricField {
138+ {
139+ Name : gaugeFloat64ValueName ,
140+ Value : floatValue ,
141+ }}
142+ gaugeFloat64 := gometrics .NewGaugeFloat64 ()
143+ gaugeFloat64 .Update (floatValue )
144+
145+ expectedTimerMetric := expectedCounterMetric
146+ copy (expectedTimerMetric .Fields , expectedCounterMetric .Fields )
147+ expectedTimerMetric .Fields = []dtos.MetricField {
148+ {
149+ Name : timerCountName ,
150+ Value : int64 (0 ),
151+ }}
152+ expectedTimerMetric .Fields = append (expectedTimerMetric .Fields ,
153+ []dtos.MetricField {
154+ {Name : timerMinName , Value : int64 (0 )},
155+ {Name : timerMaxName , Value : int64 (0 )},
156+ {Name : timerMeanName , Value : float64 (0 )},
157+ {Name : timerStddevName , Value : float64 (0 )},
158+ {Name : timerVarianceName , Value : float64 (0 )},
159+ }... )
160+ timer := gometrics .NewTimer ()
161+
162+ expectedHistogramMetric := expectedCounterMetric
163+ copy (expectedHistogramMetric .Fields , expectedCounterMetric .Fields )
164+ expectedHistogramMetric .Fields = []dtos.MetricField {
165+ {
166+ Name : histogramCountName ,
167+ Value : int64 (0 ),
168+ }}
169+ expectedHistogramMetric .Fields = append (expectedHistogramMetric .Fields ,
170+ []dtos.MetricField {
171+ {Name : histogramMinName , Value : int64 (0 )},
172+ {Name : histogramMaxName , Value : int64 (0 )},
173+ {Name : histogramMeanName , Value : float64 (0 )},
174+ {Name : histogramStddevName , Value : float64 (0 )},
175+ {Name : histogramVarianceName , Value : float64 (0 )},
176+ }... )
177+ histogram := gometrics .NewHistogram (gometrics .NewUniformSample (1028 ))
178+
179+ tests := []struct {
180+ Name string
181+ Metric interface {}
182+ ExpectedMetric * dtos.Metric
183+ ExpectError bool
184+ }{
185+ {"Happy path - Counter" , counter , & expectedCounterMetric , false },
186+ {"Happy path - Gauge" , gauge , & expectedGaugeMetric , false },
187+ {"Happy path - GaugeFloat64" , gaugeFloat64 , & expectedGaugeFloat64Metric , false },
188+ {"Happy path - Timer" , timer , & expectedTimerMetric , false },
189+ {"Happy path - Histogram" , histogram , & expectedHistogramMetric , false },
190+ {"No Metrics" , nil , nil , false },
191+ {"Unsupported Metric" , gometrics .NewMeter (), nil , true },
192+ }
193+
194+ for _ , test := range tests {
195+ t .Run (test .Name , func (t * testing.T ) {
196+ mockClient := & mocks.MessageClient {}
197+ mockClient .On ("Publish" , mock .Anything , mock .Anything ).Return (nil ).Run (func (args mock.Arguments ) {
198+ metricArg := args .Get (0 )
199+ require .NotNil (t , metricArg )
200+ message , ok := metricArg .(types.MessageEnvelope )
201+ require .True (t , ok )
202+ //actual := dtos.Metric{}
203+ actual , err := types.GetMsgPayload [dtos.Metric ](message )
204+ require .NoError (t , err )
205+ assert .Equal (t , expectedMetricName , actual .Name )
206+ actual .Timestamp = test .ExpectedMetric .Timestamp
207+ assert .Equal (t , * test .ExpectedMetric , actual )
208+ topicArg := args .Get (1 )
209+ require .NotNil (t , topicArg )
210+ assert .Equal (t , expectedTopic , topicArg )
211+ })
212+
213+ dic := di .NewContainer (di.ServiceConstructorMap {
214+ container .MessagingClientName : func (get di.Get ) interface {} {
215+ return mockClient
216+ },
217+ })
218+
219+ target := NewMessageBusReporter (logger .NewMockClient (), common .DefaultBaseTopic , expectedServiceName , dic , expectedTelemetryConfig )
220+
221+ if test .Metric != nil {
222+ err = reg .Register (expectedMetricName , test .Metric )
223+ require .NoError (t , err )
224+ defer reg .Unregister (expectedMetricName )
225+ }
226+
227+ err := target .Report (reg , nil )
228+
229+ if test .ExpectError {
230+ require .Error (t , err )
231+ mockClient .AssertNotCalled (t , "Publish" )
232+ return
233+ }
234+
235+ require .NoError (t , err )
236+
237+ if test .ExpectedMetric == nil {
238+ mockClient .AssertNotCalled (t , "Publish" )
239+ }
240+ })
241+ }
242+ }
243+
244+ func TestMessageBusReporter_ReportWithEnv (t * testing.T ) {
245+ _ = os .Setenv ("EDGEX_MSG_BASE64_PAYLOAD" , common .ValueTrue )
246+ defer os .Setenv ("EDGEX_MSG_BASE64_PAYLOAD" , common .ValueFalse )
247+
248+ expectedServiceName := "test-service"
249+ expectedMetricName := "test-metric"
250+ unexpectedMetricName := "disabled-metric"
251+ expectedTopic := common .BuildTopic (common .DefaultBaseTopic , common .MetricsPublishTopic , expectedServiceName , expectedMetricName )
252+
253+ expectedTelemetryConfig := & config.TelemetryInfo {
254+ Interval : "30s" ,
255+ Metrics : map [string ]bool {
256+ expectedMetricName : true ,
257+ unexpectedMetricName : false ,
258+ },
259+ Tags : nil ,
260+ }
261+
100262 expectedTags := []dtos.MetricTag {
101263 {
102264 Name : serviceNameTagKey ,
@@ -149,7 +311,6 @@ func TestMessageBusReporter_Report(t *testing.T) {
149311 Name : timerCountName ,
150312 Value : float64 (0 ),
151313 }}
152- expectedTimerMetric .Fields [0 ].Value = float64 (0 )
153314 expectedTimerMetric .Fields = append (expectedTimerMetric .Fields ,
154315 []dtos.MetricField {
155316 {Name : timerMinName , Value : float64 (0 )},
@@ -167,7 +328,6 @@ func TestMessageBusReporter_Report(t *testing.T) {
167328 Name : histogramCountName ,
168329 Value : float64 (0 ),
169330 }}
170- expectedHistogramMetric .Fields [0 ].Value = float64 (0 )
171331 expectedHistogramMetric .Fields = append (expectedHistogramMetric .Fields ,
172332 []dtos.MetricField {
173333 {Name : histogramMinName , Value : float64 (0 )},
@@ -201,8 +361,7 @@ func TestMessageBusReporter_Report(t *testing.T) {
201361 require .NotNil (t , metricArg )
202362 message , ok := metricArg .(types.MessageEnvelope )
203363 require .True (t , ok )
204- actual := dtos.Metric {}
205- err := json .Unmarshal (message .Payload , & actual )
364+ actual , err := types.GetMsgPayload [dtos.Metric ](message )
206365 require .NoError (t , err )
207366 assert .Equal (t , expectedMetricName , actual .Name )
208367 actual .Timestamp = test .ExpectedMetric .Timestamp
0 commit comments