Skip to content

Commit b1d0a82

Browse files
authored
Merge pull request #127 from kube-logging/fix/viper-breaking-change
fix: ini config loading
2 parents a2b2fcc + 91eddab commit b1d0a82

6 files changed

Lines changed: 60 additions & 46 deletions

File tree

conf/configuration.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,54 @@ package conf
1717
import (
1818
"fmt"
1919

20+
"github.com/go-viper/encoding/ini"
2021
log "github.com/sirupsen/logrus"
2122
"github.com/spf13/viper"
2223
)
2324

24-
func Init() {
25+
var Viper *viper.Viper
2526

26-
viper.AddConfigPath("./conf/")
27-
viper.SetConfigName("config")
27+
func Init() {
28+
codecRegistry := viper.NewCodecRegistry()
29+
codecRegistry.RegisterCodec("ini", &ini.Codec{})
30+
Viper = viper.NewWithOptions(
31+
viper.WithCodecRegistry(codecRegistry),
32+
)
33+
Viper.SetConfigName("config")
34+
Viper.SetConfigType("ini")
35+
Viper.AddConfigPath("./conf/")
2836

29-
if err := viper.ReadInConfig(); err != nil {
37+
if err := Viper.ReadInConfig(); err != nil {
3038
log.Warnf("Error reading config file, %s", err)
3139
}
3240

33-
viper.SetDefault("logging.level", "info")
41+
Viper.SetDefault("logging.level", "info")
3442

35-
level, lErr := log.ParseLevel(viper.GetString("logging.level"))
43+
level, lErr := log.ParseLevel(Viper.GetString("logging.level"))
3644
if lErr != nil {
3745
panic("unrecognized loglevel")
3846
}
3947
log.SetLevel(level)
4048

41-
fmt.Printf("Using config: %s\n", viper.ConfigFileUsed())
42-
viper.SetDefault("message.count", 0)
43-
viper.SetDefault("message.randomise", true)
44-
viper.SetDefault("message.event-per-sec", 2)
45-
viper.SetDefault("message.byte-per-sec", 200)
46-
viper.SetDefault("message.max-random-hosts", 1000)
47-
viper.SetDefault("message.max-random-apps", 100)
48-
viper.SetDefault("message.host", "hostname")
49-
viper.SetDefault("message.appname", "appname")
50-
51-
viper.SetDefault("api.addr", ":11000")
52-
viper.SetDefault("api.basePath", "/")
53-
54-
viper.SetDefault("nginx.enabled", false)
55-
viper.SetDefault("apache.enabled", false)
56-
viper.SetDefault("golang.enabled", false)
57-
viper.SetDefault("golang.time_format", "02/Jan/2006:15:04:05 -0700")
58-
viper.SetDefault("golang.weight.error", 0)
59-
viper.SetDefault("golang.weight.info", 1)
60-
viper.SetDefault("golang.weight.warning", 0)
61-
viper.SetDefault("golang.weight.debug", 0)
49+
fmt.Printf("Using config: %s\n", Viper.ConfigFileUsed())
50+
Viper.SetDefault("message.count", 0)
51+
Viper.SetDefault("message.randomise", true)
52+
Viper.SetDefault("message.event-per-sec", 2)
53+
Viper.SetDefault("message.byte-per-sec", 200)
54+
Viper.SetDefault("message.max-random-hosts", 1000)
55+
Viper.SetDefault("message.max-random-apps", 100)
56+
Viper.SetDefault("message.host", "hostname")
57+
Viper.SetDefault("message.appname", "appname")
58+
59+
Viper.SetDefault("api.addr", ":11000")
60+
Viper.SetDefault("api.basePath", "/")
61+
62+
Viper.SetDefault("nginx.enabled", false)
63+
Viper.SetDefault("apache.enabled", false)
64+
Viper.SetDefault("golang.enabled", false)
65+
Viper.SetDefault("golang.time_format", "02/Jan/2006:15:04:05 -0700")
66+
Viper.SetDefault("golang.weight.error", 0)
67+
Viper.SetDefault("golang.weight.info", 1)
68+
Viper.SetDefault("golang.weight.warning", 0)
69+
Viper.SetDefault("golang.weight.debug", 0)
6270
}

formats/golang/golang.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525
wr "github.com/mroth/weightedrand"
2626
"github.com/prometheus/client_golang/prometheus"
2727
log "github.com/sirupsen/logrus"
28-
"github.com/spf13/viper"
28+
29+
"github.com/kube-logging/log-generator/conf"
2930
)
3031

3132
type GolangLogIntensity struct {
@@ -88,7 +89,7 @@ func NewGolangLogRandom(i GolangLogIntensity) *GolangLog {
8889
}
8990

9091
func (g GolangLog) String() (string, float64) {
91-
g.Time = time.Now().Format(viper.GetString("golang.time_format"))
92+
g.Time = time.Now().Format(conf.Viper.GetString("golang.time_format"))
9293
g.MSG = g.newRandomMessage()
9394

9495
out, err := json.MarshalIndent(g, "", " ")

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/cenkalti/backoff/v4 v4.3.0
88
github.com/dhoomakethu/stress v0.0.0-20230620054616-291ff04e1c89
99
github.com/gin-gonic/gin v1.10.0
10+
github.com/go-viper/encoding/ini v0.1.1
1011
github.com/kube-logging/log-generator/formats/custom v0.0.0-20250206095042-0df4b7b9896b
1112
github.com/kube-logging/log-generator/log v0.0.0-20250206095042-0df4b7b9896b
1213
github.com/lthibault/jitterbug v2.0.0+incompatible
@@ -63,5 +64,6 @@ require (
6364
golang.org/x/sys v0.30.0 // indirect
6465
golang.org/x/text v0.22.0 // indirect
6566
google.golang.org/protobuf v1.36.5 // indirect
67+
gopkg.in/ini.v1 v1.67.0 // indirect
6668
gopkg.in/yaml.v3 v3.0.1 // indirect
6769
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
4444
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
4545
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
4646
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
47+
github.com/go-viper/encoding/ini v0.1.1 h1:MVWY7B2XNw7lnOqHutGRc97bF3rP7omOdgjdMPAJgbs=
48+
github.com/go-viper/encoding/ini v0.1.1/go.mod h1:Pfi4M2V1eAGJVZ5q6FrkHPhtHED2YgLlXhvgMVrB+YQ=
4749
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
4850
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
4951
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
@@ -167,6 +169,8 @@ google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojt
167169
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
168170
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
169171
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
172+
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
173+
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
170174
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
171175
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
172176
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

loggen/loggen.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"github.com/gin-gonic/gin"
2929
"github.com/lthibault/jitterbug"
3030
logger "github.com/sirupsen/logrus"
31-
"github.com/spf13/viper"
3231

32+
"github.com/kube-logging/log-generator/conf"
3333
"github.com/kube-logging/log-generator/formats"
3434
"github.com/kube-logging/log-generator/formats/golang"
3535
"github.com/kube-logging/log-generator/formats/web"
@@ -60,9 +60,9 @@ type LogGenRequest struct {
6060

6161
func New() *LogGen {
6262
return &LogGen{
63-
EventPerSec: viper.GetInt("message.event-per-sec"),
64-
BytePerSec: viper.GetInt("message.byte-per-sec"),
65-
Randomise: viper.GetBool("message.randomise"),
63+
EventPerSec: conf.Viper.GetInt("message.event-per-sec"),
64+
BytePerSec: conf.Viper.GetInt("message.byte-per-sec"),
65+
Randomise: conf.Viper.GetBool("message.randomise"),
6666
ActiveRequests: List{list.New()},
6767
}
6868
}
@@ -184,16 +184,16 @@ func uintPtr(u uint) *uint {
184184

185185
func (l *LogGen) golangSet() error {
186186
if l.GolangLog.ErrorWeight == nil {
187-
l.GolangLog.ErrorWeight = uintPtr(viper.GetUint("golang.weight.error"))
187+
l.GolangLog.ErrorWeight = uintPtr(conf.Viper.GetUint("golang.weight.error"))
188188
}
189189
if l.GolangLog.WarningWeight == nil {
190-
l.GolangLog.WarningWeight = uintPtr(viper.GetUint("golang.weight.warning"))
190+
l.GolangLog.WarningWeight = uintPtr(conf.Viper.GetUint("golang.weight.warning"))
191191
}
192192
if l.GolangLog.InfoWeight == nil {
193-
l.GolangLog.InfoWeight = uintPtr(viper.GetUint("golang.weight.info"))
193+
l.GolangLog.InfoWeight = uintPtr(conf.Viper.GetUint("golang.weight.info"))
194194
}
195195
if l.GolangLog.DebugWeight == nil {
196-
l.GolangLog.DebugWeight = uintPtr(viper.GetUint("golang.weight.debug"))
196+
l.GolangLog.DebugWeight = uintPtr(conf.Viper.GetUint("golang.weight.debug"))
197197
}
198198

199199
return nil
@@ -256,18 +256,18 @@ func (l *LogGen) Run() {
256256
} else if l.BytePerSec > 0 {
257257
ticker = tickerForByte(l.BytePerSec, jitter)
258258
}
259-
count := viper.GetInt("message.count")
259+
count := conf.Viper.GetInt("message.count")
260260

261-
if len(viper.GetString("destination.network")) != 0 {
262-
l.writer = newNetworkWriter(viper.GetString("destination.network"), viper.GetString("destination.address"))
261+
if len(conf.Viper.GetString("destination.network")) != 0 {
262+
l.writer = newNetworkWriter(conf.Viper.GetString("destination.network"), conf.Viper.GetString("destination.address"))
263263
} else {
264264
l.writer = newStdoutWriter()
265265
}
266266

267267
l.golangSet()
268268

269269
for range ticker.C {
270-
if viper.GetBool("nginx.enabled") {
270+
if conf.Viper.GetBool("nginx.enabled") {
271271
l.sendIfCount(count, &counter, func() (log.Log, error) {
272272
if l.Randomise {
273273
return formats.NewRandomWeb("nginx", web.TemplateFS)
@@ -276,7 +276,7 @@ func (l *LogGen) Run() {
276276
}
277277
})
278278
}
279-
if viper.GetBool("apache.enabled") {
279+
if conf.Viper.GetBool("apache.enabled") {
280280
l.sendIfCount(count, &counter, func() (log.Log, error) {
281281
if l.Randomise {
282282
return formats.NewRandomWeb("apache", web.TemplateFS)
@@ -285,7 +285,7 @@ func (l *LogGen) Run() {
285285
}
286286
})
287287
}
288-
if viper.GetBool("golang.enabled") {
288+
if conf.Viper.GetBool("golang.enabled") {
289289
l.sendIfCount(count, &counter, func() (log.Log, error) {
290290
return formats.NewGolangRandom(l.GolangLog), nil
291291
})

main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/gin-gonic/gin"
2626
log "github.com/sirupsen/logrus"
27-
"github.com/spf13/viper"
2827

2928
"github.com/kube-logging/log-generator/conf"
3029
"github.com/kube-logging/log-generator/loggen"
@@ -120,8 +119,8 @@ func exceptionsGoCall(c *gin.Context) {
120119
func main() {
121120
metrics.Startup = time.Now()
122121

123-
apiAddr := viper.GetString("api.addr")
124-
apiBasePath := viper.GetString("api.basePath")
122+
apiAddr := conf.Viper.GetString("api.addr")
123+
apiBasePath := conf.Viper.GetString("api.basePath")
125124

126125
flag.Parse()
127126

0 commit comments

Comments
 (0)