我正在尝试使用 promql 包 here
package main
import (
"fmt"
"github.com/prometheus/prometheus/promql/parser"
)
func main() {
fmt.Println("Hello")
parser.ParseExpr("foobar")
}
导入时遇到问题。这是错误:
no required module provides package github.com/prometheus/prometheus/promql/parser; to add it: go get github.com/prometheus/prometheus/promql/parser (compile)
我尝试按照建议运行 go get github.com/prometheus/prometheus/promql/parser
但失败了。
go get: module github.com/prometheus/prometheus@upgrade found (v2.5.0+incompatible), but does not contain package github.com/prometheus/prometheus/promql/parser
这是我目前的 go.mod
:
module foo.com/bar/parser
go 1.17
require github.com/prometheus/prometheus v2.5.0+incompatible // indirect
最佳答案
使用go get github.com/prometheus/prometheus@83032011a5d3e6102624fe58241a374a7201fee8
(该提交是此时的最新版本,v2.33.4)
需要这样做的原因is that
This is a known issue with Go Modules. The semantic versioning of Prometheus versions the behavior of Prometheus as a server, not its code as a library. By changing the module path to v2, we would suggest that Prometheus obeys the contract of Go Modules as a library, but it doesn't, i.e. there are many breaking changes to expect even in a minor release.
and :
Prometheus was not intended to be used as a library. Now that has changed, and it is intended to be used as such, even if we do not accept all general-purpose contributions.
您看到的错误是因为 go get
默认情况下正在抓取旧版本 v2.5.0
,该版本于 2018 年发布,不包含 解析器
包。发生这种情况是因为 Prometheus 使用的版本控制方案与 Go 假定的版本控制方案不一致。
参见this issue了解更多信息。
关于go - PromQL 模块丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71289498/