Skip to content

Commit 9e474a9

Browse files
committed
use a random set of "adjective-verb" style hostnames and apps instead of host-%d/app-%d in syslog msg templates
Signed-off-by: Mate Ory <[email protected]>
1 parent ba82c3f commit 9e474a9

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

conf/config.ini.sample

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
# The amount of bytes to emit/s (default: 200)
1515
#byte-per-sec =
1616

17+
# Number of different random host names to generate
18+
#max-random-hosts = 1000
19+
# Number of different app names to generate
20+
#max-random-apps = 100
21+
# Random seed for host/app list generation so that the same set of host/app names are used (if >0)
22+
#seed = 0
23+
1724
[api]
1825
# Server listen address (default: "":11000")
1926
#addr =

formats/logfactory.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/kube-logging/log-generator/formats/syslog"
2121
"github.com/kube-logging/log-generator/formats/web"
2222
"github.com/kube-logging/log-generator/log"
23+
"github.com/spf13/viper"
2324
)
2425

2526
func FormatsByType() map[string][]string {
@@ -51,12 +52,22 @@ func LogFactory(logType string, format string, randomise bool) (log.Log, error)
5152
}
5253
}
5354

55+
var syslogRandomService *syslog.RandomService
56+
57+
func syslogRandom() *syslog.RandomService {
58+
if syslogRandomService == nil {
59+
syslogRandomService = syslog.NewRandomService(
60+
viper.GetInt("message.max-random-hosts"), viper.GetInt("message.max-random-apps"), viper.GetInt64("message.seed"))
61+
}
62+
return syslogRandomService
63+
}
64+
5465
func NewSyslog(format string) (*log.LogTemplate, error) {
55-
return log.NewLogTemplate(format, syslog.TemplateFS, syslog.SampleData())
66+
return log.NewLogTemplate(format, syslog.TemplateFS, syslogRandom().SampleData())
5667
}
5768

5869
func NewRandomSyslog(format string) (*log.LogTemplate, error) {
59-
return log.NewLogTemplate(format, syslog.TemplateFS, syslog.RandomData())
70+
return log.NewLogTemplate(format, syslog.TemplateFS, syslogRandom().RandomData())
6071
}
6172

6273
func SyslogFormatNames() []string {

formats/syslog/syslog.go

+46-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package syslog
1717
import (
1818
"embed"
1919
"fmt"
20+
"math/rand"
2021
"strconv"
22+
"strings"
2123
"time"
2224

2325
"github.com/Pallinder/go-randomdata"
24-
"github.com/spf13/viper"
2526
)
2627

2728
//go:embed *.tmpl
@@ -59,7 +60,47 @@ func (t TemplateData) Severity() string {
5960
return strconv.FormatInt(int64(t.severity), 10)
6061
}
6162

62-
func SampleData() TemplateData {
63+
type RandomService struct {
64+
Hosts []string
65+
Apps []string
66+
}
67+
68+
func NewRandomService(maxHosts int, maxApps int, seed int64) *RandomService {
69+
svc := new(RandomService)
70+
71+
// we might experience a race condition here, but we are dealing with pseudorandom data anyway
72+
if seed != 0 {
73+
randomdata.CustomRand(rand.New(rand.NewSource(seed)))
74+
defer randomdata.CustomRand(rand.New(rand.NewSource(time.Now().UnixNano())))
75+
}
76+
77+
svc.Hosts = make([]string, maxHosts)
78+
for i := 0; i < maxHosts; i++ {
79+
svc.Hosts[i] = strings.ToLower(randomdata.SillyName())
80+
}
81+
82+
svc.Apps = make([]string, maxApps)
83+
for i := 0; i < maxApps; i++ {
84+
svc.Apps[i] = strings.ToLower(randomdata.SillyName())
85+
}
86+
return svc
87+
}
88+
89+
func (r *RandomService) RandomHost() string {
90+
if len(r.Hosts) == 0 {
91+
return "host"
92+
}
93+
return r.Hosts[randomdata.Number(0, len(r.Hosts))]
94+
}
95+
96+
func (r *RandomService) RandomApp() string {
97+
if len(r.Apps) == 0 {
98+
return "app"
99+
}
100+
return r.Apps[randomdata.Number(0, len(r.Apps))]
101+
}
102+
103+
func (r *RandomService) SampleData() TemplateData {
63104
return TemplateData{
64105
Facility: 20,
65106
severity: 5,
@@ -72,13 +113,13 @@ func SampleData() TemplateData {
72113
}
73114
}
74115

75-
func RandomData() TemplateData {
116+
func (r *RandomService) RandomData() TemplateData {
76117
return TemplateData{
77118
Facility: randomdata.Number(0, 24),
78119
severity: randomdata.Number(0, 8),
79120
dateTime: time.Now().UTC(),
80-
Host: fmt.Sprintf("host-%d", randomdata.Number(viper.GetInt("message.max-random-hosts"))+1),
81-
AppName: fmt.Sprintf("app%d", randomdata.Number(viper.GetInt("message.max-random-apps"))+1),
121+
Host: r.RandomHost(),
122+
AppName: r.RandomApp(),
82123
PID: randomdata.Number(1, 10000),
83124
Seq: randomdata.Number(1, 10000),
84125
Msg: fmt.Sprintf("An application event log entry %s %s", randomdata.Noun(), randomdata.Noun()),

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright © 2021 Cisco Systems, Inc. and/or its affiliates
2+
// Copyright © 2023 Kube logging authors
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
45
// you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)