common icon indicating copy to clipboard operation
common copied to clipboard

How to use model.Value?

Open JackGzj opened this issue 4 years ago • 2 comments

I am using the Go client for Prometheus, but I wonder how to parse the response data in model.Value (including Vector and Matrix). I have checked the source code, only to find the String() method to get the Vector like {pod="test-001-deployment-896b6d55b-bs586"} => 0.06435052290435223 @[1598949467.625] {pod="test-003-deployment-665d48fb7f-dlxfb"} => 0.03620828100637146 @[1598949467.625]. So, how can I use the model data gracefully?

JackGzj avatar Sep 01 '20 08:09 JackGzj

It makes more sense to ask questions like this on the prometheus-users mailing list rather than in a GitHub issue. On the mailing list, more people are available to potentially respond to your question, and the whole community can benefit from the answers provided.

brian-brazil avatar Sep 01 '20 08:09 brian-brazil

For anyone else stumbling upon this issue, model.Value is an interface.

If you're in the same situation that I am in, and you're trying to mock an API call from Prometheus that returns model.Value, you can use something like this:

model.Value(
    model.Vector{
        ...
    }
)


You're asking the Go compiler to treat this concrete type as the interface it implements.

Another example here

isabelgiang avatar Sep 20 '21 20:09 isabelgiang