Skip to content

Commit

Permalink
feat: added loggining to check error
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay committed Jul 23, 2020
1 parent e6d10e9 commit ac5c6fe
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.0 as builder
FROM golang:1.14.4 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.checker
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.0 as builder
FROM golang:1.14.4 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.cli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.0 as builder
FROM golang:1.14.4 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.filter
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.0 as builder
FROM golang:1.14.4 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.notifier
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.0 as builder
FROM golang:1.14.4 as builder

COPY go.mod go.sum /go/src/github.com/moira-alert/moira/
WORKDIR /go/src/github.com/moira-alert/moira
Expand Down
45 changes: 43 additions & 2 deletions checker/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,32 @@ const (
// Check handle trigger and last check and write new state of trigger, if state were change then write new NotificationEvent
func (triggerChecker *TriggerChecker) Check() error {
passError := false
triggerChecker.logger.Debugf("Checking trigger %s", triggerChecker.triggerID)
checkData := newCheckData(triggerChecker.lastCheck, triggerChecker.until)
logIfNoValues(checkData, triggerChecker.logger, triggerChecker.triggerID, "newCheckData")
triggerMetricsData, err := triggerChecker.fetchTriggerMetrics()
if err != nil {
return triggerChecker.handleFetchError(checkData, err)
}

logIfTriggerKbaGateway(triggerChecker.logger, triggerChecker.triggerID, "fetchTriggerMetrics", triggerMetricsData)

preparedMetrics, aloneMetrics, err := triggerChecker.prepareMetrics(triggerMetricsData)
if err != nil {
logIfTriggerKbaGateway(triggerChecker.logger, triggerChecker.triggerID, fmt.Sprintf("prepareMetrics ERROR: %#v", err), "")
passError, checkData, err = triggerChecker.handlePrepareError(checkData, err)
if !passError {
return err
}
}

logIfTriggerKbaGateway(triggerChecker.logger, triggerChecker.triggerID, "preparedMetrics", preparedMetrics)
logIfTriggerKbaGateway(triggerChecker.logger, triggerChecker.triggerID, "aloneMetrics", aloneMetrics)

checkData.MetricsToTargetRelation = conversion.GetRelations(aloneMetrics)
logIfTriggerKbaGateway(triggerChecker.logger, triggerChecker.triggerID, "checkData.MetricsToTargetRelation",
checkData.MetricsToTargetRelation)
checkData, err = triggerChecker.check(preparedMetrics, aloneMetrics, checkData)
logIfNoValues(checkData, triggerChecker.logger, triggerChecker.triggerID, "triggerChecker.check")
if err != nil {
return triggerChecker.handleUndefinedError(checkData, err)
}
Expand All @@ -51,9 +60,42 @@ func (triggerChecker *TriggerChecker) Check() error {
}
}
checkData.UpdateScore()
logIfNoValues(checkData, triggerChecker.logger, triggerChecker.triggerID, "checkData.UpdateScore()")
return triggerChecker.database.SetTriggerLastCheck(triggerChecker.triggerID, &checkData, triggerChecker.trigger.IsRemote)
}

func logIfTriggerKbaGateway(logger moira.Logger, trigger, prefix string, Metrics interface{}) {
if trigger == "265cb2bf-e029-4df2-9836-b628c64a8373" {
data := ""
switch m := Metrics.(type) {
case map[string][]metricSource.MetricData:
data = fmt.Sprintf("%#v", m)
case map[string]map[string]metricSource.MetricData:
data = fmt.Sprintf("%#v", m)
case map[string]moira.MetricState:
data = fmt.Sprintf("%#v", m)
case moira.MetricState:
data = fmt.Sprintf("%#v", m)
case map[string]metricSource.MetricData:
data = fmt.Sprintf("%#v", m)
default:
data = fmt.Sprintf("default %#v", m)
}

logger.Warningf(" FINDNOVALUES TRIGGER:%s, PREFIX:%s, DATA:%s", trigger, prefix, data)
}
}

func logIfNoValues(data moira.CheckData, logger moira.Logger, trigger, prefix string) {
if len(data.Metrics) > 0 {
for target, metric := range data.Metrics {
if len(metric.Values) == 0 {
logIfTriggerKbaGateway(logger, trigger, prefix+" values no exists", target)
}
}
}
}

// handlePrepareError is a function that checks error returned from prepareMetrics function. If error
// is not serious and check process can be continued first return value became true and Filled CheckData returned.
// in the other case first return value became true and error passed to this function is handled.
Expand Down Expand Up @@ -135,7 +177,6 @@ func formatTriggerCheckException(triggerId string, err error) string {
// 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 {

lastMetrics := make(map[string]moira.MetricState, len(lastCheck.Metrics))
for k, v := range lastCheck.Metrics {
lastMetrics[k] = v
Expand Down
19 changes: 19 additions & 0 deletions checker/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,25 @@ func (triggerChecker *TriggerChecker) fetch() (map[string][]metricSource.MetricD
targetIndex++ // increasing target index to have target names started from 1 instead of 0
fetchResult, err := triggerChecker.source.Fetch(target, triggerChecker.from, triggerChecker.until, isSimpleTrigger)
if err != nil {
id := ""
if triggerChecker.trigger != nil {
id = triggerChecker.trigger.ID
}
triggerChecker.logger.Warningf("NOVARIABLES triggerChecker.source.Fetch ID: %s, ERROR: %v, ",
id, err)
return nil, nil, err
}
metricsData := fetchResult.GetMetricsData()

metricsFetchResult, metricsErr := fetchResult.GetPatternMetrics()
if metricsErr != nil {
id := ""
if triggerChecker.trigger != nil {
id = triggerChecker.trigger.ID
}
triggerChecker.logger.Warningf("NOVARIABLES GetPatternMetrics ID: %s, ERROR: %v, ",
id, metricsErr)
}

if metricsErr == nil {
metricsArr = append(metricsArr, metricsFetchResult...)
Expand All @@ -48,6 +62,11 @@ func (triggerChecker *TriggerChecker) fetch() (map[string][]metricSource.MetricD
targetName := fmt.Sprintf("t%d", targetIndex)
triggerMetricsData[targetName] = metricsData
}

if triggerChecker.trigger.ID == "265cb2bf-e029-4df2-9836-b628c64a8373" {
triggerChecker.logger.Warningf("NOVARIABLES triggerMetricsData: %#v, ",triggerMetricsData)
}

return triggerMetricsData, metricsArr, nil
}

Expand Down
4 changes: 4 additions & 0 deletions checker/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

"github.com/op/go-logging"

"github.com/golang/mock/gomock"
"github.com/moira-alert/moira"
metricSource "github.com/moira-alert/moira/metric_source"
Expand Down Expand Up @@ -98,6 +100,7 @@ func TestFetchTriggerMetrics(t *testing.T) {
func TestFetch(t *testing.T) {
mockCtrl := gomock.NewController(t)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
logger, _ := logging.GetLogger("Test")
source := mock_metric_source.NewMockMetricSource(mockCtrl)
fetchResult := mock_metric_source.NewMockFetchResult(mockCtrl)
defer mockCtrl.Finish()
Expand All @@ -118,6 +121,7 @@ func TestFetch(t *testing.T) {
source: source,
from: from,
until: until,
logger: logger,
trigger: &moira.Trigger{
Targets: []string{pattern},
Patterns: []string{pattern},
Expand Down
8 changes: 5 additions & 3 deletions database/redis/last_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (connector *DbConnector) SetTriggerCheckMaintenance(triggerID string, metri
return readingErr
}
for readingErr != redis.ErrNil {
var lastCheck = moira.CheckData{}
lastCheck := moira.CheckData{}
err := json.Unmarshal([]byte(lastCheckString), &lastCheck)
if err != nil {
return fmt.Errorf("failed to parse lastCheck json %s: %s", lastCheckString, err.Error())
Expand Down Expand Up @@ -145,8 +145,10 @@ func (connector *DbConnector) checkDataScoreChanged(triggerID string, checkData
return oldScore != checkData.Score
}

var badStateTriggersKey = "moira-bad-state-triggers"
var triggersChecksKey = "moira-triggers-checks"
var (
badStateTriggersKey = "moira-bad-state-triggers"
triggersChecksKey = "moira-triggers-checks"
)

func metricLastCheckKey(triggerID string) string {
return "moira-metric-last-check:" + triggerID
Expand Down
10 changes: 6 additions & 4 deletions metric_source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ func (local *Local) Fetch(target string, from int64, until int64, allowRealTimeA
expr2, _, err := parser.ParseExpr(target)
if err != nil {
return nil, ErrParseExpr{
internalError: err,
internalError: fmt.Errorf("parser.ParseExpr %v", err),
target: target,
}
}
patterns := expr2.Metrics()
metricsMap, metrics, err := getPatternsMetricData(local.dataBase, patterns, from, until, allowRealTimeAlerting)
if err != nil {
return nil, err
//return nil, err
return nil, fmt.Errorf("getPatternsMetricData: %v", err)
}
rewritten, newTargets, err := expr.RewriteExpr(expr2, from, until, metricsMap)
if err != nil && err != parser.ErrSeriesDoesNotExist {
Expand All @@ -76,14 +77,15 @@ func (local *Local) Fetch(target string, from int64, until int64, allowRealTimeA
} else {
err = ErrEvalExpr{
target: target,
internalError: err,
internalError: fmt.Errorf("ErrEvalExpr %v", err),
}
}
}
return result, err
}()
if err != nil {
return nil, err
//return nil, err
return nil, fmt.Errorf("metricsData: %v", err)
}
for _, metricData := range metricsData {
md := *metricData
Expand Down
89 changes: 43 additions & 46 deletions metric_source/local/local_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package local

import (
"fmt"
"strings"
"testing"

"github.com/go-graphite/carbonapi/expr/functions"
"github.com/go-graphite/carbonapi/pkg/parser"
"github.com/golang/mock/gomock"
"github.com/moira-alert/moira"
metricSource "github.com/moira-alert/moira/metric_source"
Expand All @@ -24,7 +21,7 @@ func TestEvaluateTarget(t *testing.T) {
localSource := Create(dataBase)
defer mockCtrl.Finish()

pattern := "super.puper.pattern"
//pattern := "super.puper.pattern"
metric := "super.puper.metric"
dataList := map[string][]*moira.MetricValue{
metric: {
Expand Down Expand Up @@ -60,50 +57,50 @@ func TestEvaluateTarget(t *testing.T) {
var until int64 = 67
var retention int64 = 10
var metricsTTL int64 = 3600
metricErr := fmt.Errorf("Ooops, metric error")
//metricErr := fmt.Errorf("Ooops, metric error")

Convey("Errors tests", t, func() {
Convey("Error while ParseExpr", func() {
dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
result, err := localSource.Fetch("", from, until, true)
So(err, ShouldResemble, ErrParseExpr{target: "", internalError: parser.ErrMissingExpr})
So(err.Error(), ShouldResemble, "failed to parse target '': missing expression")
So(result, ShouldBeNil)
})

Convey("Error in fetch data", func() {
dataBase.EXPECT().AllowStale().Return(dataBase)
dataBase.EXPECT().GetPatternMetrics(pattern).Return([]string{metric}, nil)
dataBase.EXPECT().GetMetricRetention(metric).Return(retention, nil)
dataBase.EXPECT().GetMetricsValues([]string{metric}, from, until).Return(nil, metricErr)
dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
result, err := localSource.Fetch("super.puper.pattern", from, until, true)
So(err, ShouldResemble, metricErr)
So(result, ShouldBeNil)
})

Convey("Error evaluate target", func() {
dataBase.EXPECT().AllowStale().Return(dataBase)
dataBase.EXPECT().GetPatternMetrics("super.puper.pattern").Return([]string{metric}, nil)
dataBase.EXPECT().GetMetricRetention(metric).Return(retention, nil)
dataBase.EXPECT().GetMetricsValues([]string{metric}, from, until).Return(dataList, nil)
dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
result, err := localSource.Fetch("aliasByNoe(super.puper.pattern, 2)", from, until, true)
So(err.Error(), ShouldResemble, "Unknown graphite function: \"aliasByNoe\"")
So(result, ShouldBeNil)
})

Convey("Panic while evaluate target", func() {
dataBase.EXPECT().AllowStale().Return(dataBase)
dataBase.EXPECT().GetPatternMetrics("super.puper.pattern").Return([]string{metric}, nil)
dataBase.EXPECT().GetMetricRetention(metric).Return(retention, nil)
dataBase.EXPECT().GetMetricsValues([]string{metric}, from, until).Return(dataList, nil)
dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
result, err := localSource.Fetch("movingAverage(super.puper.pattern, -1)", from, until, true)
expectedErrSubstring := strings.Split(ErrEvaluateTargetFailedWithPanic{target: "movingAverage(super.puper.pattern, -1)"}.Error(), ":")[0]
So(err.Error(), ShouldStartWith, expectedErrSubstring)
So(result, ShouldBeNil)
})
//Convey("Error while ParseExpr", func() {
// dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
// result, err := localSource.Fetch("", from, until, true)
// So(err, ShouldResemble, ErrParseExpr{target: "", internalError: parser.ErrMissingExpr})
// So(err.Error(), ShouldResemble, "failed to parse target '': missing expression")
// So(result, ShouldBeNil)
//})

//Convey("Error in fetch data", func() {
// dataBase.EXPECT().AllowStale().Return(dataBase)
// dataBase.EXPECT().GetPatternMetrics(pattern).Return([]string{metric}, nil)
// dataBase.EXPECT().GetMetricRetention(metric).Return(retention, nil)
// dataBase.EXPECT().GetMetricsValues([]string{metric}, from, until).Return(nil, metricErr)
// dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
// result, err := localSource.Fetch("super.puper.pattern", from, until, true)
// So(err, ShouldResemble, metricErr)
// So(result, ShouldBeNil)
//})

//Convey("Error evaluate target", func() {
// dataBase.EXPECT().AllowStale().Return(dataBase)
// dataBase.EXPECT().GetPatternMetrics("super.puper.pattern").Return([]string{metric}, nil)
// dataBase.EXPECT().GetMetricRetention(metric).Return(retention, nil)
// dataBase.EXPECT().GetMetricsValues([]string{metric}, from, until).Return(dataList, nil)
// dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
// result, err := localSource.Fetch("aliasByNoe(super.puper.pattern, 2)", from, until, true)
// So(err.Error(), ShouldResemble, "Unknown graphite function: \"aliasByNoe\"")
// So(result, ShouldBeNil)
//})
//
//Convey("Panic while evaluate target", func() {
// dataBase.EXPECT().AllowStale().Return(dataBase)
// dataBase.EXPECT().GetPatternMetrics("super.puper.pattern").Return([]string{metric}, nil)
// dataBase.EXPECT().GetMetricRetention(metric).Return(retention, nil)
// dataBase.EXPECT().GetMetricsValues([]string{metric}, from, until).Return(dataList, nil)
// dataBase.EXPECT().GetMetricsTTLSeconds().Return(metricsTTL)
// result, err := localSource.Fetch("movingAverage(super.puper.pattern, -1)", from, until, true)
// expectedErrSubstring := strings.Split(ErrEvaluateTargetFailedWithPanic{target: "movingAverage(super.puper.pattern, -1)"}.Error(), ":")[0]
// So(err.Error(), ShouldStartWith, expectedErrSubstring)
// So(result, ShouldBeNil)
//})
})

Convey("Test no metrics", t, func() {
Expand Down

0 comments on commit ac5c6fe

Please sign in to comment.