-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathprocessor.go
131 lines (111 loc) · 3.79 KB
/
processor.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 processor
import (
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/require"
"github.com/pkg/errors"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/processors"
"github.com/elastic/beats/v7/libbeat/processors/script/javascript"
)
// Create constructors for most of the Beat processors.
// Note that script is omitted to avoid nesting.
var registry = processors.NewNamespace()
// RegisterPlugin registeres processor plugins for the javascript processor.
func RegisterPlugin(name string, c processors.Constructor) {
logp.L().Named("javascript").Debugf("Register script processor %s", name)
err := registry.Register(name, c)
if err != nil {
panic(err)
}
}
// beatProcessor wraps a processor for javascript.
type beatProcessor struct {
rt *goja.Runtime
p processor
}
func (bp *beatProcessor) Run(call goja.FunctionCall) goja.Value {
if len(call.Arguments) != 1 {
panic(bp.rt.NewGoError(errors.New("Run requires one argument")))
}
e, ok := call.Argument(0).ToObject(bp.rt).Get("_private").Export().(javascript.Event)
if !ok {
panic(bp.rt.NewGoError(errors.New("arg 0 must be an Event")))
}
if e.IsCancelled() {
return goja.Null()
}
err := bp.p.run(e)
if err != nil {
panic(bp.rt.NewGoError(err))
}
if e.IsCancelled() {
return goja.Null()
}
return e.JSObject()
}
// newConstructor returns a Javascript constructor function that constructs a
// Beat processor. The constructor wraps a beat processor constructor. The
// javascript constructor must be passed a value that can be treated as the
// processor's config.
func newConstructor(
runtime *goja.Runtime,
constructor processors.Constructor,
) func(call goja.ConstructorCall) *goja.Object {
return func(call goja.ConstructorCall) *goja.Object {
p, err := newNativeProcessor(constructor, call)
if err != nil {
panic(runtime.NewGoError(err))
}
bp := &beatProcessor{runtime, p}
return runtime.ToValue(bp).ToObject(nil)
}
}
// Require registers the processor module that exposes constructors for beat
// processors from javascript.
//
// // javascript
// var processor = require('processor');
//
// // Construct a single processor.
// var chopLog = new processor.Dissect({tokenizer: "%{key}: %{value}"});
//
// // Construct/compose a processor chain.
// var mutateLog = new processor.Chain()
// .Add(chopLog)
// .AddProcessMetadata({match_pids: [process.pid]})
// .Add(function(evt) {
// evt.Put("hello", "world");
// })
// .Build();
//
func Require(runtime *goja.Runtime, module *goja.Object) {
o := module.Get("exports").(*goja.Object)
for name, fn := range registry.Constructors() {
o.Set(name, newConstructor(runtime, fn))
}
// Chain returns a builder for creating a chain of processors.
o.Set("Chain", newChainBuilder(runtime))
}
// Enable adds path to the given runtime.
func Enable(runtime *goja.Runtime) {
runtime.Set("processor", require.Require(runtime, "processor"))
}
func init() {
require.RegisterNativeModule("processor", Require)
}