Skip to content

Commit c0c55f0

Browse files
authored
Merge pull request #132 from mahmednabil109/add_httplogger_options
Add http config options
2 parents 6d0dc8e + 087509f commit c0c55f0

4 files changed

Lines changed: 57 additions & 23 deletions

File tree

cmds/contest/server/server.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/linuxboot/contest/pkg/config"
2020
"github.com/linuxboot/contest/pkg/job"
2121
"github.com/linuxboot/contest/pkg/jobmanager"
22+
"github.com/linuxboot/contest/pkg/loggerhook"
2223
"github.com/linuxboot/contest/pkg/logging"
2324
"github.com/linuxboot/contest/pkg/pluginregistry"
2425
"github.com/linuxboot/contest/pkg/storage"
@@ -48,17 +49,28 @@ var (
4849
flagTargetLocker *string
4950
flagInstanceTag *string
5051
flagLogLevel *string
51-
flagAdminServerAddr *string
5252
flagPauseTimeout *time.Duration
5353
flagResumeJobs *bool
5454
flagTargetLockDuration *time.Duration
55+
// http logger parameters
56+
flagAdminServerAddr *string
57+
flagHttpLoggerBufferSize *int
58+
flagHttpLoggerMaxBatchSize *int
59+
flagHttpLoggerMaxBatchCount *int
60+
flagHttpLoggerBatchSendFreq *time.Duration
61+
flagHttpLoggerTimeout *time.Duration
5562
)
5663

5764
func initFlags(cmd string) {
5865
flagSet = flag.NewFlagSet(cmd, flag.ContinueOnError)
5966
flagDBURI = flagSet.String("dbURI", config.DefaultDBURI, "Database URI")
6067
flagListenAddr = flagSet.String("listenAddr", ":8080", "Listen address and port")
6168
flagAdminServerAddr = flagSet.String("adminServerAddr", "", "Addr of the admin server to connect to")
69+
flagHttpLoggerBufferSize = flagSet.Int("loggerBufferSize", loggerhook.DefaultBufferSize, "buffer size for the http logger hook")
70+
flagHttpLoggerMaxBatchSize = flagSet.Int("loggerMaxBatchSize", loggerhook.DefaultMaxBatchSize, "max size (in bytes) of a logs batch to be sent if it reaches/exceeds it")
71+
flagHttpLoggerMaxBatchCount = flagSet.Int("loggerMaxBatchCount", loggerhook.DefaultMaxBatchCount, "max count of logs in a batch")
72+
flagHttpLoggerBatchSendFreq = flagSet.Duration("loggerBatchSendFreq", loggerhook.DefaultBatchSendFreq, "duration that defines the batch sending freq")
73+
flagHttpLoggerTimeout = flagSet.Duration("loggerTimeout", loggerhook.DefaultLogTimeout, "logs send timeout")
6274
flagServerID = flagSet.String("serverID", "", "Set a static server ID, e.g. the host name or another unique identifier. If unset, will use the listener's default")
6375
flagProcessTimeout = flagSet.Duration("processTimeout", api.DefaultEventTimeout, "API request processing timeout")
6476
flagTargetLocker = flagSet.String("targetLocker", "auto", "Target locker implementation to use, \"auto\" follows DBURI setting")
@@ -154,7 +166,17 @@ func Main(pluginConfig *PluginConfig, cmd string, args []string, sigs <-chan os.
154166
logrusOpts := logging.DefaultOptions()
155167

156168
if *flagAdminServerAddr != "" {
157-
logrusOpts = append(logrusOpts, bundles.OptionHttpLoggerAddr(*flagAdminServerAddr))
169+
logrusOpts = append(
170+
logrusOpts,
171+
bundles.OptionHttpLoggerConfig(loggerhook.Config{
172+
Addr: *flagAdminServerAddr,
173+
BufferSize: *flagHttpLoggerBufferSize,
174+
MaxBatchSize: *flagHttpLoggerMaxBatchSize,
175+
MaxBatchCount: *flagHttpLoggerMaxBatchCount,
176+
BatchSendFreq: *flagHttpLoggerBatchSendFreq,
177+
LogTimeout: *flagHttpLoggerTimeout,
178+
}),
179+
)
158180
}
159181

160182
ctx, cancel := logrusctx.NewContext(logLevel, logrusOpts...)

pkg/loggerhook/httphook.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ import (
1616
"github.com/sirupsen/logrus"
1717
)
1818

19-
var (
20-
DefaultBufferSize = 10
21-
MaxBatchSize = 500000 // size in bytes
22-
MaxBatchCount = 100
23-
BatchSendFreq = 1 * time.Second
24-
DefaultLogTimeout = 1 * time.Second
19+
const (
20+
DefaultBufferSize = 10
21+
DefaultMaxBatchSize = 500000 // size in bytes
22+
DefaultMaxBatchCount = 100
23+
DefaultBatchSendFreq = 1 * time.Second
24+
DefaultLogTimeout = 1 * time.Second
2525
)
2626

27+
type Config struct {
28+
Addr string
29+
BufferSize int
30+
MaxBatchSize int
31+
MaxBatchCount int
32+
BatchSendFreq time.Duration
33+
LogTimeout time.Duration
34+
}
35+
2736
// Batch defines a log batch that handles the size in bytes of the logs
2837
type Batch struct {
2938
addr string
@@ -68,25 +77,27 @@ func (b *Batch) PostAndReset() error {
6877
}
6978

7079
type HttpHook struct {
80+
cfg Config
7181
batch Batch
7282
batchTicker *time.Ticker
7383

7484
logChan chan server.Log
7585
closeChan chan struct{}
7686
}
7787

78-
func NewHttpHook(addr string) (*HttpHook, error) {
79-
url, err := url.ParseRequestURI(addr)
88+
func NewHttpHook(cfg Config) (*HttpHook, error) {
89+
url, err := url.ParseRequestURI(cfg.Addr)
8090
if err != nil {
8191
return nil, err
8292
}
8393
// add the endpoint to the server addr
8494
url.Path = path.Join(url.Path, "log")
8595

8696
hh := HttpHook{
97+
cfg: cfg,
8798
batch: NewBatch(url.String()),
88-
batchTicker: time.NewTicker(BatchSendFreq),
89-
logChan: make(chan server.Log, DefaultBufferSize),
99+
batchTicker: time.NewTicker(cfg.BatchSendFreq),
100+
logChan: make(chan server.Log, cfg.BufferSize),
90101
closeChan: make(chan struct{}),
91102
}
92103

@@ -123,7 +134,7 @@ func (hh *HttpHook) Fire(entry *logrus.Entry) error {
123134
}
124135

125136
// timeout is used to not block the service on the logging
126-
timeout := time.After(DefaultLogTimeout)
137+
timeout := time.After(hh.cfg.LogTimeout)
127138
select {
128139
case hh.logChan <- log:
129140
// do nothing
@@ -140,15 +151,15 @@ func (hh *HttpHook) logHandler() {
140151
select {
141152
case log := <-hh.logChan:
142153
hh.batch.Add(log)
143-
if hh.batch.Count() > MaxBatchCount || hh.batch.Size() > uint64(MaxBatchSize) {
154+
if hh.batch.Count() > hh.cfg.MaxBatchCount || hh.batch.Size() > uint64(hh.cfg.MaxBatchSize) {
144155
err := hh.batch.PostAndReset()
145156
if err != nil {
146157
fmt.Fprintf(os.Stderr, "Send Batch failed: %v", err)
147158
break
148159
}
149160
// if the batch is sent
150161
// to avoid ticking on an empty batch
151-
hh.batchTicker.Reset(BatchSendFreq)
162+
hh.batchTicker.Reset(hh.cfg.BatchSendFreq)
152163
}
153164
case <-hh.batchTicker.C:
154165
if hh.batch.Size() > 0 {

pkg/xcontext/bundles/logrusctx/new_context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func NewContext(logLevel logger.Level, opts ...bundles.Option) (xcontext.Context
9090
),
9191
)
9292

93-
if cfg.HttpLoggerAddr != "" {
94-
httpHook, err := loggerhook.NewHttpHook(cfg.HttpLoggerAddr)
93+
if cfg.HttpLoggerConfig.Addr != "" {
94+
httpHook, err := loggerhook.NewHttpHook(cfg.HttpLoggerConfig)
9595
if err == nil {
9696
// clean the http logger on ctx canceling
9797
go func() {

pkg/xcontext/bundles/options.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package bundles
77

88
import (
9+
"github.com/linuxboot/contest/pkg/loggerhook"
910
"github.com/linuxboot/contest/pkg/xcontext"
1011
)
1112

@@ -70,12 +71,11 @@ func (opt OptionTimestampFormat) apply(cfg *Config) {
7071
cfg.TimestampFormat = string(opt)
7172
}
7273

73-
// OptionHttpLoggerAddr is used to create a logger hook to send logs via http
74-
// to admin server
75-
type OptionHttpLoggerAddr string
74+
// OptionHttpLoggerConfig is used to define the different config for the http logger
75+
type OptionHttpLoggerConfig loggerhook.Config
7676

77-
func (opt OptionHttpLoggerAddr) apply(cfg *Config) {
78-
cfg.HttpLoggerAddr = string(opt)
77+
func (opt OptionHttpLoggerConfig) apply(cfg *Config) {
78+
cfg.HttpLoggerConfig = loggerhook.Config(opt)
7979
}
8080

8181
// Config is a configuration state resulted from Option-s.
@@ -84,9 +84,10 @@ type Config struct {
8484
TracerReportCaller bool
8585
TimestampFormat string
8686
VerboseCaller bool
87-
HttpLoggerAddr string
8887
Tracer xcontext.Tracer
8988
Format LogFormat
89+
// http logger specific options
90+
HttpLoggerConfig loggerhook.Config
9091
}
9192

9293
// GetConfig processes passed Option-s and returns the resulting state as Config.

0 commit comments

Comments
 (0)