Skip to content

Commit

Permalink
feat: request succeed total
Browse files Browse the repository at this point in the history
  • Loading branch information
ev1lQuark committed Jun 8, 2023
1 parent 4efa162 commit a2d2b98
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions metrics/prometheus/after_invocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ func (reporter *PrometheusReporter) ReportAfterInvocation(ctx context.Context, i
reporter.reportRTSummaryVec(role, &labels, cost.Milliseconds())
reporter.reportRequestsTotalCounterVec(role, &labels)
reporter.decRequestsProcessingGaugeVec(role, &labels)

if res != nil && res.Error() == nil {
// succeed
reporter.incRequestsSucceedTotalCounterVec(role, &labels)
}
}
4 changes: 4 additions & 0 deletions metrics/prometheus/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
)

var (
defaultHistogramBucket = []float64{10, 50, 100, 200, 500, 1000, 10000}
)

func buildLabels(url *common.URL) prometheus.Labels {
return prometheus.Labels{
applicationNameKey: url.GetParam(constant.ApplicationKey, ""),
Expand Down
1 change: 1 addition & 0 deletions metrics/prometheus/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ const (

totalField = "total"
processingField = "processing"
succeedField = "succeed"
)
8 changes: 8 additions & 0 deletions metrics/prometheus/metric_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ type metricSet struct {
providerRequestsTotalCounterVec *prometheus.CounterVec
// report the provider-side's processing request counter data
providerRequestsProcessingGaugeVec *prometheus.GaugeVec
// The number of requests successfully received by the provider
providerRequestsSucceedTotalCounterVec *prometheus.CounterVec

// report the consumer-side's request total counter data
consumerRequestsTotalCounterVec *prometheus.CounterVec
// report the consumer-side's processing request counter data
consumerRequestsProcessingGaugeVec *prometheus.GaugeVec
// The number of successful requests sent by consumers
consumerRequestsSucceedTotalCounterVec *prometheus.CounterVec
}

var labelNames = []string{applicationNameKey, groupKey, hostnameKey, interfaceKey, ipKey, methodKey, versionKey}
Expand All @@ -57,6 +61,8 @@ func (ms *metricSet) initAndRegister(reporterConfig *metrics.ReporterConfig) {
ms.providerRequestsTotalCounterVec = newCounterVec(buildMetricsName(providerField, requestsField, totalField), reporterConfig.Namespace, labelNames)
ms.consumerRequestsProcessingGaugeVec = newGaugeVec(buildMetricsName(consumerField, requestsField, processingField), reporterConfig.Namespace, labelNames)
ms.providerRequestsProcessingGaugeVec = newGaugeVec(buildMetricsName(providerField, requestsField, processingField), reporterConfig.Namespace, labelNames)
ms.consumerRequestsSucceedTotalCounterVec = newCounterVec(buildMetricsName(consumerField, requestsField, succeedField, totalField), reporterConfig.Namespace, labelNames)
ms.providerRequestsSucceedTotalCounterVec = newCounterVec(buildMetricsName(providerField, requestsField, succeedField, totalField), reporterConfig.Namespace, labelNames)

prometheus.DefaultRegisterer.MustRegister(
ms.consumerRTSummaryVec,
Expand All @@ -65,6 +71,8 @@ func (ms *metricSet) initAndRegister(reporterConfig *metrics.ReporterConfig) {
ms.providerRequestsTotalCounterVec,
ms.consumerRequestsProcessingGaugeVec,
ms.providerRequestsProcessingGaugeVec,
ms.consumerRequestsSucceedTotalCounterVec,
ms.providerRequestsSucceedTotalCounterVec,
)
}

Expand Down
14 changes: 11 additions & 3 deletions metrics/prometheus/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ import (
)

var (
reporterInstance *PrometheusReporter
reporterInitOnce sync.Once
defaultHistogramBucket = []float64{10, 50, 100, 200, 500, 1000, 10000}
reporterInstance *PrometheusReporter
reporterInitOnce sync.Once
)

// should initialize after loading configuration
Expand Down Expand Up @@ -147,3 +146,12 @@ func (reporter *PrometheusReporter) decRequestsProcessingGaugeVec(role string, l
reporter.consumerRequestsProcessingGaugeVec.With(*labels).Dec()
}
}

func (reporter *PrometheusReporter) incRequestsSucceedTotalCounterVec(role string, labels *prometheus.Labels) {
switch role {
case providerField:
reporter.providerRequestsSucceedTotalCounterVec.With(*labels).Inc()
case consumerField:
reporter.consumerRequestsSucceedTotalCounterVec.With(*labels).Inc()
}
}
3 changes: 3 additions & 0 deletions metrics/prometheus/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestPrometheusReporter_Report(t *testing.T) {

assert.False(t, isConsumer(url))
ctx := context.Background()
reporter.ReportBeforeInvocation(ctx, invoker, inv)
reporter.ReportAfterInvocation(ctx, invoker, inv, 100*time.Millisecond, nil)

// consumer side
Expand All @@ -60,6 +61,7 @@ func TestPrometheusReporter_Report(t *testing.T) {
"BDTService&organization=ikurento.com&owner=ZX&registry.role=0&retries=&" +
"service.filter=echo%2Ctoken%2Caccesslog&timestamp=1569153406&token=934804bf-b007-4174-94eb-96e3e1d60cc7&version=&warmup=100")
invoker = protocol.NewBaseInvoker(url)
reporter.ReportBeforeInvocation(ctx, invoker, inv)
reporter.ReportAfterInvocation(ctx, invoker, inv, 100*time.Millisecond, nil)

// invalid role
Expand All @@ -70,5 +72,6 @@ func TestPrometheusReporter_Report(t *testing.T) {
"BDTService&organization=ikurento.com&owner=ZX&registry.role=9&retries=&" +
"service.filter=echo%2Ctoken%2Caccesslog&timestamp=1569153406&token=934804bf-b007-4174-94eb-96e3e1d60cc7&version=&warmup=100")
invoker = protocol.NewBaseInvoker(url)
reporter.ReportBeforeInvocation(ctx, invoker, inv)
reporter.ReportAfterInvocation(ctx, invoker, inv, 100*time.Millisecond, nil)
}

0 comments on commit a2d2b98

Please sign in to comment.