Skip to content

Commit

Permalink
PR #292: Add unique request id
Browse files Browse the repository at this point in the history
This patch add proxy.header.requestid configuration option
for specifying header name for adding unique ID to each http
request proxied by fabio.

This header value can be logged in fabio access log and used
for request processing tracking across different request
handling applications.
  • Loading branch information
bkmit authored and magiconair committed May 16, 2017
1 parent f6da7ac commit 616b85b
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Proxy struct {
TLSHeader string
TLSHeaderValue string
GZIPContentTypes *regexp.Regexp
RequestID string
}

type Runtime struct {
Expand Down
1 change: 1 addition & 0 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.StringVar(&cfg.Proxy.ClientIPHeader, "proxy.header.clientip", defaultConfig.Proxy.ClientIPHeader, "header for the request ip")
f.StringVar(&cfg.Proxy.TLSHeader, "proxy.header.tls", defaultConfig.Proxy.TLSHeader, "header for TLS connections")
f.StringVar(&cfg.Proxy.TLSHeaderValue, "proxy.header.tls.value", defaultConfig.Proxy.TLSHeaderValue, "value for TLS connection header")
f.StringVar(&cfg.Proxy.RequestID, "proxy.header.requestid", defaultConfig.Proxy.RequestID, "header for reqest id")
f.StringVar(&gzipContentTypesValue, "proxy.gzip.contenttype", defaultValues.GZIPContentTypesValue, "regexp of content types to compress")
f.StringSliceVar(&listenerValue, "proxy.addr", defaultValues.ListenerValue, "listener config")
f.KVSliceVar(&certSourcesValue, "proxy.cs", defaultValues.CertSourcesValue, "certificate sources")
Expand Down
7 changes: 7 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
args: []string{"-proxy.header.requestid", "value"},
cfg: func(cfg *Config) *Config {
cfg.Proxy.RequestID = "value"
return cfg
},
},
{
args: []string{"-proxy.gzip.contenttype", `^text/.*$`},
cfg: func(cfg *Config) *Config {
Expand Down
9 changes: 9 additions & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@
# proxy.header.tls.value =


# proxy.header.requestid configures the header for the adding a unique request id.
# When set non-empty value the proxy will set this header on every request to the
# unique UUID value.
#
# The default is
#
# proxy.header.requestid =


# proxy.gzip.contenttype configures which responses should be compressed.
#
# By default, responses sent to the client are not compressed even if the
Expand Down
25 changes: 25 additions & 0 deletions proxy/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ func TestProxyProducesCorrectXffHeader(t *testing.T) {
}
}

func TestProxyRequestIDHeader(t *testing.T) {
got := "not called"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got = r.Header.Get("X-Request-ID")
}))
defer server.Close()

proxy := httptest.NewServer(&HTTPProxy{
Config: config.Proxy{RequestID: "X-Request-Id"},
Transport: http.DefaultTransport,
UUID: func() string { return "f47ac10b-58cc-0372-8567-0e02b2c3d479" },
Lookup: func(r *http.Request) *route.Target {
return &route.Target{URL: mustParse(server.URL)}
},
})
defer proxy.Close()

req, _ := http.NewRequest("GET", proxy.URL, nil)
mustDo(req)

if want := "f47ac10b-58cc-0372-8567-0e02b2c3d479"; got != want {
t.Errorf("got %v, but want %v", got, want)
}
}

func TestProxyNoRouteStaus(t *testing.T) {
proxy := httptest.NewServer(&HTTPProxy{
Config: config.Proxy{NoRouteStatus: 999},
Expand Down
13 changes: 13 additions & 0 deletions proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/fabiolb/fabio/metrics"
"github.com/fabiolb/fabio/proxy/gzip"
"github.com/fabiolb/fabio/route"
"github.com/fabiolb/fabio/uuid"
)

// HTTPProxy is a dynamic reverse proxy for HTTP and HTTPS protocols.
Expand Down Expand Up @@ -48,6 +49,10 @@ type HTTPProxy struct {

// Logger is the access logger for the requests.
Logger logger.Logger

// UUID returns a unique id in uuid format.
// If UUID is nil, uuid.NewUUID() is used.
UUID func() string
}

func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -66,6 +71,14 @@ func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

if p.Config.RequestID != "" {
id := p.UUID
if id == nil {
id = uuid.NewUUID
}
r.Header.Set(p.Config.RequestID, id())
}

// build the request url since r.URL will get modified
// by the reverse proxy and contains only the RequestURI anyway
requestURL := &url.URL{
Expand Down
62 changes: 62 additions & 0 deletions uuid/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package uuid

// Fast UUID formatting adapted from
// https://github.com/m4rw3r/uuid/blob/master/uuid.go

// hexchar2byte contains the integer byte-value represented by a hexadecimal character,
// 255 if it is an invalid character.
var hexchar2byte = []byte{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
}

// halfbyte2hexchar contains an array of character values corresponding to
// hexadecimal values for the position in the array, 0 to 15 (0x0-0xf, half-byte).
var halfbyte2hexchar = []byte{
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102,
}

// ToString formats raw UUID bytes as a standard UUID string
func ToString(u [24]byte) string {
/* It is a lot (~10x) faster to allocate a byte slice of specific size and
then use a lookup table to write the characters to the byte-array and
finally cast to string instead of using fmt.Sprintf() */
/* Slightly faster to not use make([]byte, 36), guessing either call
overhead or slice-header overhead is the cause */
b := [36]byte{}

for i, n := range []int{
0, 2, 4, 6,
9, 11,
14, 16,
19, 21,
24, 26, 28, 30, 32, 34,
} {
b[n] = halfbyte2hexchar[(u[i]>>4)&0x0f]
b[n+1] = halfbyte2hexchar[u[i]&0x0f]
}

b[8] = '-'
b[13] = '-'
b[18] = '-'
b[23] = '-'

/* Oddly does not seem to cause a memory allocation,
internal data-array is most likely just moved over
to the string-header: */
return string(b[:])
}
12 changes: 12 additions & 0 deletions uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uuid

import (
"github.com/rogpeppe/fastuuid"
)

var generator = fastuuid.MustNewGenerator()

// NewUUID return UUID in string fromat
func NewUUID() string {
return ToString(generator.Next())
}

0 comments on commit 616b85b

Please sign in to comment.