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

Revert "Dynamic update config for logger level & metric enable" #2199

Merged
merged 1 commit into from
Feb 1, 2023
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: 0 additions & 8 deletions config/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
13 changes: 0 additions & 13 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package config

import (
"github.com/creasty/defaults"
"github.com/dubbogo/gost/log/logger"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -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())
}
}
}
6 changes: 0 additions & 6 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
88 changes: 26 additions & 62 deletions metrics/prometheus/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
}