From 6fb0f3f74287e2669410faaf63ce126056f10b41 Mon Sep 17 00:00:00 2001 From: Nikolay Date: Thu, 25 Jun 2020 13:45:54 +0500 Subject: [PATCH] feat: added loggining --- checker/check.go | 19 +++++++++++++++++-- metric_source/remote/remote.go | 10 +++++++--- metric_source/remote/remote_test.go | 11 +++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/checker/check.go b/checker/check.go index 5df990340..32e0de834 100644 --- a/checker/check.go +++ b/checker/check.go @@ -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: @@ -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) } diff --git a/metric_source/remote/remote.go b/metric_source/remote/remote.go index 49710141c..6be1c48ff 100644 --- a/metric_source/remote/remote.go +++ b/metric_source/remote/remote.go @@ -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 } diff --git a/metric_source/remote/remote_test.go b/metric_source/remote/remote_test.go index 2bb9430a2..3c90e5f20 100644 --- a/metric_source/remote/remote_test.go +++ b/metric_source/remote/remote_test.go @@ -61,15 +61,17 @@ 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() { @@ -77,6 +79,7 @@ func TestFetch(t *testing.T) { 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") }) }