/
main.go
85 lines (67 loc) · 1.64 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
client "code.cloudfoundry.org/go-log-cache/v2"
envstruct "code.cloudfoundry.org/go-envstruct"
"code.cloudfoundry.org/go-loggregator/v9/rpc/loggregator_v2"
)
func main() {
log := log.New(os.Stderr, "", 0)
cfg := loadConfig()
httpClient := newHTTPClient(cfg)
logcache_client := client.NewClient(cfg.Addr, client.WithHTTPClient(httpClient))
visitor := func(es []*loggregator_v2.Envelope) bool {
for _, e := range es {
fmt.Printf("%+v\n", e)
}
return true
}
now := time.Now()
opts := []client.WalkOption{
client.WithWalkBackoff(client.NewAlwaysRetryBackoff(time.Second)),
client.WithWalkLogger(log),
}
if cfg.Duration != 0 {
log.Printf("Have duration (%v). Walking finite window...", cfg.Duration)
opts = append(opts,
client.WithWalkStartTime(now.Add(-cfg.Duration)),
client.WithWalkEndTime(now),
)
}
client.Walk(
context.Background(),
cfg.SourceID,
visitor,
logcache_client.Read,
opts...,
)
}
type config struct {
Addr string `env:"ADDR, required"`
AuthToken string `env:"AUTH_TOKEN, required"`
SourceID string `env:"SOURCE_ID, required"`
Duration time.Duration `env:"DURATION"`
}
func loadConfig() config {
c := config{}
if err := envstruct.Load(&c); err != nil {
log.Fatal(err)
}
return c
}
type HTTPClient struct {
cfg config
client *http.Client
}
func newHTTPClient(c config) *HTTPClient {
return &HTTPClient{cfg: c, client: http.DefaultClient}
}
func (h *HTTPClient) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", h.cfg.AuthToken)
return h.client.Do(req)
}