Skip to content

Instantly share code, notes, and snippets.

@rikatz
Created July 29, 2022 13:19
Show Gist options
  • Save rikatz/345335708bea7babd298e9ee3b39fd4f to your computer and use it in GitHub Desktop.
Save rikatz/345335708bea7babd298e9ee3b39fd4f to your computer and use it in GitHub Desktop.
package main
import (
"context"
"encoding/base64"
"flag"
"fmt"
"log"
"math/rand"
"time"
promremote "github.com/castai/promwrite"
)
var (
prometheusRemoteHost string
prometheusRemoteUsername string
prometheusRemotePassword string
)
func main() {
flag.StringVar(&prometheusRemoteHost, "prometheus-host", "", "Prometheus Remote write endpoint")
flag.StringVar(&prometheusRemoteUsername, "prometheus-username", "29596", "Username as in Grafana Cloud")
flag.StringVar(&prometheusRemotePassword, "prometheus-password", "", "Password as in Grafana Cloud")
rand.Seed(time.Now().UnixMilli())
flag.Parse()
if prometheusRemoteHost == "" || prometheusRemotePassword == "" || prometheusRemoteUsername == "" {
log.Fatal("Invalid options")
}
ctx := context.Background()
cli := promremote.NewClient(prometheusRemoteHost)
headers := make(map[string]string)
userPass := prometheusRemoteUsername + ":" + prometheusRemotePassword
encodedPass := base64.StdEncoding.EncodeToString([]byte(userPass))
headers["Authorization"] = fmt.Sprintf("Basic %s", encodedPass)
authorizationHeader := promremote.WriteHeaders(headers)
metric := promremote.WriteRequest{
TimeSeries: []promremote.TimeSeries{
{
Labels: []promremote.Label{
{
Name: "__name__",
Value: "ricardo_metric",
},
},
Sample: promremote.Sample{
Time: time.Now(),
Value: rand.Float64() * 100,
},
},
},
}
_, err := cli.Write(ctx, &metric, authorizationHeader)
if err != nil {
log.Fatalf("error: %s", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment