This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 314b09d5e refactor: simplify configuration when enable metrics (#2408)
314b09d5e is described below

commit 314b09d5edb8d21ada1de0a4cd59f84b56b85d71
Author: Wang Guan <[email protected]>
AuthorDate: Mon Sep 4 14:04:53 2023 +0800

    refactor: simplify configuration when enable metrics (#2408)
---
 common/constant/default.go    | 2 +-
 config/reference_config.go    | 7 +++++++
 config/service_config.go      | 7 +++++++
 config/service_config_test.go | 2 +-
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/common/constant/default.go b/common/constant/default.go
index 8f5ca5584..a27da2f4d 100644
--- a/common/constant/default.go
+++ b/common/constant/default.go
@@ -59,7 +59,7 @@ const (
        // DefaultServiceFilters defines default service filters, it is highly 
recommended
        // that put the AdaptiveServiceProviderFilterKey at the end.
        DefaultServiceFilters = EchoFilterKey + "," +
-               MetricsFilterKey + "," + TokenFilterKey + "," + 
AccessLogFilterKey + "," + TpsLimitFilterKey + "," +
+               TokenFilterKey + "," + AccessLogFilterKey + "," + 
TpsLimitFilterKey + "," +
                GenericServiceFilterKey + "," + ExecuteLimitFilterKey + "," + 
GracefulShutdownProviderFilterKey
 
        DefaultReferenceFilters = GracefulShutdownConsumerFilterKey
diff --git a/config/reference_config.go b/config/reference_config.go
index 06797a8ec..64d7c53a3 100644
--- a/config/reference_config.go
+++ b/config/reference_config.go
@@ -74,6 +74,7 @@ type ReferenceConfig struct {
        TracingKey       string `yaml:"tracing-key" 
json:"tracing-key,omitempty" propertiy:"tracing-key"`
        rootConfig       *RootConfig
        metaDataType     string
+       metricsEnable    bool
        MeshProviderPort int `yaml:"mesh-provider-port" 
json:"mesh-provider-port,omitempty" propertiy:"mesh-provider-port"`
 }
 
@@ -118,6 +119,9 @@ func (rc *ReferenceConfig) Init(root *RootConfig) error {
        if rc.TracingKey == "" {
                rc.TracingKey = root.Consumer.TracingKey
        }
+       if root.Metric.Enable != nil {
+               rc.metricsEnable = *root.Metric.Enable
+       }
        if rc.Check == nil {
                rc.Check = &root.Consumer.Check
        }
@@ -355,6 +359,9 @@ func (rc *ReferenceConfig) getURLMap() url.Values {
        if rc.Generic != "" {
                defaultReferenceFilter = constant.GenericFilterKey + "," + 
defaultReferenceFilter
        }
+       if rc.metricsEnable {
+               defaultReferenceFilter += fmt.Sprintf(",%s", 
constant.MetricsFilterKey)
+       }
        urlMap.Set(constant.ReferenceFilterKey, mergeValue(rc.Filter, "", 
defaultReferenceFilter))
 
        for _, v := range rc.Methods {
diff --git a/config/service_config.go b/config/service_config.go
index 976071a2b..1b0038ed0 100644
--- a/config/service_config.go
+++ b/config/service_config.go
@@ -82,6 +82,7 @@ type ServiceConfig struct {
        RCRegistriesMap map[string]*RegistryConfig
        ProxyFactoryKey string
        adaptiveService bool
+       metricsEnable   bool // whether append metrics filter to filter chain
        unexported      *atomic.Bool
        exported        *atomic.Bool
        export          bool // a flag to control whether the current service 
should export or not
@@ -144,6 +145,9 @@ func (s *ServiceConfig) Init(rc *RootConfig) error {
        if s.TracingKey == "" {
                s.TracingKey = rc.Provider.TracingKey
        }
+       if rc.Metric.Enable != nil {
+               s.metricsEnable = *rc.Metric.Enable
+       }
        err := s.check()
        if err != nil {
                panic(err)
@@ -427,6 +431,9 @@ func (s *ServiceConfig) getUrlMap() url.Values {
        if s.adaptiveService {
                filters += fmt.Sprintf(",%s", 
constant.AdaptiveServiceProviderFilterKey)
        }
+       if s.metricsEnable {
+               filters += fmt.Sprintf(",%s", constant.MetricsFilterKey)
+       }
        urlMap.Set(constant.ServiceFilterKey, filters)
 
        // filter special config
diff --git a/config/service_config_test.go b/config/service_config_test.go
index da6b5af0d..4f5954acf 100644
--- a/config/service_config_test.go
+++ b/config/service_config_test.go
@@ -114,7 +114,7 @@ func TestNewServiceConfigBuilder(t *testing.T) {
                values := serviceConfig.getUrlMap()
                assert.Equal(t, values.Get("methods.Say.weight"), "0")
                assert.Equal(t, values.Get("methods.Say.tps.limit.rate"), "")
-               assert.Equal(t, values.Get(constant.ServiceFilterKey), 
"echo,metrics,token,accesslog,tps,generic_service,execute,pshutdown")
+               assert.Equal(t, values.Get(constant.ServiceFilterKey), 
"echo,token,accesslog,tps,generic_service,execute,pshutdown")
        })
 
        t.Run("Implement", func(t *testing.T) {

Reply via email to