Skip to content

Commit

Permalink
Prevent collector deregistration on failed server conection (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Sturm authored and bernd committed Jun 29, 2018
1 parent d92f8e5 commit 3a319b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
9 changes: 6 additions & 3 deletions api/graylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func RequestBackendList(httpClient *http.Client, checksum string, ctx *context.C
msg := "Fetching backend list"
system.GlobalStatus.Set(backends.StatusError, msg)
log.Errorf("[RequestBackendList] %s: %v", msg, err)
return graylog.ResponseBackendList{}, err
}

if resp != nil {
Expand Down Expand Up @@ -131,7 +132,7 @@ func RequestConfiguration(
return configurationResponse, nil
}

func UpdateRegistration(httpClient *http.Client, ctx *context.Ctx, status *graylog.StatusRequest) graylog.ResponseCollectorRegistration {
func UpdateRegistration(httpClient *http.Client, ctx *context.Ctx, status *graylog.StatusRequest) (graylog.ResponseCollectorRegistration, error) {
c := rest.NewClient(httpClient, ctx)
c.BaseURL = ctx.ServerUrl

Expand All @@ -157,7 +158,7 @@ func UpdateRegistration(httpClient *http.Client, ctx *context.Ctx, status *grayl
r, err := c.NewRequest("PUT", "/sidecars/"+ctx.NodeId, nil, registration)
if err != nil {
log.Error("[UpdateRegistration] Can not initialize REST request")
return graylog.ResponseCollectorRegistration{}
return graylog.ResponseCollectorRegistration{}, err
}

respBody := new(graylog.ResponseCollectorRegistration)
Expand All @@ -167,8 +168,10 @@ func UpdateRegistration(httpClient *http.Client, ctx *context.Ctx, status *grayl
ctx.UserConfig.SendStatus = false
} else if resp != nil && resp.StatusCode != 202 {
log.Error("[UpdateRegistration] Bad response from Graylog server: ", resp.Status)
return graylog.ResponseCollectorRegistration{}, err
} else if err != nil && err != io.EOF { // err is nil for GL 2.2 and EOF for 2.1 and earlier
log.Error("[UpdateRegistration] Failed to report collector status to server: ", err)
return graylog.ResponseCollectorRegistration{}, err
}

// Update configuration if provided
Expand All @@ -181,7 +184,7 @@ func UpdateRegistration(httpClient *http.Client, ctx *context.Ctx, status *grayl
daemon.HandleCollectorActions(respBody.CollectorActions)
}

return *respBody
return *respBody, nil
}

func updateRuntimeConfiguration(respBody *graylog.ResponseCollectorRegistration, ctx *context.Ctx) error {
Expand Down
23 changes: 15 additions & 8 deletions services/periodicals.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ func StartPeriodicals(context *context.Ctx) {

go func() {
backendChecksum, configurationChecksum := "", ""
var err error
for {
time.Sleep(time.Duration(context.UserConfig.UpdateInterval) * time.Second)

// backend list is needed before configuration assignments are fetched
backendChecksum = fetchBackendList(httpClient, backendChecksum, context)
backendChecksum, err = fetchBackendList(httpClient, backendChecksum, context)
if err != nil {
continue
}
// registration response contains configuration assignments
response := updateCollectorRegistration(httpClient, context)
response, err := updateCollectorRegistration(httpClient, context)
if err != nil {
continue
}
assignments.Store.Update(response.Assignments)
// create process instances
daemon.Daemon.SyncWithAssignments(context)
Expand All @@ -56,20 +63,20 @@ func StartPeriodicals(context *context.Ctx) {
}

// report collector status to Graylog server
func updateCollectorRegistration(httpClient *http.Client, context *context.Ctx) graylog.ResponseCollectorRegistration {
func updateCollectorRegistration(httpClient *http.Client, context *context.Ctx) (graylog.ResponseCollectorRegistration, error) {
statusRequest := api.NewStatusRequest()
return api.UpdateRegistration(httpClient, context, &statusRequest)
}

func fetchBackendList(httpClient *http.Client, checksum string, ctx *context.Ctx) string {
func fetchBackendList(httpClient *http.Client, checksum string, ctx *context.Ctx) (string, error) {
response, err := api.RequestBackendList(httpClient, checksum, ctx)
if err != nil {
log.Error("Can't fetch configuration from Graylog API: ", err)
return ""
log.Error("Can't fetch collector list from Graylog API: ", err)
return "", err
}
if response.IsEmpty() {
// etag match, skipping all other actions
return response.Checksum
return response.Checksum, nil
}

backendList := []backends.Backend{}
Expand All @@ -78,7 +85,7 @@ func fetchBackendList(httpClient *http.Client, checksum string, ctx *context.Ctx
}
backends.Store.Update(backendList)

return response.Checksum
return response.Checksum, nil
}

// fetch configuration periodically
Expand Down

0 comments on commit 3a319b0

Please sign in to comment.