From a645070c6b3a9a29b21a73c1f10cefe20d7ace5f Mon Sep 17 00:00:00 2001 From: Xuewei Niu Date: Wed, 1 Feb 2023 14:16:01 +0800 Subject: [PATCH] Revert "Dynamic update config for logger level & metric enable (#2180)" (#2199) This reverts commit 4c6a99bee53e64e6d883673d2f7f463b42b5c42b. --- config/logger_config.go | 8 ---- config/metric_config.go | 13 ----- config/root_config.go | 6 --- metrics/prometheus/reporter.go | 88 ++++++++++------------------------ 4 files changed, 26 insertions(+), 89 deletions(-) diff --git a/config/logger_config.go b/config/logger_config.go index 872c444c6f..5a499b9cba 100644 --- a/config/logger_config.go +++ b/config/logger_config.go @@ -170,11 +170,3 @@ func (lcb *LoggerConfigBuilder) SetZapConfig(zapConfig ZapConfig) *LoggerConfigB func (lcb *LoggerConfigBuilder) Build() *LoggerConfig { return lcb.loggerConfig } - -// DynamicUpdateProperties dynamically update properties. -func (lc *LoggerConfig) DynamicUpdateProperties(newLoggerConfig *LoggerConfig) { - if newLoggerConfig != nil && lc.ZapConfig.Level != newLoggerConfig.ZapConfig.Level { - lc.ZapConfig.Level = newLoggerConfig.ZapConfig.Level - logger.Infof("LoggerConfig's ZapConfig Level was dynamically updated, new value:%v", lc.ZapConfig.Level) - } -} diff --git a/config/metric_config.go b/config/metric_config.go index 509907007f..8b8a04415a 100644 --- a/config/metric_config.go +++ b/config/metric_config.go @@ -19,7 +19,6 @@ package config import ( "github.com/creasty/defaults" - "github.com/dubbogo/gost/log/logger" "github.com/pkg/errors" ) @@ -82,15 +81,3 @@ func NewMetricConfigBuilder() *MetricConfigBuilder { func (mcb *MetricConfigBuilder) Build() *MetricConfig { return mcb.metricConfig } - -// DynamicUpdateProperties dynamically update properties. -func (mc *MetricConfig) DynamicUpdateProperties(newMetricConfig *MetricConfig) { - if newMetricConfig != nil { - if newMetricConfig.Enable != mc.Enable { - mc.Enable = newMetricConfig.Enable - logger.Infof("MetricConfig's Enable was dynamically updated, new value:%v", mc.Enable) - - extension.GetMetricReporter("prometheus", mc.ToReporterConfig()) - } - } -} diff --git a/config/root_config.go b/config/root_config.go index ee0387575e..88cd00e363 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -395,10 +395,4 @@ func (rc *RootConfig) Process(event *config_center.ConfigChangeEvent) { } // dynamically update consumer rc.Consumer.DynamicUpdateProperties(updateRootConfig.Consumer) - - // dynamically update logger - rc.Logger.DynamicUpdateProperties(updateRootConfig.Logger) - - // dynamically update metric - rc.Metric.DynamicUpdateProperties(updateRootConfig.Metric) } diff --git a/metrics/prometheus/reporter.go b/metrics/prometheus/reporter.go index 666670312c..9777c740e4 100644 --- a/metrics/prometheus/reporter.go +++ b/metrics/prometheus/reporter.go @@ -78,7 +78,6 @@ func init() { // if you want to use this feature, you need to initialize your prometheus. // https://prometheus.io/docs/guides/go-application/ type PrometheusReporter struct { - reporterServer *http.Server reporterConfig *metrics.ReporterConfig // report the consumer-side's rt gauge data consumerRTSummaryVec *prometheus.SummaryVec @@ -104,10 +103,6 @@ type PrometheusReporter struct { // the role in url must be consumer or provider // or it will be ignored func (reporter *PrometheusReporter) Report(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation, cost time.Duration, res protocol.Result) { - if !reporter.reporterConfig.Enable { - return - } - url := invoker.GetURL() var rtVec *prometheus.SummaryVec if isProvider(url) { @@ -225,20 +220,29 @@ func newPrometheusReporter(reporterConfig *metrics.ReporterConfig) metrics.Repor consumerRTSummaryVec: newSummaryVec(consumerPrefix+serviceKey+rtSuffix, reporterConfig.Namespace, labelNames, reporterConfig.SummaryMaxAge), providerRTSummaryVec: newSummaryVec(providerPrefix+serviceKey+rtSuffix, reporterConfig.Namespace, labelNames, reporterConfig.SummaryMaxAge), } - prom.DefaultRegisterer.MustRegister(reporterInstance.consumerRTSummaryVec, reporterInstance.providerRTSummaryVec) - }) - } + metricsExporter, err := ocprom.NewExporter(ocprom.Options{ + Registry: prom.DefaultRegisterer.(*prom.Registry), + }) + if err != nil { + logger.Errorf("new prometheus reporter with error = %s", err) + return + } - if reporterConfig.Enable { - if reporterConfig.Mode == metrics.ReportModePull { - go reporterInstance.startupServer(reporterConfig) - } - // todo pushgateway support - } else { - reporterInstance.shutdownServer() + if reporterConfig.Enable { + if reporterConfig.Mode == metrics.ReportModePull { + go func() { + mux := http.NewServeMux() + mux.Handle(reporterConfig.Path, metricsExporter) + if err := http.ListenAndServe(":"+reporterConfig.Port, mux); err != nil { + logger.Warnf("new prometheus reporter with error = %s", err) + } + }() + } + // todo pushgateway support + } + }) } - return reporterInstance } @@ -373,65 +377,25 @@ func (reporter *PrometheusReporter) incSummary(summaryName string, toSetValue fl } func SetGaugeWithLabel(gaugeName string, val float64, label prometheus.Labels) { - if reporterInstance.reporterConfig.Enable { - reporterInstance.setGauge(gaugeName, val, label) - } + reporterInstance.setGauge(gaugeName, val, label) } func SetGauge(gaugeName string, val float64) { - if reporterInstance.reporterConfig.Enable { - reporterInstance.setGauge(gaugeName, val, make(prometheus.Labels)) - } + reporterInstance.setGauge(gaugeName, val, make(prometheus.Labels)) } func IncCounterWithLabel(counterName string, label prometheus.Labels) { - if reporterInstance.reporterConfig.Enable { - reporterInstance.incCounter(counterName, label) - } + reporterInstance.incCounter(counterName, label) } func IncCounter(summaryName string) { - if reporterInstance.reporterConfig.Enable { - reporterInstance.incCounter(summaryName, make(prometheus.Labels)) - } + reporterInstance.incCounter(summaryName, make(prometheus.Labels)) } func IncSummaryWithLabel(counterName string, val float64, label prometheus.Labels) { - if reporterInstance.reporterConfig.Enable { - reporterInstance.incSummary(counterName, val, label) - } + reporterInstance.incSummary(counterName, val, label) } func IncSummary(summaryName string, val float64) { - if reporterInstance.reporterConfig.Enable { - reporterInstance.incSummary(summaryName, val, make(prometheus.Labels)) - } -} - -func (reporter *PrometheusReporter) startupServer(reporterConfig *metrics.ReporterConfig) { - metricsExporter, err := ocprom.NewExporter(ocprom.Options{ - Registry: prom.DefaultRegisterer.(*prom.Registry), - }) - if err != nil { - logger.Errorf("new prometheus reporter with error = %s", err) - return - } - - // start server - mux := http.NewServeMux() - mux.Handle(reporterConfig.Path, metricsExporter) - reporterInstance.reporterServer = &http.Server{Addr: ":" + reporterConfig.Port, Handler: mux} - if err := reporterInstance.reporterServer.ListenAndServe(); err != nil { - logger.Warnf("new prometheus reporter with error = %s", err) - } -} - -func (reporter *PrometheusReporter) shutdownServer() { - if reporterInstance.reporterServer != nil { - err := reporterInstance.reporterServer.Shutdown(context.Background()) - if err != nil { - logger.Errorf("shutdown prometheus reporter with error = %s, prometheus reporter close now", err) - reporterInstance.reporterServer.Close() - } - } + reporterInstance.incSummary(summaryName, val, make(prometheus.Labels)) }