Skip to content

Instantly share code, notes, and snippets.

@gouthamve
Created December 23, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gouthamve/464e3f4e3b930fa33198c87a7b2dfd56 to your computer and use it in GitHub Desktop.
Save gouthamve/464e3f4e3b930fa33198c87a7b2dfd56 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"flag"
"fmt"
"net/url"
"time"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/value"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/storage/remote"
)
var (
cortexEndpoint string
tenantID string
password string
query string
duration time.Duration
)
func main() {
flag.StringVar(&cortexEndpoint, "cortex.endpoint", "http://localhost:8080/api/prom/api/v1/read", "The endpoint of Cortex.")
flag.StringVar(&tenantID, "cortex.tenant-id", "", "What tenant ID to set.")
flag.StringVar(&password, "cortex.tenant-password", "", "The password to set for access.")
flag.StringVar(&query, "cortex.selector", "up{}", "The PromQL selector.")
flag.DurationVar(&duration, "cortex.duration", time.Hour, "The duration to download.")
flag.Parse()
u, err := url.Parse(cortexEndpoint)
checkError(err)
client, err := remote.NewReadClient("oooooooh-la-la", &remote.ClientConfig{
URL: &config_util.URL{
URL: u,
},
Timeout: model.Duration(time.Minute),
HTTPClientConfig: config_util.HTTPClientConfig{
BasicAuth: &config_util.BasicAuth{
Username: tenantID,
Password: config_util.Secret(password),
},
},
})
checkError(err)
matchers, err := parser.ParseMetricSelector(query)
checkError(err)
pbQuery, err := remote.ToQuery(time.Now().Add(-duration).Unix()*1000, time.Now().Unix()*1000, matchers, nil)
checkError(err)
resp, err := client.Read(context.Background(), pbQuery)
checkError(err)
for _, sample := range resp.Timeseries[0].Samples {
fmt.Println(sample.GetTimestamp(), sample.GetValue(), value.IsStaleNaN(sample.GetValue()))
}
return
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment