From 882cd1af8b9d38792f21eeecd1cf88e0abaf3181 Mon Sep 17 00:00:00 2001 From: Gerard Vanloo Date: Mon, 28 Aug 2023 09:44:27 -0400 Subject: [PATCH] fix: allow generated logs to have unique hostnames --- internal/config.go | 29 +++++++++++++++-------------- internal/generator/generator.go | 10 ++++++++-- main.go | 2 ++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/internal/config.go b/internal/config.go index a065e50..80dc53e 100644 --- a/internal/config.go +++ b/internal/config.go @@ -1,18 +1,19 @@ package internal type Options struct { - Command string - Destination string - OutputFile string - ClientURL string - DisableSecurityCheck bool - LogsPerSecond int - LogType string - LogFormat string - LabelType string - SyntheticPayloadSize int - Tenant string - QueriesPerMinute int - Query string - QueryRange string + Command string + Destination string + OutputFile string + ClientURL string + DisableSecurityCheck bool + LogsPerSecond int + LogType string + LogFormat string + LabelType string + SyntheticPayloadSize int + RequireUniqueHostname bool + Tenant string + QueriesPerMinute int + Query string + QueryRange string } diff --git a/internal/generator/generator.go b/internal/generator/generator.go index 403c8b7..75c1da3 100644 --- a/internal/generator/generator.go +++ b/internal/generator/generator.go @@ -3,6 +3,7 @@ package generator import ( "context" "fmt" + "math/rand" "os" "time" @@ -112,7 +113,7 @@ func NewLogGenerator(opts Options) (*LogGenerator, error) { return &generator, nil } -func (g *LogGenerator) GenerateLogs(logType LogType, logFormat Format, logSize int, labelOpts LabelSetOptions) { +func (g *LogGenerator) GenerateLogs(logType LogType, logFormat Format, logSize int, labelOpts LabelSetOptions, isHostnameUnique bool) { host, err := os.Hostname() if err != nil { log.Fatalf("error getting hostname: %s", err) @@ -122,6 +123,11 @@ func (g *LogGenerator) GenerateLogs(logType LogType, logFormat Format, logSize i var lineCount int64 = 0 + logHostname = host + if isHostnameUnique { + logHostname = fmt.Sprintf("%s.%032X", host, rand.Uint64()) + } + for { next := time.Now().UTC().Add(1 * time.Second) @@ -131,7 +137,7 @@ func (g *LogGenerator) GenerateLogs(logType LogType, logFormat Format, logSize i log.Fatalf("error creating log: %s", err) } - formattedLogLine, err := FormatLog(logFormat, host, lineCount, logLine) + formattedLogLine, err := FormatLog(logFormat, logHostname, lineCount, logLine) if err != nil { log.Fatalf("error formating log: %s", err) } diff --git a/main.go b/main.go index 644092e..33bc155 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ func init() { flag.StringVar(&opts.LogType, "log-type", "simple", "Overwrite to control the type of logs generated. Allowed values: simple, application, synthetic.") flag.StringVar(&opts.LogFormat, "log-format", "default", "Overwrite to control the format of logs generated. Allowed values: default, crio (mimic CRIO output), csv, json") flag.StringVar(&opts.LabelType, "label-type", "none", "Overwrite to control what labels are included in Loki logs. Allowed values: none, client, client-host") + flag.BoolVar(&opts.RequireUniqueHostname, "require-unique-hostname", false, "Ensures that the hostname field is unique by adding a random integer to the end.") flag.IntVar(&opts.SyntheticPayloadSize, "synthetic-payload-size", 100, "Overwrite to control size of synthetic log line.") flag.StringVar(&opts.Tenant, "tenant", "test", "Loki tenant ID for writing logs.") flag.IntVar(&opts.QueriesPerMinute, "queries-per-minute", 1, "The rate to generate queries. This rate may not always be achievable.") @@ -74,6 +75,7 @@ func main() { generator.Format(opts.LogFormat), opts.SyntheticPayloadSize, generator.LabelSetOptions(opts.LabelType), + opts.RequireUniqueHostname, ) case "query": querierOpts := querier.Options{