Created
December 28, 2022 06:44
-
-
Save motoki317/d4128453ed024bb83000ed191272197d to your computer and use it in GitHub Desktop.
Prometheus pushgateway helper script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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