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

Use summary type to observe p99 #1875

Merged
merged 6 commits into from
May 8, 2022
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
2 changes: 2 additions & 0 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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"`
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"`
}

func (mc *MetricConfig) ToReporterConfig() *metrics.ReporterConfig {
Expand All @@ -51,6 +52,7 @@ func (mc *MetricConfig) ToReporterConfig() *metrics.ReporterConfig {
defaultMetricsReportConfig.Port = mc.Port
defaultMetricsReportConfig.Path = mc.Path
defaultMetricsReportConfig.PushGatewayAddress = mc.PushGatewayAddress
defaultMetricsReportConfig.SummaryMaxAge = mc.SummaryMaxAge
return defaultMetricsReportConfig
}

Expand Down
28 changes: 15 additions & 13 deletions metrics/prometheus/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ 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 {
reporterConfig *metrics.ReporterConfig
// report the consumer-side's rt gauge data
consumerRTGaugeVec *prometheus.GaugeVec
consumerRTSummaryVec *prometheus.SummaryVec
// report the provider-side's rt gauge data
providerRTGaugeVec *prometheus.GaugeVec
providerRTSummaryVec *prometheus.SummaryVec
// todo tps support
// report the consumer-side's tps gauge data
consumerTPSGaugeVec *prometheus.GaugeVec
Expand All @@ -102,11 +103,11 @@ type PrometheusReporter struct {
// or it will be ignored
func (reporter *PrometheusReporter) Report(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation, cost time.Duration, res protocol.Result) {
url := invoker.GetURL()
var rtVec *prometheus.GaugeVec
var rtVec *prometheus.SummaryVec
if isProvider(url) {
rtVec = reporter.providerRTGaugeVec
rtVec = reporter.providerRTSummaryVec
} else if isConsumer(url) {
rtVec = reporter.consumerRTGaugeVec
rtVec = reporter.consumerRTSummaryVec
} else {
logger.Warnf("The url belongs neither the consumer nor the provider, "+
"so the invocation will be ignored. url: %s", url.String())
Expand All @@ -121,7 +122,7 @@ func (reporter *PrometheusReporter) Report(ctx context.Context, invoker protocol
timeoutKey: url.GetParam(timeoutKey, ""),
}
costMs := cost.Nanoseconds()
rtVec.With(labels).Set(float64(costMs))
rtVec.With(labels).Observe(float64(costMs))
}

func newHistogramVec(name, namespace string, labels []string) *prometheus.HistogramVec {
Expand Down Expand Up @@ -176,7 +177,7 @@ func newSummary(name, namespace string) prometheus.Summary {

// newSummaryVec create SummaryVec, the Namespace is dubbo
// the objectives is from my experience.
func newSummaryVec(name, namespace string, labels []string) *prometheus.SummaryVec {
func newSummaryVec(name, namespace string, labels []string, maxAge int64) *prometheus.SummaryVec {
return prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Expand All @@ -189,6 +190,7 @@ func newSummaryVec(name, namespace string, labels []string) *prometheus.SummaryV
0.99: 0.001,
0.999: 0.0001,
},
MaxAge: time.Duration(maxAge),
},
labels,
)
Expand All @@ -212,12 +214,12 @@ func newPrometheusReporter(reporterConfig *metrics.ReporterConfig) metrics.Repor
if reporterInstance == nil {
reporterInitOnce.Do(func() {
reporterInstance = &PrometheusReporter{
namespace: reporterConfig.Namespace,
consumerRTGaugeVec: newGaugeVec(consumerPrefix+serviceKey+rtSuffix, reporterConfig.Namespace, labelNames),
providerRTGaugeVec: newGaugeVec(providerPrefix+serviceKey+rtSuffix, reporterConfig.Namespace, labelNames),
reporterConfig: reporterConfig,
namespace: reporterConfig.Namespace,
consumerRTSummaryVec: newSummaryVec(consumerPrefix+serviceKey+rtSuffix, reporterConfig.Namespace, labelNames, reporterConfig.SummaryMaxAge),
providerRTSummaryVec: newSummaryVec(providerPrefix+serviceKey+rtSuffix, reporterConfig.Namespace, labelNames, reporterConfig.SummaryMaxAge),
}

prom.DefaultRegisterer.MustRegister(reporterInstance.consumerRTGaugeVec, reporterInstance.providerRTGaugeVec)
prom.DefaultRegisterer.MustRegister(reporterInstance.consumerRTSummaryVec, reporterInstance.providerRTSummaryVec)
metricsExporter, err := ocprom.NewExporter(ocprom.Options{
Registry: prom.DefaultRegisterer.(*prom.Registry),
})
Expand Down Expand Up @@ -328,7 +330,7 @@ func (reporter *PrometheusReporter) incSummary(summaryName string, toSetValue fl
for k, _ := range labelMap {
keyList = append(keyList, k)
}
newSummaryVec := newSummaryVec(summaryName, reporter.namespace, keyList)
newSummaryVec := newSummaryVec(summaryName, reporter.namespace, keyList, reporter.reporterConfig.SummaryMaxAge)
_ = prom.DefaultRegisterer.Register(newSummaryVec)
reporter.userSummaryVec.Store(summaryName, newSummaryVec)
newSummaryVec.With(labelMap).Observe(toSetValue)
Expand Down
4 changes: 4 additions & 0 deletions metrics/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ import (
"dubbo.apache.org/dubbo-go/v3/protocol"
)

const DefMaxAge = 600000000000

type ReporterConfig struct {
Enable bool
Namespace string
Mode ReportMode
Port string
Path string
PushGatewayAddress string
SummaryMaxAge int64
}

type ReportMode string
Expand All @@ -50,6 +53,7 @@ func NewReporterConfig() *ReporterConfig {
Path: "/metrics",
Mode: ReportModePull,
PushGatewayAddress: "",
SummaryMaxAge: DefMaxAge,
}
}

Expand Down