Skip to content

Commit

Permalink
refactor: metrics config (#2465)
Browse files Browse the repository at this point in the history
  • Loading branch information
ev1lQuark authored Oct 30, 2023
1 parent bbe0bff commit 3d97d4c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
23 changes: 9 additions & 14 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ type MetricConfig struct {
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"`
EnableMetadata *bool `default:"true" yaml:"enable-metadata" json:"enable-metadata,omitempty" property:"enable-metadata"`
EnableRegistry *bool `default:"true" yaml:"enable-registry" json:"enable-registry,omitempty" property:"enable-registry"`
EnableConfigCenter *bool `default:"true" yaml:"enable-config-center" json:"enable-config-center,omitempty" property:"enable-config-center"`
EnableRpc *bool `default:"true" yaml:"enable-rpc" json:"enable-rpc,omitempty" property:"enable-rpc"`
EnableMetadata *bool `default:"false" yaml:"enable-metadata" json:"enable-metadata,omitempty" property:"enable-metadata"`
EnableRegistry *bool `default:"false" yaml:"enable-registry" json:"enable-registry,omitempty" property:"enable-registry"`
EnableConfigCenter *bool `default:"false" yaml:"enable-config-center" json:"enable-config-center,omitempty" property:"enable-config-center"`
Prometheus *PrometheusConfig `yaml:"prometheus" json:"prometheus" property:"prometheus"`
Aggregation *AggregateConfig `yaml:"aggregation" json:"aggregation" property:"aggregation"`
rootConfig *RootConfig
Expand All @@ -60,7 +59,7 @@ type PrometheusConfig struct {
}

type Exporter struct {
Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"`
Enabled *bool `default:"true" yaml:"enabled" json:"enabled,omitempty" property:"enabled"`
}

type PushgatewayConfig struct {
Expand Down Expand Up @@ -93,7 +92,9 @@ func (mc *MetricConfig) Init(rc *RootConfig) error {
return err
}
mc.rootConfig = rc
metrics.Init(mc.toURL())
if *mc.Enable {
metrics.Init(mc.toURL())
}
return nil
}

Expand All @@ -120,11 +121,6 @@ func (mcb *MetricConfigBuilder) SetConfigCenterEnabled(enabled bool) *MetricConf
return mcb
}

func (mcb *MetricConfigBuilder) SetRpcEnabled(enabled bool) *MetricConfigBuilder {
mcb.metricConfig.EnableRpc = &enabled
return mcb
}

func (mcb *MetricConfigBuilder) Build() *MetricConfig {
return mcb.metricConfig
}
Expand All @@ -137,15 +133,14 @@ func (mc *MetricConfig) DynamicUpdateProperties(newMetricConfig *MetricConfig) {
// prometheus://localhost:9090?&histogram.enabled=false&prometheus.exporter.enabled=false
func (mc *MetricConfig) toURL() *common.URL {
url, _ := common.NewURL("localhost", common.WithProtocol(mc.Protocol))
url.SetParam(constant.PrometheusExporterEnabledKey, strconv.FormatBool(*mc.Enable)) // for compatibility
url.SetParam(constant.PrometheusExporterMetricsPortKey, mc.Port)
url.SetParam(constant.PrometheusExporterMetricsPathKey, mc.Path)
url.SetParam(constant.ApplicationKey, mc.rootConfig.Application.Name)
url.SetParam(constant.AppVersionKey, mc.rootConfig.Application.Version)
url.SetParam(constant.RpcEnabledKey, strconv.FormatBool(*mc.Enable))
url.SetParam(constant.MetadataEnabledKey, strconv.FormatBool(*mc.EnableMetadata))
url.SetParam(constant.RegistryEnabledKey, strconv.FormatBool(*mc.EnableRegistry))
url.SetParam(constant.ConfigCenterEnabledKey, strconv.FormatBool(*mc.EnableConfigCenter))
url.SetParam(constant.RpcEnabledKey, strconv.FormatBool(*mc.EnableRpc))
if mc.Aggregation != nil {
url.SetParam(constant.AggregationEnabledKey, strconv.FormatBool(*mc.Aggregation.Enabled))
url.SetParam(constant.AggregationBucketNumKey, strconv.Itoa(mc.Aggregation.BucketNum))
Expand All @@ -154,7 +149,7 @@ func (mc *MetricConfig) toURL() *common.URL {
if mc.Prometheus != nil {
if mc.Prometheus.Exporter != nil {
exporter := mc.Prometheus.Exporter
url.SetParam(constant.PrometheusExporterEnabledKey, strconv.FormatBool(*exporter.Enabled || *mc.Enable)) // for compatibility
url.SetParam(constant.PrometheusExporterEnabledKey, strconv.FormatBool(*exporter.Enabled))
}
if mc.Prometheus.Pushgateway != nil {
pushGateWay := mc.Prometheus.Pushgateway
Expand Down
2 changes: 0 additions & 2 deletions config/metric_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ func TestMetricConfigBuilder(t *testing.T) {
SetConfigCenterEnabled(false).
SetMetadataEnabled(false).
SetRegistryEnabled(false).
SetRpcEnabled(false).
Build()
enable := false
assert.Equal(t, &MetricConfig{
EnableConfigCenter: &enable,
EnableMetadata: &enable,
EnableRegistry: &enable,
EnableRpc: &enable,
}, config)
}

0 comments on commit 3d97d4c

Please sign in to comment.