├── .github ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTORS ├── DEVELOPER.md ├── Gemfile ├── LICENSE ├── NOTICE.TXT ├── README.md ├── Rakefile ├── docs └── index.asciidoc ├── lib └── logstash │ └── codecs │ └── avro.rb ├── logstash-codec-avro.gemspec └── spec └── codecs └── avro_spec.rb /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Logstash 2 | 3 | All contributions are welcome: ideas, patches, documentation, bug reports, 4 | complaints, etc! 5 | 6 | Programming is not a required skill, and there are many ways to help out! 7 | It is more important to us that you are able to contribute. 8 | 9 | That said, some basic guidelines, which you are free to ignore :) 10 | 11 | ## Want to learn? 12 | 13 | Want to lurk about and see what others are doing with Logstash? 14 | 15 | * The irc channel (#logstash on irc.freenode.org) is a good place for this 16 | * The [forum](https://discuss.elastic.co/c/logstash) is also 17 | great for learning from others. 18 | 19 | ## Got Questions? 20 | 21 | Have a problem you want Logstash to solve for you? 22 | 23 | * You can ask a question in the [forum](https://discuss.elastic.co/c/logstash) 24 | * Alternately, you are welcome to join the IRC channel #logstash on 25 | irc.freenode.org and ask for help there! 26 | 27 | ## Have an Idea or Feature Request? 28 | 29 | * File a ticket on [GitHub](https://github.com/elastic/logstash/issues). Please remember that GitHub is used only for issues and feature requests. If you have a general question, the [forum](https://discuss.elastic.co/c/logstash) or IRC would be the best place to ask. 30 | 31 | ## Something Not Working? Found a Bug? 32 | 33 | If you think you found a bug, it probably is a bug. 34 | 35 | * If it is a general Logstash or a pipeline issue, file it in [Logstash GitHub](https://github.com/elasticsearch/logstash/issues) 36 | * If it is specific to a plugin, please file it in the respective repository under [logstash-plugins](https://github.com/logstash-plugins) 37 | * or ask the [forum](https://discuss.elastic.co/c/logstash). 38 | 39 | # Contributing Documentation and Code Changes 40 | 41 | If you have a bugfix or new feature that you would like to contribute to 42 | logstash, and you think it will take more than a few minutes to produce the fix 43 | (ie; write code), it is worth discussing the change with the Logstash users and developers first! You can reach us via [GitHub](https://github.com/elastic/logstash/issues), the [forum](https://discuss.elastic.co/c/logstash), or via IRC (#logstash on freenode irc) 44 | Please note that Pull Requests without tests will not be merged. If you would like to contribute but do not have experience with writing tests, please ping us on IRC/forum or create a PR and ask our help. 45 | 46 | ## Contributing to plugins 47 | 48 | Check our [documentation](https://www.elastic.co/guide/en/logstash/current/contributing-to-logstash.html) on how to contribute to plugins or write your own! It is super easy! 49 | 50 | ## Contribution Steps 51 | 52 | 1. Test your changes! [Run](https://github.com/elastic/logstash#testing) the test suite 53 | 2. Please make sure you have signed our [Contributor License 54 | Agreement](https://www.elastic.co/contributor-agreement/). We are not 55 | asking you to assign copyright to us, but to give us the right to distribute 56 | your code without restriction. We ask this of all contributors in order to 57 | assure our users of the origin and continuing existence of the code. You 58 | only need to sign the CLA once. 59 | 3. Send a pull request! Push your changes to your fork of the repository and 60 | [submit a pull 61 | request](https://help.github.com/articles/using-pull-requests). In the pull 62 | request, describe what your changes do and mention any bugs/issues related 63 | to the pull request. 64 | 65 | 66 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please post all product and debugging questions on our [forum](https://discuss.elastic.co/c/logstash). Your questions will reach our wider community members there, and if we confirm that there is a bug, then we can open a new issue here. 2 | 3 | For all general issues, please provide the following details for fast resolution: 4 | 5 | - Version: 6 | - Operating System: 7 | - Config File (if you have sensitive info, please remove it): 8 | - Sample Data: 9 | - Steps to Reproduce: 10 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thanks for contributing to Logstash! If you haven't already signed our CLA, here's a handy link: https://www.elastic.co/contributor-agreement/ 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | Gemfile.lock 3 | .bundle 4 | .idea 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | import: 2 | - logstash-plugins/.ci:travis/travis.yml@1.x -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 3.4.1 2 | - Fixes `(Errno::ENOENT) No such file or directory` error [#43](https://github.com/logstash-plugins/logstash-codec-avro/pull/43) 3 | 4 | ## 3.4.0 5 | - Add `encoding` option to select the encoding of Avro payload, could be `binary` or `base64` [#39](https://github.com/logstash-plugins/logstash-codec-avro/pull/39) 6 | 7 | ## 3.3.1 8 | - Pin avro gem to 1.10.x, as 1.11+ requires ruby 2.6+ [#37](https://github.com/logstash-plugins/logstash-codec-avro/pull/37) 9 | 10 | ## 3.3.0 11 | - Add ECS support. Add target option and event.original [#36](https://github.com/logstash-plugins/logstash-codec-avro/pull/36) 12 | 13 | ## 3.2.4 14 | - [DOC] Add clarifications on partial deserialization [#35](https://github.com/logstash-plugins/logstash-codec-avro/pull/35) 15 | 16 | ## 3.2.3 17 | - Update gemspec summary 18 | 19 | ## 3.2.2 20 | - Fix some documentation issues 21 | 22 | ## 3.2.0 23 | - Fixed an issue with the encoding that prevented certain fields from being serialized in a way compatible with the Kafka plugins 24 | 25 | ## 3.1.0 26 | - Introduce `tag_on_failure` option to tag events with `_avroparsefailure` instead of throwing an exception when decoding 27 | 28 | ## 3.0.0 29 | - breaking: Update to new Event API 30 | 31 | ## 2.0.4 32 | - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash 33 | 34 | ## 2.0.3 35 | - New dependency requirements for logstash-core for the 5.0 release 36 | 37 | ## 2.0.0 38 | - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully, 39 | instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895 40 | - Dependency on logstash-core update to 2.0 41 | 42 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | The following is a list of people who have contributed ideas, code, bug 2 | reports, or in general have helped logstash along its way. 3 | 4 | Contributors: 5 | * Pier-Hugues Pellerin (ph) 6 | * Tal Levy (talevy) 7 | * Luca Belluccini (lucabelluccini) 8 | 9 | Note: If you've sent us patches, bug reports, or otherwise contributed to 10 | Logstash, and you aren't on the list above and want to be, please let us know 11 | and we'll make sure you're here. Contributions from folks like you are what make 12 | open source awesome. 13 | -------------------------------------------------------------------------------- /DEVELOPER.md: -------------------------------------------------------------------------------- 1 | Logstash Avro Codec 2 | =================== 3 | 4 | How to Install 5 | -------------- 6 | 7 | ``` 8 | bin/plugin install logstash-avro-codec 9 | ``` 10 | 11 | How to Use 12 | ---------- 13 | You can use this codec to decode avro messages 14 | in a Kafka topic input. 15 | 16 | Here is an example schema for tweets. 17 | 18 | ### tweet.avsc 19 | ``` 20 | { 21 | "type" : "record", 22 | "name" : "twitter_schema", 23 | "namespace" : "com.miguno.avro", 24 | "fields" : [ { 25 | "name" : "username", 26 | "type" : "string", 27 | "doc" : "Name of the user account on Twitter.com" 28 | }, { 29 | "name" : "tweet", 30 | "type" : "string", 31 | "doc" : "The content of the user's Twitter message" 32 | }, { 33 | "name" : "timestamp", 34 | "type" : "long", 35 | "doc" : "Unix epoch time in seconds" 36 | } ], 37 | "doc:" : "A basic schema for storing Twitter messages" 38 | } 39 | ``` 40 | 41 | Along with the logstash config for reading in messages of this 42 | type using the avro codec with the logstash-input-kafka plugin. 43 | 44 | ### logstash.conf 45 | 46 | ``` 47 | input { 48 | kafka { 49 | topic_id => 'test_topic' 50 | codec => avro { 51 | schema_uri => 'tweet.avsc' 52 | } 53 | } 54 | } 55 | 56 | output { 57 | stdout { 58 | codec => rubydebug 59 | } 60 | } 61 | ``` 62 | 63 | ### Running the setup 64 | ``` 65 | bin/logstash -f logstash.conf 66 | ``` 67 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash" 6 | use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1" 7 | 8 | if Dir.exist?(logstash_path) && use_logstash_source 9 | gem 'logstash-core', :path => "#{logstash_path}/logstash-core" 10 | gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api" 11 | end 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2020 Elastic and contributors 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE.TXT: -------------------------------------------------------------------------------- 1 | Elasticsearch 2 | Copyright 2012-2015 Elasticsearch 3 | 4 | This product includes software developed by The Apache Software 5 | Foundation (http://www.apache.org/). -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Logstash Plugin 2 | 3 | [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-codec-avro.svg)](https://travis-ci.com/logstash-plugins/logstash-codec-avro) 4 | 5 | This is a plugin for [Logstash](https://github.com/elastic/logstash). 6 | 7 | It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way. 8 | 9 | ## Documentation 10 | 11 | Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/). 12 | 13 | - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive 14 | - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide 15 | 16 | ## Need Help? 17 | 18 | Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum. 19 | 20 | ## Developing 21 | 22 | ### 1. Plugin Developement and Testing 23 | 24 | #### Code 25 | - To get started, you'll need JRuby with the Bundler gem installed. 26 | 27 | - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example). 28 | 29 | - Install dependencies 30 | ```sh 31 | bundle install 32 | ``` 33 | 34 | #### Test 35 | 36 | - Update your dependencies 37 | 38 | ```sh 39 | bundle install 40 | ``` 41 | 42 | - Run tests 43 | 44 | ```sh 45 | bundle exec rspec 46 | ``` 47 | 48 | ### 2. Running your unpublished Plugin in Logstash 49 | 50 | #### 2.1 Run in a local Logstash clone 51 | 52 | - Edit Logstash `Gemfile` and add the local plugin path, for example: 53 | ```ruby 54 | gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome" 55 | ``` 56 | - Install plugin 57 | ```sh 58 | # Logstash 2.3 and higher 59 | bin/logstash-plugin install --no-verify 60 | 61 | # Prior to Logstash 2.3 62 | bin/plugin install --no-verify 63 | 64 | ``` 65 | - Run Logstash with your plugin 66 | ```sh 67 | bin/logstash -e 'filter {awesome {}}' 68 | ``` 69 | At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash. 70 | 71 | #### 2.2 Run in an installed Logstash 72 | 73 | You 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: 74 | 75 | - Build your plugin gem 76 | ```sh 77 | gem build logstash-filter-awesome.gemspec 78 | ``` 79 | - Install the plugin from the Logstash home 80 | ```sh 81 | # Logstash 2.3 and higher 82 | bin/logstash-plugin install --no-verify 83 | 84 | # Prior to Logstash 2.3 85 | bin/plugin install --no-verify 86 | 87 | ``` 88 | - Start Logstash and proceed to test the plugin 89 | 90 | ## Contributing 91 | 92 | All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin. 93 | 94 | Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here. 95 | 96 | It is more important to the community that you are able to contribute. 97 | 98 | For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file. -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | @files=[] 2 | 3 | task :default do 4 | system("rake -T") 5 | end 6 | 7 | require "logstash/devutils/rake" 8 | -------------------------------------------------------------------------------- /docs/index.asciidoc: -------------------------------------------------------------------------------- 1 | :plugin: avro 2 | :type: codec 3 | 4 | /////////////////////////////////////////// 5 | START - GENERATED VARIABLES, DO NOT EDIT! 6 | /////////////////////////////////////////// 7 | :version: %VERSION% 8 | :release_date: %RELEASE_DATE% 9 | :changelog_url: %CHANGELOG_URL% 10 | :include_path: ../../../../logstash/docs/include 11 | /////////////////////////////////////////// 12 | END - GENERATED VARIABLES, DO NOT EDIT! 13 | /////////////////////////////////////////// 14 | 15 | [id="plugins-{type}s-{plugin}"] 16 | 17 | === Avro codec plugin 18 | 19 | include::{include_path}/plugin_header.asciidoc[] 20 | 21 | ==== Description 22 | 23 | Read serialized Avro records as Logstash events 24 | 25 | This plugin is used to serialize Logstash events as 26 | Avro datums, as well as deserializing Avro datums into 27 | Logstash events. 28 | 29 | [id="plugins-{type}s-{plugin}-ecs_metadata"] 30 | ==== Event Metadata and the Elastic Common Schema (ECS) 31 | 32 | The plugin behaves the same regardless of ECS compatibility, except adding the original message to `[event][original]`. 33 | 34 | ==== Encoding 35 | 36 | This codec is for serializing individual Logstash events 37 | as Avro datums that are Avro binary blobs. It does not encode 38 | Logstash events into an Avro file. 39 | 40 | 41 | ==== Decoding 42 | 43 | This codec is for deserializing individual Avro records. It is not for reading 44 | Avro files. Avro files have a unique format that must be handled upon input. 45 | 46 | .Partial deserialization 47 | [NOTE] 48 | ================================================================================ 49 | Avro format is known to support partial deserialization of arbitrary fields, 50 | providing a schema containing a subset of the schema which was used to serialize 51 | the data. 52 | This codec *doesn't support partial deserialization of arbitrary fields*. 53 | Partial deserialization _might_ work only when providing a schema which contains 54 | the first `N` fields of the schema used to serialize the data (and 55 | in the same order). 56 | ================================================================================ 57 | 58 | ==== Usage 59 | Example usage with Kafka input. 60 | 61 | [source,ruby] 62 | ---------------------------------- 63 | input { 64 | kafka { 65 | codec => avro { 66 | schema_uri => "/tmp/schema.avsc" 67 | } 68 | } 69 | } 70 | filter { 71 | ... 72 | } 73 | output { 74 | ... 75 | } 76 | ---------------------------------- 77 | 78 | [id="plugins-{type}s-{plugin}-options"] 79 | ==== Avro Codec Configuration Options 80 | 81 | [cols="<,<,<",options="header",] 82 | |======================================================================= 83 | |Setting |Input type|Required 84 | | <> | <>|No 85 | | <> | <>, one of `["binary", "base64"]`|No 86 | | <> |<>|Yes 87 | | <> |<>|No 88 | | <> |<>|No 89 | |======================================================================= 90 | 91 |   92 | 93 | [id="plugins-{type}s-{plugin}-ecs_compatibility"] 94 | ===== `ecs_compatibility` 95 | 96 | * Value type is <> 97 | * Supported values are: 98 | ** `disabled`: Avro data added at root level 99 | ** `v1`,`v8`: Elastic Common Schema compliant behavior (`[event][original]` is also added) 100 | 101 | Controls this plugin's compatibility with the {ecs-ref}[Elastic Common Schema (ECS)]. 102 | 103 | [id="plugins-{type}s-{plugin}-encoding"] 104 | ===== `encoding` 105 | 106 | * Value can be any of: `binary`, `base64` 107 | * Default value is `base64` 108 | 109 | Set encoding for Avro's payload. 110 | Use `base64` (default) to indicate that this codec sends or expects to receive base64-encoded bytes. 111 | 112 | Set this option to `binary` to indicate that this codec sends or expects to receive binary Avro data. 113 | 114 | 115 | [id="plugins-{type}s-{plugin}-schema_uri"] 116 | ===== `schema_uri` 117 | 118 | * This is a required setting. 119 | * Value type is <> 120 | * There is no default value for this setting. 121 | 122 | schema path to fetch the schema from. 123 | This can be a 'http' or 'file' scheme URI 124 | example: 125 | 126 | * http - `http://example.com/schema.avsc` 127 | * file - `/path/to/schema.avsc` 128 | 129 | [id="plugins-{type}s-{plugin}-tag_on_failure"] 130 | ===== `tag_on_failure` 131 | 132 | * Value type is <> 133 | * Default value is `false` 134 | 135 | tag events with `_avroparsefailure` when decode fails 136 | 137 | [id="plugins-{type}s-{plugin}-target"] 138 | ===== `target` 139 | 140 | * Value type is <> 141 | * There is no default value for this setting. 142 | * This is only relevant when decode data into an event 143 | 144 | Define the target field for placing the values. If this setting is not 145 | set, the Avro data will be stored at the root (top level) of the event. 146 | 147 | *Example* 148 | [source,ruby] 149 | ---------------------------------- 150 | input { 151 | kafka { 152 | codec => avro { 153 | schema_uri => "/tmp/schema.avsc" 154 | target => "[document]" 155 | } 156 | } 157 | } 158 | ---------------------------------- 159 | -------------------------------------------------------------------------------- /lib/logstash/codecs/avro.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require "open-uri" 3 | require "avro" 4 | require "base64" 5 | require "logstash/codecs/base" 6 | require "logstash/event" 7 | require "logstash/timestamp" 8 | require "logstash/util" 9 | require 'logstash/plugin_mixins/ecs_compatibility_support' 10 | require 'logstash/plugin_mixins/ecs_compatibility_support/target_check' 11 | require 'logstash/plugin_mixins/validator_support/field_reference_validation_adapter' 12 | require 'logstash/plugin_mixins/event_support/event_factory_adapter' 13 | 14 | # Read serialized Avro records as Logstash events 15 | # 16 | # This plugin is used to serialize Logstash events as 17 | # Avro datums, as well as deserializing Avro datums into 18 | # Logstash events. 19 | # 20 | # ==== Encoding 21 | # 22 | # This codec is for serializing individual Logstash events 23 | # as Avro datums that are Avro binary blobs. It does not encode 24 | # Logstash events into an Avro file. 25 | # 26 | # 27 | # ==== Decoding 28 | # 29 | # This codec is for deserializing individual Avro records. It is not for reading 30 | # Avro files. Avro files have a unique format that must be handled upon input. 31 | # 32 | # 33 | # ==== Usage 34 | # Example usage with Kafka input. 35 | # 36 | # [source,ruby] 37 | # ---------------------------------- 38 | # input { 39 | # kafka { 40 | # codec => avro { 41 | # schema_uri => "/tmp/schema.avsc" 42 | # } 43 | # } 44 | # } 45 | # filter { 46 | # ... 47 | # } 48 | # output { 49 | # ... 50 | # } 51 | # ---------------------------------- 52 | class LogStash::Codecs::Avro < LogStash::Codecs::Base 53 | config_name "avro" 54 | 55 | include LogStash::PluginMixins::ECSCompatibilitySupport(:disabled, :v1, :v8 => :v1) 56 | include LogStash::PluginMixins::ECSCompatibilitySupport::TargetCheck 57 | 58 | extend LogStash::PluginMixins::ValidatorSupport::FieldReferenceValidationAdapter 59 | 60 | include LogStash::PluginMixins::EventSupport::EventFactoryAdapter 61 | 62 | BINARY_ENCODING = "binary".freeze 63 | BASE64_ENCODING = "base64".freeze 64 | 65 | # Set encoding for Avro's payload. 66 | # Use `base64` (default) encoding to convert the raw binary bytes to a `base64` encoded string. 67 | # Set this option to `binary` to use the plain binary bytes. 68 | config :encoding, :validate => [BINARY_ENCODING, BASE64_ENCODING], :default => BASE64_ENCODING 69 | 70 | # schema path to fetch the schema from. 71 | # This can be a 'http' or 'file' scheme URI 72 | # example: 73 | # 74 | # * http - `http://example.com/schema.avsc` 75 | # * file - `/path/to/schema.avsc` 76 | config :schema_uri, :validate => :string, :required => true 77 | 78 | # tag events with `_avroparsefailure` when decode fails 79 | config :tag_on_failure, :validate => :boolean, :default => false 80 | 81 | # Defines a target field for placing decoded fields. 82 | # If this setting is omitted, data gets stored at the root (top level) of the event. 83 | # 84 | # NOTE: the target is only relevant while decoding data into a new event. 85 | config :target, :validate => :field_reference 86 | 87 | def open_and_read(uri_string) 88 | URI.open(uri_string, &:read) 89 | end 90 | 91 | public 92 | def initialize(*params) 93 | super 94 | @original_field = ecs_select[disabled: nil, v1: '[event][original]'] 95 | end 96 | 97 | def register 98 | @schema = Avro::Schema.parse(open_and_read(schema_uri)) 99 | end 100 | 101 | public 102 | def decode(data) 103 | if encoding == BASE64_ENCODING 104 | datum = StringIO.new(Base64.strict_decode64(data)) rescue StringIO.new(data) 105 | else 106 | datum = StringIO.new(data) 107 | end 108 | decoder = Avro::IO::BinaryDecoder.new(datum) 109 | datum_reader = Avro::IO::DatumReader.new(@schema) 110 | event = targeted_event_factory.new_event(datum_reader.read(decoder)) 111 | event.set(@original_field, data.dup.freeze) if @original_field 112 | yield event 113 | rescue => e 114 | if tag_on_failure 115 | @logger.error("Avro parse error, original data now in message field", :error => e) 116 | yield event_factory.new_event("message" => data, "tags" => ["_avroparsefailure"]) 117 | else 118 | raise e 119 | end 120 | end 121 | 122 | public 123 | def encode(event) 124 | dw = Avro::IO::DatumWriter.new(@schema) 125 | buffer = StringIO.new 126 | encoder = Avro::IO::BinaryEncoder.new(buffer) 127 | dw.write(event.to_hash, encoder) 128 | if encoding == BASE64_ENCODING 129 | @on_event.call(event, Base64.strict_encode64(buffer.string)) 130 | else 131 | @on_event.call(event, buffer.string) 132 | end 133 | end 134 | end 135 | -------------------------------------------------------------------------------- /logstash-codec-avro.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | 3 | s.name = 'logstash-codec-avro' 4 | s.version = '3.4.1' 5 | s.platform = 'java' 6 | s.licenses = ['Apache-2.0'] 7 | s.summary = "Reads serialized Avro records as Logstash events" 8 | s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program" 9 | s.authors = ["Elastic"] 10 | s.email = 'info@elastic.co' 11 | s.homepage = "https://www.elastic.co/logstash" 12 | s.require_paths = ["lib"] 13 | 14 | # Files 15 | s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "docs/**/*"] 16 | 17 | # Tests 18 | s.test_files = s.files.grep(%r{^(test|spec|features)/}) 19 | 20 | # Special flag to let us know this is actually a logstash plugin 21 | s.metadata = { "logstash_plugin" => "true", "logstash_group" => "codec" } 22 | 23 | # Gem dependencies 24 | s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" 25 | s.add_runtime_dependency "avro", "~> 1.10.2" #(Apache 2.0 license) 26 | s.add_runtime_dependency 'logstash-mixin-ecs_compatibility_support', '~> 1.3' 27 | s.add_runtime_dependency 'logstash-mixin-event_support', '~> 1.0' 28 | s.add_runtime_dependency 'logstash-mixin-validator_support', '~> 1.0' 29 | 30 | s.add_development_dependency 'logstash-devutils' 31 | s.add_development_dependency 'insist' 32 | 33 | end 34 | 35 | -------------------------------------------------------------------------------- /spec/codecs/avro_spec.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require 'logstash/devutils/rspec/spec_helper' 3 | require 'insist' 4 | require 'avro' 5 | require 'base64' 6 | require 'logstash/codecs/avro' 7 | require 'logstash/event' 8 | require 'logstash/plugin_mixins/ecs_compatibility_support/spec_helper' 9 | 10 | describe LogStash::Codecs::Avro, :ecs_compatibility_support, :aggregate_failures do 11 | 12 | ecs_compatibility_matrix(:disabled, :v1, :v8 => :v1) do |ecs_select| 13 | before(:each) do 14 | allow_any_instance_of(described_class).to receive(:ecs_compatibility).and_return(ecs_compatibility) 15 | end 16 | 17 | context "non binary data" do 18 | let (:avro_config) {{ 'schema_uri' => ' 19 | {"type": "record", "name": "Test", 20 | "fields": [{"name": "foo", "type": ["null", "string"]}, 21 | {"name": "bar", "type": "int"}]}' }} 22 | let (:test_event_hash) { { "foo" => "hello", "bar" => 10 } } 23 | let (:test_event) {LogStash::Event.new(test_event_hash)} 24 | 25 | subject do 26 | allow_any_instance_of(LogStash::Codecs::Avro).to \ 27 | receive(:open_and_read).and_return(avro_config['schema_uri']) 28 | next LogStash::Codecs::Avro.new(avro_config) 29 | end 30 | 31 | context "#decode" do 32 | it "should return an LogStash::Event from raw and base64 encoded avro data" do 33 | schema = Avro::Schema.parse(avro_config['schema_uri']) 34 | dw = Avro::IO::DatumWriter.new(schema) 35 | buffer = StringIO.new 36 | encoder = Avro::IO::BinaryEncoder.new(buffer) 37 | dw.write(test_event.to_hash, encoder) 38 | 39 | subject.decode(Base64.strict_encode64(buffer.string)) do |event| 40 | insist {event.is_a? LogStash::Event} 41 | insist {event.get("foo")} == test_event.get("foo") 42 | insist {event.get("bar")} == test_event.get("bar") 43 | expect(event.get('[event][original]')).to eq(Base64.strict_encode64(buffer.string)) if ecs_compatibility != :disabled 44 | end 45 | subject.decode(buffer.string) do |event| 46 | insist {event.is_a? LogStash::Event} 47 | insist {event.get("foo")} == test_event.get("foo") 48 | insist {event.get("bar")} == test_event.get("bar") 49 | expect(event.get('[event][original]')).to eq(buffer.string) if ecs_compatibility != :disabled 50 | end 51 | end 52 | 53 | it "should throw exception if decoding fails" do 54 | expect {subject.decode("not avro") {|_| }}.to raise_error NoMethodError 55 | end 56 | end 57 | 58 | context "with binary encoding" do 59 | let (:avro_config) { super().merge('encoding' => 'binary') } 60 | 61 | it "should return an LogStash::Event from raw and base64 encoded avro data" do 62 | schema = Avro::Schema.parse(avro_config['schema_uri']) 63 | dw = Avro::IO::DatumWriter.new(schema) 64 | buffer = StringIO.new 65 | encoder = Avro::IO::BinaryEncoder.new(buffer) 66 | dw.write(test_event.to_hash, encoder) 67 | 68 | subject.decode(buffer.string) do |event| 69 | expect(event).to be_a_kind_of(LogStash::Event) 70 | expect(event.get("foo")).to eq(test_event.get("foo")) 71 | expect(event.get("bar")).to eq(test_event.get("bar")) 72 | expect(event.get('[event][original]')).to eq(buffer.string) if ecs_compatibility != :disabled 73 | end 74 | end 75 | 76 | it "should raise an error if base64 encoded data is provided" do 77 | schema = Avro::Schema.parse(avro_config['schema_uri']) 78 | dw = Avro::IO::DatumWriter.new(schema) 79 | buffer = StringIO.new 80 | encoder = Avro::IO::BinaryEncoder.new(buffer) 81 | dw.write(test_event.to_hash, encoder) 82 | 83 | expect {subject.decode(Base64.strict_encode64(buffer.string))}.to raise_error 84 | end 85 | end 86 | 87 | context "#decode with tag_on_failure" do 88 | let (:avro_config) {{ 'schema_uri' => ' 89 | {"type": "record", "name": "Test", 90 | "fields": [{"name": "foo", "type": ["null", "string"]}, 91 | {"name": "bar", "type": "int"}]}', 92 | 'tag_on_failure' => true}} 93 | 94 | it "should tag event on failure" do 95 | subject.decode("not avro") do |event| 96 | insist {event.is_a? LogStash::Event} 97 | insist {event.get("tags")} == ["_avroparsefailure"] 98 | end 99 | end 100 | end 101 | 102 | context "#decode with target" do 103 | let(:avro_target) { "avro_target" } 104 | let (:avro_config) {{ 'schema_uri' => ' 105 | {"type": "record", "name": "Test", 106 | "fields": [{"name": "foo", "type": ["null", "string"]}, 107 | {"name": "bar", "type": "int"}]}', 108 | 'target' => avro_target}} 109 | 110 | it "should return an LogStash::Event with content in target" do 111 | schema = Avro::Schema.parse(avro_config['schema_uri']) 112 | dw = Avro::IO::DatumWriter.new(schema) 113 | buffer = StringIO.new 114 | encoder = Avro::IO::BinaryEncoder.new(buffer) 115 | dw.write(test_event.to_hash, encoder) 116 | 117 | subject.decode(buffer.string) do |event| 118 | insist {event.get("[#{avro_target}][foo]")} == test_event.get("foo") 119 | insist {event.get("[#{avro_target}][bar]")} == test_event.get("bar") 120 | end 121 | end 122 | end 123 | 124 | context "#encode" do 125 | it "should return avro data from a LogStash::Event" do 126 | got_event = false 127 | subject.on_event do |event, data| 128 | schema = Avro::Schema.parse(avro_config['schema_uri']) 129 | datum = StringIO.new(Base64.strict_decode64(data)) 130 | decoder = Avro::IO::BinaryDecoder.new(datum) 131 | datum_reader = Avro::IO::DatumReader.new(schema) 132 | record = datum_reader.read(decoder) 133 | 134 | insist {record["foo"]} == test_event.get("foo") 135 | insist {record["bar"]} == test_event.get("bar") 136 | insist {event.is_a? LogStash::Event} 137 | got_event = true 138 | end 139 | subject.encode(test_event) 140 | insist {got_event} 141 | end 142 | 143 | context "with binary encoding" do 144 | let (:avro_config) { super().merge('encoding' => 'binary') } 145 | 146 | it "should return avro data from a LogStash::Event not base64 encoded" do 147 | got_event = false 148 | subject.on_event do |event, data| 149 | schema = Avro::Schema.parse(avro_config['schema_uri']) 150 | datum = StringIO.new(data) 151 | decoder = Avro::IO::BinaryDecoder.new(datum) 152 | datum_reader = Avro::IO::DatumReader.new(schema) 153 | record = datum_reader.read(decoder) 154 | 155 | expect(event).to be_a_kind_of(LogStash::Event) 156 | expect(event.get("foo")).to eq(test_event.get("foo")) 157 | expect(event.get("bar")).to eq(test_event.get("bar")) 158 | got_event = true 159 | end 160 | subject.encode(test_event) 161 | expect(got_event).to be true 162 | end 163 | end 164 | 165 | context "binary data" do 166 | 167 | let (:avro_config) {{ 'schema_uri' => '{"namespace": "com.systems.test.data", 168 | "type": "record", 169 | "name": "TestRecord", 170 | "fields": [ 171 | {"name": "name", "type": ["string", "null"]}, 172 | {"name": "longitude", "type": ["double", "null"]}, 173 | {"name": "latitude", "type": ["double", "null"]} 174 | ] 175 | }' }} 176 | let (:test_event) {LogStash::Event.new({ "name" => "foo", "longitude" => 21.01234.to_f, "latitude" => 111.0123.to_f })} 177 | 178 | subject do 179 | allow_any_instance_of(LogStash::Codecs::Avro).to \ 180 | receive(:open_and_read).and_return(avro_config['schema_uri']) 181 | next LogStash::Codecs::Avro.new(avro_config) 182 | end 183 | 184 | it "should correctly encode binary data" do 185 | schema = Avro::Schema.parse(avro_config['schema_uri']) 186 | dw = Avro::IO::DatumWriter.new(schema) 187 | buffer = StringIO.new 188 | encoder = Avro::IO::BinaryEncoder.new(buffer) 189 | dw.write(test_event.to_hash, encoder) 190 | 191 | subject.decode(Base64.strict_encode64(buffer.string)) do |event| 192 | insist {event.is_a? LogStash::Event} 193 | insist {event.get("name")} == test_event.get("name") 194 | insist {event.get("longitude")} == test_event.get("longitude") 195 | insist {event.get("latitude")} == test_event.get("latitude") 196 | end 197 | end 198 | end 199 | end 200 | 201 | end 202 | end 203 | end 204 | --------------------------------------------------------------------------------