-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
94 lines (84 loc) · 3.96 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
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/ViaQ/cluster-logging-load-client/internal"
"github.com/ViaQ/cluster-logging-load-client/internal/generator"
"github.com/ViaQ/cluster-logging-load-client/internal/querier"
)
var (
opts = internal.Options{}
logLevel string
)
func init() {
pflag.StringVar(&logLevel, "log-level", "error", "Overwrite to control the level of logs emitted. Allowed values: debug, info, warning, error")
pflag.StringVar(&opts.Command, "command", "generate", "Overwrite to control if logs are generated or queried. Allowed values: generate, query.")
pflag.StringVar(&opts.Destination, "destination", "stdout", "Overwrite to control where logs are queried or written to. Allowed values: loki, elasticsearch, stdout, file.")
pflag.StringVar(&opts.OutputFile, "file", "output.txt", "The name of the file to write logs to. Only available for \"File\" destinations.")
pflag.StringVar(&opts.ClientURL, "url", "", "URL of Promtail, LogCLI, or Elasticsearch client.")
pflag.BoolVar(&opts.DisableSecurityCheck, "disable-security-check", false, "Disable security check in HTTPS client.")
pflag.IntVar(&opts.LogsPerSecond, "logs-per-second", 1, "The rate to generate logs. This rate may not always be achievable.")
pflag.StringVar(&opts.LogType, "log-type", "simple", "Overwrite to control the type of logs generated. Allowed values: application, audit, simple, synthetic.")
pflag.StringVar(&opts.LogFormat, "log-format", "default", "Overwrite to control the format of logs generated. Allowed values: default, crio (mimic CRIO output), csv, json, raw")
pflag.StringVar(&opts.LabelType, "label-type", "none", "Overwrite to control what labels are included in Loki logs. Allowed values: none, client, client-host")
pflag.BoolVar(&opts.UseRandomHostname, "use-random-hostname", false, "Ensures that the hostname field is unique by adding a random integer to the end.")
pflag.IntVar(&opts.SyntheticPayloadSize, "synthetic-payload-size", 100, "Overwrite to control size of synthetic log line.")
pflag.StringVar(&opts.Tenant, "tenant", "test", "Loki tenant ID for writing logs.")
pflag.IntVar(&opts.QueriesPerMinute, "queries-per-minute", 1, "The rate to generate queries. This rate may not always be achievable.")
pflag.StringVar(&opts.Query, "query", "", "Query to use to get logs from storage.")
pflag.StringVar(&opts.QueryRange, "query-range", "1s", "Duration of time period to query for logs (Loki only).")
pflag.Parse()
}
func main() {
ll, err := log.ParseLevel(logLevel)
if err != nil {
ll = log.ErrorLevel
}
log.SetLevel(ll)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
if configJSON, err := json.MarshalIndent(opts, "", "\t"); err != nil {
log.Infof("configuration:\n%s\n", configJSON)
}
switch opts.Command {
case "generate":
generatorOpts := generator.Options{
Client: generator.ClientType(opts.Destination),
ClientURL: opts.ClientURL,
FileName: opts.OutputFile,
Tenant: opts.Tenant,
DisableSecurityCheck: opts.DisableSecurityCheck,
LogsPerSecond: opts.LogsPerSecond,
}
logGenerator, err := generator.NewLogGenerator(generatorOpts)
if err != nil {
panic(err)
}
logGenerator.GenerateLogs(
generator.LogType(opts.LogType),
generator.Format(opts.LogFormat),
opts.SyntheticPayloadSize,
generator.LabelSetOptions(opts.LabelType),
opts.UseRandomHostname,
)
case "query":
querierOpts := querier.Options{
Client: querier.ClientType(opts.Destination),
ClientURL: opts.ClientURL,
Tenant: opts.Tenant,
DisableSecurityCheck: opts.DisableSecurityCheck,
QueriesPerMinute: opts.QueriesPerMinute,
QueryRange: opts.QueryRange,
}
logQuerier, err := querier.NewLogQuerier(querierOpts)
if err != nil {
panic(err)
}
logQuerier.QueryLogs(opts.Query)
default:
panic(fmt.Errorf("unknown command :%s", opts.Command))
}
}