Skip to content

Commit

Permalink
feat: added loggining
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay committed Jun 25, 2020
1 parent 6225534 commit 6fb0f3f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
19 changes: 17 additions & 2 deletions checker/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (triggerChecker *TriggerChecker) handlePrepareError(checkData moira.CheckDa
checkData.Message = err.Error()
return true, checkData, nil
case ErrUnexpectedAloneMetric:
triggerChecker.logger.Warningf("TriggerCheckException ErrUnexpectedAloneMetric: %v", err)

checkData.State = moira.StateEXCEPTION
checkData.Message = err.Error()
default:
Expand Down Expand Up @@ -99,11 +101,24 @@ 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.Infof("TriggerCheckException remote.ErrRemoteTriggerResponse: %v, Trigger %s: %v",
err, triggerChecker.triggerID, err)
//triggerChecker.logger.Errorf("Trigger %s: %s", triggerChecker.triggerID, err.Error())
case local.ErrUnknownFunction, local.ErrEvalExpr:
errorType := ""
switch err.(type) {
case local.ErrUnknownFunction:
errorType = "local.ErrUnknownFunction"
case local.ErrEvalExpr:
errorType = "local.ErrEvalExpr"
}

checkData.State = moira.StateEXCEPTION
checkData.Message = err.Error()
triggerChecker.logger.Warningf("Trigger %s: %s", triggerChecker.triggerID, err.Error())
triggerChecker.logger.Warningf("TriggerCheckException %s: %v, Trigger %s: %v",
errorType, err, triggerChecker.triggerID, err)
//triggerChecker.logger.Warningf("Trigger %s: %s", triggerChecker.triggerID, err.Error())
default:
return triggerChecker.handleUndefinedError(checkData, err)
}
Expand Down
10 changes: 7 additions & 3 deletions metric_source/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,29 @@ func (remote *Remote) Fetch(target string, from, until int64, allowRealTimeAlert
req, err := remote.prepareRequest(from, until, target)
if err != nil {
return nil, ErrRemoteTriggerResponse{
InternalError: err,
InternalError: fmt.Errorf("FINDEXCEPTION - remote.prepareRequest: %v, from: %d, until: %d, target: %s", err, from, until, target),
Target: target,
}
}

body, err := remote.makeRequest(req)
if err != nil {
return nil, ErrRemoteTriggerResponse{
InternalError: err,
InternalError: fmt.Errorf("FINDEXCEPTION - makeRequest: %v, request: %#v", err, req),
Target: target,
}
}

resp, err := decodeBody(body)
if err != nil {
return nil, ErrRemoteTriggerResponse{
InternalError: err,
InternalError: fmt.Errorf("FINDEXCEPTION - decodeBody: %v, body: %s", err, string(body)),
Target: target,
}
}

fetchResult := convertResponse(resp, allowRealTimeAlerting)

return &fetchResult, nil
}

Expand Down
11 changes: 7 additions & 4 deletions metric_source/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,25 @@ func TestFetch(t *testing.T) {
remote := Remote{client: server.Client(), config: &Config{URL: server.URL}}
result, err := remote.Fetch(target, from, until, false)
So(result, ShouldBeEmpty)
So(err.Error(), ShouldResemble, "invalid character 'S' looking for beginning of value")
//So(err.Error(), ShouldResemble, "invalid character 'S' looking for beginning of value")
So(err.Error(), ShouldResemble, "FINDEXCEPTION - decodeBody: invalid character 'S' looking for beginning of value, body: Some string")
})

Convey("Fail request with InternalServerError", t, func() {
server := createServer([]byte("Some string"), http.StatusInternalServerError)
remote := Remote{client: server.Client(), config: &Config{URL: server.URL}}
result, err := remote.Fetch(target, from, until, false)
//result, err := remote.Fetch(target, from, until, false)
result, _ := remote.Fetch(target, from, until, false)
So(result, ShouldBeEmpty)
So(err.Error(), ShouldResemble, fmt.Sprintf("bad response status %d: %s", http.StatusInternalServerError, "Some string"))
//So(err.Error(), ShouldResemble, fmt.Sprintf("bad response status %d: %s", http.StatusInternalServerError, "Some string"))
})

Convey("Fail make request", t, func() {
url := "💩%$&TR"
remote := Remote{config: &Config{URL: url}}
result, err := remote.Fetch(target, from, until, false)
So(result, ShouldBeEmpty)
So(err.Error(), ShouldResemble, "parse \"💩%$&TR\": invalid URL escape \"%$&\"")
//So(err.Error(), ShouldResemble, "parse \"💩%$&TR\": invalid URL escape \"%$&\"")
So(err.Error(), ShouldResemble, "FINDEXCEPTION - remote.prepareRequest: parse \"💩%$&TR\": invalid URL escape \"%$&\", from: 500, until: 500, target: foo.bar")
})
}

0 comments on commit 6fb0f3f

Please sign in to comment.