Home > Enterprise >  PromQL Module Missing
PromQL Module Missing

Time:02-28

I am trying to use the promql package here

package main

import (
    "fmt"    
    "github.com/prometheus/prometheus/promql/parser"
)

func main() {
    fmt.Println("Hello")
    parser.ParseExpr("foobar")    
}

Having trouble importing. This is the error:

no required module provides package github.com/prometheus/prometheus/promql/parser; to add it:
    go get github.com/prometheus/prometheus/promql/parser (compile)

I tried to run go get github.com/prometheus/prometheus/promql/parser as suggested but it fails.

go get: module github.com/prometheus/prometheus@upgrade found (v2.5.0 incompatible), but does not contain package github.com/prometheus/prometheus/promql/parser

Here is my go.mod currently:

module foo.com/bar/parser

go 1.17

require github.com/prometheus/prometheus v2.5.0 incompatible // indirect

CodePudding user response:

Use go get github.com/prometheus/prometheus@83032011a5d3e6102624fe58241a374a7201fee8 (that commit is the latest release at this point in time, v2.33.4)

The reason this is needed 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.

The error you are seeing is because go get is grabbing an old release v2.5.0 by default which was released back in 2018 and does not include the parser package. This happens because the versioning scheme used by Prometheus does not align with that assumed by Go.

See this issue for additional info.

  • Related