Skip to content

Commit

Permalink
Add timing.
Browse files Browse the repository at this point in the history
  • Loading branch information
BartVerc committed Apr 13, 2021
1 parent 30a56dd commit e044dd1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Configuration:
plugin:
headers-by-request:
urlHeaderRequest: "http://127.0.0.1/resolve"
enableTiming: true
```
Request:
Expand Down
5 changes: 4 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
10 changes: 10 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e044dd1

Please sign in to comment.