-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_splitter_decoder.go
78 lines (65 loc) · 2.44 KB
/
stream_splitter_decoder.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
/***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2012
# the Initial Developer. All Rights Reserved.
#
# ***** END LICENSE BLOCK *****/
package stream_aggregator
import (
"time"
"encoding/json"
. "github.com/mozilla-services/heka/pipeline"
"strings"
)
type StreamSplitterDecoderConfig struct {
// Keyed to the message field that should be filled in, the value will be
// interpolated so it can use capture parts from the message match.
Delimiter string `toml:"delimiter"` // Delimiter used to append to end of each protobuf for splitting on when decoding later.
// Defaults to '\n'
}
type StreamSplitterDecoder struct {
*StreamSplitterDecoderConfig
dRunner DecoderRunner
}
func (ld *StreamSplitterDecoder) ConfigStruct() interface{} {
return &StreamSplitterDecoderConfig{
Delimiter: "\n",
}
}
func (ld *StreamSplitterDecoder) Init(config interface{}) (err error) {
ld.StreamSplitterDecoderConfig = config.(*StreamSplitterDecoderConfig)
return
}
// Heka will call this to give us access to the runner.
func (ld *StreamSplitterDecoder) SetDecoderRunner(dr DecoderRunner) {
ld.dRunner = dr
}
// Runs the message payload against decoder's map of JSONPaths. If
// there's a match, the message will be populated based on the
// decoder's message template, with capture values interpolated into
// the message template values.
func (ld *StreamSplitterDecoder) Decode(pack *PipelinePack) (packs []*PipelinePack, err error) {
var f interface{}
delimiter := ld.Delimiter
bodySlice := strings.Split(pack.Message.GetPayload(), delimiter)
for _,element := range bodySlice {
err := json.Unmarshal([]byte(element), &f)
if err == nil {
newpack := ld.dRunner.NewPack()
newpack.Message.SetPayload(string(element))
newpack.Message.SetTimestamp(time.Now().UTC().UnixNano())
packs = append(packs, newpack)
}
}
pack.Recycle(nil)
return
}
func init() {
RegisterPlugin("StreamSplitterDecoder", func() interface{} {
return new(StreamSplitterDecoder)
})
}