Skip to content

Instantly share code, notes, and snippets.

@motoki317
Created December 28, 2022 06:44
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 motoki317/d4128453ed024bb83000ed191272197d to your computer and use it in GitHub Desktop.
Save motoki317/d4128453ed024bb83000ed191272197d to your computer and use it in GitHub Desktop.
Prometheus pushgateway helper script
package main
import (
"flag"
"io"
"log"
"net/http"
"time"
)
var (
source = flag.String("s", "", "Source URL")
dest = flag.String("d", "", "Destination URL")
interval = flag.Int("i", 10000, "Interval in milliseconds")
repeat = flag.Int("r", 6, "Number of times to repeat")
)
func must[T any](res T, err error) T {
if err != nil {
panic(err)
}
return res
}
func main() {
flag.Parse()
log.SetFlags(log.Lmicroseconds)
if *source == "" {
panic("source cannot be empty")
}
if *dest == "" {
panic("destination cannot be empty")
}
if *interval <= 0 {
panic("interval cannot be 0 or negative")
}
if *repeat <= 0 {
panic("repeat cannot be 0 or negative")
}
start := time.Now()
c := http.DefaultClient
for i := 0; i < *repeat; i++ {
source := must(c.Do(must(http.NewRequest("GET", *source, nil))))
destReq := must(c.Do(must(http.NewRequest("POST", *dest, source.Body))))
resp := must(io.ReadAll(destReq.Body))
log.Println(string(resp))
_ = source.Body.Close()
_ = destReq.Body.Close()
if i == *repeat-1 {
break
}
next := start.Add(time.Duration(*interval) * time.Millisecond * time.Duration(i+1))
time.Sleep(next.Sub(time.Now()))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment