Skip to content

Commit ef32187

Browse files
authored
Merge pull request #820 from jinlinGuan/message-issue-381
feat: Update to use go-mod-messaging updated Msgenvelope functions
2 parents 36ed06c + 689d11e commit ef32187

File tree

4 files changed

+175
-23
lines changed

4 files changed

+175
-23
lines changed

bootstrap/metrics/reporter.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package metrics
1616

1717
import (
18-
"encoding/json"
18+
"context"
1919
"errors"
2020
"fmt"
2121

@@ -171,17 +171,10 @@ func (r *messageBusReporter) Report(registry gometrics.Registry, metricTags map[
171171
return
172172
}
173173

174-
payload, err := json.Marshal(nextMetric)
175-
if err != nil {
176-
errs = multierror.Append(errs, fmt.Errorf("failed to marshal metric '%s' to JSON: %s", nextMetric.Name, err.Error()))
177-
return
178-
}
179-
180-
message := types.MessageEnvelope{
181-
CorrelationID: uuid.NewString(),
182-
Payload: payload,
183-
ContentType: common.ContentTypeJSON,
184-
}
174+
ctx := context.Background()
175+
ctx = context.WithValue(ctx, common.CorrelationHeader, uuid.NewString()) //nolint: staticcheck
176+
ctx = context.WithValue(ctx, common.ContentType, common.ContentTypeJSON) //nolint: staticcheck
177+
message := types.NewMessageEnvelope(nextMetric, ctx)
185178

186179
topic := common.BuildTopic(r.baseMetricsTopic, name)
187180
if err := r.messageClient.Publish(message, topic); err != nil {

bootstrap/metrics/reporter_test.go

Lines changed: 164 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package metrics
1616

1717
import (
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

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ go 1.23
44

55
require (
66
github.com/eclipse/paho.mqtt.golang v1.5.0
7-
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.11
7+
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.12
88
github.com/edgexfoundry/go-mod-core-contracts/v4 v4.0.0-dev.22
9-
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.10
9+
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.11
1010
github.com/edgexfoundry/go-mod-registry/v4 v4.0.0-dev.3
1111
github.com/edgexfoundry/go-mod-secrets/v4 v4.0.0-dev.5
1212
github.com/golang-jwt/jwt/v5 v5.2.1

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
6868
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6969
github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o=
7070
github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk=
71-
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.11 h1:VCDeyEwhSb01dhJ3Rw0DbtV8pc18TILAoUGSFn7NR64=
72-
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.11/go.mod h1:ltUpMcOpJSzmabBtZox5qg1AK2wEikvZJyIBXtJ7mUQ=
71+
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.12 h1:2P7kCcGMd5BX9NFubHxfZ+Q19Q0ctMwh11rgSyPT5uc=
72+
github.com/edgexfoundry/go-mod-configuration/v4 v4.0.0-dev.12/go.mod h1:sx5Zx+zAhmlFRNK2EblvOnCuUc099F2mHLWtZGfoWB8=
7373
github.com/edgexfoundry/go-mod-core-contracts/v4 v4.0.0-dev.22 h1:XzDtbAmvp/v+DZlFoSgttnUQsIXGhlf+H8xgGxBClUA=
7474
github.com/edgexfoundry/go-mod-core-contracts/v4 v4.0.0-dev.22/go.mod h1:D35HIMZkFFy82shKtPYaEL3Nn+ZNEjUjZI1RLn1j23E=
75-
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.10 h1:xvDQDIJtmj/ZCmKzbAzg3h1F2ZdWz1MPoJSNfYZANGc=
76-
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.10/go.mod h1:ibaiw7r3RgLYDuuFfWT1kh//bjP+onDOOQsnSsdD4E8=
75+
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.11 h1:JNhS2eIPNJI5Ie043pvxGlP+4SLXglGw+dHAqCguC48=
76+
github.com/edgexfoundry/go-mod-messaging/v4 v4.0.0-dev.11/go.mod h1:BfIFNR+dinBvg3s2bHoIuRwpw5KwCbMtjtIu5T/0WwM=
7777
github.com/edgexfoundry/go-mod-registry/v4 v4.0.0-dev.3 h1:6tw6JqEJDOqo2lEgxjZ+scvsub5R20WGpInCuoxS6zE=
7878
github.com/edgexfoundry/go-mod-registry/v4 v4.0.0-dev.3/go.mod h1:QpZW5bWxsk0Leh1nvgojBZrpHA/B6dSw6LgT0zxh9hg=
7979
github.com/edgexfoundry/go-mod-secrets/v4 v4.0.0-dev.5 h1:PnbvMnedIlbqXsnUp2+i18BJ9e6CJ7GzwNA9vjPU3Jk=

0 commit comments

Comments
 (0)