improve syncer debugging with trace logging

This commit is contained in:
Brad Rydzewski 2019-09-06 14:49:50 -07:00
parent 870e967ee8
commit 8627d2100a
4 changed files with 81 additions and 1 deletions

View file

@ -37,6 +37,7 @@ import (
globalsecrets "github.com/drone/drone/handler/api/secrets"
"github.com/drone/drone/handler/api/system"
"github.com/drone/drone/handler/api/user"
"github.com/drone/drone/handler/api/user/remote"
"github.com/drone/drone/handler/api/users"
"github.com/drone/drone/logger"
@ -274,6 +275,9 @@ func (s Server) Handler() http.Handler {
// TODO(bradrydzewski) finalize the name for this endpoint.
r.Get("/builds", user.HandleRecent(s.Repos))
r.Get("/builds/recent", user.HandleRecent(s.Repos))
// expose remote endpoints (e.g. to github)
r.Get("/remote/repos", remote.HandleRepos(s.Repoz))
})
r.Route("/users", func(r chi.Router) {

View file

@ -0,0 +1,41 @@
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package remote
import (
"net/http"
"github.com/drone/drone/core"
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/handler/api/request"
"github.com/drone/drone/logger"
)
// HandleRepos returns an http.HandlerFunc that write a json-encoded
// list of repositories to the response body.
func HandleRepos(repos core.RepositoryService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
viewer, _ := request.UserFrom(r.Context())
list, err := repos.List(r.Context(), viewer)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).WithError(err).
Debugln("api: cannot list remote repositories")
} else {
render.JSON(w, list, 200)
}
}
}

View file

@ -104,6 +104,19 @@ func (s *Synchronizer) Sync(ctx context.Context, user *core.User) (*core.Batch,
for _, repo := range repos {
if s.match(repo) {
remote[repo.UID] = repo
if logrus.GetLevel() == logrus.TraceLevel {
logger.WithField("namespace", repo.Namespace).
WithField("name", repo.Name).
WithField("uid", repo.UID).
Traceln("syncer: remote repository matches filter")
}
} else {
if logrus.GetLevel() == logrus.TraceLevel {
logger.WithField("namespace", repo.Namespace).
WithField("name", repo.Name).
WithField("uid", repo.UID).
Traceln("syncer: remote repository does not match filter")
}
}
}
}
@ -141,11 +154,18 @@ func (s *Synchronizer) Sync(ctx context.Context, user *core.User) (*core.Batch,
v.Updated = time.Now().Unix()
v.Version = 1
batch.Insert = append(batch.Insert, v)
if logrus.GetLevel() == logrus.TraceLevel {
logger.WithField("namespace", v.Namespace).
WithField("name", v.Name).
WithField("uid", v.UID).
Traceln("syncer: remote repository not in database")
}
}
//
// STEP4 find repos that exist in the remote system and
// in the local system, but with incorect data. Update.
// in the local system, but with incorrect data. Update.
//
for k, v := range local {
@ -158,6 +178,13 @@ func (s *Synchronizer) Sync(ctx context.Context, user *core.User) (*core.Batch,
v.Synced = time.Now().Unix()
v.Updated = time.Now().Unix()
batch.Update = append(batch.Update, v)
if logrus.GetLevel() == logrus.TraceLevel {
logger.WithField("namespace", v.Namespace).
WithField("name", v.Name).
WithField("uid", v.UID).
Traceln("syncer: repository requires update")
}
}
}
@ -172,6 +199,13 @@ func (s *Synchronizer) Sync(ctx context.Context, user *core.User) (*core.Batch,
continue
}
batch.Revoke = append(batch.Revoke, v)
if logrus.GetLevel() == logrus.TraceLevel {
logger.WithField("namespace", v.Namespace).
WithField("name", v.Name).
WithField("uid", v.UID).
Traceln("syncer: repository in database not in remote repository list")
}
}
//

View file

@ -27,6 +27,7 @@ var noContext = context.Background()
func init() {
logrus.SetOutput(ioutil.Discard)
logrus.SetLevel(logrus.TraceLevel)
}
func TestSync(t *testing.T) {