@@ -4,11 +4,13 @@ import (
4
4
"bufio"
5
5
"bytes"
6
6
"compress/gzip"
7
+ "flag"
7
8
"fmt"
8
9
"io"
9
10
"strings"
10
11
"time"
11
12
13
+ "github.com/kentik/ktranslate"
12
14
"github.com/kentik/ktranslate/pkg/formats/util"
13
15
"github.com/kentik/ktranslate/pkg/kt"
14
16
"github.com/kentik/ktranslate/pkg/rollup"
@@ -18,21 +20,31 @@ import (
18
20
)
19
21
20
22
const (
21
- actionEntry = `{"index ":{}}`
23
+ actionEntryFormat = `{"%s ":{}}`
22
24
)
23
25
26
+ var (
27
+ actionEntrySet string
28
+ )
29
+
30
+ func init () {
31
+ flag .StringVar (& actionEntrySet , "elastic.action" , "index" , "Use this action when sending to elastic." )
32
+ }
33
+
24
34
var json = jsoniter .ConfigFastest
25
35
26
36
type ElasticsearchFormat struct {
27
37
logger.ContextL
28
38
compression kt.Compression
29
39
useGzip bool
40
+ action string
30
41
}
31
42
32
- func NewFormat (log logger.Underlying , compression kt.Compression ) (* ElasticsearchFormat , error ) {
43
+ func NewFormat (log logger.Underlying , compression kt.Compression , cfg * ktranslate. ElasticFormatConfig ) (* ElasticsearchFormat , error ) {
33
44
ef := & ElasticsearchFormat {
34
45
ContextL : logger .NewContextLFromUnderlying (logger.SContext {S : "elasticsearchFormat" }, log ),
35
46
compression : compression ,
47
+ action : fmt .Sprintf (actionEntryFormat , cfg .Action ),
36
48
}
37
49
38
50
switch compression {
@@ -44,6 +56,8 @@ func NewFormat(log logger.Underlying, compression kt.Compression) (*Elasticsearc
44
56
return nil , fmt .Errorf ("Invalid compression (%s): format json only supports none|gzip" , compression )
45
57
}
46
58
59
+ ef .Infof ("Using action %s" , ef .action )
60
+
47
61
return ef , nil
48
62
}
49
63
@@ -62,7 +76,7 @@ func (f *ElasticsearchFormat) To(msgs []*kt.JCHF, serBuf []byte) (*kt.Output, er
62
76
63
77
esBulkData := []string {}
64
78
for _ , m := range msgsNew {
65
- data , err := serialize (m )
79
+ data , err := serialize (m , f . action )
66
80
if err != nil {
67
81
return nil , err
68
82
}
@@ -108,7 +122,7 @@ func (f *ElasticsearchFormat) From(raw *kt.Output) ([]map[string]interface{}, er
108
122
for sc .Scan () {
109
123
// check for ES action and ignore
110
124
v := sc .Text ()
111
- if v == actionEntry {
125
+ if v == f . action {
112
126
continue
113
127
}
114
128
msg := & kt.JCHF {}
@@ -131,7 +145,7 @@ func (f *ElasticsearchFormat) Rollup(rolls []rollup.Rollup) (*kt.Output, error)
131
145
// serialize rolls
132
146
esBulkData := []string {}
133
147
for _ , m := range rolls {
134
- data , err := serialize (m )
148
+ data , err := serialize (m , f . action )
135
149
if err != nil {
136
150
return nil , err
137
151
}
@@ -166,8 +180,8 @@ func (f *ElasticsearchFormat) handleSynth(in *kt.JCHF) map[string]interface{} {
166
180
return attr
167
181
}
168
182
169
- func serialize (o interface {}) (string , error ) {
170
- s := actionEntry + "\n "
183
+ func serialize (o interface {}, action string ) (string , error ) {
184
+ s := action + "\n "
171
185
data , err := json .Marshal (o )
172
186
if err != nil {
173
187
return "" , err
0 commit comments