{"id":15608560,"url":"https://github.com/kares/logstash-filter-javascript","last_synced_at":"2026-05-01T15:34:57.697Z","repository":{"id":66674077,"uuid":"307660790","full_name":"kares/logstash-filter-javascript","owner":"kares","description":"Javascript in Logstash","archived":false,"fork":false,"pushed_at":"2020-10-30T14:01:34.000Z","size":109,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-04T15:24:54.700Z","etag":null,"topics":["javascript","logstash","logstash-filter","logstash-plugin"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kares.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-27T10:18:46.000Z","updated_at":"2020-10-30T14:01:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"2c78bc98-7855-4195-882d-81f7958bcb63","html_url":"https://github.com/kares/logstash-filter-javascript","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kares%2Flogstash-filter-javascript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kares%2Flogstash-filter-javascript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kares%2Flogstash-filter-javascript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kares%2Flogstash-filter-javascript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kares","download_url":"https://codeload.github.com/kares/logstash-filter-javascript/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246194857,"owners_count":20738756,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","logstash","logstash-filter","logstash-plugin"],"created_at":"2024-10-03T05:21:24.569Z","updated_at":"2026-05-01T15:34:57.652Z","avatar_url":"https://github.com/kares.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Logstash Javascript Filter\n\n[![Travis Build Status](https://travis-ci.com/kares/logstash-filter-javascript.svg)](https://travis-ci.com/kares/logstash-filter-javascript)\n\nThis is a plugin for [Logstash](https://www.elastic.co/guide/en/logstash/current/introduction.html), compatible with LS \nversions **\u003e= 6.8**, that allows you to interact with pipeline events using scripts written in Javascript. \nIt works in a similar way as Logstash's (official) [Ruby filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-ruby.html). \n\n**Only Java 8 - 13 is supported**, running LS on Java 15 or later won't work due the removal of the Nashorn Javascript engine.\n\n**DISCLAIMER: Plugin is considered a (working) experiment and is no way as battle tested as (official) Logstash plugins supported \nby [Elastic](https://www.elastic.co/support/matrix#matrix_logstash_plugins).** \n\nIn general, performance wise, you can expect the same throughput as with the Ruby filter.\nAlso, performance of the Nashorn Javascript engine *might* vary between Java versions.\n\n## Usage\n\nTo inline Javascript in your filter, place all code in the `code` option. This code will be executed for every event the \nfilter receives. For example, to cancel 90% of events, you can do this:\n```javascript\n  filter {\n    javascript {\n      code =\u003e \"if (java.lang.Math.random() \u003c= 0.9) event.cancel()\"\n    }\n  }\n```\n\nYou can also place JS code in the `init` option - it will be executed only once during the plugin's initialization phase.\nThis is a great place to \"feature validate\" the Javascript engine:\n\n```javascript\n  filter {\n    javascript {\n      init =\u003e \"if (Number.MIN_VALUE \u003c= 0) throw new Error(0); if (parseInt('f*ck', 16) !== 15) throw 'f*ck'\"\n      code =\u003e 'event.setField(\"message\", \"b\" + \"a\" + +\"a\" + \"a\")'\n    }\n  }\n```\n\n### Installing\n\n`$LS_HOME/bin/logstash-plugin install logstash-filter-javascript`\n\n### Configuration\n\nTO-BE-CONTINUED...\n\n### Differences from Ruby filter\n\nUnlike the Ruby filter, which allows you to hook into the LS execution runtime, the Javascript filter starts an isolated\nJS engine on every filter use.\n\nThere's no `new_event_block` callback hook implemented in the Javascript filter, this one (if requested) deserves more \nthought as it just felt a bit \"hacky\" to copy what the Ruby filter does. \n\nThe Javascript filter does not expose a `register` function (with `script_params`), instead you can use `init_parameters` \nto set variables in the global scope which will than be accessible from within the `filter` function. \n\n### Tips \u0026 Tricks\n\nNashorn defaults to ECMAScript 5.1 by default which lacks the compact arrow `arg =\u003e ...` syntax or the `let` keyword.\nThere's (incomplete) support for ECMA 6 but requires setting a system properly, navigate to *config/jvm.options* and add :\n```\n-Dnashorn.args=--language=es6\n```\n\nBe aware of scripting Java types with Nashorn as not all native Javascript APIs will handle those seamlessly and \nmight lead to surprising results e.g.\n\n```javascript\nvar json = JSON.stringify(event.toMap()); // undefined\n// as JSON does not handle a java.util.Map returned from the LS event\n\n// one can instead convert Java types to native JS objects e.g.\nvar map = event.toMap()\nvar obj = {}\nfor each (var key in map.keySet()) obj[key] = map.get(key) // Nashorn for-each extension for Java arrays/collections\n```\n\nYou can set plain-old Javascript objects as values on the event, LS will see them as maps and convert them accordingly:\n\n```javascript\nvar obj = { foo: \"bar\", truthy: true, aNull: null, number: 11.1 }\nevent.setField('js.values', obj)\n// be aware when JS values contain function types as they might lead to issues\n```\n\n```javascript\n// using JS types with a Java type system might lead to issues e.g. setting it on an event e.g.\nevent.setField('unexpected-value', undefined); // LS will complain not being able to handle :\n// Missing Converter handling for full class name=jdk.nashorn.internal.runtime.Undefined\n```\n\nThere's no `console.log` with Nashorn, however you could use Java's system output for debugging purposes :\n```javascript\nfunction puts(msg) {\n  java.lang.System.out.println(msg)  \n}\n\nputs('event: ' + event.toMap());\n\n// or simply the built-in print method :\nprint('event: ', event)\n```\n**NOTE**: be aware to remove such debugging statements in production to not fill up LS' standard output!\n\n## Developing\n\n### 1. Plugin Development and Testing\n\n#### Code\n\n- To get started, you'll need JRuby (\u003e= 9.1) with the Bundler gem installed.\n\n- Install dependencies\n```sh\njruby -S bundle\n```\n\n#### Test\n\n```sh\njruby -rbundler/setup -S rspec\n```\n\n### 2. Running your unpublished Plugin in Logstash\n\n- Edit Logstash's `Gemfile` and add the local plugin path e.g.:\n```ruby\ngem \"logstash-filter-javascript\", :path =\u003e \"path/to/local/logstash-filter-javascript\"\n```\n- Install plugin\n```sh\nbin/logstash-plugin install --no-verify\n```\n- Run Logstash with your plugin\n```sh\nbin/logstash -e \"filter { javascript { code =\u003e \\\"print('Hello from JS: ' + event.getField('message'))\\\" } }\"\n```\nAt this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.\n\n#### 2.2 Run in an installed Logstash\n\nYou can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:\n\n- Build your plugin gem\n```sh\ngem build logstash-filter-awesome.gemspec\n```\n- Install the plugin from the Logstash home\n```sh\n# Logstash 2.3 and higher\nbin/logstash-plugin install --no-verify\n\n# Prior to Logstash 2.3\nbin/plugin install --no-verify\n\n```\n- Start Logstash and proceed to test the plugin\n\n## Copyright\n\n(c) 2020 [Karol Bucek](https://github.com/kares).\nSee LICENSE (http://www.apache.org/licenses/LICENSE-2.0) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkares%2Flogstash-filter-javascript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkares%2Flogstash-filter-javascript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkares%2Flogstash-filter-javascript/lists"}