Skip to content

Commit

Permalink
fix: allow generated logs to have unique hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-GV committed Aug 28, 2023
1 parent 954cd01 commit 882cd1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
29 changes: 15 additions & 14 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 8 additions & 2 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"context"
"fmt"
"math/rand"
"os"
"time"

Expand Down Expand Up @@ -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)
Expand All @@ -122,6 +123,11 @@ func (g *LogGenerator) GenerateLogs(logType LogType, logFormat Format, logSize i

var lineCount int64 = 0

logHostname = host

Check failure on line 126 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / test

undefined: logHostname
if isHostnameUnique {
logHostname = fmt.Sprintf("%s.%032X", host, rand.Uint64())

Check failure on line 128 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / test

undefined: logHostname
}

for {
next := time.Now().UTC().Add(1 * time.Second)

Expand All @@ -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)

Check failure on line 140 in internal/generator/generator.go

View workflow job for this annotation

GitHub Actions / test

undefined: logHostname
if err != nil {
log.Fatalf("error formating log: %s", err)
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -74,6 +75,7 @@ func main() {
generator.Format(opts.LogFormat),
opts.SyntheticPayloadSize,
generator.LabelSetOptions(opts.LabelType),
opts.RequireUniqueHostname,
)
case "query":
querierOpts := querier.Options{
Expand Down

0 comments on commit 882cd1a

Please sign in to comment.