From e044dd14a2fa04d7b4ea47b81903a262a06578f1 Mon Sep 17 00:00:00 2001 From: Bart Vercoulen Date: Tue, 13 Apr 2021 14:49:42 +0200 Subject: [PATCH] Add timing. --- .traefik.yml | 1 + README.md | 1 + config.go | 5 ++++- router.go | 10 ++++++++++ router_test.go | 1 + 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.traefik.yml b/.traefik.yml index e695cac..96bf78b 100644 --- a/.traefik.yml +++ b/.traefik.yml @@ -13,3 +13,4 @@ summary: Requesting headers dynamically via a url post. # Test data the plugin will use to test itself on startup testData: urlHeaderRequest: http://127.0.0.1/resolve + enableTiming: true \ No newline at end of file diff --git a/README.md b/README.md index fcc8db4..b0ddd1d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Configuration: plugin: headers-by-request: urlHeaderRequest: "http://127.0.0.1/resolve" + enableTiming: true ``` Request: diff --git a/config.go b/config.go index afa3af4..a7be73a 100644 --- a/config.go +++ b/config.go @@ -3,9 +3,12 @@ package headers_by_request // Config the plugin configuration. type Config struct { UrlHeaderRequest string `json:"urlHeaderRequest,omitempty"` + EnableTiming bool `json:"enableTiming,omitempty"` } // CreateConfig creates the default plugin configuration. func CreateConfig() *Config { - return &Config{ UrlHeaderRequest: ""} + return &Config{ + UrlHeaderRequest: "", + EnableTiming: false} } diff --git a/router.go b/router.go index f4e4168..f05d3d0 100644 --- a/router.go +++ b/router.go @@ -22,6 +22,7 @@ type Router struct { // Our custom configuration dynamicHeaderUrl string + enableTiming bool } // Function needed for Traefik to recognize this module as a plugin @@ -40,6 +41,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h return &Router{ dynamicHeaderUrl: config.UrlHeaderRequest, + enableTiming: config.EnableTiming, next: next, name: name, }, nil @@ -51,6 +53,10 @@ type HeadersRequested struct { func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + startTime := time.Time{} + if a.enableTiming { + startTime = time.Now() + } log.Println(fmt.Sprintf("Resolving header for %s", req.URL)) requestBody, err := json.Marshal(map[string]string{ @@ -96,5 +102,9 @@ func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { req.Header.Set(key, value) } + if a.enableTiming { + timeDiff := time.Now().Sub(startTime) + log.Println(fmt.Sprintf("%s took %s", a.name, timeDiff)) + } a.next.ServeHTTP(rw, req) } diff --git a/router_test.go b/router_test.go index 32cc2fd..8e0630f 100644 --- a/router_test.go +++ b/router_test.go @@ -53,6 +53,7 @@ func TestRouter(t *testing.T) { cfg := headers_by_request.CreateConfig() cfg.UrlHeaderRequest = mockServerURL + cfg.EnableTiming = true handler, err := headers_by_request.New(ctx, next, cfg, "headers-by-request") if err != nil {