Skip to content

Commit 94cebb3

Browse files
authored
feat: add backpressure, body size limit and metrics to pushgw proxy remote write (#3141)
1 parent 910793a commit 94cebb3

3 files changed

Lines changed: 251 additions & 86 deletions

File tree

pushgw/pconf/conf.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,27 @@ type Pushgw struct {
2222
UpdateTargetByUrlConcurrency int
2323

2424
GetHeartbeatFromMetric bool // 是否从时序数据中提取机器心跳时间,默认 false
25-
BusiGroupLabelKey string
26-
IdentMetrics []string
27-
IdentStatsThreshold int
28-
IdentDropThreshold int // 每分钟单个 ident 的样本数超过该阈值,则丢弃
29-
WriteConcurrency int
30-
LabelRewrite bool
31-
ForceUseServerTS bool
32-
DebugSample map[string]string
33-
DropSample []map[string]string
34-
WriterOpt WriterGlobalOpt
35-
Writers []WriterOptions
36-
KafkaWriters []KafkaWriterOptions
25+
BusiGroupLabelKey string
26+
IdentMetrics []string
27+
IdentStatsThreshold int
28+
IdentDropThreshold int // 每分钟单个 ident 的样本数超过该阈值,则丢弃
29+
WriteConcurrency int
30+
31+
// ProxyInflightMax 控制 /proxy/v1/write 的并发上限。超过阈值直接返回 429,
32+
// 把背压交给客户端 WAL(remote_write 协议原生支持)。<=0 使用默认值。
33+
ProxyInflightMax int
34+
35+
// ProxyMaxBodyBytes 限制 /proxy/v1/write 单个请求 body 的最大字节数,超过返回 413。
36+
// 和 ProxyInflightMax 配套:并发 × 单请求大小 = pushgw 内存占用上限。<=0 使用默认值。
37+
ProxyMaxBodyBytes int64
38+
39+
LabelRewrite bool
40+
ForceUseServerTS bool
41+
DebugSample map[string]string
42+
DropSample []map[string]string
43+
WriterOpt WriterGlobalOpt
44+
Writers []WriterOptions
45+
KafkaWriters []KafkaWriterOptions
3746
}
3847

3948
type WriterGlobalOpt struct {
@@ -190,6 +199,14 @@ func (p *Pushgw) PreCheck() {
190199
p.IdentDropThreshold = 5000000
191200
}
192201

202+
if p.ProxyInflightMax <= 0 {
203+
p.ProxyInflightMax = 1000
204+
}
205+
206+
if p.ProxyMaxBodyBytes <= 0 {
207+
p.ProxyMaxBodyBytes = 32 * 1024 * 1024
208+
}
209+
193210
for index := range p.Writers {
194211
for _, relabel := range p.Writers[index].WriteRelabels {
195212
if relabel.Regex == "" {

pushgw/pstat/pstat.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,58 @@ var (
105105
},
106106
[]string{"operation", "status"},
107107
)
108+
109+
GaugeProxyRemoteWriteInflight = prometheus.NewGauge(prometheus.GaugeOpts{
110+
Namespace: namespace,
111+
Subsystem: subsystem,
112+
Name: "proxy_remote_write_inflight",
113+
Help: "Current number of in-flight requests on /proxy/v1/write.",
114+
})
115+
116+
CounterProxyRemoteWriteOverLimitTotal = prometheus.NewCounter(prometheus.CounterOpts{
117+
Namespace: namespace,
118+
Subsystem: subsystem,
119+
Name: "proxy_remote_write_over_limit_total",
120+
Help: "Number of /proxy/v1/write requests rejected with 429 due to in-flight over limit.",
121+
})
122+
123+
CounterProxyRemoteWriteBodyTooLargeTotal = prometheus.NewCounter(prometheus.CounterOpts{
124+
Namespace: namespace,
125+
Subsystem: subsystem,
126+
Name: "proxy_remote_write_body_too_large_total",
127+
Help: "Number of /proxy/v1/write requests rejected with 413 due to body size over limit.",
128+
})
129+
130+
CounterProxyRemoteWriteTotal = prometheus.NewCounter(prometheus.CounterOpts{
131+
Namespace: namespace,
132+
Subsystem: subsystem,
133+
Name: "proxy_remote_write_total",
134+
Help: "Number of /proxy/v1/write requests received.",
135+
})
136+
137+
CounterProxyForwardTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
138+
Namespace: namespace,
139+
Subsystem: subsystem,
140+
Name: "proxy_forward_total",
141+
Help: "Number of forwards performed by /proxy/v1/write.",
142+
}, []string{"url"})
143+
144+
CounterProxyForwardErrorTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
145+
Namespace: namespace,
146+
Subsystem: subsystem,
147+
Name: "proxy_forward_error_total",
148+
Help: "Number of forward errors on /proxy/v1/write.",
149+
}, []string{"url", "reason"})
150+
151+
ProxyForwardDuration = prometheus.NewHistogramVec(
152+
prometheus.HistogramOpts{
153+
Namespace: namespace,
154+
Subsystem: subsystem,
155+
Buckets: []float64{.001, .01, .1, 1, 5, 10},
156+
Name: "proxy_forward_duration_seconds",
157+
Help: "Forward latencies on /proxy/v1/write in seconds.",
158+
}, []string{"url"},
159+
)
108160
)
109161

110162
func init() {
@@ -121,5 +173,12 @@ func init() {
121173
GaugeSampleQueueSize,
122174
CounterPushQueueOverLimitTotal,
123175
RedisOperationLatency,
176+
GaugeProxyRemoteWriteInflight,
177+
CounterProxyRemoteWriteOverLimitTotal,
178+
CounterProxyRemoteWriteBodyTooLargeTotal,
179+
CounterProxyRemoteWriteTotal,
180+
CounterProxyForwardTotal,
181+
CounterProxyForwardErrorTotal,
182+
ProxyForwardDuration,
124183
)
125184
}

0 commit comments

Comments
 (0)