Skip to content

Commit 89ef54d

Browse files
authored
Add metric counter for queued requests in throttling control behavior (alibaba#431)
* Add `flow_wait_total` counter
1 parent 683c229 commit 89ef54d

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

core/flow/slot.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package flow
1717
import (
1818
"github.com/alibaba/sentinel-golang/core/base"
1919
"github.com/alibaba/sentinel-golang/core/stat"
20+
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
2021
"github.com/alibaba/sentinel-golang/logging"
2122
"github.com/alibaba/sentinel-golang/util"
2223
"github.com/pkg/errors"
@@ -27,9 +28,17 @@ const (
2728
)
2829

2930
var (
30-
DefaultSlot = &Slot{}
31+
DefaultSlot = &Slot{}
32+
flowWaitCount = metric_exporter.NewCounter(
33+
"flow_wait_total",
34+
"Flow wait count",
35+
[]string{"resource"})
3136
)
3237

38+
func init() {
39+
metric_exporter.Register(flowWaitCount)
40+
}
41+
3342
type Slot struct {
3443
}
3544

@@ -58,6 +67,7 @@ func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
5867
}
5968
if r.Status() == base.ResultStatusShouldWait {
6069
if nanosToWait := r.NanosToWait(); nanosToWait > 0 {
70+
flowWaitCount.Add(float64(ctx.Input.BatchCount), ctx.Resource.Name())
6171
// Handle waiting action.
6272
util.Sleep(nanosToWait)
6373
}

exporter/metric/empty_exporter.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package metric
2+
3+
import "net/http"
4+
5+
type EmptyExporter struct {
6+
}
7+
8+
func (e *EmptyExporter) HTTPHandler() http.Handler {
9+
return nil
10+
}
11+
12+
type emptyMetric struct {
13+
}
14+
15+
func (e emptyMetric) Register() error {
16+
return nil
17+
}
18+
19+
func (e emptyMetric) Unregister() bool {
20+
return false
21+
}
22+
23+
func (e emptyMetric) Reset() {
24+
return
25+
}
26+
27+
func newEmptyExporter() *EmptyExporter {
28+
return &EmptyExporter{}
29+
}
30+
31+
type emptyCounter struct {
32+
emptyMetric
33+
}
34+
35+
func (e emptyCounter) Add(value float64, labelValues ...string) {
36+
return
37+
}
38+
39+
func (e *EmptyExporter) NewCounter(name, desc string, labelNames []string) Counter {
40+
return emptyCounter{}
41+
}
42+
43+
type emptyGauge struct {
44+
emptyMetric
45+
}
46+
47+
func (e emptyGauge) Set(value float64, labelValues ...string) {
48+
return
49+
}
50+
51+
func (e *EmptyExporter) NewGauge(name, desc string, labelNames []string) Gauge {
52+
return &emptyGauge{}
53+
}
54+
55+
type emptyHistogram struct {
56+
emptyMetric
57+
}
58+
59+
func (e emptyHistogram) Observe(value float64, labelValues ...string) {
60+
return
61+
}
62+
63+
func (e *EmptyExporter) NewHistogram(name, desc string, buckets []float64, labelNames []string) Histogram {
64+
return &emptyHistogram{}
65+
}

exporter/metric/exporter.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ var (
3333
)
3434

3535
func init() {
36-
exporter = newPrometheusExporter()
36+
if config.MetricExportHTTPAddr() != "" {
37+
exporter = newPrometheusExporter()
38+
} else {
39+
exporter = newEmptyExporter()
40+
}
3741

3842
host, _ = os.Hostname()
3943
if host == "" {

0 commit comments

Comments
 (0)