Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Created May 30, 2024 19:01
Show Gist options
  • Save kavirajk/abb9bdeefc58d8ceb3907d5b66c01a06 to your computer and use it in GitHub Desktop.
Save kavirajk/abb9bdeefc58d8ceb3907d5b66c01a06 to your computer and use it in GitHub Desktop.
Ingest old logs into Loki
package main
import (
"fmt"
"io"
"net/http"
"strings"
"time"
)
// ingest log lines with older timestamps
// payload
const (
payload = `{
"streams": [
{
"stream": {
"job": "varlogs",
"service_name": "varlogs",
"filename": "test_file3.log"
},
"values": [
[ "%d", "very long line xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ],
[ "%d", "super long line yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" ]
]
}
]
}
`
)
const (
url = "http://localhost:3100/loki/api/v1/push"
method = "POST"
)
var (
bitOld = time.Now().Add(-2 * time.Hour)
)
func main() {
client := http.DefaultClient
t := bitOld
for i := 0; i < 1000; i++ {
t = t.Add(time.Second)
ts := t.UnixNano()
p := fmt.Sprintf(payload, ts, ts)
resp, err := client.Post(url, "application/json", strings.NewReader(p))
if err != nil {
panic(err)
}
if resp.StatusCode/100 != 2 {
b, err := io.ReadAll(resp.Body)
if err != nil {
panic("error reading response body")
}
fmt.Println("status code", resp.StatusCode)
panic(string(b))
}
}
fmt.Println("success")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment