Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log parse duration errors #487

Merged
merged 7 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Unreleased changes are available as `avenga/couper:edge` container.
* provided via `-d` command-line flag or `COUPER_FILE_DIRECTORY` environment variable
* `beta_health`-block to `backend`-block to enable continuous health-checks for defined backends ([#313](https://github.com/avenga/couper/pull/313))
* `backends.<name>.health` variable to access the current health-check state _(subject to change)_
* Log malformed duration settings ([#487](https://github.com/avenga/couper/pull/487))

* **Changed**
* Permission handling: ([#477](https://github.com/avenga/couper/pull/477))
Expand Down
2 changes: 1 addition & 1 deletion handler/transport/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ func (b *Backend) evalTransport(httpCtx *hcl.EvalContext, params hcl.Body, req *

return b.transportConf.
WithTarget(originURL.Scheme, originURL.Host, hostname, proxyURL).
WithTimings(connectTimeout, ttfbTimeout, timeout), nil
WithTimings(connectTimeout, ttfbTimeout, timeout, log), nil
}

// setUserAgent sets an empty one if none is present or empty
Expand Down
13 changes: 8 additions & 5 deletions handler/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,20 @@ func (c *Config) WithTarget(scheme, origin, hostname, proxyURL string) *Config {
return &conf
}

func (c *Config) WithTimings(connect, ttfb, timeout string) *Config {
func (c *Config) WithTimings(connect, ttfb, timeout string, logger *logrus.Entry) *Config {
conf := *c
parseDuration(connect, &conf.ConnectTimeout)
parseDuration(ttfb, &conf.TTFBTimeout)
parseDuration(timeout, &conf.Timeout)
parseDuration(connect, &conf.ConnectTimeout, logger)
parseDuration(ttfb, &conf.TTFBTimeout, logger)
parseDuration(timeout, &conf.Timeout, logger)
return &conf
}

// parseDuration sets the target value if the given duration string is not empty.
func parseDuration(src string, target *time.Duration) {
func parseDuration(src string, target *time.Duration, logger *logrus.Entry) {
d, err := time.ParseDuration(src)
if err != nil {
logger.WithError(err).Warning("using default timing of ", target, " because an error occured")
}
if src != "" && err != nil {
return
}
Expand Down
7 changes: 6 additions & 1 deletion handler/transport/transport_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package transport

import (
"context"
"testing"
"time"

"github.com/sirupsen/logrus"
)

func Test_parseDuration(t *testing.T) {
log := logrus.New()

var target time.Duration
parseDuration("1ms", &target)
parseDuration("1ms", &target, log.WithContext(context.TODO()))

if target != 1000000 {
t.Errorf("Unexpected duration given: %#v", target)
Expand Down
21 changes: 21 additions & 0 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,24 @@ func TestHTTPServer_RequestID(t *testing.T) {
})
}
}

func TestHTTPServer_parseDuration(t *testing.T) {
helper := test.New(t)
client := newClient()

shutdown, logHook := newCouper("testdata/integration/config/16_couper.hcl", test.New(t))
defer shutdown()

logHook.Reset()
req, err := http.NewRequest(http.MethodGet, "http://anyserver:8080/", nil)
helper.Must(err)

_, err = client.Do(req)
helper.Must(err)

logs := logHook.AllEntries()

if logs[0].Message != `using default timing of 0s because an error occured: time: invalid duration "xxx"` {
t.Errorf("%#v", logs[0].Message)
}
}
10 changes: 10 additions & 0 deletions server/testdata/integration/config/16_couper.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
server {
endpoint "/" {
proxy {
backend {
origin = "${env.COUPER_TEST_BACKEND_ADDR}"
timeout = "xxx"
}
}
}
}