Skip to content

Commit

Permalink
Merge pull request #182 from allegro/feature/sync-metrics
Browse files Browse the repository at this point in the history
Fixes #181 | Count sync (de)registration
  • Loading branch information
tomez authored Feb 17, 2017
2 parents e51fdef + fe30fd6 commit fb88626
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ func (s *Sync) syncServices() error {
return fmt.Errorf("Can't get Consul services: %v", err)
}

s.registerAppTasksNotFoundInConsul(apps, services)
s.deregisterConsulServicesNotFoundInMarathon(apps, services)
registerCount := s.registerAppTasksNotFoundInConsul(apps, services)
deregisterCount := s.deregisterConsulServicesNotFoundInMarathon(apps, services)

log.Info("Syncing services finished")
metrics.UpdateGauge("sync.register", int64(registerCount))
metrics.UpdateGauge("sync.deregister", int64(deregisterCount))

log.Infof("Syncing services finished. Stats, register: %d, deregister: %d", registerCount, deregisterCount)
return nil
}

Expand Down Expand Up @@ -115,7 +118,8 @@ func (s *Sync) resolveHostname() error {
return nil
}

func (s *Sync) deregisterConsulServicesNotFoundInMarathon(marathonApps []*apps.App, services []*service.Service) {
func (s *Sync) deregisterConsulServicesNotFoundInMarathon(marathonApps []*apps.App, services []*service.Service) int {
deregisterCount := 0
runningTasks := s.marathonTaskIdsSet(marathonApps)
for _, service := range services {
taskIDInTag, err := service.TaskId()
Expand All @@ -132,14 +136,18 @@ func (s *Sync) deregisterConsulServicesNotFoundInMarathon(marathonApps []*apps.A
"Id": service.ID,
"Address": service.RegisteringAgentAddress,
}).Error("Can't deregister service")
} else {
deregisterCount++
}
} else {
log.WithField("Id", service.ID).Debug("Service is running")
}
}
return deregisterCount
}

func (s *Sync) registerAppTasksNotFoundInConsul(marathonApps []*apps.App, services []*service.Service) {
func (s *Sync) registerAppTasksNotFoundInConsul(marathonApps []*apps.App, services []*service.Service) int {
registerCount := 0
registrationsUnderTaskIds := s.taskIdsInConsulServices(services)
for _, app := range marathonApps {
if !app.IsConsulApp() {
Expand All @@ -158,6 +166,8 @@ func (s *Sync) registerAppTasksNotFoundInConsul(marathonApps []*apps.App, servic
err := s.serviceRegistry.Register(&task, app)
if err != nil {
log.WithError(err).WithField("Id", task.ID).Error("Can't register task")
} else {
registerCount++
}
} else {
log.WithField("Id", task.ID).Debug("Task is not healthy. Not Registering")
Expand All @@ -170,6 +180,7 @@ func (s *Sync) registerAppTasksNotFoundInConsul(marathonApps []*apps.App, servic
}
}
}
return registerCount
}

func (s *Sync) taskIdsInConsulServices(services []*service.Service) map[apps.TaskID]int {
Expand Down

0 comments on commit fb88626

Please sign in to comment.