Using these packages:
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
I am running minio_cluster_usage_total_bytes{job=1} via Query:
promAPI := v1.NewAPI(client)
promquery := fmt.Sprintf("minio_cluster_usage_total_bytes{job=\"1\"}")
res, _, err := promAPI.Query(context.Background(), promquery, time.Now())
if err != nil {
log.Println(err)
}
Printing the response, via log.Println(res.String()) and log.Println(res.Type()) shows this:
2024/03/05 11:16:08 minio_cluster_usage_total_bytes{instance="", job="1", server="127.0.0.1:9000"} => 18429647893 @[1709658968.29]
2024/03/05 11:16:08 vector
How can I extract just the resulting value, i.e. 18429647893 in this case? I would very much prefer to avoid string parsing functions if at all possible.
Yeah, it's a little gnarly but the documentation helps uncover the solution.
APIQuerymethod returns types that implement an interfacemodel.Valuemodel.Valueimplementing types require aTypemethod that returnsValueTypeYour code should switch over these values but, in this case it will be
ValVectorYou can type assert
restomodel.Vectorwhich is an alias for[]*Sample(whereSampleand has fields includingValueandTimestamp)In your case, I think you'll have a single
[]*Sampleentry but, for consistency, I've interated over the slice.So: