-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
57 lines (48 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// The purpose of this example is to show how to stream the contents of a JSON
// logfile and gzip them on the fly.
package main
import (
"context"
"log"
"os"
"github.com/axiomhq/axiom-go/axiom"
"github.com/axiomhq/axiom-go/axiom/ingest"
)
func main() {
// Export "AXIOM_DATASET" in addition to the required environment variables.
dataset := os.Getenv("AXIOM_DATASET")
if dataset == "" {
log.Fatal("AXIOM_DATASET is required")
}
// 1. Open the file to ingest.
f, err := os.Open("logs.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// 2. Wrap it in a gzip enabled reader.
encoder := axiom.GzipEncoder()
r, err := encoder(f)
if err != nil {
log.Fatal(err)
}
// 3. Initialize the Axiom API client.
client, err := axiom.NewClient()
if err != nil {
log.Fatal(err)
}
// 4. Ingest ⚡
// Note the JSON content type and Gzip content encoding being set because
// the client does not auto sense them.
res, err := client.Ingest(context.Background(), dataset, r, axiom.JSON, axiom.Gzip,
// Set a custom timestamp field (default used by the server is "_time").
ingest.SetTimestampField("timestamp"),
)
if err != nil {
log.Fatal(err)
}
// 5. Make sure everything went smoothly.
for _, fail := range res.Failures {
log.Print(fail.Error)
}
}