From 40b3c177035c12d66d598348c30c18568f45f381 Mon Sep 17 00:00:00 2001 From: SALLEYRON Julien Date: Thu, 30 Nov 2017 12:18:03 +0100 Subject: [PATCH] Fix metrics problem on multiple entrypoints --- integration/basic_test.go | 35 +++++++++++++++++++++++++ integration/fixtures/simple_stats.toml | 30 +++++++++++++++++++++ integration/resources/compose/stats.yml | 4 +++ server/server.go | 8 ++++-- 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 integration/fixtures/simple_stats.toml create mode 100644 integration/resources/compose/stats.yml diff --git a/integration/basic_test.go b/integration/basic_test.go index 2d78343af..017205d7c 100644 --- a/integration/basic_test.go +++ b/integration/basic_test.go @@ -189,6 +189,41 @@ func (s *SimpleSuite) TestApiOnSameEntryPoint(c *check.C) { c.Assert(err, checker.IsNil) } +func (s *SimpleSuite) TestStatsWithMultipleEntryPoint(c *check.C) { + s.createComposeProject(c, "stats") + s.composeProject.Start(c) + + whoami1 := "http://" + s.composeProject.Container(c, "whoami1").NetworkSettings.IPAddress + ":80" + whoami2 := "http://" + s.composeProject.Container(c, "whoami2").NetworkSettings.IPAddress + ":80" + + file := s.adaptFile(c, "fixtures/simple_stats.toml", struct { + Server1 string + Server2 string + }{whoami1, whoami2}) + cmd, output := s.traefikCmd(withConfigFile(file)) + defer output(c) + + err := cmd.Start() + c.Assert(err, checker.IsNil) + defer cmd.Process.Kill() + + err = try.GetRequest("http://127.0.0.1:8080/api", 1*time.Second, try.StatusCodeIs(http.StatusOK)) + c.Assert(err, checker.IsNil) + + err = try.GetRequest("http://127.0.0.1:8080/api/providers", 1*time.Second, try.BodyContains("PathPrefix")) + c.Assert(err, checker.IsNil) + + err = try.GetRequest("http://127.0.0.1:8000/whoami", 1*time.Second, try.StatusCodeIs(http.StatusOK)) + c.Assert(err, checker.IsNil) + + err = try.GetRequest("http://127.0.0.1:8080/whoami", 1*time.Second, try.StatusCodeIs(http.StatusOK)) + c.Assert(err, checker.IsNil) + + err = try.GetRequest("http://127.0.0.1:8080/health", 1*time.Second, try.BodyContains(`"total_status_code_count":{"200":2}`)) + c.Assert(err, checker.IsNil) + +} + func (s *SimpleSuite) TestNoAuthOnPing(c *check.C) { s.createComposeProject(c, "base") s.composeProject.Start(c) diff --git a/integration/fixtures/simple_stats.toml b/integration/fixtures/simple_stats.toml new file mode 100644 index 000000000..b5e38511b --- /dev/null +++ b/integration/fixtures/simple_stats.toml @@ -0,0 +1,30 @@ +debug=true + +[entryPoints] + [entryPoints.http] + address = ":8000" + +[api] +[file] + [backends] + [backends.backend1] + [backends.backend1.servers.server1] + url = "{{ .Server1 }}" + [backends.backend2] + [backends.backend2.servers.server1] + url = "{{ .Server2 }}" + + [frontends] + [frontends.frontend1] + entrypoints=["http"] + + backend = "backend1" + [frontends.frontend1.routes.test_1] + rule = "PathPrefix:/whoami" + + [frontends.frontend2] + backend = "backend2" + entrypoints=["traefik"] + + [frontends.frontend2.routes.test_1] + rule = "PathPrefix:/whoami" diff --git a/integration/resources/compose/stats.yml b/integration/resources/compose/stats.yml new file mode 100644 index 000000000..0256f9622 --- /dev/null +++ b/integration/resources/compose/stats.yml @@ -0,0 +1,4 @@ +whoami1: + image: emilevauge/whoami +whoami2: + image: emilevauge/whoami \ No newline at end of file diff --git a/server/server.go b/server/server.go index 70e2f72cc..3bffebc1f 100644 --- a/server/server.go +++ b/server/server.go @@ -289,10 +289,14 @@ func (s *Server) setupServerEntryPoint(newServerEntryPointName string, newServer serverMiddlewares = append(serverMiddlewares, middlewares.NewMetricsWrapper(s.metricsRegistry, newServerEntryPointName)) } if s.globalConfiguration.API != nil { - s.globalConfiguration.API.Stats = thoas_stats.New() + if s.globalConfiguration.API.Stats == nil { + s.globalConfiguration.API.Stats = thoas_stats.New() + } serverMiddlewares = append(serverMiddlewares, s.globalConfiguration.API.Stats) if s.globalConfiguration.API.Statistics != nil { - s.globalConfiguration.API.StatsRecorder = middlewares.NewStatsRecorder(s.globalConfiguration.API.Statistics.RecentErrors) + if s.globalConfiguration.API.StatsRecorder == nil { + s.globalConfiguration.API.StatsRecorder = middlewares.NewStatsRecorder(s.globalConfiguration.API.Statistics.RecentErrors) + } serverMiddlewares = append(serverMiddlewares, s.globalConfiguration.API.StatsRecorder) }