Skip to content

Commit

Permalink
Add request id uuidv4 format option #31 (#53)
Browse files Browse the repository at this point in the history
* Add support for uuid request id. Fixes #31

* Map settings request id format value to http config and fix default values

* Fix imports and formatting

* Fix formatting

* Add request-id-format flag
  • Loading branch information
cvhariharan authored Oct 19, 2020
1 parent 7090273 commit 9267902
Show file tree
Hide file tree
Showing 13 changed files with 824 additions and 13 deletions.
1 change: 1 addition & 0 deletions command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (r Run) Execute(args Args, config *config.Gateway, logEntry *logrus.Entry)
set.StringVar(&httpConf.HealthPath, "health-path", httpConf.HealthPath, "-health-path /healthz")
set.IntVar(&httpConf.ListenPort, "p", httpConf.ListenPort, "-p 8080")
set.BoolVar(&httpConf.UseXFH, "xfh", httpConf.UseXFH, "-xfh")
set.StringVar(&httpConf.RequestIDFormat, "request-id-format", httpConf.RequestIDFormat, "-request-id-format uuid4")
if err := set.Parse(args.Filter(set)); err != nil {
return err
}
Expand Down
25 changes: 16 additions & 9 deletions config/runtime/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ type ServerMux struct {

// HTTPConfig represents the configuration of the ingress HTTP server.
type HTTPConfig struct {
HealthPath string `env:"health_path"`
ListenPort int `env:"default_port"`
UseXFH bool `env:"xfh"`
Timings HTTPTimings
HealthPath string `env:"health_path"`
ListenPort int `env:"default_port"`
UseXFH bool `env:"xfh"`
RequestIDFormat string `env:"request_id_format"`
Timings HTTPTimings
}

type HTTPTimings struct {
Expand Down Expand Up @@ -55,7 +56,8 @@ var DefaultHTTP = &HTTPConfig{
ShutdownDelay: time.Second * 5,
ShutdownTimeout: time.Second * 5,
},
ListenPort: 8080,
ListenPort: 8080,
RequestIDFormat: "common",
}

// NewHTTPConfig creates the server config which could be overridden in order:
Expand All @@ -72,10 +74,11 @@ func NewHTTPConfig(c *config.Gateway) *HTTPConfig {

func newHTTPConfigFrom(s *config.Settings) *HTTPConfig {
return &HTTPConfig{
HealthPath: s.HealthPath,
ListenPort: s.DefaultPort,
UseXFH: s.XForwardedHost,
Timings: DefaultHTTP.Timings,
HealthPath: s.HealthPath,
ListenPort: s.DefaultPort,
UseXFH: s.XForwardedHost,
RequestIDFormat: s.RequestIDFormat,
Timings: DefaultHTTP.Timings,
}
}

Expand All @@ -96,6 +99,10 @@ func (c *HTTPConfig) Merge(o *HTTPConfig) *HTTPConfig {
c.UseXFH = o.UseXFH
}

if o.RequestIDFormat != "" {
c.RequestIDFormat = o.RequestIDFormat
}

return c
}

Expand Down
9 changes: 5 additions & 4 deletions config/settings.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

type Settings struct {
DefaultPort int `hcl:"default_port,optional"`
HealthPath string `hcl:"health_path,optional"`
LogFormat string `hcl:"log_format,optional"`
XForwardedHost bool `hcl:"xfh,optional"`
DefaultPort int `hcl:"default_port,optional"`
HealthPath string `hcl:"health_path,optional"`
LogFormat string `hcl:"log_format,optional"`
XForwardedHost bool `hcl:"xfh,optional"`
RequestIDFormat string `hcl:"request_id_format,optional"`
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
github.com/hashicorp/hcl/v2 v2.5.1
github.com/rs/xid v1.2.1
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.6.0
github.com/zclconf/go-cty v1.2.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand Down
6 changes: 6 additions & 0 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/rs/xid"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"

"github.com/avenga/couper/config/env"
Expand Down Expand Up @@ -55,6 +56,11 @@ func New(cmdCtx context.Context, log logrus.FieldLogger, conf *runtime.HTTPConfi
uidFn := func() string {
return xid.New().String()
}
if conf != nil && conf.RequestIDFormat == "uuid4" {
uidFn = func() string {
return uuid.NewV4().String()
}
}

logConf := *logging.DefaultConfig
logConf.TypeFieldKey = "couper_access"
Expand Down
23 changes: 23 additions & 0 deletions vendor/github.com/satori/go.uuid/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/satori/go.uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions vendor/github.com/satori/go.uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9267902

Please sign in to comment.