Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added initial SDK level App service metrics #1098

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app-service-template/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go 1.17

require (
github.com/edgexfoundry/app-functions-sdk-go/v2 v2.1.0
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.25
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.26
github.com/google/uuid v1.3.0
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a
github.com/stretchr/testify v1.7.1
Expand All @@ -19,7 +19,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/diegoholiveira/jsonlogic v1.0.1-0.20200220175622-ab7989be08b9 // indirect
github.com/eclipse/paho.mqtt.golang v1.3.5 // indirect
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.13 // indirect
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.16 // indirect
github.com/edgexfoundry/go-mod-configuration/v2 v2.2.0-dev.3 // indirect
github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.12 // indirect
github.com/edgexfoundry/go-mod-registry/v2 v2.2.0-dev.3 // indirect
Expand Down
8 changes: 4 additions & 4 deletions app-service-template/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ github.com/diegoholiveira/jsonlogic v1.0.1-0.20200220175622-ab7989be08b9 h1:NAHC
github.com/diegoholiveira/jsonlogic v1.0.1-0.20200220175622-ab7989be08b9/go.mod h1:9STzWAIpeXT1gYFvw0JM+BkyMmPKYv/ztBNgXX4hAOw=
github.com/eclipse/paho.mqtt.golang v1.3.5 h1:sWtmgNxYM9P2sP+xEItMozsR3w0cqZFlqnNN1bdl41Y=
github.com/eclipse/paho.mqtt.golang v1.3.5/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.13 h1:KqMprarS3TFgWoSqNBCI3omvBC8MnRL34Rr92aUN2JE=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.13/go.mod h1:eolD76cvsIAUzn63YN7MGyeOWWMidsFKCJjIpo+DK14=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.16 h1:e6/XBUP88TVPUpKW+CiHdi6WNQfJape1FQq2ygXTMb0=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.16/go.mod h1:6Q+rUmzAizdpU3sqnzMzSHmX7Z69qN69xxl0up/mQQA=
github.com/edgexfoundry/go-mod-configuration/v2 v2.2.0-dev.3 h1:dTTExUFHza9eJmTABr8G4KOE8JKBMzZCVC3wARiwIg4=
github.com/edgexfoundry/go-mod-configuration/v2 v2.2.0-dev.3/go.mod h1:YP17JhMnXTitowXE13QJwFaKo0oc03iyoKLjWAYl4FE=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.25 h1:EKkJf49mDUZF2N82bcHV//onD7qNTuZrNGWgP2byHqU=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.25/go.mod h1:jyfVSx7mI3u/o/oo10COxBRBvJ8O/9I3z2xAwPmNt/Q=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.26 h1:YebVI3gAJwFTMXKzB2erUonBC8eNrr+qLUGdtXFMjKs=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.26/go.mod h1:jyfVSx7mI3u/o/oo10COxBRBvJ8O/9I3z2xAwPmNt/Q=
github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.12 h1:u4ndXe8j1xYR1+axSgcgbD2OGvx7Z9v9thv0KBZp2TI=
github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.12/go.mod h1:+X6C0h8ZTJe+lLU2AGJfiAzCJK3zL+yM6cej9VC+79E=
github.com/edgexfoundry/go-mod-registry/v2 v2.2.0-dev.3 h1:Z1sR4g+guLsUNJw3z3gxaz472wt0gYu1XCQ4frqJl/o=
Expand Down
5 changes: 4 additions & 1 deletion app-service-template/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ LogLevel = "INFO"
Interval = "30s"
PublishTopicPrefix = "edgex/telemetry" # /<service-name>/<metric-name> will be added to this Publish Topic prefix
[Writable.Telemetry.Metrics] # All service's metric names must be present in this list.
# TODO: Remove sample metric and implement meaningful metrics if any needed.
MessagesReceived = true
PipelineMessagesProcessed = true # Your pipeline IDs are added to this name for the actual metric name reported
PipelineMessageProcessingTime = true # Your pipeline IDs are added to this name for the actual metric name reported
# TODO: Remove sample custom metric and implement meaningful custom metrics if any needed.
EventsConvertedToXML = true
[Writable.Telemetry.Tags] # Contains the service level tags to be attached to all the service's metrics
# Gateway="my-iot-gateway" # Tag must be added here or via Consul Env Override can only chnage existing value, not added new ones.
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ require (
bitbucket.org/bertimus9/systemstat v0.0.0-20180207000608-0eeff89b0690
github.com/diegoholiveira/jsonlogic v1.0.1-0.20200220175622-ab7989be08b9
github.com/eclipse/paho.mqtt.golang v1.3.5
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.13
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.25
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.16
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.26
github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.12
github.com/edgexfoundry/go-mod-registry/v2 v2.2.0-dev.3
github.com/fxamacker/cbor/v2 v2.4.0
github.com/gomodule/redigo v1.8.8
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-multierror v1.1.1
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a
github.com/stretchr/testify v1.7.1
)

Expand Down Expand Up @@ -52,7 +53,6 @@ require (
github.com/pebbe/zmq4 v1.2.7 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a // indirect
github.com/spiffe/go-spiffe/v2 v2.0.0 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ github.com/diegoholiveira/jsonlogic v1.0.1-0.20200220175622-ab7989be08b9 h1:NAHC
github.com/diegoholiveira/jsonlogic v1.0.1-0.20200220175622-ab7989be08b9/go.mod h1:9STzWAIpeXT1gYFvw0JM+BkyMmPKYv/ztBNgXX4hAOw=
github.com/eclipse/paho.mqtt.golang v1.3.5 h1:sWtmgNxYM9P2sP+xEItMozsR3w0cqZFlqnNN1bdl41Y=
github.com/eclipse/paho.mqtt.golang v1.3.5/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.13 h1:KqMprarS3TFgWoSqNBCI3omvBC8MnRL34Rr92aUN2JE=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.13/go.mod h1:eolD76cvsIAUzn63YN7MGyeOWWMidsFKCJjIpo+DK14=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.16 h1:e6/XBUP88TVPUpKW+CiHdi6WNQfJape1FQq2ygXTMb0=
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.16/go.mod h1:6Q+rUmzAizdpU3sqnzMzSHmX7Z69qN69xxl0up/mQQA=
github.com/edgexfoundry/go-mod-configuration/v2 v2.2.0-dev.3 h1:dTTExUFHza9eJmTABr8G4KOE8JKBMzZCVC3wARiwIg4=
github.com/edgexfoundry/go-mod-configuration/v2 v2.2.0-dev.3/go.mod h1:YP17JhMnXTitowXE13QJwFaKo0oc03iyoKLjWAYl4FE=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.25 h1:EKkJf49mDUZF2N82bcHV//onD7qNTuZrNGWgP2byHqU=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.25/go.mod h1:jyfVSx7mI3u/o/oo10COxBRBvJ8O/9I3z2xAwPmNt/Q=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.26 h1:YebVI3gAJwFTMXKzB2erUonBC8eNrr+qLUGdtXFMjKs=
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.26/go.mod h1:jyfVSx7mI3u/o/oo10COxBRBvJ8O/9I3z2xAwPmNt/Q=
github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.12 h1:u4ndXe8j1xYR1+axSgcgbD2OGvx7Z9v9thv0KBZp2TI=
github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.12/go.mod h1:+X6C0h8ZTJe+lLU2AGJfiAzCJK3zL+yM6cej9VC+79E=
github.com/edgexfoundry/go-mod-registry/v2 v2.2.0-dev.3 h1:Z1sR4g+guLsUNJw3z3gxaz472wt0gYu1XCQ4frqJl/o=
Expand Down
27 changes: 15 additions & 12 deletions internal/app/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/mock"

"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces/mocks"

Expand Down Expand Up @@ -57,13 +58,19 @@ var baseUrl = "http://localhost:"
func TestMain(m *testing.M) {
// No remote and no file results in STDOUT logging only
lc = logger.NewMockClient()
mockMetricsManager := &mocks.MetricsManager{}
mockMetricsManager.On("Register", mock.Anything, mock.Anything, mock.Anything).Return(nil)

dic = di.NewContainer(di.ServiceConstructorMap{
bootstrapContainer.LoggingClientInterfaceName: func(get di.Get) interface{} {
return lc
},
container.ConfigurationName: func(get di.Get) interface{} {
return &common.ConfigurationStruct{}
},
bootstrapContainer.MetricsManagerInterfaceName: func(get di.Get) interface{} {
return mockMetricsManager
},
})

target = NewService("unitTest", nil, "")
Expand Down Expand Up @@ -236,7 +243,8 @@ func TestAddBackgroundPublisher_HTTP(t *testing.T) {

func TestSetupHTTPTrigger(t *testing.T) {
sdk := Service{
lc: lc,
lc: lc,
dic: dic,
config: &common.ConfigurationStruct{
Trigger: common.TriggerInfo{
Type: "htTp",
Expand All @@ -256,7 +264,8 @@ func TestSetupHTTPTrigger(t *testing.T) {

func TestSetupMessageBusTrigger(t *testing.T) {
sdk := Service{
lc: lc,
lc: lc,
dic: dic,
config: &common.ConfigurationStruct{
Trigger: common.TriggerInfo{
Type: TriggerTypeMessageBus,
Expand All @@ -275,7 +284,8 @@ func TestSetupMessageBusTrigger(t *testing.T) {

func TestSetDefaultFunctionsPipelineNoTransforms(t *testing.T) {
sdk := Service{
lc: lc,
lc: lc,
dic: dic,
config: &common.ConfigurationStruct{
Trigger: common.TriggerInfo{
Type: TriggerTypeMessageBus,
Expand All @@ -290,6 +300,7 @@ func TestSetDefaultFunctionsPipelineNoTransforms(t *testing.T) {
func TestSetDefaultFunctionsPipelineOneTransform(t *testing.T) {
service := Service{
lc: lc,
dic: dic,
runtime: runtime.NewGolangRuntime("", nil, dic),
config: &common.ConfigurationStruct{
Trigger: common.TriggerInfo{
Expand Down Expand Up @@ -891,16 +902,8 @@ func TestService_SubscriptionClient(t *testing.T) {
}

func TestService_MetricsManager(t *testing.T) {
// MetricsManager Mock added to DIC in TestMain()
actual := target.MetricsManager()
assert.Nil(t, actual)

dic.Update(di.ServiceConstructorMap{
bootstrapContainer.MetricsManagerInterfaceName: func(get di.Get) interface{} {
return &mocks.MetricsManager{}
},
})

actual = target.MetricsManager()
assert.NotNil(t, actual)
}

Expand Down
17 changes: 15 additions & 2 deletions internal/app/triggerfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"fmt"
"strings"

gometrics "github.com/rcrowley/go-metrics"

"github.com/edgexfoundry/app-functions-sdk-go/v2/internal"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/common"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/http"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/messagebus"
Expand All @@ -40,7 +43,12 @@ func (svc *Service) setupTrigger(configuration *common.ConfigurationStruct) inte
var t interfaces.Trigger

bnd := NewTriggerServiceBinding(svc)
mp := &triggerMessageProcessor{bnd}
mp := &triggerMessageProcessor{
bnd: bnd,
messagesReceived: gometrics.NewCounter()}

svc.MetricsManager().Register(internal.MessagesReceivedName, mp.messagesReceived, nil)
svc.lc.Infof("%s metric has been registered and will be reported", internal.MessagesReceivedName)

switch triggerType := strings.ToUpper(configuration.Trigger.Type); triggerType {
case TriggerTypeHTTP:
Expand Down Expand Up @@ -88,7 +96,12 @@ func (svc *Service) RegisterCustomTriggerFactory(name string,

svc.customTriggerFactories[nu] = func(sdk *Service) (interfaces.Trigger, error) {
binding := NewTriggerServiceBinding(sdk)
messageProcessor := &triggerMessageProcessor{binding}
messageProcessor := &triggerMessageProcessor{
bnd: binding,
messagesReceived: gometrics.NewCounter()}

svc.MetricsManager().Register(internal.MessagesReceivedName, messageProcessor.messagesReceived, nil)
svc.lc.Infof("%s metric has been registered and will be reported", internal.MessagesReceivedName)

cfg := interfaces.TriggerConfig{
Logger: sdk.lc,
Expand Down
17 changes: 11 additions & 6 deletions internal/app/triggerfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestRegisterCustomTrigger(t *testing.T) {
builder := func(c interfaces.TriggerConfig) (interfaces.Trigger, error) {
return &trig, nil
}
sdk := Service{config: &common.ConfigurationStruct{}}
sdk := Service{config: &common.ConfigurationStruct{}, dic: dic}

err := sdk.RegisterCustomTriggerFactory(name, builder)

Expand All @@ -99,7 +99,8 @@ func TestSetupTrigger_HTTP(t *testing.T) {
Type: TriggerTypeHTTP,
},
},
lc: logger.MockLogger{},
lc: logger.MockLogger{},
dic: dic,
}

trigger := sdk.setupTrigger(sdk.config)
Expand All @@ -115,7 +116,8 @@ func TestSetupTrigger_EdgeXMessageBus(t *testing.T) {
Type: TriggerTypeMessageBus,
},
},
lc: logger.MockLogger{},
lc: logger.MockLogger{},
dic: dic,
}

trigger := sdk.setupTrigger(sdk.config)
Expand Down Expand Up @@ -165,7 +167,8 @@ func Test_Service_setupTrigger_CustomType(t *testing.T) {
Type: triggerName,
},
},
lc: logger.MockLogger{},
lc: logger.MockLogger{},
dic: dic,
}

err := sdk.RegisterCustomTriggerFactory(triggerName, func(c interfaces.TriggerConfig) (interfaces.Trigger, error) {
Expand All @@ -188,7 +191,8 @@ func Test_Service_SetupTrigger_CustomTypeError(t *testing.T) {
Type: triggerName,
},
},
lc: logger.MockLogger{},
lc: logger.MockLogger{},
dic: dic,
}

err := sdk.RegisterCustomTriggerFactory(triggerName, func(c interfaces.TriggerConfig) (interfaces.Trigger, error) {
Expand All @@ -210,7 +214,8 @@ func Test_Service_SetupTrigger_CustomTypeNotFound(t *testing.T) {
Type: triggerName,
},
},
lc: logger.MockLogger{},
lc: logger.MockLogger{},
dic: dic,
}

trigger := sdk.setupTrigger(sdk.config)
Expand Down
23 changes: 15 additions & 8 deletions internal/app/triggermessageprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ package app

import (
"fmt"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/appfunction"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/common"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/runtime"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
"sync"

"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/messaging"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
"github.com/hashicorp/go-multierror"
"sync"
gometrics "github.com/rcrowley/go-metrics"

"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/appfunction"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/common"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/runtime"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
)

type simpleTriggerServiceBinding struct {
Expand Down Expand Up @@ -61,12 +64,14 @@ func (b *simpleTriggerServiceBinding) Config() *common.ConfigurationStruct {

// triggerMessageProcessor wraps the ServiceBinding interface so that we can attach methods
type triggerMessageProcessor struct {
bnd trigger.ServiceBinding
bnd trigger.ServiceBinding
messagesReceived gometrics.Counter
}

// Process provides runtime orchestration to pass the envelope / context to the pipeline.
// Deprecated: This does NOT support multi-pipeline usage. Will send a message to the default pipeline ONLY and throw if not configured. Use MessageReceived.
func (mp *triggerMessageProcessor) Process(ctx interfaces.AppFunctionContext, envelope types.MessageEnvelope) error {
mp.messagesReceived.Inc(1)
context, ok := ctx.(*appfunction.Context)
if !ok {
return fmt.Errorf("App Context must be an instance of internal appfunction.Context. Use NewAppContext to create instance.")
Expand All @@ -89,8 +94,8 @@ func (mp *triggerMessageProcessor) Process(ctx interfaces.AppFunctionContext, en

// MessageReceived provides runtime orchestration to pass the envelope / context to configured pipeline(s) along with a response callback to execute on each completion.
func (mp *triggerMessageProcessor) MessageReceived(ctx interfaces.AppFunctionContext, envelope types.MessageEnvelope, responseHandler interfaces.PipelineResponseHandler) error {
mp.messagesReceived.Inc(1)
lc := mp.bnd.LoggingClient()

lc.Debugf("trigger attempting to find pipeline(s) for topic %s", envelope.ReceivedTopic)

// ensure we have a context established that we can safely cast to *appfunction.Context to pass to runtime
Expand All @@ -109,6 +114,8 @@ func (mp *triggerMessageProcessor) MessageReceived(ctx interfaces.AppFunctionCon

for _, pipeline := range pipelines {
pipelinesWaitGroup.Add(1)
pipeline.MessagesProcessed.Inc(1)

go func(p *interfaces.FunctionPipeline, wg *sync.WaitGroup, errCollector func(error)) {
defer wg.Done()

Expand Down
Loading