Skip to content

[common] metricsfx separation of modules with external tally and without #6928

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

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 21 additions & 6 deletions common/metrics/metricsfx/metricsfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,36 @@ import (
)

// Module provides metrics client for fx application.
var Module = fx.Module("metricfx",
var Module = fx.Module("metricsfx",
fx.Provide(buildClient))

// ModuleForExternalScope provides metrics client for fx application when tally.Scope is created outside.
var ModuleForExternalScope = fx.Module("metricsfx",
fx.Provide(func(params serviceIdxParams) metrics.ServiceIdx {
return service.GetMetricsServiceIdx(params.ServiceFullName, params.Logger)
}),
fx.Provide(buildClientFromTally))

type clientParams struct {
fx.In

Logger log.Logger
ServiceFullName string `name:"service-full-name"`
SvcCfg config.Service
Scope tally.Scope `optional:"true"` // maybe filled outside
}

func buildClient(params clientParams) metrics.Client {
if params.Scope == nil {
params.Scope = params.SvcCfg.Metrics.NewScope(params.Logger, params.ServiceFullName)
}
return metrics.NewClient(params.Scope, service.GetMetricsServiceIdx(params.ServiceFullName, params.Logger))
scope := params.SvcCfg.Metrics.NewScope(params.Logger, params.ServiceFullName)
return buildClientFromTally(scope, service.GetMetricsServiceIdx(params.ServiceFullName, params.Logger))
}

type serviceIdxParams struct {
fx.In

Logger log.Logger
ServiceFullName string `name:"service-full-name"`
}

func buildClientFromTally(scope tally.Scope, serviceID metrics.ServiceIdx) metrics.Client {
return metrics.NewClient(scope, serviceID)
}
40 changes: 40 additions & 0 deletions common/metrics/metricsfx/metricsfx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package metricsfx

import (
"testing"

"github.com/uber-go/tally"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"

"github.com/uber/cadence/common/config"
"github.com/uber/cadence/common/log/testlogger"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/service"
)

func TestModule(t *testing.T) {
fxApp := fxtest.New(t,
testlogger.Module(t),
fx.Provide(fx.Annotated{
Target: func() string { return service.Frontend },
Name: "service-full-name"},
func() config.Service {
return config.Service{}
}),
Module,
fx.Invoke(func(mc metrics.Client) {}))
fxApp.RequireStart().RequireStop()
}

func TestModuleWithExternalScope(t *testing.T) {
fxApp := fxtest.New(t,
testlogger.Module(t),
fx.Provide(func() tally.Scope { return tally.NoopScope },
fx.Annotated{
Target: func() string { return service.Frontend },
Name: "service-full-name"}),
ModuleForExternalScope,
fx.Invoke(func(mc metrics.Client) {}))
fxApp.RequireStart().RequireStop()
}
Loading