Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow generated logs to have unique hostnames #29

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options struct {
LogFormat string
LabelType string
SyntheticPayloadSize int
UseRandomHostname bool
Tenant string
QueriesPerMinute int
Query string
Expand Down
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, randomizeHostname 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
if randomizeHostname {
logHostname = fmt.Sprintf("%s.%032X", host, rand.Uint64())
}

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)
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.UseRandomHostname, "use-random-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.UseRandomHostname,
)
case "query":
querierOpts := querier.Options{
Expand Down