缺少 PromQL 模块

PromQL Module Missing

我正在尝试使用 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 年发布,不包含 parser 包。发生这种情况是因为 Prometheus 使用的版本控制方案与 Go 假设的不一致。

有关其他信息,请参阅 this issue