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

Prometheus No compression #189

Closed
wants to merge 1 commit into from
Closed

Conversation

dingdayu
Copy link

@dingdayu dingdayu commented Apr 7, 2020

起因于在暴露 Prometheus 采集端点时错误响应的问题。

经追查,因 prometheus/client_golangServeHTTP() 中会检查响应头(Accept-Encoding) 实现压缩,这样在启用gzip中间件后,会出现重复压缩。

现象: Prometheus 无法正常采集暴露信息,浏览器访问会直接下载。

以下三种方案:

  1. 添加 gzip 白名单(此 PR 变更)
  2. /metrics 注册在 gzip 前面,避免压缩
  3. 禁用 prometheus/client_golang 压缩(见下)

禁用 prometheus/client_golang` 压缩

// Prometheus 监控访问点
func Prometheus(c *gin.Context) {
	// register promhttp.HandlerOpts DisableCompression
	promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
		DisableCompression: true,
	})).ServeHTTP(c.Writer, c.Request)
	//promhttp.Handler().ServeHTTP(c.Writer, c.Request)
}

我是更推荐使用 方案三 但提交一个 PR 以作记录,方便遇到同样问题的朋友能够搜索到。

起因于在暴露 Prometheus 采集端点时错误响应的问题。

经追查,因 `prometheus/client_golang` 在 `ServeHTTP()` 中会检查响应头(`Accept-Encoding`) 实现压缩,这样在启用`gzip`中间件后,会出现重复压缩。

现象: Prometheus 无法正常采集暴露信息,浏览器访问会直接下载。

以下三种方案: 
1. 添加 `gzip` 白名单(此 PR 变更)
2. 将 `/metrics` 注册在 `gzip` 前面,避免压缩
3. 禁用 `prometheus/client_golang` 压缩(见下)

#### 禁用 prometheus/client_golang` 压缩

```go
// Prometheus 监控访问点
func Prometheus(c *gin.Context) {
func Prometheus(c *gin.Context) {
	// register promhttp.HandlerOpts DisableCompression
	promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
		DisableCompression: true,
	})).ServeHTTP(c.Writer, c.Request)
	//promhttp.Handler().ServeHTTP(c.Writer, c.Request)
}
```
@dingdayu
Copy link
Author

dingdayu commented Apr 7, 2020

dingdayu added a commit to dingdayu/godemo that referenced this pull request Apr 7, 2020
@appleboy
Copy link
Member

appleboy commented Aug 9, 2020

We move discussion to https://github.com/gin-contrib/gzip

Copy link
Member

@appleboy appleboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@appleboy appleboy closed this Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants