1
0
Fork 0

refactor: Logs & errors review.

- log & error: remove format if not necessary, add if necessary.
- add constants for k8s annotations.
- fix typos
This commit is contained in:
Fernandez Ludovic 2017-05-26 17:03:14 +02:00 committed by Ludovic Fernandez
parent 994e135368
commit cbccdd51c5
26 changed files with 125 additions and 128 deletions

View file

@ -27,5 +27,4 @@ func (oxylogger *OxyLogger) Errorf(format string, args ...interface{}) {
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
//templatesRenderer.HTML(w, http.StatusNotFound, "notFound", nil)
}

View file

@ -88,7 +88,7 @@ func (dep *DefaultEntryPoints) String() string {
func (dep *DefaultEntryPoints) Set(value string) error {
entrypoints := strings.Split(value, ",")
if len(entrypoints) == 0 {
return errors.New("Bad DefaultEntryPoints format: " + value)
return fmt.Errorf("bad DefaultEntryPoints format: %s", value)
}
for _, entrypoint := range entrypoints {
*dep = append(*dep, entrypoint)
@ -127,7 +127,7 @@ func (ep *EntryPoints) Set(value string) error {
regex := regexp.MustCompile("(?:Name:(?P<Name>\\S*))\\s*(?:Address:(?P<Address>\\S*))?\\s*(?:TLS:(?P<TLS>\\S*))?\\s*((?P<TLSACME>TLS))?\\s*(?:CA:(?P<CA>\\S*))?\\s*(?:Redirect.EntryPoint:(?P<RedirectEntryPoint>\\S*))?\\s*(?:Redirect.Regex:(?P<RedirectRegex>\\S*))?\\s*(?:Redirect.Replacement:(?P<RedirectReplacement>\\S*))?\\s*(?:Compress:(?P<Compress>\\S*))?")
match := regex.FindAllStringSubmatch(value, -1)
if match == nil {
return errors.New("Bad EntryPoints format: " + value)
return fmt.Errorf("bad EntryPoints format: %s", value)
}
matchResult := match[0]
result := make(map[string]string)
@ -269,10 +269,10 @@ func (certs *Certificates) CreateTLSConfig() (*tls.Config, error) {
if errKey == nil {
isAPath = true
} else {
return nil, fmt.Errorf("bad TLS Certificate KeyFile format, expected a path")
return nil, errors.New("bad TLS Certificate KeyFile format, expected a path")
}
} else if errKey == nil {
return nil, fmt.Errorf("bad TLS Certificate KeyFile format, expected a path")
return nil, errors.New("bad TLS Certificate KeyFile format, expected a path")
}
cert := tls.Certificate{}
@ -314,7 +314,7 @@ func (certs *Certificates) Set(value string) error {
for _, certificate := range certificates {
files := strings.Split(certificate, ",")
if len(files) != 2 {
return errors.New("Bad certificates format: " + value)
return fmt.Errorf("bad certificates format: %s", value)
}
*certs = append(*certs, Certificate{
CertFile: files[0],

View file

@ -167,12 +167,12 @@ func (r *Rules) parseRules(expression string, onRule func(functionName string, f
// get function
parsedFunctions := strings.FieldsFunc(rule, f)
if len(parsedFunctions) == 0 {
return errors.New("Error parsing rule: '" + rule + "'")
return fmt.Errorf("error parsing rule: '%s'", rule)
}
functionName := strings.TrimSpace(parsedFunctions[0])
parsedFunction, ok := functions[functionName]
if !ok {
return errors.New("Error parsing rule: '" + rule + "'. Unknown function: '" + parsedFunctions[0] + "'")
return fmt.Errorf("error parsing rule: '%s'. Unknown function: '%s'", rule, parsedFunctions[0])
}
parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...)
fargs := func(c rune) bool {
@ -181,7 +181,7 @@ func (r *Rules) parseRules(expression string, onRule func(functionName string, f
// get function
parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs)
if len(parsedArgs) == 0 {
return errors.New("Error parsing args from rule: '" + rule + "'")
return fmt.Errorf("error parsing args from rule: '%s'", rule)
}
for i := range parsedArgs {
@ -215,12 +215,12 @@ func (r *Rules) Parse(expression string) (*mux.Route, error) {
}
} else {
return errors.New("Method not found: '" + functionName + "'")
return fmt.Errorf("Method not found: '%s'", functionName)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("Error parsing rule: %v", err)
return nil, fmt.Errorf("error parsing rule: %v", err)
}
return resultRoute, nil
}
@ -235,7 +235,7 @@ func (r *Rules) ParseDomains(expression string) ([]string, error) {
return nil
})
if err != nil {
return nil, fmt.Errorf("Error parsing domains: %v", err)
return nil, fmt.Errorf("error parsing domains: %v", err)
}
return fun.Map(types.CanonicalDomain, domains).([]string), nil
}

View file

@ -19,15 +19,14 @@ func TestParseOneRule(t *testing.T) {
routeResult, err := rules.Parse(expression)
if err != nil {
t.Fatal("Error while building route for Host:foo.bar")
t.Fatalf("Error while building route for Host:foo.bar: %s", err)
}
request, err := http.NewRequest("GET", "http://foo.bar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
if routeMatch == false {
t.Log(err)
t.Fatal("Rule Host:foo.bar don't match")
if !routeMatch {
t.Fatalf("Rule Host:foo.bar don't match: %s", err)
}
}
@ -41,23 +40,21 @@ func TestParseTwoRules(t *testing.T) {
routeResult, err := rules.Parse(expression)
if err != nil {
t.Fatal("Error while building route for Host:foo.bar;Path:/FOObar")
t.Fatalf("Error while building route for Host:foo.bar;Path:/FOObar: %s", err)
}
request, err := http.NewRequest("GET", "http://foo.bar/foobar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
if routeMatch == true {
t.Log(err)
t.Fatal("Rule Host:foo.bar;Path:/FOObar don't match")
if routeMatch {
t.Fatalf("Rule Host:foo.bar;Path:/FOObar don't match: %s", err)
}
request, err = http.NewRequest("GET", "http://foo.bar/FOObar", nil)
routeMatch = routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
if routeMatch == false {
t.Log(err)
t.Fatal("Rule Host:foo.bar;Path:/FOObar don't match")
if !routeMatch {
t.Fatalf("Rule Host:foo.bar;Path:/FOObar don't match: %s", err)
}
}
@ -92,7 +89,7 @@ func TestPriorites(t *testing.T) {
rules := &Rules{route: &serverRoute{route: router.NewRoute()}}
routeFoo, err := rules.Parse("PathPrefix:/foo")
if err != nil {
t.Fatal("Error while building route for PathPrefix:/foo")
t.Fatalf("Error while building route for PathPrefix:/foo: %s", err)
}
fooHandler := &fakeHandler{name: "fooHandler"}
routeFoo.Handler(fooHandler)
@ -100,40 +97,40 @@ func TestPriorites(t *testing.T) {
if !router.Match(&http.Request{URL: &url.URL{
Path: "/foo",
}}, &mux.RouteMatch{}) {
t.Fatalf("Error matching route")
t.Fatal("Error matching route")
}
if router.Match(&http.Request{URL: &url.URL{
Path: "/fo",
}}, &mux.RouteMatch{}) {
t.Fatalf("Error matching route")
t.Fatal("Error matching route")
}
multipleRules := &Rules{route: &serverRoute{route: router.NewRoute()}}
routeFoobar, err := multipleRules.Parse("PathPrefix:/foobar")
if err != nil {
t.Fatal("Error while building route for PathPrefix:/foobar")
t.Fatalf("Error while building route for PathPrefix:/foobar: %s", err)
}
foobarHandler := &fakeHandler{name: "foobarHandler"}
routeFoobar.Handler(foobarHandler)
if !router.Match(&http.Request{URL: &url.URL{
Path: "/foo",
}}, &mux.RouteMatch{}) {
t.Fatalf("Error matching route")
t.Fatal("Error matching route")
}
fooMatcher := &mux.RouteMatch{}
if !router.Match(&http.Request{URL: &url.URL{
Path: "/foobar",
}}, fooMatcher) {
t.Fatalf("Error matching route")
t.Fatal("Error matching route")
}
if fooMatcher.Handler == foobarHandler {
t.Fatalf("Error matching priority")
t.Fatal("Error matching priority")
}
if fooMatcher.Handler != fooHandler {
t.Fatalf("Error matching priority")
t.Fatal("Error matching priority")
}
routeFoo.Priority(1)
@ -144,15 +141,15 @@ func TestPriorites(t *testing.T) {
if !router.Match(&http.Request{URL: &url.URL{
Path: "/foobar",
}}, foobarMatcher) {
t.Fatalf("Error matching route")
t.Fatal("Error matching route")
}
if foobarMatcher.Handler != foobarHandler {
t.Fatalf("Error matching priority")
t.Fatal("Error matching priority")
}
if foobarMatcher.Handler == fooHandler {
t.Fatalf("Error matching priority")
t.Fatal("Error matching priority")
}
}
@ -160,6 +157,4 @@ type fakeHandler struct {
name string
}
func (h *fakeHandler) ServeHTTP(http.ResponseWriter, *http.Request) {
}
func (h *fakeHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}

View file

@ -91,7 +91,7 @@ func NewServer(globalConfiguration GlobalConfiguration) *Server {
var err error
server.accessLoggerMiddleware, err = accesslog.NewLogHandler(globalConfiguration.AccessLogsFile)
if err != nil {
log.Warn("Unable to create log handler: %s", err)
log.Warnf("Unable to create log handler: %s", err)
}
return server
}
@ -147,7 +147,7 @@ func (server *Server) Close() {
if ctx.Err() == context.Canceled {
return
} else if ctx.Err() == context.DeadlineExceeded {
log.Warnf("Timeout while stopping traefik, killing instance ✝")
log.Warn("Timeout while stopping traefik, killing instance ✝")
os.Exit(1)
}
}(ctx)
@ -736,6 +736,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
negroni.Use(metricsMiddlewareBackend)
}
}
ipWhitelistMiddleware, err := configureIPWhitelistMiddleware(frontend.WhitelistSourceRange)
if err != nil {
log.Fatalf("Error creating IP Whitelister: %s", err)
@ -761,6 +762,7 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
negroni.Use(authMiddleware)
}
}
if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
log.Debugf("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression)
cbreaker, err := middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger))

View file

@ -126,7 +126,8 @@ func (provider *WebProvider) Provide(configurationChan chan<- types.ConfigMessag
systemRouter.Methods("GET").Path(provider.Path).HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
http.Redirect(response, request, provider.Path+"dashboard/", 302)
})
systemRouter.Methods("GET").PathPrefix(provider.Path + "dashboard/").Handler(http.StripPrefix(provider.Path+"dashboard/", http.FileServer(&assetfs.AssetFS{Asset: autogen.Asset, AssetInfo: autogen.AssetInfo, AssetDir: autogen.AssetDir, Prefix: "static"})))
systemRouter.Methods("GET").PathPrefix(provider.Path + "dashboard/").
Handler(http.StripPrefix(provider.Path+"dashboard/", http.FileServer(&assetfs.AssetFS{Asset: autogen.Asset, AssetInfo: autogen.AssetInfo, AssetDir: autogen.AssetDir, Prefix: "static"})))
// expvars
if provider.server.globalConfiguration.Debug {