Skip to content

Commit

Permalink
Issue #238: Make route update logging format configurable
Browse files Browse the repository at this point in the history
This patch makes the log format of routing table changes configurable.
The "delta" option logs the additions and deletions and is the new
default. The "full" option restores the previous behavior of logging the
full routing table.
  • Loading branch information
magiconair committed Feb 10, 2017
1 parent 19c9053 commit e056671
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Proxy struct {
TLSHeader string
TLSHeaderValue string
GZIPContentTypes *regexp.Regexp
LogRoutes string
}

type Runtime struct {
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var defaultConfig = &Config{
DialTimeout: 30 * time.Second,
FlushInterval: time.Second,
LocalIP: LocalIPString(),
LogRoutes: "delta",
},
Registry: Registry{
Backend: "consul",
Expand Down
1 change: 1 addition & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.DurationVar(&readTimeout, "proxy.readtimeout", defaultValues.ReadTimeout, "read timeout for incoming requests")
f.DurationVar(&writeTimeout, "proxy.writetimeout", defaultValues.WriteTimeout, "write timeout for outgoing responses")
f.DurationVar(&cfg.Proxy.FlushInterval, "proxy.flushinterval", defaultConfig.Proxy.FlushInterval, "flush interval for streaming responses")
f.StringVar(&cfg.Proxy.LogRoutes, "proxy.log.routes", defaultConfig.Proxy.LogRoutes, "log format of routing table updates")
f.StringVar(&cfg.Metrics.Target, "metrics.target", defaultConfig.Metrics.Target, "metrics backend")
f.StringVar(&cfg.Metrics.Prefix, "metrics.prefix", defaultConfig.Metrics.Prefix, "prefix for reported metrics")
f.StringVar(&cfg.Metrics.Names, "metrics.names", defaultConfig.Metrics.Names, "route metric name template")
Expand Down
7 changes: 7 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
args: []string{"-proxy.log.routes", "foobar"},
cfg: func(cfg *Config) *Config {
cfg.Proxy.LogRoutes = "foobar"
return cfg
},
},
{
args: []string{"-registry.backend", "value"},
cfg: func(cfg *Config) *Config {
Expand Down
13 changes: 13 additions & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@
# proxy.gzip.contenttype =


# proxy.log.routes configures the log output format of routing table updates.
#
# Changes to the routing table are written to the standard log. This option
# configures the output format:
#
# delta: additions and deletions
# all: complete routing table
#
# The default is
#
# proxy.log.routes = delta


# registry.backend configures which backend is used.
# Supported backends are: consul, static, file
#
Expand Down
46 changes: 42 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
Expand All @@ -9,6 +10,7 @@ import (
"os"
"runtime"
"runtime/debug"
"strings"

"github.com/eBay/fabio/admin"
"github.com/eBay/fabio/config"
Expand All @@ -20,6 +22,7 @@ import (
"github.com/eBay/fabio/registry/file"
"github.com/eBay/fabio/registry/static"
"github.com/eBay/fabio/route"
dmp "github.com/sergi/go-diff/diffmatchpatch"
)

// version contains the version number
Expand Down Expand Up @@ -59,7 +62,7 @@ func main() {

initRuntime(cfg)
initBackend(cfg)
go watchBackend()
go watchBackend(cfg)
startAdmin(cfg)

// create proxies after metrics since they use the metrics registry.
Expand Down Expand Up @@ -161,7 +164,7 @@ func initBackend(cfg *config.Config) {
}
}

func watchBackend() {
func watchBackend(cfg *config.Config) {
var (
last string
svccfg string
Expand Down Expand Up @@ -190,12 +193,47 @@ func watchBackend() {
continue
}
route.SetTable(t)
log.Printf("[INFO] Updated config to\n%s", t)

logRoutes(last, next, cfg.Proxy.LogRoutes)
last = next
}
}

func logRoutes(last, next, format string) {
fmtDiff := func(diffs []dmp.Diff) string {
var b bytes.Buffer
for _, d := range diffs {
t := strings.TrimSpace(d.Text)
if t == "" {
continue
}
switch d.Type {
case dmp.DiffDelete:
b.WriteString("- ")
b.WriteString(strings.Replace(t, "\n", "\n- ", -1))
case dmp.DiffInsert:
b.WriteString("+ ")
b.WriteString(strings.Replace(t, "\n", "\n+ ", -1))
}
}
return b.String()
}

const defFormat = "delta"
switch format {
case "delta":
if delta := fmtDiff(dmp.New().DiffMain(last, next, true)); delta != "" {
log.Printf("[INFO] Config updates\n%s", delta)
}

case "all":
log.Printf("[INFO] Updated config to\n%s", next)

default:
log.Printf("[WARN] Invalid route format %q. Defaulting to %q", format, defFormat)
logRoutes(last, next, defFormat)
}
}

func toJSON(v interface{}) string {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
Expand Down

0 comments on commit e056671

Please sign in to comment.