This repository was archived by the owner on Sep 26, 2018. It is now read-only.
forked from traefik/traefik
-
Notifications
You must be signed in to change notification settings - Fork 0
Add backend conn stats JSON REST API and UI support #10
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6d7357
Add backend connection stats
mihaitodor 1e632c3
Add backend connection stats UI
mihaitodor e4f1d4e
Adjust bar plot to fit long Ox labels
mihaitodor 38555a9
Reduce conn stats data retention time in the UI to 30 minutes
mihaitodor 2915907
Add ConnLimits end-to-end integration test
mihaitodor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/containous/traefik/integration/utils" | ||
| "github.com/go-check/check" | ||
|
|
||
| checker "github.com/vdemeester/shakers" | ||
| ) | ||
|
|
||
| // ConnStats test suites (using libcompose) | ||
| type ConnStatsSuite struct { | ||
| BaseSuite | ||
| dummyConnStats ConnStats | ||
| whoamiReq *http.Request | ||
| } | ||
|
|
||
| type ConnStats struct { | ||
| File struct { | ||
| Backends struct { | ||
| Whoami struct { | ||
| MaxConn int `json:"max_conn"` | ||
| TotalConn int `json:"total_conn"` | ||
| } `json:"whoami"` | ||
| } `json:"backends"` | ||
| } `json:"file"` | ||
| } | ||
|
|
||
| func (s *ConnStatsSuite) SetUpSuite(c *check.C) { | ||
| s.createComposeProject(c, "conn_stats") | ||
| s.composeProject.Start(c) | ||
|
|
||
| err := json.Unmarshal( | ||
| []byte(`{"file":{"backends":{"whoami":{"max_conn":2,"total_conn":0}}}}`), | ||
| &s.dummyConnStats, | ||
| ) | ||
| c.Assert(err, checker.IsNil) | ||
|
|
||
| s.whoamiReq, err = http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/whoami", nil) | ||
| c.Assert(err, checker.IsNil) | ||
| } | ||
|
|
||
| func (s *ConnStatsSuite) TearDownSuite(c *check.C) { | ||
| if s.composeProject != nil { | ||
| s.composeProject.Stop(c) | ||
| } | ||
| } | ||
|
|
||
| func (s *ConnStatsSuite) ValidateWhoamiResponse(c *check.C, expectedResponseStatusCode int) { | ||
| whoamiResp, err := http.DefaultClient.Do(s.whoamiReq) | ||
| c.Assert(err, checker.IsNil) | ||
| c.Assert(whoamiResp.StatusCode, checker.Equals, expectedResponseStatusCode) | ||
| } | ||
|
|
||
| func (s *ConnStatsSuite) ValidateConnStats(c *check.C, totalConn int) { | ||
| resp, err := http.Get("http://127.0.0.1:8080/api/conn_stats") | ||
| c.Assert(err, checker.IsNil) | ||
|
|
||
| connStatsPayload, err := ioutil.ReadAll(resp.Body) | ||
| c.Assert(err, checker.IsNil) | ||
|
|
||
| var connStats ConnStats | ||
| err = json.Unmarshal( | ||
| connStatsPayload, | ||
| &connStats, | ||
| ) | ||
| c.Assert(err, checker.IsNil) | ||
|
|
||
| s.dummyConnStats.File.Backends.Whoami.TotalConn = totalConn | ||
| c.Assert(connStats, checker.DeepEquals, s.dummyConnStats) | ||
| } | ||
|
|
||
| func (s *ConnStatsSuite) TestEndToEndWorkflow(c *check.C) { | ||
| whoamiHost := s.composeProject.Container(c, "whoami").NetworkSettings.IPAddress | ||
|
|
||
| file := s.adaptFile(c, "fixtures/conn_stats/simple.toml", struct { | ||
| Server string | ||
| }{whoamiHost}) | ||
| defer os.Remove(file) | ||
| cmd := exec.Command(traefikBinary, "--configFile="+file) | ||
|
|
||
| err := cmd.Start() | ||
| c.Assert(err, checker.IsNil) | ||
| defer cmd.Process.Kill() | ||
|
|
||
| // Wait for traefik to start | ||
| err = utils.TryRequest( | ||
| "http://127.0.0.1:8080/api/providers", | ||
| 60*time.Second, | ||
| func(res *http.Response) error { | ||
| body, err := ioutil.ReadAll(res.Body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !strings.Contains(string(body), "Path:/whoami") { | ||
| return errors.New("Incorrect traefik config: " + string(body)) | ||
| } | ||
| return nil | ||
| }, | ||
| ) | ||
| c.Assert(err, checker.IsNil) | ||
|
|
||
| // Make sure we can reach the whoami backend server | ||
| s.ValidateWhoamiResponse(c, http.StatusOK) | ||
| s.ValidateConnStats(c, 0) | ||
|
|
||
| // Start two long running requests to the backend to reach the connection limit | ||
| whoamiWaitReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/whoami?wait=2s", nil) | ||
| c.Assert(err, checker.IsNil) | ||
| var wg sync.WaitGroup | ||
| wg.Add(2) | ||
| for i := 0; i < 2; i++ { | ||
| go func() { | ||
| resp, err := http.DefaultClient.Do(whoamiWaitReq) | ||
| c.Assert(err, checker.IsNil) | ||
| c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) | ||
| wg.Done() | ||
| }() | ||
| } | ||
|
|
||
| // Wait a bit for the backend to become saturated | ||
| time.Sleep(time.Second * 1) | ||
|
|
||
| s.ValidateWhoamiResponse(c, http.StatusTooManyRequests) | ||
| s.ValidateConnStats(c, 2) | ||
|
|
||
| // Wait for the backend to become available again | ||
| wg.Wait() | ||
|
|
||
| s.ValidateWhoamiResponse(c, http.StatusOK) | ||
| s.ValidateConnStats(c, 0) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| defaultEntryPoints = ["http"] | ||
|
|
||
| logLevel = "DEBUG" | ||
|
|
||
| [entryPoints] | ||
| [entryPoints.http] | ||
| address = ":8000" | ||
|
|
||
| [web] | ||
| address = ":8080" | ||
|
|
||
| [file] | ||
| [frontends] | ||
| [frontends.whoami] | ||
| backend = "whoami" | ||
| [frontends.whoami.routes.route] | ||
| rule = "Path:/whoami" | ||
|
|
||
| [backends] | ||
| [backends.whoami] | ||
| [backends.whoami.maxconn] | ||
| amount = 2 | ||
| extractorfunc = "request.host" | ||
| [backends.whoami.servers.server] | ||
| url = "http://{{.Server}}:80" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| whoami: | ||
| image: emilevauge/whoami |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 'use strict'; | ||
| var angular = require('angular'); | ||
|
|
||
| var traefikCoreConnStats = 'traefik.core.conn_stats'; | ||
| module.exports = traefikCoreConnStats; | ||
|
|
||
| angular | ||
| .module(traefikCoreConnStats, ['ngResource']) | ||
| .factory('ConnStats', ConnStats); | ||
|
|
||
| /** @ngInject */ | ||
| function ConnStats($resource) { | ||
| return $resource('../api/conn_stats'); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am using these reflection hacks for now, but I'll talk with the project maintainers about customising their oxy fork to expose the
totalConnectionsfield (and maybe add more stats). Not sure what they're planning to do with that fork, considering that the maintainers of vulcand/oxy aren't very responsive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just FYI, I noticed a while back their vendored oxy is different than the main oxy so I think they're already doing some customization.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I can send them a PR for that lib too, but let's first see if they're interested in it and if they want any changes in the design. Before I send them the PR, I was hoping to get it running in our system to make sure everything is fine. There are loads of moving pieces...