diff --git a/compat.go b/compat.go index 19202bfeef..d2e138c967 100644 --- a/compat.go +++ b/compat.go @@ -273,14 +273,12 @@ func compatMetricConfig(c *global.MetricConfig) *config.MetricConfig { return nil } return &config.MetricConfig{ - Mode: c.Mode, - Namespace: c.Namespace, - Enable: c.Enable, - Port: c.Port, - Path: c.Path, - PushGatewayAddress: c.PushGatewayAddress, - SummaryMaxAge: c.SummaryMaxAge, - Protocol: c.Protocol, + Enable: c.Enable, + Port: c.Port, + Path: c.Path, + Prometheus: compatMetricPrometheusConfig(c.Prometheus), + Aggregation: compatMetricAggregationConfig(c.Aggregation), + Protocol: c.Protocol, } } @@ -369,3 +367,47 @@ func compatTLSConfig(c *global.TLSConfig) *config.TLSConfig { TLSServerName: c.TLSServerName, } } + +func compatMetricAggregationConfig(a *global.AggregateConfig) *config.AggregateConfig { + if a == nil { + return nil + } + return &config.AggregateConfig{ + Enabled: a.Enabled, + BucketNum: a.BucketNum, + TimeWindowSeconds: a.TimeWindowSeconds, + } +} + +func compatMetricPrometheusConfig(c *global.PrometheusConfig) *config.PrometheusConfig { + if c == nil { + return nil + } + return &config.PrometheusConfig{ + Exporter: compatMetricPrometheusExporter(c.Exporter), + Pushgateway: compatMetricPrometheusGateway(c.Pushgateway), + } +} + +func compatMetricPrometheusExporter(e *global.Exporter) *config.Exporter { + if e == nil { + return nil + } + return &config.Exporter{ + Enabled: e.Enabled, + } +} + +func compatMetricPrometheusGateway(g *global.PushgatewayConfig) *config.PushgatewayConfig { + if g == nil { + return nil + } + return &config.PushgatewayConfig{ + Enabled: g.Enabled, + BaseUrl: g.BaseUrl, + Job: g.Job, + Username: g.Username, + Password: g.Password, + PushInterval: g.PushInterval, + } +} diff --git a/config/application_config.go b/config/application_config.go index 60ea32f1fa..5bbdbdd3eb 100644 --- a/config/application_config.go +++ b/config/application_config.go @@ -30,7 +30,7 @@ import ( // ApplicationConfig is a configuration for current applicationConfig, whether the applicationConfig is a provider or a consumer type ApplicationConfig struct { Organization string `default:"dubbo-go" yaml:"organization" json:"organization,omitempty" property:"organization"` - Name string `default:"dubbo.io" yaml:"name" json:"name,omitempty" property:"name"` + Name string `yaml:"name" json:"name,omitempty" property:"name"` Module string `default:"sample" yaml:"module" json:"module,omitempty" property:"module"` Group string `yaml:"group" json:"group,omitempty" property:"module"` Version string `yaml:"version" json:"version,omitempty" property:"version"` diff --git a/global/metric_config.go b/global/metric_config.go index 17df20aa9b..ce6124c346 100644 --- a/global/metric_config.go +++ b/global/metric_config.go @@ -19,14 +19,36 @@ package global // MetricConfig This is the config struct for all metrics implementation type MetricConfig struct { - 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 *bool `default:"false" 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"` - SummaryMaxAge int64 `default:"600000000000" yaml:"summary-max-age" json:"summary-max-age,omitempty" property:"summary-max-age"` - Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` + Enable *bool `default:"false" 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"` + Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` + Prometheus *PrometheusConfig `yaml:"prometheus" json:"prometheus" property:"prometheus"` + Aggregation *AggregateConfig `yaml:"aggregation" json:"aggregation" property:"aggregation"` +} + +type AggregateConfig struct { + Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"` + BucketNum int `default:"10" yaml:"bucket-num" json:"bucket-num,omitempty" property:"bucket-num"` + TimeWindowSeconds int `default:"120" yaml:"time-window-seconds" json:"time-window-seconds,omitempty" property:"time-window-seconds"` +} + +type PrometheusConfig struct { + Exporter *Exporter `yaml:"exporter" json:"exporter,omitempty" property:"exporter"` + Pushgateway *PushgatewayConfig `yaml:"pushgateway" json:"pushgateway,omitempty" property:"pushgateway"` +} + +type Exporter struct { + Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"` +} + +type PushgatewayConfig struct { + Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"` + BaseUrl string `default:"" yaml:"base-url" json:"base-url,omitempty" property:"base-url"` + Job string `default:"default_dubbo_job" yaml:"job" json:"job,omitempty" property:"job"` + Username string `default:"" yaml:"username" json:"username,omitempty" property:"username"` + Password string `default:"" yaml:"password" json:"password,omitempty" property:"password"` + PushInterval int `default:"30" yaml:"push-interval" json:"push-interval,omitempty" property:"push-interval"` } func DefaultMetricConfig() *MetricConfig { @@ -34,47 +56,109 @@ func DefaultMetricConfig() *MetricConfig { return &MetricConfig{} } +func defaultPrometheusConfig() *PrometheusConfig { + return &PrometheusConfig{Exporter: &Exporter{}, Pushgateway: &PushgatewayConfig{}} +} + type MetricOption func(*MetricConfig) -func WithMetric_Mode(mode string) MetricOption { +func WithMetric_AggregateEnabled() MetricOption { return func(cfg *MetricConfig) { - cfg.Mode = mode + if cfg.Aggregation == nil { + cfg.Aggregation = &AggregateConfig{} + } + enabled := true + cfg.Aggregation.Enabled = &enabled } } -func WithMetric_Namespace(namespace string) MetricOption { +func WithMetric_AggregateBucketNum(num int) MetricOption { return func(cfg *MetricConfig) { - cfg.Namespace = namespace + if cfg.Aggregation == nil { + cfg.Aggregation = &AggregateConfig{} + } + cfg.Aggregation.BucketNum = num } } -func WithMetric_Enable(enable bool) MetricOption { +func WithMetric_AggregateTimeWindowSeconds(seconds int) MetricOption { return func(cfg *MetricConfig) { - cfg.Enable = &enable + if cfg.Aggregation == nil { + cfg.Aggregation = &AggregateConfig{} + } + cfg.Aggregation.TimeWindowSeconds = seconds } } -func WithMetric_Port(port string) MetricOption { +func WithMetric_PrometheusEnabled() MetricOption { return func(cfg *MetricConfig) { - cfg.Port = port + if cfg.Prometheus == nil { + cfg.Prometheus.Exporter = &Exporter{} + } + enabled := true + cfg.Prometheus.Exporter.Enabled = &enabled } } -func WithMetric_Path(path string) MetricOption { +func WithMetric_PrometheusGatewayUrl(url string) MetricOption { return func(cfg *MetricConfig) { - cfg.Path = path + if cfg.Prometheus == nil { + cfg.Prometheus = defaultPrometheusConfig() + } + cfg.Prometheus.Pushgateway.BaseUrl = url } } -func WithMetric_PushGatewayAddress(address string) MetricOption { +func WithMetric_PrometheusGatewayJob(job string) MetricOption { return func(cfg *MetricConfig) { - cfg.PushGatewayAddress = address + if cfg.Prometheus == nil { + cfg.Prometheus = defaultPrometheusConfig() + } + cfg.Prometheus.Pushgateway.Job = job } } -func WithMetric_SummaryMaxAge(age int64) MetricOption { +func WithMetric_PrometheusGatewayUsername(username string) MetricOption { return func(cfg *MetricConfig) { - cfg.SummaryMaxAge = age + if cfg.Prometheus == nil { + cfg.Prometheus = defaultPrometheusConfig() + } + cfg.Prometheus.Pushgateway.Username = username + } +} + +func WithMetric_PrometheusGatewayPassword(password string) MetricOption { + return func(cfg *MetricConfig) { + if cfg.Prometheus == nil { + cfg.Prometheus = defaultPrometheusConfig() + } + cfg.Prometheus.Pushgateway.Password = password + } +} +func WithMetric_PrometheusGatewayInterval(interval int) MetricOption { + return func(cfg *MetricConfig) { + if cfg.Prometheus == nil { + cfg.Prometheus = defaultPrometheusConfig() + } + cfg.Prometheus.Pushgateway.PushInterval = interval + } +} + +func WithMetric_Enable(enable bool) MetricOption { + return func(cfg *MetricConfig) { + cfg.Enable = &enable + } +} + +func WithMetric_Port(port string) MetricOption { + return func(cfg *MetricConfig) { + cfg.Port = port + } +} + +func WithMetric_Path(path string) MetricOption { + return func(cfg *MetricConfig) { + cfg.Path = path } } diff --git a/go.mod b/go.mod index 6c534697d1..e64da08013 100644 --- a/go.mod +++ b/go.mod @@ -70,6 +70,7 @@ require ( go.uber.org/atomic v1.10.0 go.uber.org/multierr v1.8.0 // indirect go.uber.org/zap v1.21.0 + golang.org/x/net v0.8.0 golang.org/x/oauth2 v0.6.0 // indirect google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef google.golang.org/grpc v1.52.0 diff --git a/options.go b/options.go index 51e9b2df0e..b471732029 100644 --- a/options.go +++ b/options.go @@ -126,7 +126,7 @@ func (rc *InstanceOptions) init(opts ...InstanceOption) error { if err := rcCompat.MetadataReport.Init(rcCompat); err != nil { return err } - if err := rcCompat.Metric.Init(); err != nil { + if err := rcCompat.Metric.Init(rcCompat); err != nil { return err } for _, t := range rcCompat.Tracing {