Skip to content

Commit

Permalink
Update global metric config to match with change in main
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Oct 11, 2023
1 parent ac275cd commit 51c1aab
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 32 deletions.
58 changes: 50 additions & 8 deletions compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}
2 changes: 1 addition & 1 deletion config/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
128 changes: 106 additions & 22 deletions global/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,146 @@ 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 {
// return a new config without setting any field means there is not any default value for initialization
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
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 51c1aab

Please sign in to comment.