From e6c808614b497cfe755db805379e1c5345ba0d6d Mon Sep 17 00:00:00 2001 From: Leonard Goodell Date: Wed, 10 Jan 2024 11:44:30 -0700 Subject: [PATCH 1/2] feat: Add metric for Store And Forward queue size closes #1138 Signed-off-by: Leonard Goodell --- internal/constant.go | 26 ++++++++++++-------------- internal/runtime/storeforward.go | 24 +++++++++++++++++++----- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/internal/constant.go b/internal/constant.go index b3416a2a4..d223f7371 100644 --- a/internal/constant.go +++ b/internal/constant.go @@ -16,14 +16,7 @@ package internal -import ( - "github.com/edgexfoundry/go-mod-core-contracts/v3/common" -) - -const ( - ApiTriggerRoute = common.ApiBase + "/trigger" - ApiAddSecretRoute = common.ApiBase + "/secret" -) +import "github.com/edgexfoundry/go-mod-core-contracts/v3/common" // SDKVersion indicates the version of the SDK - will be overwritten by build var SDKVersion = "0.0.0" @@ -31,7 +24,13 @@ var SDKVersion = "0.0.0" // ApplicationVersion indicates the version of the application itself, not the SDK - will be overwritten by build var ApplicationVersion = "0.0.0" -// Names for the Common Application Service Metrics +// Misc Constants +const ( + ApiTriggerRoute = common.ApiBase + "/trigger" + MessageBusSubscribeTopics = "SubscribeTopics" +) + +// Common Application Service Metrics constants const ( MessagesReceivedName = "MessagesReceived" InvalidMessagesReceivedName = "InvalidMessagesReceived" @@ -43,9 +42,8 @@ const ( HttpExportErrorsName = "HttpExportErrors" MqttExportSizeName = "MqttExportSize" MqttExportErrorsName = "MqttExportErrors" - MessageBusSubscribeTopics = "SubscribeTopics" - MessageBusPublishTopic = "PublishTopic" -) + StoreForwardQueueSizeName = "StoreForwardQueueSize" -// MetricsReservoirSize is the default Metrics Sample Reservoir size -const MetricsReservoirSize = 1028 + // MetricsReservoirSize is the default Metrics Sample Reservoir size + MetricsReservoirSize = 1028 +) diff --git a/internal/runtime/storeforward.go b/internal/runtime/storeforward.go index eae01561e..b6561eefe 100644 --- a/internal/runtime/storeforward.go +++ b/internal/runtime/storeforward.go @@ -24,6 +24,7 @@ import ( "sync/atomic" "time" + "github.com/edgexfoundry/app-functions-sdk-go/v3/internal" "github.com/edgexfoundry/app-functions-sdk-go/v3/internal/appfunction" "github.com/edgexfoundry/app-functions-sdk-go/v3/internal/bootstrap/container" "github.com/edgexfoundry/app-functions-sdk-go/v3/pkg/interfaces" @@ -48,16 +49,29 @@ type storeForwardInfo struct { } func newStoreAndForward(runtime *FunctionsPipelineRuntime, dic *di.Container, serviceKey string) *storeForwardInfo { - return &storeForwardInfo{ + sf := &storeForwardInfo{ runtime: runtime, dic: dic, lc: bootstrapContainer.LoggingClientFrom(dic.Get), serviceKey: serviceKey, - // Using counter metric now for the retry on success feature because it is thread safe - // and will also be used in future metrics feature to track size of the S&F queue. - // TODO: Register counter metric when using it for the metrics capability - dataCount: gometrics.NewCounter(), + dataCount: gometrics.NewCounter(), } + + metricsManager := bootstrapContainer.MetricsManagerFrom(dic.Get) + if metricsManager == nil { + sf.lc.Errorf("Unable to register %s metric: MetricsManager is not available.", internal.StoreForwardQueueSizeName) + return sf + } + + err := metricsManager.Register(internal.StoreForwardQueueSizeName, sf.dataCount, nil) + if err != nil { + sf.lc.Errorf("Unable to register metric %s. Collection will continue, but metric will not be reported: %v", + internal.StoreForwardQueueSizeName, err) + } + + sf.lc.Infof("%s metric has been registered and will be reported (if enabled)", internal.StoreForwardQueueSizeName) + + return sf } func (sf *storeForwardInfo) startStoreAndForwardRetryLoop( From 11d2ee9f0a08fe06469bcf7be552dcac216cae0b Mon Sep 17 00:00:00 2001 From: Leonard Goodell Date: Tue, 16 Jan 2024 20:49:14 -0700 Subject: [PATCH 2/2] fix: Adjust failing unit test Signed-off-by: Leonard Goodell --- internal/runtime/storeforward_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/runtime/storeforward_test.go b/internal/runtime/storeforward_test.go index d18eb2856..80aec27a3 100644 --- a/internal/runtime/storeforward_test.go +++ b/internal/runtime/storeforward_test.go @@ -243,6 +243,8 @@ func TestStoreForLaterRetry(t *testing.T) { func TestTriggerRetry(t *testing.T) { mockLogger := &loggerMocks.LoggingClient{} + mockLogger.On("Infof", "%s metric has been registered and will be reported (if enabled)", "StoreForwardQueueSize") + dic.Update(di.ServiceConstructorMap{ bootstrapContainer.LoggingClientInterfaceName: func(get di.Get) interface{} { return mockLogger