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

Update: #385

Closed
wants to merge 9 commits into from
18 changes: 18 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func (a *Agent) StartMotanAgentFromConfig(config *cfg.Config) {
// start metrics reporter early, here agent context has already initialized
metrics.StartReporter(a.Context)
a.registerStatusSampler()
a.initSwitchers()
a.initStatus()
a.initClusters()
a.startServerAgent()
Expand All @@ -223,6 +224,23 @@ func (a *Agent) StartMotanAgentFromConfig(config *cfg.Config) {
a.startAgent()
}

func (a *Agent) initSwitchers() {
// init metrics request application switcher
enableMetricsReqApp := a.agentURL.GetParam(motan.EnableMetricsReqApp, "false")
on, e := strconv.ParseBool(enableMetricsReqApp)
if e != nil {
vlog.Warningln("illegal value for enableMetricsReqApp, use default value: false")
} else {
switcher := motan.GetSwitcherManager().GetSwitcher(motan.MetricsReqApplication)
if switcher == nil {
vlog.Warningln("metrics request application switcher is nil, use default value: false")
} else {
vlog.Infoln("enableMetricsReqApp: ", on, ", set metrics request application switcher value")
switcher.SetValue(on)
}
}
}

func (a *Agent) startRegistryFailback() {
vlog.Infoln("start agent failback")
ticker := time.NewTicker(registry.DefaultFailbackInterval * time.Millisecond)
Expand Down
6 changes: 6 additions & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ const (
UnixSockProtocolFlag = "unix://"
)

// metrics request application
const (
MetricsReqApplication = "metricsReqApp"
EnableMetricsReqApp = "enableMetricsReqApp"
)

// attachment keys
const (
XForwardedForLower = "x-forwarded-for" // used as motan default proxy key
Expand Down
2 changes: 1 addition & 1 deletion filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func RegistDefaultFilters(extFactory motan.ExtensionFactory) {
})

extFactory.RegistExtFilter(Metrics, func() motan.Filter {
return &MetricsFilter{}
return &MetricsFilter{switcher: motan.GetSwitcherManager().GetSwitcher(motan.MetricsReqApplication)}
})

extFactory.RegistExtFilter(CircuitBreaker, func() motan.Filter {
Expand Down
23 changes: 21 additions & 2 deletions filter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ const (
MetricsSlowCountSuffix = ".slow_count"
)

func init() {
motan.GetSwitcherManager().Register(motan.MetricsReqApplication, false)
}

type MetricsFilter struct {
next motan.EndPointFilter
next motan.EndPointFilter
switcher *motan.Switcher
}

func (m *MetricsFilter) GetIndex() int {
return 2
}

func (m *MetricsFilter) NewFilter(url *motan.URL) motan.Filter {
return &MetricsFilter{}
return &MetricsFilter{
switcher: motan.GetSwitcherManager().GetSwitcher(motan.MetricsReqApplication),
}
}

func (m *MetricsFilter) GetName() string {
Expand Down Expand Up @@ -83,6 +90,18 @@ func (m *MetricsFilter) Filter(caller motan.Caller, request motan.Request) motan
application := request.GetAttachment(protocol.MSource)
if provider {
application = caller.GetURL().GetParam(motan.ApplicationKey, "")
} else {
// to support application in caller URL
callerApplication := caller.GetURL().GetParam(motan.ApplicationKey, "")
if m.switcher.IsOpen() {
if application != callerApplication {
keys := []string{role, callerApplication, request.GetMethod()}
addMetricWithKeys(request.GetAttachment(protocol.MGroup), "", request.GetServiceName(),
keys, time.Since(start).Nanoseconds()/1e6, response)
}
} else {
application = callerApplication
}
}
keys := []string{role, application, request.GetMethod()}
addMetricWithKeys(request.GetAttachment(protocol.MGroup), "", request.GetServiceName(),
Expand Down
22 changes: 22 additions & 0 deletions filter/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ func TestMetricsFilter(t *testing.T) {
assert.Equal(t, 1, int(metrics.GetStatItem(testGroup, "", testService).SnapshotAndClear().Count(getKeysStr(test.keys)+MetricsTotalCountSuffix)), "metric count")
})
}
// test different client application
motan.GetSwitcherManager().GetSwitcher(motan.MetricsReqApplication).SetValue(true)
request3 := request.Clone().(motan.Request)
request3.SetAttachment(protocol.MSource, "test")
time.Sleep(10 * time.Millisecond)
mf.Filter(ep, request3)
time.Sleep(1000 * time.Millisecond)
snapShot := metrics.GetStatItem(testGroup, "", testService).SnapshotAndClear()
// The metrics filter has do escape
assert.Equal(t, 1, int(snapShot.Count(getKeysStr([]string{"motan-client-agent", application, testMethod})+MetricsTotalCountSuffix)), "metric count")
assert.Equal(t, 1, int(snapShot.Count(getKeysStr([]string{"motan-client-agent", "test", testMethod})+MetricsTotalCountSuffix)), "metric count")
// test switcher
motan.GetSwitcherManager().GetSwitcher(motan.MetricsReqApplication).SetValue(false)
request4 := request.Clone().(motan.Request)
time.Sleep(10 * time.Millisecond)
mf.Filter(ep, request4)
time.Sleep(1000 * time.Millisecond)
snapShot1 := metrics.GetStatItem(testGroup, "", testService).SnapshotAndClear()
// The metrics filter has do escape
assert.Equal(t, 1, int(snapShot1.Count(getKeysStr([]string{"motan-client-agent", application, testMethod})+MetricsTotalCountSuffix)), "metric count")
assert.Equal(t, 0, int(snapShot1.Count(getKeysStr([]string{"motan-client-agent", "test", testMethod})+MetricsTotalCountSuffix)), "metric count")

}

func TestAddMetric(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions permission/whitelist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package permission

var (
whitelistMap = map[string]bool{
"/version": true,
"/prometheus/metrics": true,
}
)

func InWhiteList(url string) bool {
if _, ok := whitelistMap[url]; ok {
return true
}
return false
}
Loading