go - PromQL 模块丢失

标签 go promql go-modules

我正在尝试使用 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/

相关文章:

go - 获取未初始化 slice 的类型

go - 如何在 Gin 中对路线进行分组?

pointers - 何时使用指针

prometheus - 从系列结果 PromQL 中省略标签

alert - PromQL:查询警报是否静音

go - 如何为 Go-Micro 服务修复端口

Prometheus:如何使用probe_http_duration_seconds从黑盒导出器创建SLI/SLO?

go - 构建命令行参数 : cannot load local package: cannot find module providing package

go - 如何使用带有环境变量或相对路径的替换指令

Go Modules 无法识别 GOPATH 下的文件