-
Notifications
You must be signed in to change notification settings - Fork 349
/
sys_sum.go
93 lines (79 loc) · 2.8 KB
/
sys_sum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2022 iLogtail Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validator
import (
"context"
"strconv"
"github.com/alibaba/ilogtail/pkg/doc"
"github.com/alibaba/ilogtail/pkg/logger"
"github.com/alibaba/ilogtail/pkg/protocol"
"github.com/mitchellh/mapstructure"
)
const sumSystemValidatorName = "sys_sum_checker"
type sumSystemValidator struct {
Field string `mapstructure:"field" comment:"group by field"`
FilterKey string `mapstructure:"filter_key" comment:"where filter_key=filter_val"`
FilterVal string `mapstructure:"filter_val" comment:"where filter_key=filter_val"`
ExpectReceivedMinimumNum int `mapstructure:"expect_received_minimum_num" comment:"the expected minimum received log group number"`
sum int
}
func (s *sumSystemValidator) Description() string {
return "system sum validator group by field"
}
func (s *sumSystemValidator) Start() error {
return nil
}
func (s *sumSystemValidator) Valid(group *protocol.LogGroup) {
for _, log := range group.Logs {
var num int
var err error
equals := true
for _, content := range log.Contents {
if content.Key == s.Field {
num, err = strconv.Atoi(content.Value)
if err != nil {
logger.Error(context.Background(), "ILLEGAL_FIELD", "the field must be number", content.Key)
continue
}
}
if s.FilterKey != "" && s.FilterKey == content.Key {
equals = s.FilterVal == content.Value
}
}
if equals {
s.sum += num
}
}
}
func (s *sumSystemValidator) FetchResult() (reports []*Report) {
logger.Debugf(context.Background(), "want field log sum number %d, got %d", s.ExpectReceivedMinimumNum, s.sum)
if s.ExpectReceivedMinimumNum > s.sum {
reports = append(reports, &Report{Validator: sumSystemValidatorName, Name: "log_field_sum_number", Want: strconv.Itoa(s.ExpectReceivedMinimumNum), Got: strconv.Itoa(s.sum)})
}
return
}
func (s *sumSystemValidator) Name() string {
return sumSystemValidatorName
}
func init() {
RegisterSystemValidatorCreator(sumSystemValidatorName, func(spec map[string]interface{}) (SystemValidator, error) {
f := new(sumSystemValidator)
err := mapstructure.Decode(spec, f)
if err != nil {
return nil, err
}
return f, nil
})
doc.Register("sys_validator", sumSystemValidatorName, new(sumSystemValidator))
}