Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 991a4da

Browse files
committedApr 1, 2019
refactoring to eliminate unnecessary copy && add flag
1 parent 40d5258 commit 991a4da

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed
 

‎libbeat/processors/actions/truncate_fields.go

+29-17
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type truncateFields struct {
4444
truncate truncater
4545
}
4646

47-
type truncater func(*truncateFields, []byte) ([]byte, error)
47+
type truncater func(*truncateFields, []byte) ([]byte, bool, error)
4848

4949
func init() {
5050
processors.RegisterPlugin("truncate_fields",
@@ -99,80 +99,92 @@ func (f *truncateFields) truncateSingleField(field string, event *beat.Event) (*
9999
if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound {
100100
return event, nil
101101
}
102-
return event, fmt.Errorf("could not fetch value for key: %s, Error: %+v", field, err)
102+
return event, errors.Wrapf(err, "could not fetch value for key: %s", field)
103103
}
104104

105105
switch value := v.(type) {
106-
case string:
107-
return f.addTruncatedString(field, value, event)
108106
case []byte:
109107
return f.addTruncatedByte(field, value, event)
108+
case string:
109+
return f.addTruncatedString(field, value, event)
110110
default:
111111
return event, fmt.Errorf("value cannot be truncated: %+v", value)
112112
}
113113

114114
}
115115

116116
func (f *truncateFields) addTruncatedString(field, value string, event *beat.Event) (*beat.Event, error) {
117-
truncated, err := f.truncate(f, []byte(value))
117+
truncated, isTruncated, err := f.truncate(f, []byte(value))
118118
if err != nil {
119119
return event, err
120120
}
121121
_, err = event.PutValue(field, string(truncated))
122122
if err != nil {
123123
return event, fmt.Errorf("could not add truncated string value for key: %s, Error: %+v", field, err)
124124
}
125+
126+
if isTruncated {
127+
common.AddTagsWithKey(event.Fields, "log.flags", []string{"truncated"})
128+
}
129+
125130
return event, nil
126131
}
127132

128133
func (f *truncateFields) addTruncatedByte(field string, value []byte, event *beat.Event) (*beat.Event, error) {
129-
truncated, err := f.truncate(f, value)
134+
truncated, isTruncated, err := f.truncate(f, value)
130135
if err != nil {
131136
return event, err
132137
}
133138
_, err = event.PutValue(field, truncated)
134139
if err != nil {
135140
return event, fmt.Errorf("could not add truncated byte slice value for key: %s, Error: %+v", field, err)
136141
}
142+
143+
if isTruncated {
144+
common.AddTagsWithKey(event.Fields, "log.flags", []string{"truncated"})
145+
}
146+
137147
return event, nil
138148
}
139149

140-
func (f *truncateFields) truncateBytes(value []byte) ([]byte, error) {
150+
func (f *truncateFields) truncateBytes(value []byte) ([]byte, bool, error) {
141151
size := len(value)
142-
if size > f.config.MaxBytes {
143-
size = f.config.MaxBytes
152+
if size <= f.config.MaxBytes {
153+
return value, false, nil
144154
}
145155

156+
size = f.config.MaxBytes
146157
truncated := make([]byte, size)
147158
n := copy(truncated, value[:size])
148159
if n != size {
149-
return nil, fmt.Errorf("unexpected number of bytes were copied")
160+
return nil, false, fmt.Errorf("unexpected number of bytes were copied")
150161
}
151-
return truncated, nil
162+
return truncated, true, nil
152163
}
153164

154-
func (f *truncateFields) truncateCharacters(value []byte) ([]byte, error) {
165+
func (f *truncateFields) truncateCharacters(value []byte) ([]byte, bool, error) {
155166
count := utf8.RuneCount(value)
156-
if count > f.config.MaxChars {
157-
count = f.config.MaxChars
167+
if count <= f.config.MaxChars {
168+
return value, false, nil
158169
}
159170

171+
count = f.config.MaxChars
160172
r := bytes.NewReader(value)
161173
w := bytes.NewBuffer(nil)
162174

163175
for i := 0; i < count; i++ {
164176
r, _, err := r.ReadRune()
165177
if err != nil {
166-
return nil, err
178+
return nil, false, err
167179
}
168180

169181
_, err = w.WriteRune(r)
170182
if err != nil {
171-
return nil, err
183+
return nil, false, err
172184
}
173185
}
174186

175-
return w.Bytes(), nil
187+
return w.Bytes(), true, nil
176188
}
177189

178190
func (f *truncateFields) String() string {

‎libbeat/processors/actions/truncate_fields_test.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestTruncateFields(t *testing.T) {
3030

3131
var tests = map[string]struct {
3232
MaxBytes int
33-
MaxCount int
33+
MaxChars int
3434
Input common.MapStr
3535
Output common.MapStr
3636
ShouldError bool
@@ -43,6 +43,9 @@ func TestTruncateFields(t *testing.T) {
4343
},
4444
Output: common.MapStr{
4545
"message": "too",
46+
"log": common.MapStr{
47+
"flags": []string{"truncated"},
48+
},
4649
},
4750
ShouldError: false,
4851
TruncateFunc: (*truncateFields).truncateBytes,
@@ -54,6 +57,9 @@ func TestTruncateFields(t *testing.T) {
5457
},
5558
Output: common.MapStr{
5659
"message": []byte("too"),
60+
"log": common.MapStr{
61+
"flags": []string{"truncated"},
62+
},
5763
},
5864
ShouldError: false,
5965
TruncateFunc: (*truncateFields).truncateBytes,
@@ -92,7 +98,7 @@ func TestTruncateFields(t *testing.T) {
9298
TruncateFunc: (*truncateFields).truncateBytes,
9399
},
94100
"do not truncate characters of short byte line": {
95-
MaxCount: 6,
101+
MaxChars: 6,
96102
Input: common.MapStr{
97103
"message": []byte("ez jó"), // this is good (hungarian)
98104
},
@@ -114,12 +120,15 @@ func TestTruncateFields(t *testing.T) {
114120
TruncateFunc: (*truncateFields).truncateBytes,
115121
},
116122
"truncate characters of too long byte line": {
117-
MaxCount: 10,
123+
MaxChars: 10,
118124
Input: common.MapStr{
119125
"message": []byte("ez egy túl hosszú sor"), // this is a too long line (hungarian)
120126
},
121127
Output: common.MapStr{
122128
"message": []byte("ez egy túl"), // this is a too (hungarian)
129+
"log": common.MapStr{
130+
"flags": []string{"truncated"},
131+
},
123132
},
124133
ShouldError: false,
125134
TruncateFunc: (*truncateFields).truncateCharacters,
@@ -131,6 +140,9 @@ func TestTruncateFields(t *testing.T) {
131140
},
132141
Output: common.MapStr{
133142
"message": []byte("ez egy tú"), // this is a "to" (hungarian)
143+
"log": common.MapStr{
144+
"flags": []string{"truncated"},
145+
},
134146
},
135147
ShouldError: false,
136148
TruncateFunc: (*truncateFields).truncateBytes,
@@ -143,7 +155,7 @@ func TestTruncateFields(t *testing.T) {
143155
config: truncateFieldsConfig{
144156
Fields: []string{"message"},
145157
MaxBytes: test.MaxBytes,
146-
MaxCount: test.MaxCount,
158+
MaxChars: test.MaxChars,
147159
FailOnError: true,
148160
},
149161
truncate: test.TruncateFunc,

0 commit comments

Comments
 (0)
Please sign in to comment.