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

fix: add metrics basic support #1540

Merged
merged 4 commits into from
Oct 27, 2021
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
8 changes: 4 additions & 4 deletions common/extension/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ import (

// we couldn't store the instance because the some instance may initialize before loading configuration
// so lazy initialization will be better.
var metricReporterMap = make(map[string]func() metrics.Reporter, 4)
var metricReporterMap = make(map[string]func(config *metrics.ReporterConfig) metrics.Reporter, 4)

// SetMetricReporter sets a reporter with the @name
func SetMetricReporter(name string, reporterFunc func() metrics.Reporter) {
func SetMetricReporter(name string, reporterFunc func(config *metrics.ReporterConfig) metrics.Reporter) {
metricReporterMap[name] = reporterFunc
}

// GetMetricReporter finds the reporter with @name.
// if not found, it will panic.
// we should know that this method usually is called when system starts, so we should panic
func GetMetricReporter(name string) metrics.Reporter {
func GetMetricReporter(name string, config *metrics.ReporterConfig) metrics.Reporter {
reporterFunc, found := metricReporterMap[name]
if !found {
panic("Cannot find the reporter with name: " + name)
}
return reporterFunc()
return reporterFunc(config)
}
4 changes: 2 additions & 2 deletions common/extension/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ import (
func TestGetMetricReporter(t *testing.T) {
reporter := &mockReporter{}
name := "mock"
SetMetricReporter(name, func() metrics.Reporter {
SetMetricReporter(name, func(config *metrics.ReporterConfig) metrics.Reporter {
return reporter
})
res := GetMetricReporter(name)
res := GetMetricReporter(name, metrics.NewReporterConfig())
assert.Equal(t, reporter, res)
}

Expand Down
2 changes: 1 addition & 1 deletion config/consumer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
type ConsumerConfig struct {
Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
// support string
RegistryIDs []string `yaml:"registryIDs" json:"registryIDs,omitempty" property:"registryIDs"`
RegistryIDs []string `yaml:"registry-ids" json:"registry-ids,omitempty" property:"registry-ids"`

RequestTimeout string `default:"3s" yaml:"request-timeout" json:"request-timeout,omitempty" property:"request-timeout"`
ProxyFactory string `default:"default" yaml:"proxy" json:"proxy,omitempty" property:"proxy"`
Expand Down
88 changes: 42 additions & 46 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,56 @@

package config

var defaultHistogramBucket = []float64{10, 50, 100, 200, 500, 1000, 10000}
import (
"github.com/creasty/defaults"

"github.com/pkg/errors"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/metrics"
)

// MetricConfig This is the config struct for all metrics implementation
type MetricConfig struct {
Reporters []string `yaml:"reporters" json:"reporters,omitempty"`
// TODO s?
HistogramBucket []float64 `yaml:"histogram_bucket" json:"histogram_bucket,omitempty"`
Mode string `default:"pull" yaml:"mode" json:"mode,omitempty" property:"mode"` // push or pull,
Namespace string `default:"dubbo" yaml:"namespace" json:"namespace,omitempty" property:"namespace"`
Enable string `default:"true" yaml:"enable" json:"enable,omitempty" property:"enable"`
Port string `default:"9090" yaml:"port" json:"port,omitempty" property:"port"`
Path string `default:"/metrics" yaml:"path" json:"path,omitempty" property:"path"`
PushGatewayAddress string `default:"" yaml:"push-gateway-address" json:"push-gateway-address,omitempty" property:"push-gateway-address"`
}

// nolint
func (mc *MetricConfig) Init() error {
return nil
func (m *MetricConfig) ToReporterConfig() *metrics.ReporterConfig {
defaultMetricsReportConfig := metrics.NewReporterConfig()
if m.Mode == metrics.ReportModePush {
defaultMetricsReportConfig.Mode = metrics.ReportModePush
}
if m.Namespace != "" {
defaultMetricsReportConfig.Namespace = m.Namespace
}

defaultMetricsReportConfig.Enable = m.Enable == "1"
defaultMetricsReportConfig.Port = m.Port
defaultMetricsReportConfig.Path = m.Path
defaultMetricsReportConfig.PushGatewayAddress = m.PushGatewayAddress
return defaultMetricsReportConfig
}

// GetHistogramBucket find the histogram bucket
// if it's empty, the default value will be return
func (mc *MetricConfig) GetHistogramBucket() []float64 {
if len(mc.HistogramBucket) == 0 {
mc.HistogramBucket = defaultHistogramBucket
// nolint
func (mc *MetricConfig) Init() error {
if mc == nil {
return errors.New("metrics config is null")
}
if err := defaults.Set(mc); err != nil {
return err
}
return mc.HistogramBucket
if err := verify(mc); err != nil {
return err
}
extension.GetMetricReporter("prometheus", mc.ToReporterConfig())
return nil
}

type MetricConfigBuilder struct {
Expand All @@ -49,40 +78,7 @@ func NewMetricConfigBuilder() *MetricConfigBuilder {
return &MetricConfigBuilder{metricConfig: &MetricConfig{}}
}

// nolint
func (mcb *MetricConfigBuilder) SetReporters(reporters []string) *MetricConfigBuilder {
mcb.metricConfig.Reporters = reporters
return mcb
}

// nolint
func (mcb *MetricConfigBuilder) AddReporter(reporter string) *MetricConfigBuilder {
if mcb.metricConfig.Reporters == nil {
mcb.metricConfig.Reporters = make([]string, 0)
}
mcb.metricConfig.Reporters = append(mcb.metricConfig.Reporters, reporter)
return mcb
}

// nolint
func (mcb *MetricConfigBuilder) SetHistogramBucket(histogramBucket []float64) *MetricConfigBuilder {
mcb.metricConfig.HistogramBucket = histogramBucket
return mcb
}

// nolint
func (mcb *MetricConfigBuilder) AddBucket(bucket float64) *MetricConfigBuilder {
if mcb.metricConfig.HistogramBucket == nil {
mcb.metricConfig.HistogramBucket = make([]float64, 0)
}
mcb.metricConfig.HistogramBucket = append(mcb.metricConfig.HistogramBucket, bucket)
return mcb
}

// nolint
func (mcb *MetricConfigBuilder) Build() *MetricConfig {
if err := mcb.metricConfig.Init(); err != nil {
panic(err)
}
return mcb.metricConfig
}
2 changes: 1 addition & 1 deletion config/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ProviderConfig struct {
// Deprecated Register whether registration is required
Register bool `yaml:"register" json:"register" property:"register"`
// RegistryIDs is registry ids list
RegistryIDs []string `yaml:"registryIDs" json:"registryIDs" property:"registryIDs"`
RegistryIDs []string `yaml:"registry-ids" json:"registry-ids" property:"registry-ids"`
// Services services
Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"`

Expand Down
2 changes: 1 addition & 1 deletion config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type ReferenceConfig struct {
URL string `yaml:"url" json:"url,omitempty" property:"url"`
Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
Protocol string `default:"dubbo" yaml:"protocol" json:"protocol,omitempty" property:"protocol"`
RegistryIDs []string `yaml:"registryIDs" json:"registryIDs,omitempty" property:"registryIDs"`
RegistryIDs []string `yaml:"registry-ids" json:"registry-ids,omitempty" property:"registry-ids"`
Cluster string `yaml:"cluster" json:"cluster,omitempty" property:"cluster"`
Loadbalance string `yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"`
Retries string `yaml:"retries" json:"retries,omitempty" property:"retries"`
Expand Down
1 change: 1 addition & 0 deletions config/router_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
import (
_ "dubbo.apache.org/dubbo-go/v3/cluster/router/chain"
"dubbo.apache.org/dubbo-go/v3/common/constant"
_ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus"
)

// RouterConfig is the configuration of the router.
Expand Down
4 changes: 2 additions & 2 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ import (
type ServiceConfig struct {
id string
Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
ProtocolIDs []string `default:"[\"dubbo\"]" validate:"required" yaml:"protocolIDs" json:"protocolIDs,omitempty" property:"protocolIDs"` // multi protocolIDs support, split by ','
ProtocolIDs []string `default:"[\"dubbo\"]" validate:"required" yaml:"protocol-ids" json:"protocol-ids,omitempty" property:"protocol-ids"` // multi protocolIDs support, split by ','
Interface string `validate:"required" yaml:"interface" json:"interface,omitempty" property:"interface"`
RegistryIDs []string `yaml:"registryIDs" json:"registryIDs,omitempty" property:"registryIDs"`
RegistryIDs []string `yaml:"registry-ids" json:"registry-ids,omitempty" property:"registry-ids"`
Cluster string `default:"failover" yaml:"cluster" json:"cluster,omitempty" property:"cluster"`
Loadbalance string `default:"random" yaml:"loadbalance" json:"loadbalance,omitempty" property:"loadbalance"`
Group string `yaml:"group" json:"group,omitempty" property:"group"`
Expand Down
4 changes: 2 additions & 2 deletions config/testdata/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ dubbo:
services:
helloService:
interface: org.dubbo.service.HelloService
registryIDs: nacos,zk
registry-ids: nacos,zk
orderService:
interface: org.dubbo.service.OrderService
registryIDs: nacos
registry-ids: nacos
provider:
register: true
services:
2 changes: 1 addition & 1 deletion config/testdata/config/app/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dubbo:
interface: org.github.dubbo.HelloService # must be compatible with grpc or dubbo-java
provider:
register: true
registryIDs: nacos
registry-ids: nacos
services:
helloService:
protocol: dubbo
Expand Down
4 changes: 2 additions & 2 deletions config/testdata/config/provider/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ dubbo:
address: nacos://127.0.0.1:8848
provider:
register: true
registryIDs:
registry-ids:
- nacos
- zk
services:
helloService:
interface: org.dubbo.service.HelloService
registryIDs: nacos,zk
registry-ids: nacos,zk
orderService:
interface: org.dubbo.service.OrderService
4 changes: 2 additions & 2 deletions config/testdata/config/provider/registry_application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ dubbo:
timeout: 3s
address: naocs://127.0.0.1:8848
provider:
registryIDs: nacos
registry-ids: nacos
services:
HelloService:
interface: org.dubbo.service.HelloService
registryIDs: nacos,zk
registry-ids: nacos,zk
OrderService:
interface: org.dubbo.service.OrderService
2 changes: 1 addition & 1 deletion config/testdata/consumer_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ registries :

references:
"UserProvider":
registryIDs: "hangzhouzk,shanghaizk"
registry-ids: "hangzhouzk,shanghaizk"
filter: ""
protocol : "dubbo"
version: "1.0"
Expand Down
2 changes: 1 addition & 1 deletion config/testdata/consumer_config_with_configcenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ config_center:
address: "127.0.0.1"
references:
"UserProvider":
registryIDs: "hangzhouzk,shanghaizk"
registry-ids: "hangzhouzk,shanghaizk"
filter: ""
protocol : "dubbo"
interface : "com.ikurento.user.UserProvider"
Expand Down
2 changes: 1 addition & 1 deletion config/testdata/consumer_config_withoutProtocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ registries :

references:
"UserProvider":
registryIDs: "hangzhouzk,shanghaizk"
registry-ids: "hangzhouzk,shanghaizk"
filter: ""
version: "1.0"
group: "as"
Expand Down
2 changes: 1 addition & 1 deletion config/testdata/provider_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ registries :

services:
"UserProvider":
registryIDs: "hangzhouzk,shanghaizk"
registry-ids: "hangzhouzk,shanghaizk"
filter: ""
# the name of limiter
tps.limiter: "default"
Expand Down
2 changes: 1 addition & 1 deletion config/testdata/provider_config_withoutProtocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ registries :

services:
"UserProvider":
registryIDs: "hangzhouzk,shanghaizk"
registry-ids: "hangzhouzk,shanghaizk"
filter: ""
# equivalent to interface of dubbo.xml
interface : "com.ikurento.user.UserProvider"
Expand Down
1 change: 1 addition & 0 deletions config_center/apollo/mockDubbogo.yaml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"appId":"testApplication_yang","cluster":"default","namespaceName":"mockDubbogo.yaml","releaseKey":"20191104105242-0f13805d89f834a4","configurations":{"registries.hangzhouzk.username":"11111"}}
2 changes: 1 addition & 1 deletion filter/metrics/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (p *Filter) OnResponse(ctx context.Context, res protocol.Result, invoker pr
func newFilter() filter.Filter {
if metricFilterInstance == nil {
reporters := make([]metrics.Reporter, 0, 1)
reporters = append(reporters, extension.GetMetricReporter("prometheus"))
reporters = append(reporters, extension.GetMetricReporter("prometheus", metrics.NewReporterConfig()))
metricFilterInstance = &Filter{
reporters: reporters,
}
Expand Down
4 changes: 1 addition & 3 deletions filter/metrics/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/metrics"
_ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus"
"dubbo.apache.org/dubbo-go/v3/protocol"
Expand All @@ -41,9 +40,8 @@ import (

func TestMetricsFilterInvoke(t *testing.T) {
// prepare the mock reporter
config.GetMetricConfig().Reporters = []string{"mock"}
mk := &mockReporter{}
extension.SetMetricReporter("mock", func() metrics.Reporter {
extension.SetMetricReporter("mock", func(config *metrics.ReporterConfig) metrics.Reporter {
return mk
})

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module dubbo.apache.org/dubbo-go/v3
go 1.15

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.0
github.com/RoaringBitmap/roaring v0.7.1
github.com/Workiva/go-datastructures v1.0.52
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
Expand All @@ -19,7 +20,6 @@ require (
github.com/go-co-op/gocron v1.9.0
github.com/go-playground/validator/v10 v10.9.0
github.com/go-resty/resty/v2 v2.3.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.4.4
github.com/golang/protobuf v1.5.2
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
Expand All @@ -34,7 +34,6 @@ require (
github.com/opentracing/opentracing-go v1.2.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.28.0 // indirect
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b
github.com/stretchr/testify v1.7.0
github.com/zouyx/agollo/v3 v3.4.5
Expand Down
Loading