Skip to content

Commit

Permalink
Merge pull request #131 from andrewkroh/bugfix/omit-null-fields
Browse files Browse the repository at this point in the history
Removing 'fields' from JSON output when the field is null.
  • Loading branch information
ruflin committed Oct 22, 2015
2 parents e13f033 + c529fc7 commit 8941095
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All documentation about Filebeat can be found here.
### Backward Compatibility Breaks

### Bugfixes
- Omit 'fields' from event JSON when null. #126

### Added

Expand Down
14 changes: 1 addition & 13 deletions beat/filebeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package beat
import (
"fmt"
"os"
"time"

"github.com/elastic/libbeat/beat"
"github.com/elastic/libbeat/cfgfile"
Expand Down Expand Up @@ -126,18 +125,7 @@ func Publish(beat *beat.Beat, fb *Filebeat) {

pubEvents := make([]common.MapStr, 0, len(events))
for _, event := range events {
bEvent := common.MapStr{
"timestamp": common.Time(time.Now()),
"source": event.Source,
"offset": event.Offset,
"line": event.Line,
"message": event.Text,
"fields": event.Fields,
"fileinfo": event.Fileinfo,
"type": "log",
}

pubEvents = append(pubEvents, bEvent)
pubEvents = append(pubEvents, event.ToMapStr())
}

beat.Events.PublishEvents(pubEvents, publisher.Sync)
Expand Down
4 changes: 2 additions & 2 deletions etc/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ env:
format: YYYY-MM-DDTHH:MM:SS.milliZ
example: 2015-01-24T14:06:05.071Z
description: >
The timestamp when the log line is published. The precision is in milliseconds.
The timezone is UTC.
The timestamp when the log line was read. The precision is in
milliseconds. The timezone is UTC.
- name: type
description: >
Expand Down
1 change: 1 addition & 0 deletions harvester/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (h *Harvester) Harvest() {

line++
event := &input.FileEvent{
ReadTime: lastReadTime,
Source: &h.Path,
Offset: h.Offset,
Line: line,
Expand Down
21 changes: 21 additions & 0 deletions input/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package input

import (
"os"
"time"

"github.com/elastic/libbeat/common"
"github.com/elastic/libbeat/logp"
)

Expand All @@ -15,6 +17,7 @@ type File struct {

// FileEvent is sent to the output and must contain all relevant information
type FileEvent struct {
ReadTime time.Time
Source *string
Offset int64
Line uint64
Expand Down Expand Up @@ -44,6 +47,24 @@ func (f *FileEvent) GetState() *FileState {
return state
}

func (f *FileEvent) ToMapStr() common.MapStr {
event := common.MapStr{
"timestamp": common.Time(f.ReadTime),
"source": f.Source,
"offset": f.Offset,
"line": f.Line,
"message": f.Text,
"fileinfo": f.Fileinfo,
"type": "log",
}

if f.Fields != nil {
event["fields"] = f.Fields
}

return event
}

// Check that the file isn't a symlink, mode is regular or file is nil
func (f *File) IsRegularFile() bool {
if f.File == nil {
Expand Down
8 changes: 8 additions & 0 deletions input/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,11 @@ func TestSafeFileRotateExistingFile(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, []byte("new filebeat 2"), contents)
}

func TestFileEventToMapStr(t *testing.T) {
// Test 'fields' is not present when it is nil.
event := FileEvent{}
mapStr := event.ToMapStr()
_, found := mapStr["fields"]
assert.False(t, found)
}

0 comments on commit 8941095

Please sign in to comment.