This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
86 lines (70 loc) · 1.99 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
86
package main
import (
"context"
"fmt"
"log"
"net/http"
"time"
envstruct "code.cloudfoundry.org/go-envstruct"
"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
"code.cloudfoundry.org/log-cache/pkg/client"
)
func main() {
cfg := loadConfig()
httpClient := newHTTPClient(cfg)
logcache_client := client.NewClient(cfg.Addr, client.WithHTTPClient(httpClient))
visitor := func(es []*loggregator_v2.Envelope) bool {
fmt.Println("*********************Start Window********************")
defer fmt.Println("**********************End Window*********************")
for _, e := range es {
if cfg.PrintTimestamps {
fmt.Printf("%d\n", time.Unix(0, e.GetTimestamp()).Unix())
continue
}
fmt.Printf("%+v\n", e)
}
return true
}
walker := client.BuildWalker(cfg.SourceID, logcache_client.Read)
client.Window(
context.Background(),
visitor,
walker,
client.WithWindowWidth(cfg.WindowWidth),
client.WithWindowInterval(cfg.WindowInterval),
client.WithWindowStartTime(time.Unix(0, cfg.StartTime)),
)
}
type config struct {
Addr string `env:"ADDR, required"`
AuthToken string `env:"AUTH_TOKEN, required"`
SourceID string `env:"SOURCE_ID, required"`
WindowInterval time.Duration `env:"WINDOW_INTERVAL"`
WindowWidth time.Duration `env:"WINDOW_WIDTH"`
StartTime int64 `env:"START_TIME"`
PrintTimestamps bool `env:"PRINT_TIMESTAMP"`
}
func loadConfig() config {
c := config{
WindowWidth: time.Hour,
WindowInterval: time.Minute,
}
if err := envstruct.Load(&c); err != nil {
log.Fatal(err)
}
if c.StartTime == 0 {
c.StartTime = time.Now().Add(-c.WindowWidth).UnixNano()
}
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)
}