Skip to content

Commit c5ec9df

Browse files
author
Greg Dubicki
committed
Make logging requests configurable
not requiring the app logging level to be set to TRACE and with configurable log address (stdout/stderr/syslog etc.)
1 parent f8963ae commit c5ec9df

File tree

14 files changed

+87
-23
lines changed

14 files changed

+87
-23
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ Usage of ./haproxy-consul-connect:
3636
-http-addr string
3737
Consul agent address (default "127.0.0.1:8500")
3838
-log-level string
39-
Log level (default "INFO")
39+
This app log level (default "INFO")
40+
-log-requests
41+
Enable logging requests by HAproxy
42+
-log-address
43+
Haproxy logs address (default: stderr with this app logs)
4044
-sidecar-for string
4145
The consul service id to proxy
4246
-sidecar-for-tag string

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/haproxytech/haproxy-consul-connect
33
go 1.13
44

55
require (
6-
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
6+
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a
77
github.com/criteo/haproxy-spoe-go v0.0.0-20190925130734-97891c13d324
88
github.com/d4l3k/messagediff v1.2.1 // indirect
99
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9

haproxy/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ global
2828
tune.ssl.default-dh-param 1024
2929
nbproc 1
3030
nbthread {{.NbThread}}
31+
log-tag haproxy_sidecar
3132
3233
userlist controller
3334
user {{.DataplaneUser}} insecure-password {{.DataplanePass}}
@@ -57,7 +58,6 @@ type baseParams struct {
5758
SocketPath string
5859
DataplaneUser string
5960
DataplanePass string
60-
LogsPath string
6161
}
6262

6363
type haConfig struct {
@@ -113,7 +113,6 @@ func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
113113
err = tmpl.Execute(cfgFile, baseParams{
114114
NbThread: runtime.GOMAXPROCS(0),
115115
SocketPath: cfg.StatsSock,
116-
LogsPath: cfg.LogsSock,
117116
DataplaneUser: dataplaneUser,
118117
DataplanePass: dataplanePass,
119118
})

haproxy/haproxy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ func (h *HAProxy) start(sd *lib.Shutdown) error {
6060
}
6161
h.haConfig = hc
6262

63-
if h.opts.LogRequests {
63+
if h.opts.LogAddress == "" {
6464
err := h.startLogger()
6565
if err != nil {
6666
return err
6767
}
68+
} else {
69+
log.Infof("not starting built-in syslog - using %s as syslog address", h.opts.LogAddress)
6870
}
6971

7072
if h.opts.EnableIntentions {
@@ -77,6 +79,7 @@ func (h *HAProxy) start(sd *lib.Shutdown) error {
7779
dpc, err := haproxy_cmd.Start(sd, haproxy_cmd.Config{
7880
HAProxyPath: h.opts.HAProxyBin,
7981
HAProxyConfigPath: hc.HAProxy,
82+
HAProxyLogWithThisApp: h.opts.LogAddress == "",
8083
DataplanePath: h.opts.DataplaneBin,
8184
DataplaneTransactionDir: hc.DataplaneTransactionDir,
8285
DataplaneSock: hc.DataplaneSock,
@@ -112,6 +115,8 @@ func (h *HAProxy) startLogger() error {
112115
}
113116
}(channel)
114117

118+
log.Infof("starting built-in syslog server at %s", h.haConfig.LogsSock)
119+
115120
return nil
116121
}
117122

haproxy/haproxy_cmd/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313
log "github.com/sirupsen/logrus"
1414
)
1515

16-
func runCommand(sd *lib.Shutdown, cmdPath string, args ...string) (*exec.Cmd, error) {
16+
func runCommand(sd *lib.Shutdown, cmdPath string, logPrefix string, logWithThisApp bool, args ...string) (*exec.Cmd, error) {
1717
_, file := path.Split(cmdPath)
1818
cmd := exec.Command(cmdPath, args...)
19-
halog.Cmd("haproxy", cmd)
19+
if logWithThisApp {
20+
halog.Cmd(logPrefix, cmd)
21+
}
2022

2123
sd.Add(1)
2224
err := cmd.Start()

haproxy/haproxy_cmd/cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
func Test_runCommand_ok(t *testing.T) {
1111
t.Parallel()
1212
sd := lib.NewShutdown()
13-
cmd, err := runCommand(sd, "ls", ".")
13+
cmd, err := runCommand(sd, "ls", "foobar", true, ".")
1414
require.NoError(t, err)
1515
cmd.Wait()
1616
}
1717

1818
func Test_runCommand_nok_wrong_path(t *testing.T) {
1919
t.Parallel()
2020
sd := lib.NewShutdown()
21-
cmd, err := runCommand(sd, "/path/to/nowhere/that/can/be/found/myExec", "--help")
21+
cmd, err := runCommand(sd, "/path/to/nowhere/that/can/be/found/myExec", "foobar", true, "--help")
2222
require.NotNil(t, err)
2323
require.Contains(t, err.Error(), "no such file or directory")
2424
require.Nil(t, cmd)

haproxy/haproxy_cmd/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
type Config struct {
2626
HAProxyPath string
2727
HAProxyConfigPath string
28+
HAProxyLogWithThisApp bool
2829
DataplanePath string
2930
DataplaneTransactionDir string
3031
DataplaneSock string
@@ -35,6 +36,8 @@ type Config struct {
3536
func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {
3637
haCmd, err := runCommand(sd,
3738
cfg.HAProxyPath,
39+
"haproxy",
40+
cfg.HAProxyLogWithThisApp,
3841
"-f",
3942
cfg.HAProxyConfigPath,
4043
)
@@ -47,6 +50,8 @@ func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {
4750

4851
cmd, err := runCommand(sd,
4952
cfg.DataplanePath,
53+
"dataplaneapi",
54+
true,
5055
"--scheme", "unix",
5156
"--socket-path", cfg.DataplaneSock,
5257
"--haproxy-bin", cfg.HAProxyPath,

haproxy/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ type Options struct {
99
StatsListenAddr string
1010
StatsRegisterService bool
1111
LogRequests bool
12+
LogAddress string
1213
}

haproxy/state.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@ func (h *HAProxy) watch(sd *lib.Shutdown) error {
9595

9696
newConsulCfg := nextState.Load().(consul.Config)
9797

98+
logAddress := h.haConfig.LogsSock
99+
if h.opts.LogAddress != "" {
100+
logAddress = h.opts.LogAddress
101+
}
102+
98103
newState, err := state.Generate(state.Options{
99104
EnableIntentions: h.opts.EnableIntentions,
100105
LogRequests: h.opts.LogRequests,
101-
LogSocket: h.haConfig.LogsSock,
106+
LogAddress: logAddress,
102107
SPOEConfigPath: h.haConfig.SPOE,
103108
SPOESocket: h.haConfig.SPOESock,
104109
}, h.haConfig, currentState, newConsulCfg)

haproxy/state/downstream.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func generateDownstream(opts Options, certStore CertificateStore, cfg consul.Dow
4444
}
4545

4646
// Logging
47-
if opts.LogRequests && opts.LogSocket != "" {
47+
if opts.LogRequests {
4848
fe.LogTarget = &models.LogTarget{
4949
ID: int64p(0),
50-
Address: opts.LogSocket,
50+
Address: opts.LogAddress,
5151
Facility: models.LogTargetFacilityLocal0,
5252
Format: models.LogTargetFormatRfc5424,
5353
}
@@ -100,10 +100,10 @@ func generateDownstream(opts Options, certStore CertificateStore, cfg consul.Dow
100100
}
101101

102102
// Logging
103-
if opts.LogRequests && opts.LogSocket != "" {
103+
if opts.LogRequests {
104104
be.LogTarget = &models.LogTarget{
105105
ID: int64p(0),
106-
Address: opts.LogSocket,
106+
Address: opts.LogAddress,
107107
Facility: models.LogTargetFacilityLocal0,
108108
Format: models.LogTargetFormatRfc5424,
109109
}

0 commit comments

Comments
 (0)