Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fb.js not effect #2

Open
zhxiaom5 opened this issue Jan 26, 2019 · 2 comments
Open

fb.js not effect #2

zhxiaom5 opened this issue Jan 26, 2019 · 2 comments

Comments

@zhxiaom5
Copy link

in fb.js, the fields.attributes.text has been added,but the filebeat output did not contain this field
need any addit config ?

@vortex314
Copy link
Owner

Can you share the fb.js and maybe a sample you are trying to parse ?

@zhxiaom5
Copy link
Author

just like below

		console.log("Javascript engine loaded "); 
		process = function(fields){
			fields.message = fields.syslog_message
			console.log(JSON.stringify(fields))
			fields.javascript="running in GO!"
//			var d = new Date(fields.timestamp)
//			console.log(" date " + d)
			return fields
		}

it seems the js changes fields cant affect the filebeat event

in the end, i change the javascript.go like this(get the js return addfields and add it to the filebeat event), it works

func (f *javaScriptEngine) Run(event *beat.Event) (*beat.Event, error) {
        var errors []string
        //      message, _ := event.Fields.GetValue("message")
        // call the JS process function
        addFields, er := f.Engine.Call("process", nil, event)
        //      fmt.Println(fields)
        if er != nil {
                f.logger.Warn("javascript error occured ", er)
        }
        // convert added fields from JS type to native Go
        val, err := addFields.Export()
        if err != nil {
                f.logger.Warn("cannot convert variable ", addFields, er)
        } else {
                vv, ok := val.(map[string]interface{})
                if ok {
                        for kkk, vvv := range vv {
                                event.PutValue(kkk, vvv)
                        }
                }
        }

        if len(errors) > 0 {
                return event, fmt.Errorf(strings.Join(errors, ", "))
        }
        return event, nil
}

the js code below

                console.log("Javascript engine loaded "); 
                process = function (fields){
                        addfields={}
                        addfields.javascript="gowork"
                        console.log(JSON.stringify(fields))
//                      var d = new Date(fields.timestamp)
//                      console.log(" date " + d)
                        return addfields
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants