Skip to content

Commit

Permalink
debug exception (#552)
Browse files Browse the repository at this point in the history
feat: added loggining

* Make TriggerCheckException log messages more consitent

Co-authored-by: Nikolay <[email protected]>
Co-authored-by: Borovsky Arkady <[email protected]>
  • Loading branch information
3 people authored Jul 6, 2020
1 parent 76053ee commit e6d10e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 7 additions & 2 deletions checker/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (triggerChecker *TriggerChecker) handlePrepareError(checkData moira.CheckDa
case ErrUnexpectedAloneMetric:
checkData.State = moira.StateEXCEPTION
checkData.Message = err.Error()
triggerChecker.logger.Warning(formatTriggerCheckException(triggerChecker.triggerID, err))
default:
return false, checkData, triggerChecker.handleUndefinedError(checkData, err)
}
Expand Down Expand Up @@ -99,11 +100,11 @@ func (triggerChecker *TriggerChecker) handleFetchError(checkData moira.CheckData
checkData.Message = fmt.Sprintf("Remote server unavailable. Trigger is not checked for %d seconds", timeSinceLastSuccessfulCheck)
checkData, err = triggerChecker.compareTriggerStates(checkData)
}
triggerChecker.logger.Errorf("Trigger %s: %s", triggerChecker.triggerID, err.Error())
triggerChecker.logger.Warning(formatTriggerCheckException(triggerChecker.triggerID, err))
case local.ErrUnknownFunction, local.ErrEvalExpr:
checkData.State = moira.StateEXCEPTION
checkData.Message = err.Error()
triggerChecker.logger.Warningf("Trigger %s: %s", triggerChecker.triggerID, err.Error())
triggerChecker.logger.Warning(formatTriggerCheckException(triggerChecker.triggerID, err))
default:
return triggerChecker.handleUndefinedError(checkData, err)
}
Expand All @@ -127,6 +128,10 @@ func (triggerChecker *TriggerChecker) handleUndefinedError(checkData moira.Check
return triggerChecker.database.SetTriggerLastCheck(triggerChecker.triggerID, &checkData, triggerChecker.trigger.IsRemote)
}

func formatTriggerCheckException(triggerId string, err error) string {
return fmt.Sprintf("TriggerCheckException %T Trigger %s: %v", err, triggerId, err)
}

// Set new last check timestamp that equal to "until" targets fetch interval
// Do not copy message, it will be set if needed
func newCheckData(lastCheck *moira.CheckData, checkTimeStamp int64) moira.CheckData {
Expand Down
2 changes: 2 additions & 0 deletions checker/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1413,12 +1413,14 @@ func TestTriggerChecker_handlePrepareError(t *testing.T) {
Convey("Test handlePrepareError", t, func() {
mockCtrl := gomock.NewController(t)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
logger, _ := logging.GetLogger("Test")

trigger := &moira.Trigger{}
triggerChecker := TriggerChecker{
triggerID: "test trigger",
trigger: trigger,
database: dataBase,
logger: logger,
}
checkData := moira.CheckData{}

Expand Down
8 changes: 7 additions & 1 deletion metric_source/remote/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ func (remote *Remote) prepareRequest(from, until int64, target string) (*http.Re

func (remote *Remote) makeRequest(req *http.Request) ([]byte, error) {
var body []byte

resp, err := remote.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}

if err != nil {
return body, err
return body, fmt.Errorf("The remote server is not available or the response was reset by timeout. " +
"TTL: %s, PATH: %s, ERROR: %v ", remote.client.Timeout.String(), req.URL.RawPath, err)
}

body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return body, err
}

if resp.StatusCode != 200 {
return body, fmt.Errorf("bad response status %d: %s", resp.StatusCode, string(body))
}

return body, nil
}

0 comments on commit e6d10e9

Please sign in to comment.