├── .travis.yml ├── .gitignore ├── Rakefile ├── NOTICE.TXT ├── examples └── rabbitmq_stdout.conf ├── Gemfile ├── CONTRIBUTORS ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── CONTRIBUTING.md ├── logstash-input-rabbitmq.gemspec ├── CHANGELOG.md ├── README.md ├── spec └── inputs │ └── rabbitmq_spec.rb ├── LICENSE ├── lib └── logstash │ └── inputs │ └── rabbitmq.rb └── docs └── index.asciidoc /.travis.yml: -------------------------------------------------------------------------------- 1 | import: 2 | - logstash-plugins/.ci:travis/travis.yml@1.x -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | Gemfile.lock 3 | .bundle 4 | vendor 5 | .idea 6 | *~ -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | @files=[] 2 | 3 | task :default do 4 | system("rake -T") 5 | end 6 | 7 | require "logstash/devutils/rake" 8 | -------------------------------------------------------------------------------- /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/). -------------------------------------------------------------------------------- /examples/rabbitmq_stdout.conf: -------------------------------------------------------------------------------- 1 | input { 2 | rabbitmq { 3 | host => "localhost" 4 | exchange => "foo" 5 | } 6 | } 7 | 8 | output { 9 | stdout { 10 | codec => rubydebug 11 | } 12 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | * Avishai Ish-Shalom (avishai-ish-shalom) 6 | * Jonathan Van Eenwyk (jdve) 7 | * Jordan Sissel (jordansissel) 8 | * João Duarte (jsvd) 9 | * Mathieu MILLET (htam-net) 10 | * Michael Klishin (michaelklishin) 11 | * Michael Zaccari (mzaccari) 12 | * Nick Ethier (nickethier) 13 | * Pier-Hugues Pellerin (ph) 14 | * Richard Pijnenburg (electrical) 15 | * Suyog Rao (suyograo) 16 | * Tim Potter (tpot) 17 | * avleen 18 | 19 | Note: If you've sent us patches, bug reports, or otherwise contributed to 20 | Logstash, and you aren't on the list above and want to be, please let us know 21 | and we'll make sure you're here. Contributions from folks like you are what make 22 | open source awesome. 23 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## RabbitMQ Input Plugin's Issue Tracker Has Moved 2 | 3 | This RabbitMQ Input Plugin is now a part of the [RabbitMQ Integration Plugin][integration-source]; 4 | this project remains open for backports of fixes from that project to the 6.x series where possible, but issues should first be filed on the [integration plugin][integration-issues]. 5 | 6 | Please post all product and debugging questions on our [forum][logstash-forum]. 7 | 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 on the appropriate project. 8 | 9 | [integration-source]: https://github.com/logstash-plugins/logstash-integration-rabbitmq 10 | [integration-issues]: https://github.com/logstash-plugins/logstash-integration-rabbitmq/issues/ 11 | [logstash-forum]: https://discuss.elastic.co/c/logstash -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## RabbitMQ Input Plugin's Source Has Moved 2 | 3 | This RabbitMQ Input Plugin is now a part of the [RabbitMQ Integration Plugin][integration-source]; 4 | this project remains open for backports of fixes from that project to the 6.x series where possible, but pull-requests should first be made on the [integration plugin][integration-pull-requests]. 5 | 6 | If you have already made commits on a clone of this stand-alone repository, it's ok! Go ahead and open the Pull Request here, and open an Issue linking to it on the [integration plugin][integration-issues] -- we'll work with you to sort it all out and to get the backport applied. 7 | 8 | ## Contributor Agreement 9 | 10 | Thanks for contributing to Logstash! If you haven't already signed our CLA, here's a handy link: https://www.elastic.co/contributor-agreement/ 11 | 12 | [integration-source]: https://github.com/logstash-plugins/logstash-integration-rabbitmq 13 | [integration-issues]: https://github.com/logstash-plugins/logstash-integration-rabbitmq/issues/ 14 | [integration-pull-requests]: https://github.com/logstash-plugins/logstash-integration-rabbitmq/pulls 15 | -------------------------------------------------------------------------------- /logstash-input-rabbitmq.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = 'logstash-input-rabbitmq' 3 | s.version = '6.0.4' 4 | s.licenses = ['Apache License (2.0)'] 5 | s.summary = "Pulls events from a RabbitMQ exchange" 6 | 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" 7 | s.authors = ["Elastic"] 8 | s.email = 'info@elastic.co' 9 | s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html" 10 | s.require_paths = ["lib"] 11 | 12 | # Files 13 | s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "docs/**/*"] 14 | 15 | # Tests 16 | s.test_files = s.files.grep(%r{^(test|spec|features)/}) 17 | 18 | # Special flag to let us know this is actually a logstash plugin 19 | s.metadata = { "logstash_plugin" => "true", "logstash_group" => "input" } 20 | 21 | # Gem dependencies 22 | s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" 23 | s.add_runtime_dependency "logstash-mixin-rabbitmq_connection", '>= 5.0.0', '< 6.0.0' 24 | 25 | s.add_runtime_dependency 'logstash-codec-json' 26 | 27 | s.add_development_dependency 'logstash-devutils' 28 | s.add_development_dependency 'logstash-codec-plain' 29 | end 30 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 6.0.4 2 | - Docs: Optional queue args: link to RabbitMQ docs instead 3 | 4 | ## 6.0.3 5 | - Docs: Set the default_codec doc attribute. 6 | 7 | ## 6.0.2 8 | - Update gemspec summary 9 | 10 | ## 6.0.1 11 | - Fix some documentation issues 12 | 13 | ## 6.0.0 14 | - Require v5.0.0 of rabbitmq-connection_mixin which obsoletes some deprecated options 15 | 16 | ## 5.2.4 17 | - Require v4.3.0 of rabbitmq-connection_mixin which bumps the major version of the java rabbitmq lib 18 | 19 | ## 5.2.3 20 | - Fix metadata decoration so that `add_field` and friends aren't overwritten by headers and properties 21 | 22 | ## 5.2.2 23 | - Remove legacy header normalization code. MarchHare does this directly now. 24 | 25 | ## 5.2.1 26 | - Bump march_hare version to fix issue where metadata would be broken for strings > 255 chars 27 | due to their transformation to rabbitmq java's LongString class 28 | 29 | ## 5.2.0 30 | - Fix behavior where plugin would block on register if server was unavailable 31 | Plugin will now always exit register successfully and just log errors on #run 32 | - Bumped connection mixin dependency to better handle proxies and error logging 33 | 34 | ## 5.1.1 35 | - Relax constraint on logstash-core-plugin-api to >= 1.60 <= 2.99 36 | 37 | ## 5.1.0 38 | - Bump mixin version to clean up TLS behavior 39 | 40 | ## 5.0.1 41 | - Republish all the gems under jruby. 42 | ## 5.0.0 43 | - Update the plugin to the version 2.0 of the plugin api, this change is required for Logstash 5.0 compatibility. See https://github.com/elastic/logstash/issues/5141 44 | # 4.0.1 45 | - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash 46 | ## 4.0.0 47 | - Ensure the consumer is done processing messages before closing channel. Fixes shutdown errors. 48 | - Use an internal queue + separate thread to accelerate processing 49 | - Disable metadata insertion by default, this slows things down majorly 50 | - Make exchange_type an optional config option with using 'exchange'. 51 | When used the exchange will always be declared 52 | 53 | ## 3.3.1 54 | - New dependency requirements for logstash-core for the 5.0 release 55 | 56 | ## 3.3.0 57 | - Fix a regression in 3.2.0 that reinstated behavior that duplicated consumers 58 | - Always declare exchanges used, the exchange now need not exist before LS starts 59 | - Allow a precise specification of the exchange type 60 | 61 | ## 3.2.0 62 | - The properties and headers of the messages are now saved in the [@metadata][rabbitmq_headers] and [@metadata][rabbitmq_properties] fields. 63 | - Logstash now shuts down if the server sends a basic.cancel method. 64 | - Reinstating the overview documentation that was lost in 3.0.0 and updating it to be clearer. 65 | - Internal: Various spec improvements that decrease flakiness and eliminate the queue littering of the integration test RabbitMQ instance. 66 | 67 | ## 3.1.5 68 | - Fix a bug where when reconnecting a duplicate consumer would be created 69 | 70 | ## 3.1.4 71 | - Fix a bug where this plugin would not properly reconnect if it lost its connection to the server while the connection would re-establish itself, the queue subscription would not 72 | 73 | ## 3.1.3 74 | - Fix a spec that shouldn't have broken with LS2.2 75 | ## 3.1.2 76 | - Upgrade march hare version to fix file perms issue 77 | ## 3.1.1 78 | - Default codec setting should have been JSON 79 | 80 | ## 3.1.0 81 | - Fix broken prefetch count parameter 82 | 83 | ## 3.0.2 84 | - Bump dependency on logstash-mixin-rabbitmq_connection 85 | 86 | ## 3.0.0 87 | - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully, 88 | instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895 89 | - Dependency on logstash-core update to 2.0 90 | 91 | * 2.0.0 92 | - Massive refactor 93 | - Implement Logstash 2.x stop behavior 94 | - Fix reconnect issues 95 | - Depend on rabbitmq_connection mixin for most connection functionality 96 | * 1.1.1 97 | - Bump march hare to 2.12.0 which fixes jar perms on unices 98 | * 1.1.0 99 | - Bump march hare version to 2.11.0 100 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Logstash Plugin 2 | 3 | [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-input-rabbitmq.svg)](https://travis-ci.com/logstash-plugins/logstash-input-rabbitmq) 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 | 10 | ## RabbitMQ Input Plugin Has Moved 11 | 12 | This RabbitMQ Input Plugin is now a part of the [RabbitMQ Integration Plugin][integration-source]; 13 | this project remains open for backports of fixes from that project to the 6.x series where possible, but issues should first be filed on the [integration plugin][integration-issues]. 14 | 15 | [integration-source]: https://github.com/logstash-plugins/logstash-integration-rabbitmq 16 | [integration-issues]: https://github.com/logstash-plugins/logstash-integration-rabbitmq/issues/ 17 | 18 | ## Documentation 19 | 20 | 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/). 21 | 22 | - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive 23 | - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide 24 | 25 | ## Need Help? 26 | 27 | Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum. 28 | 29 | ## Developing 30 | 31 | ### 1. Plugin Developement and Testing 32 | 33 | #### Code 34 | - To get started, you'll need JRuby with the Bundler gem installed. 35 | 36 | - 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). 37 | 38 | - Install dependencies 39 | ```sh 40 | bundle install 41 | ``` 42 | 43 | #### Test 44 | 45 | - Update your dependencies 46 | 47 | ```sh 48 | bundle install 49 | ``` 50 | 51 | - Run tests 52 | 53 | ```sh 54 | bundle exec rspec 55 | ``` 56 | 57 | ### 2. Running your unpublished Plugin in Logstash 58 | 59 | #### 2.1 Run in a local Logstash clone 60 | 61 | - Edit Logstash `Gemfile` and add the local plugin path, for example: 62 | ```ruby 63 | gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome" 64 | ``` 65 | - Install plugin 66 | ```sh 67 | # Logstash 2.3 and higher 68 | bin/logstash-plugin install --no-verify 69 | 70 | # Prior to Logstash 2.3 71 | bin/plugin install --no-verify 72 | 73 | ``` 74 | - Run Logstash with your plugin 75 | ```sh 76 | bin/logstash -e 'filter {awesome {}}' 77 | ``` 78 | At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash. 79 | 80 | #### 2.2 Run in an installed Logstash 81 | 82 | 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: 83 | 84 | - Build your plugin gem 85 | ```sh 86 | gem build logstash-filter-awesome.gemspec 87 | ``` 88 | - Install the plugin from the Logstash home 89 | ```sh 90 | # Logstash 2.3 and higher 91 | bin/logstash-plugin install --no-verify 92 | 93 | # Prior to Logstash 2.3 94 | bin/plugin install --no-verify 95 | 96 | ``` 97 | - Start Logstash and proceed to test the plugin 98 | 99 | ## Contributing 100 | 101 | All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin. 102 | 103 | 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. 104 | 105 | It is more important to the community that you are able to contribute. 106 | 107 | For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file. 108 | -------------------------------------------------------------------------------- /spec/inputs/rabbitmq_spec.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require "logstash/devutils/rspec/spec_helper" 3 | require "logstash/devutils/rspec/shared_examples" 4 | require "logstash/inputs/rabbitmq" 5 | require "thread" 6 | require 'logstash/event' 7 | 8 | Thread.abort_on_exception = true 9 | 10 | describe LogStash::Inputs::RabbitMQ do 11 | let(:klass) { LogStash::Inputs::RabbitMQ } 12 | let(:host) { "localhost" } 13 | let(:port) { 5672 } 14 | let(:exchange_type) { "topic" } 15 | let(:exchange) { "myexchange" } 16 | let(:queue) { "myqueue" } 17 | let(:rabbitmq_settings) { 18 | { 19 | "host" => host, 20 | "port" => port, 21 | "queue" => queue, 22 | "prefetch_count" => 123 23 | } 24 | } 25 | let(:instance) { klass.new(rabbitmq_settings) } 26 | let(:hare_info) { instance.instance_variable_get(:@hare_info) } 27 | 28 | context "when connected" do 29 | let(:connection) { double("MarchHare Connection") } 30 | let(:channel) { double("Channel") } 31 | let(:exchange) { double("Exchange") } 32 | let(:channel) { double("Channel") } 33 | let(:queue) { double("queue") } 34 | 35 | # Doing this in a before block doesn't give us enough control over scope 36 | before do 37 | allow(instance).to receive(:connect!).and_call_original 38 | allow(::MarchHare).to receive(:connect).and_return(connection) 39 | allow(connection).to receive(:create_channel).and_return(channel) 40 | allow(connection).to receive(:on_shutdown) 41 | allow(connection).to receive(:on_blocked) 42 | allow(connection).to receive(:on_unblocked) 43 | allow(channel).to receive(:exchange).and_return(exchange) 44 | allow(channel).to receive(:queue).and_return(queue) 45 | allow(channel).to receive(:prefetch=) 46 | 47 | allow(queue).to receive(:build_consumer).with(:on_cancellation => anything) 48 | allow(queue).to receive(:subscribe_with).with(any_args) 49 | allow(queue).to receive(:bind).with(any_args) 50 | end 51 | 52 | it "should default the codec to JSON" do 53 | expect(instance.codec).to be_a(LogStash::Codecs::JSON) 54 | end 55 | 56 | describe "#connect!" do 57 | subject { hare_info } 58 | 59 | context "without an exchange declared" do 60 | before do 61 | instance.register 62 | instance.setup! 63 | end 64 | 65 | it "should set the queue correctly" do 66 | expect(subject.queue).to eql(queue) 67 | end 68 | 69 | it "should set the prefetch value correctly" do 70 | expect(channel).to have_received(:prefetch=).with(123) 71 | end 72 | end 73 | 74 | context "with an exchange declared" do 75 | let(:exchange) { "exchange" } 76 | let(:key) { "routing key" } 77 | let(:rabbitmq_settings) { super().merge("exchange" => exchange, "key" => key, "exchange_type" => "fanout") } 78 | 79 | before do 80 | allow(instance).to receive(:declare_exchange!) 81 | end 82 | 83 | context "on run" do 84 | before do 85 | instance.register 86 | instance.setup! 87 | end 88 | 89 | it "should bind to the exchange" do 90 | expect(queue).to have_received(:bind).with(exchange, :routing_key => key) 91 | end 92 | 93 | it "should declare the exchange" do 94 | expect(instance).to have_received(:declare_exchange!) 95 | end 96 | end 97 | 98 | context "but not immediately available" do 99 | before do 100 | i = 0 101 | allow(queue).to receive(:bind).with(any_args) do 102 | i += 1 103 | raise "foo" if i == 1 104 | end 105 | end 106 | 107 | it "should reconnect" do 108 | instance.register 109 | instance.setup! 110 | expect(queue).to have_received(:bind).with(any_args).twice() 111 | end 112 | end 113 | 114 | context "initially unable to subscribe" do 115 | before do 116 | i = 0 117 | allow(queue).to receive(:subscribe_with).with(any_args) do 118 | i += 1 119 | raise "sub error" if i == 1 120 | end 121 | 122 | it "should retry the subscribe" do 123 | expect(queue).to have_receive(:subscribe_with).twice() 124 | end 125 | end 126 | end 127 | end 128 | end 129 | end 130 | end 131 | 132 | describe "with a live server", :integration => true do 133 | let(:klass) { LogStash::Inputs::RabbitMQ } 134 | let(:config) { {"host" => "127.0.0.1", "auto_delete" => true, "codec" => "plain", "add_field" => {"[@metadata][foo]" => "bar"} } } 135 | let(:instance) { klass.new(config) } 136 | let(:hare_info) { instance.instance_variable_get(:@hare_info) } 137 | let(:output_queue) { Queue.new } 138 | 139 | # Spawn a connection in the bg and wait up (n) seconds 140 | def spawn_and_wait(instance) 141 | instance.register 142 | 143 | output_queue # materialize in this thread 144 | 145 | Thread.new { 146 | instance.run(output_queue) 147 | } 148 | 149 | 20.times do 150 | instance.connected? ? break : sleep(0.1) 151 | end 152 | 153 | # Extra time to make sure the consumer can attach 154 | # Without this there's a chance the shutdown code will execute 155 | # before consumption begins. This is tricky to do more elegantly 156 | sleep 4 157 | end 158 | 159 | let(:test_connection) { MarchHare.connect(instance.send(:rabbitmq_settings)) } 160 | let(:test_channel) { test_connection.create_channel } 161 | 162 | before do 163 | # Materialize the instance in the current thread to prevent dupes 164 | # If you use multiple threads with lazy evaluation weird stuff happens 165 | instance 166 | spawn_and_wait(instance) 167 | 168 | test_channel # Start up the test client as well 169 | end 170 | 171 | after do 172 | instance.stop() 173 | test_channel.close 174 | test_connection.close 175 | end 176 | 177 | context "using defaults" do 178 | it "should start, connect, and stop cleanly" do 179 | expect(instance.connected?).to be_truthy 180 | end 181 | end 182 | 183 | it "should have the correct prefetch value" do 184 | expect(instance.instance_variable_get(:@hare_info).channel.prefetch).to eql(256) 185 | end 186 | 187 | describe "receiving a message with a queue + exchange specified" do 188 | let(:config) { super().merge("queue" => queue_name, "exchange" => exchange_name, "exchange_type" => "fanout", "metadata_enabled" => true) } 189 | let(:event) { output_queue.pop } 190 | let(:exchange) { test_channel.exchange(exchange_name, :type => "fanout") } 191 | let(:exchange_name) { "logstash-input-rabbitmq-#{rand(0xFFFFFFFF)}" } 192 | #let(:queue) { test_channel.queue(queue_name, :auto_delete => true) } 193 | let(:queue_name) { "logstash-input-rabbitmq-#{rand(0xFFFFFFFF)}" } 194 | 195 | after do 196 | exchange.delete 197 | end 198 | 199 | context "when the message has a payload but no message headers" do 200 | before do 201 | exchange.publish(message) 202 | end 203 | 204 | let(:message) { "Foo Message" } 205 | 206 | it "should process the message and store the payload" do 207 | expect(event.get("message")).to eql(message) 208 | end 209 | 210 | it "should save an empty message header hash" do 211 | expect(event).to include("@metadata") 212 | expect(event.get("@metadata")).to include("rabbitmq_headers") 213 | expect(event.get("[@metadata][rabbitmq_headers]")).to eq({}) 214 | end 215 | end 216 | 217 | context "when message properties are available" do 218 | before do 219 | # Don't test every single property but select a few with 220 | # different characteristics to get sufficient coverage. 221 | exchange.publish("", 222 | :properties => { 223 | :app_id => app_id, 224 | :timestamp => Java::JavaUtil::Date.new(epoch * 1000), 225 | :priority => priority, 226 | }) 227 | end 228 | 229 | let(:app_id) { "myapplication" } 230 | # Randomize the epoch we test with but limit its range to signed 231 | # ints to not assume all protocols and libraries involved use 232 | # unsigned ints for epoch values. 233 | let(:epoch) { rand(0x7FFFFFFF) } 234 | let(:priority) { 5 } 235 | 236 | it "should save message properties into a @metadata field" do 237 | expect(event).to include("@metadata") 238 | expect(event.get("@metadata")).to include("rabbitmq_properties") 239 | 240 | props = event.get("[@metadata][rabbitmq_properties") 241 | expect(props["app-id"]).to eq(app_id) 242 | expect(props["delivery-mode"]).to eq(1) 243 | expect(props["exchange"]).to eq(exchange_name) 244 | expect(props["priority"]).to eq(priority) 245 | expect(props["routing-key"]).to eq("") 246 | expect(props["timestamp"]).to eq(epoch) 247 | end 248 | end 249 | 250 | context "when message headers are available" do 251 | before do 252 | exchange.publish("", :properties => { :headers => headers }) 253 | end 254 | 255 | let (:headers) { 256 | { 257 | "arrayvalue" => [true, 123, "foo"], 258 | "boolvalue" => true, 259 | "intvalue" => 123, 260 | "stringvalue" => "foo", 261 | } 262 | } 263 | 264 | it "should save message headers into a @metadata field" do 265 | expect(event).to include("@metadata") 266 | expect(event.get("@metadata")).to include("rabbitmq_headers") 267 | expect(event.get("[@metadata][rabbitmq_headers]")).to include(headers) 268 | end 269 | 270 | it "should properly decorate the event" do 271 | expect(event.get("[@metadata][foo]")).to eq("bar") 272 | end 273 | end 274 | end 275 | 276 | describe LogStash::Inputs::RabbitMQ do 277 | it_behaves_like "an interruptible input plugin" 278 | end 279 | end 280 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/logstash/inputs/rabbitmq.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require 'logstash/plugin_mixins/rabbitmq_connection' 3 | require 'logstash/inputs/threadable' 4 | require 'logstash/event' 5 | java_import java.util.concurrent.ArrayBlockingQueue 6 | java_import java.util.concurrent.TimeUnit 7 | 8 | module LogStash 9 | module Inputs 10 | # Pull events from a http://www.rabbitmq.com/[RabbitMQ] queue. 11 | # 12 | # The default settings will create an entirely transient queue and listen for all messages by default. 13 | # If you need durability or any other advanced settings, please set the appropriate options 14 | # 15 | # This plugin uses the http://rubymarchhare.info/[March Hare] library 16 | # for interacting with the RabbitMQ server. Most configuration options 17 | # map directly to standard RabbitMQ and AMQP concepts. The 18 | # https://www.rabbitmq.com/amqp-0-9-1-reference.html[AMQP 0-9-1 reference guide] 19 | # and other parts of the RabbitMQ documentation are useful for deeper 20 | # understanding. 21 | # 22 | # The properties of messages received will be stored in the 23 | # `[@metadata][rabbitmq_properties]` field if the `@metadata_enabled` setting is checked. 24 | # Note that storing metadata may degrade performance. 25 | # The following properties may be available (in most cases dependent on whether 26 | # they were set by the sender): 27 | # 28 | # * app-id 29 | # * cluster-id 30 | # * consumer-tag 31 | # * content-encoding 32 | # * content-type 33 | # * correlation-id 34 | # * delivery-mode 35 | # * exchange 36 | # * expiration 37 | # * message-id 38 | # * priority 39 | # * redeliver 40 | # * reply-to 41 | # * routing-key 42 | # * timestamp 43 | # * type 44 | # * user-id 45 | # 46 | # For example, to get the RabbitMQ message's timestamp property 47 | # into the Logstash event's `@timestamp` field, use the date 48 | # filter to parse the `[@metadata][rabbitmq_properties][timestamp]` 49 | # field: 50 | # [source,ruby] 51 | # filter { 52 | # if [@metadata][rabbitmq_properties][timestamp] { 53 | # date { 54 | # match => ["[@metadata][rabbitmq_properties][timestamp]", "UNIX"] 55 | # } 56 | # } 57 | # } 58 | # 59 | # Additionally, any message headers will be saved in the 60 | # `[@metadata][rabbitmq_headers]` field. 61 | class RabbitMQ < LogStash::Inputs::Threadable 62 | include ::LogStash::PluginMixins::RabbitMQConnection 63 | 64 | # The properties to extract from each message and store in a 65 | # @metadata field. 66 | # 67 | # Technically the exchange, redeliver, and routing-key 68 | # properties belong to the envelope and not the message but we 69 | # ignore that distinction here. However, we extract the 70 | # headers separately via get_headers even though the header 71 | # table technically is a message property. 72 | # 73 | # Freezing all strings so that code modifying the event's 74 | # @metadata field can't touch them. 75 | # 76 | # If updating this list, remember to update the documentation 77 | # above too. 78 | MESSAGE_PROPERTIES = [ 79 | "app-id", 80 | "cluster-id", 81 | "consumer-tag", 82 | "content-encoding", 83 | "content-type", 84 | "correlation-id", 85 | "delivery-mode", 86 | "exchange", 87 | "expiration", 88 | "message-id", 89 | "priority", 90 | "redeliver", 91 | "reply-to", 92 | "routing-key", 93 | "timestamp", 94 | "type", 95 | "user-id", 96 | ].map { |s| s.freeze }.freeze 97 | 98 | INTERNAL_QUEUE_POISON=[] 99 | 100 | config_name "rabbitmq" 101 | 102 | # The default codec for this plugin is JSON. You can override this to suit your particular needs however. 103 | default :codec, "json" 104 | 105 | # The name of the queue Logstash will consume events from. If 106 | # left empty, a transient queue with an randomly chosen name 107 | # will be created. 108 | config :queue, :validate => :string, :default => "" 109 | 110 | # Is this queue durable? (aka; Should it survive a broker restart?) 111 | config :durable, :validate => :boolean, :default => false 112 | 113 | # Should the queue be deleted on the broker when the last consumer 114 | # disconnects? Set this option to `false` if you want the queue to remain 115 | # on the broker, queueing up messages until a consumer comes along to 116 | # consume them. 117 | config :auto_delete, :validate => :boolean, :default => false 118 | 119 | # Is the queue exclusive? Exclusive queues can only be used by the connection 120 | # that declared them and will be deleted when it is closed (e.g. due to a Logstash 121 | # restart). 122 | config :exclusive, :validate => :boolean, :default => false 123 | 124 | config :arguments, :validate => :array, :default => {} 125 | 126 | # Prefetch count. If acknowledgements are enabled with the `ack` 127 | # option, specifies the number of outstanding unacknowledged 128 | # messages allowed. 129 | config :prefetch_count, :validate => :number, :default => 256 130 | 131 | # Enable message acknowledgements. With acknowledgements 132 | # messages fetched by Logstash but not yet sent into the 133 | # Logstash pipeline will be requeued by the server if Logstash 134 | # shuts down. Acknowledgements will however hurt the message 135 | # throughput. 136 | # 137 | # This will only send an ack back every `prefetch_count` messages. 138 | # Working in batches provides a performance boost here. 139 | config :ack, :validate => :boolean, :default => true 140 | 141 | # If true the queue will be passively declared, meaning it must 142 | # already exist on the server. To have Logstash create the queue 143 | # if necessary leave this option as false. If actively declaring 144 | # a queue that already exists, the queue options for this plugin 145 | # (durable etc) must match those of the existing queue. 146 | config :passive, :validate => :boolean, :default => false 147 | 148 | # The name of the exchange to bind the queue to. Specify `exchange_type` 149 | # as well to declare the exchange if it does not exist 150 | config :exchange, :validate => :string 151 | 152 | # The type of the exchange to bind to. Specifying this will cause this plugin 153 | # to declare the exchange if it does not exist. 154 | config :exchange_type, :validate => :string 155 | 156 | # The routing key to use when binding a queue to the exchange. 157 | # This is only relevant for direct or topic exchanges. 158 | # 159 | # * Routing keys are ignored on fanout exchanges. 160 | # * Wildcards are not valid on direct exchanges. 161 | config :key, :validate => :string, :default => "logstash" 162 | 163 | # Amount of time in seconds to wait after a failed subscription request 164 | # before retrying. Subscribes can fail if the server goes away and then comes back. 165 | config :subscription_retry_interval_seconds, :validate => :number, :required => true, :default => 5 166 | 167 | # Enable the storage of message headers and properties in `@metadata`. This may impact performance 168 | config :metadata_enabled, :validate => :boolean, :default => false 169 | 170 | def register 171 | @internal_queue = java.util.concurrent.ArrayBlockingQueue.new(@prefetch_count*2) 172 | end 173 | 174 | def run(output_queue) 175 | setup! 176 | @output_queue = output_queue 177 | consume! 178 | end 179 | 180 | def setup! 181 | connect! 182 | declare_queue! 183 | bind_exchange! 184 | @hare_info.channel.prefetch = @prefetch_count 185 | rescue => e 186 | @logger.warn("Error while setting up connection for rabbitmq input! Will retry.", 187 | :message => e.message, 188 | :class => e.class.name, 189 | :location => e.backtrace.first) 190 | sleep_for_retry 191 | retry 192 | end 193 | 194 | def bind_exchange! 195 | if @exchange 196 | if @exchange_type # Only declare the exchange if @exchange_type is set! 197 | @logger.info? && @logger.info("Declaring exchange '#{@exchange}' with type #{@exchange_type}") 198 | @hare_info.exchange = declare_exchange!(@hare_info.channel, @exchange, @exchange_type, @durable) 199 | end 200 | @hare_info.queue.bind(@exchange, :routing_key => @key) 201 | end 202 | end 203 | 204 | def declare_queue! 205 | @hare_info.queue = declare_queue() 206 | end 207 | 208 | def declare_queue 209 | @hare_info.channel.queue(@queue, 210 | :durable => @durable, 211 | :auto_delete => @auto_delete, 212 | :exclusive => @exclusive, 213 | :passive => @passive, 214 | :arguments => @arguments) 215 | end 216 | 217 | def consume! 218 | @consumer = @hare_info.queue.build_consumer(:on_cancellation => Proc.new { on_cancellation }) do |metadata, data| 219 | @internal_queue.put [metadata, data] 220 | end 221 | 222 | begin 223 | @hare_info.queue.subscribe_with(@consumer, :manual_ack => @ack) 224 | rescue MarchHare::Exception => e 225 | @logger.warn("Could not subscribe to queue! Will retry in #{@subscription_retry_interval_seconds} seconds", :queue => @queue) 226 | 227 | sleep @subscription_retry_interval_seconds 228 | retry 229 | end 230 | 231 | internal_queue_consume! 232 | end 233 | 234 | def internal_queue_consume! 235 | i=0 236 | last_delivery_tag=nil 237 | while true 238 | payload = @internal_queue.poll(10, TimeUnit::MILLISECONDS) 239 | if !payload # Nothing in the queue 240 | if last_delivery_tag # And we have unacked stuff 241 | @hare_info.channel.ack(last_delivery_tag, true) if @ack 242 | i=0 243 | last_delivery_tag = nil 244 | end 245 | next 246 | end 247 | 248 | break if payload == INTERNAL_QUEUE_POISON 249 | 250 | metadata, data = payload 251 | @codec.decode(data) do |event| 252 | decorate(event) 253 | if @metadata_enabled 254 | event.set("[@metadata][rabbitmq_headers]", get_headers(metadata)) 255 | event.set("[@metadata][rabbitmq_properties]", get_properties(metadata)) 256 | end 257 | @output_queue << event if event 258 | end 259 | 260 | i += 1 261 | 262 | if i >= @prefetch_count 263 | @hare_info.channel.ack(metadata.delivery_tag, true) if @ack 264 | i = 0 265 | last_delivery_tag = nil 266 | else 267 | last_delivery_tag = metadata.delivery_tag 268 | end 269 | end 270 | end 271 | 272 | def stop 273 | @internal_queue.put(INTERNAL_QUEUE_POISON) 274 | shutdown_consumer 275 | close_connection 276 | end 277 | 278 | def shutdown_consumer 279 | return unless @consumer 280 | @hare_info.channel.basic_cancel(@consumer.consumer_tag) 281 | until @consumer.terminated? 282 | @logger.info("Waiting for rabbitmq consumer to terminate before stopping!", :params => self.params) 283 | sleep 1 284 | end 285 | end 286 | 287 | def on_cancellation 288 | if !stop? # If this isn't already part of a regular stop 289 | @logger.info("Received basic.cancel from #{rabbitmq_settings[:host]}, shutting down.") 290 | stop 291 | end 292 | end 293 | 294 | private 295 | def get_headers(metadata) 296 | metadata.headers || {} 297 | end 298 | 299 | private 300 | def get_properties(metadata) 301 | MESSAGE_PROPERTIES.reduce({}) do |acc, name| 302 | # The method names obviously can't contain hyphens. 303 | value = metadata.send(name.gsub("-", "_")) 304 | if value 305 | # The AMQP 0.9.1 timestamp field only has second resolution 306 | # so storing milliseconds serves no purpose and might give 307 | # the incorrect impression of a higher resolution. 308 | acc[name] = name != "timestamp" ? value : value.getTime / 1000 309 | end 310 | acc 311 | end 312 | end 313 | end 314 | end 315 | end 316 | -------------------------------------------------------------------------------- /docs/index.asciidoc: -------------------------------------------------------------------------------- 1 | :plugin: rabbitmq 2 | :type: input 3 | :default_codec: json 4 | 5 | ///////////////////////////////////////////// 6 | // RabbitMQ Input Plugin Source Has Moved // 7 | // --------------------------------------- // 8 | // The RabbitMQ Input Plugin is now a part // 9 | // of the RabbitMQ Integration. // 10 | // // 11 | // This stand-alone plugin project remains // 12 | // open for backports to the 6.x series. // 13 | ///////////////////////////////////////////// 14 | 15 | /////////////////////////////////////////// 16 | START - GENERATED VARIABLES, DO NOT EDIT! 17 | /////////////////////////////////////////// 18 | :version: %VERSION% 19 | :release_date: %RELEASE_DATE% 20 | :changelog_url: %CHANGELOG_URL% 21 | :include_path: ../../../../logstash/docs/include 22 | /////////////////////////////////////////// 23 | END - GENERATED VARIABLES, DO NOT EDIT! 24 | /////////////////////////////////////////// 25 | 26 | [id="plugins-{type}s-{plugin}"] 27 | 28 | === Rabbitmq input plugin 29 | 30 | include::{include_path}/plugin_header.asciidoc[] 31 | 32 | ==== Description 33 | 34 | Pull events from a http://www.rabbitmq.com/[RabbitMQ] queue. 35 | 36 | The default settings will create an entirely transient queue and listen for all messages by default. 37 | If you need durability or any other advanced settings, please set the appropriate options 38 | 39 | This plugin uses the http://rubymarchhare.info/[March Hare] library 40 | for interacting with the RabbitMQ server. Most configuration options 41 | map directly to standard RabbitMQ and AMQP concepts. The 42 | https://www.rabbitmq.com/amqp-0-9-1-reference.html[AMQP 0-9-1 reference guide] 43 | and other parts of the RabbitMQ documentation are useful for deeper 44 | understanding. 45 | 46 | The properties of messages received will be stored in the 47 | `[@metadata][rabbitmq_properties]` field if the `@metadata_enabled` setting is checked. 48 | Note that storing metadata may degrade performance. 49 | The following properties may be available (in most cases dependent on whether 50 | they were set by the sender): 51 | 52 | * app-id 53 | * cluster-id 54 | * consumer-tag 55 | * content-encoding 56 | * content-type 57 | * correlation-id 58 | * delivery-mode 59 | * exchange 60 | * expiration 61 | * message-id 62 | * priority 63 | * redeliver 64 | * reply-to 65 | * routing-key 66 | * timestamp 67 | * type 68 | * user-id 69 | 70 | For example, to get the RabbitMQ message's timestamp property 71 | into the Logstash event's `@timestamp` field, use the date 72 | filter to parse the `[@metadata][rabbitmq_properties][timestamp]` 73 | field: 74 | [source,ruby] 75 | filter { 76 | if [@metadata][rabbitmq_properties][timestamp] { 77 | date { 78 | match => ["[@metadata][rabbitmq_properties][timestamp]", "UNIX"] 79 | } 80 | } 81 | } 82 | 83 | Additionally, any message headers will be saved in the 84 | `[@metadata][rabbitmq_headers]` field. 85 | 86 | [id="plugins-{type}s-{plugin}-options"] 87 | ==== Rabbitmq Input Configuration Options 88 | 89 | This plugin supports the following configuration options plus the <> described later. 90 | 91 | [cols="<,<,<",options="header",] 92 | |======================================================================= 93 | |Setting |Input type|Required 94 | | <> |<>|No 95 | | <> |<>|No 96 | | <> |<>|No 97 | | <> |<>|No 98 | | <> |<>|No 99 | | <> |<>|No 100 | | <> |<>|No 101 | | <> |<>|No 102 | | <> |<>|No 103 | | <> |<>|No 104 | | <> |<>|No 105 | | <> |<>|Yes 106 | | <> |<>|No 107 | | <> |<>|No 108 | | <> |<>|No 109 | | <> |<>|No 110 | | <> |<>|No 111 | | <> |<>|No 112 | | <> |<>|No 113 | | <> |<>|No 114 | | <> |<>|No 115 | | <> |a valid filesystem path|No 116 | | <> |<>|No 117 | | <> |<>|Yes 118 | | <> |<>|No 119 | | <> |<>|No 120 | | <> |<>|No 121 | |======================================================================= 122 | 123 | Also see <> for a list of options supported by all 124 | input plugins. 125 | 126 |   127 | 128 | [id="plugins-{type}s-{plugin}-ack"] 129 | ===== `ack` 130 | 131 | * Value type is <> 132 | * Default value is `true` 133 | 134 | Enable message acknowledgements. With acknowledgements 135 | messages fetched by Logstash but not yet sent into the 136 | Logstash pipeline will be requeued by the server if Logstash 137 | shuts down. Acknowledgements will however hurt the message 138 | throughput. 139 | 140 | This will only send an ack back every `prefetch_count` messages. 141 | Working in batches provides a performance boost here. 142 | 143 | [id="plugins-{type}s-{plugin}-arguments"] 144 | ===== `arguments` 145 | 146 | * Value type is <> 147 | * Default value is `{}` 148 | 149 | Optional queue arguments as an array. 150 | 151 | Relevant RabbitMQ doc guides: 152 | 153 | * https://www.rabbitmq.com/queues.html#optional-arguments[Optional queue arguments] 154 | * https://www.rabbitmq.com/parameters.html#policies[Policies] 155 | * https://www.rabbitmq.com/quorum-queues.html[Quorum Queues] 156 | 157 | [id="plugins-{type}s-{plugin}-auto_delete"] 158 | ===== `auto_delete` 159 | 160 | * Value type is <> 161 | * Default value is `false` 162 | 163 | Should the queue be deleted on the broker when the last consumer 164 | disconnects? Set this option to `false` if you want the queue to remain 165 | on the broker, queueing up messages until a consumer comes along to 166 | consume them. 167 | 168 | [id="plugins-{type}s-{plugin}-automatic_recovery"] 169 | ===== `automatic_recovery` 170 | 171 | * Value type is <> 172 | * Default value is `true` 173 | 174 | Set this to https://www.rabbitmq.com/connections.html#automatic-recovery[automatically recover] from a broken connection. 175 | You almost certainly don't want to override this! 176 | 177 | [id="plugins-{type}s-{plugin}-connect_retry_interval"] 178 | ===== `connect_retry_interval` 179 | 180 | * Value type is <> 181 | * Default value is `1` 182 | 183 | Time in seconds to wait before retrying a connection 184 | 185 | [id="plugins-{type}s-{plugin}-connection_timeout"] 186 | ===== `connection_timeout` 187 | 188 | * Value type is <> 189 | * There is no default value for this setting. 190 | 191 | The default connection timeout in milliseconds. If not specified the timeout is infinite. 192 | 193 | [id="plugins-{type}s-{plugin}-durable"] 194 | ===== `durable` 195 | 196 | * Value type is <> 197 | * Default value is `false` 198 | 199 | Is this queue durable? (aka; Should it survive a broker restart?) 200 | 201 | [id="plugins-{type}s-{plugin}-exchange"] 202 | ===== `exchange` 203 | 204 | * Value type is <> 205 | * There is no default value for this setting. 206 | 207 | The name of the exchange to bind the queue to. Specify `exchange_type` 208 | as well to declare the exchange if it does not exist 209 | 210 | [id="plugins-{type}s-{plugin}-exchange_type"] 211 | ===== `exchange_type` 212 | 213 | * Value type is <> 214 | * There is no default value for this setting. 215 | 216 | The type of the exchange to bind to. Specifying this will cause this plugin 217 | to declare the exchange if it does not exist. 218 | 219 | [id="plugins-{type}s-{plugin}-exclusive"] 220 | ===== `exclusive` 221 | 222 | * Value type is <> 223 | * Default value is `false` 224 | 225 | Is the queue exclusive? Exclusive queues can only be used by the connection 226 | that declared them and will be deleted when it is closed (e.g. due to a Logstash 227 | restart). 228 | 229 | [id="plugins-{type}s-{plugin}-heartbeat"] 230 | ===== `heartbeat` 231 | 232 | * Value type is <> 233 | * There is no default value for this setting. 234 | 235 | https://www.rabbitmq.com/heartbeats.html[Heartbeat timeout] in seconds. 236 | If unspecified then heartbeat timeout of 60 seconds will be used. 237 | 238 | [id="plugins-{type}s-{plugin}-host"] 239 | ===== `host` 240 | 241 | * This is a required setting. 242 | * Value type is <> 243 | * There is no default value for this setting. 244 | 245 | Common functionality for the rabbitmq input/output 246 | RabbitMQ server address(es) 247 | host can either be a single host, or a list of hosts 248 | i.e. 249 | host => "localhost" 250 | or 251 | host => ["host01", "host02] 252 | 253 | if multiple hosts are provided on the initial connection and any subsequent 254 | recovery attempts of the hosts is chosen at random and connected to. 255 | Note that only one host connection is active at a time. 256 | 257 | [id="plugins-{type}s-{plugin}-key"] 258 | ===== `key` 259 | 260 | * Value type is <> 261 | * Default value is `"logstash"` 262 | 263 | The routing key to use when binding a queue to the exchange. 264 | This is only relevant for direct or topic exchanges. 265 | 266 | * Routing keys are ignored on fanout exchanges. 267 | * Wildcards are not valid on direct exchanges. 268 | 269 | [id="plugins-{type}s-{plugin}-metadata_enabled"] 270 | ===== `metadata_enabled` 271 | 272 | * Value type is <> 273 | * Default value is `false` 274 | 275 | Enable the storage of message headers and properties in `@metadata`. This may impact performance 276 | 277 | [id="plugins-{type}s-{plugin}-passive"] 278 | ===== `passive` 279 | 280 | * Value type is <> 281 | * Default value is `false` 282 | 283 | If true the queue will be passively declared, meaning it must 284 | already exist on the server. To have Logstash create the queue 285 | if necessary leave this option as false. If actively declaring 286 | a queue that already exists, the queue options for this plugin 287 | (durable etc) must match those of the existing queue. 288 | 289 | [id="plugins-{type}s-{plugin}-password"] 290 | ===== `password` 291 | 292 | * Value type is <> 293 | * Default value is `"guest"` 294 | 295 | RabbitMQ password 296 | 297 | [id="plugins-{type}s-{plugin}-port"] 298 | ===== `port` 299 | 300 | * Value type is <> 301 | * Default value is `5672` 302 | 303 | RabbitMQ port to connect on 304 | 305 | [id="plugins-{type}s-{plugin}-prefetch_count"] 306 | ===== `prefetch_count` 307 | 308 | * Value type is <> 309 | * Default value is `256` 310 | 311 | Prefetch count. If acknowledgements are enabled with the `ack` 312 | option, specifies the number of outstanding unacknowledged 313 | messages allowed. 314 | 315 | [id="plugins-{type}s-{plugin}-queue"] 316 | ===== `queue` 317 | 318 | * Value type is <> 319 | * Default value is `""` 320 | 321 | The properties to extract from each message and store in a 322 | @metadata field. 323 | 324 | Technically the exchange, redeliver, and routing-key 325 | properties belong to the envelope and not the message but we 326 | ignore that distinction here. However, we extract the 327 | headers separately via get_headers even though the header 328 | table technically is a message property. 329 | 330 | Freezing all strings so that code modifying the event's 331 | @metadata field can't touch them. 332 | 333 | If updating this list, remember to update the documentation 334 | above too. 335 | The default codec for this plugin is JSON. You can override this to suit your particular needs however. 336 | The name of the queue Logstash will consume events from. If 337 | left empty, a transient queue with an randomly chosen name 338 | will be created. 339 | 340 | [id="plugins-{type}s-{plugin}-ssl"] 341 | ===== `ssl` 342 | 343 | * Value type is <> 344 | * There is no default value for this setting. 345 | 346 | Enable or disable SSL. 347 | Note that by default remote certificate verification is off. 348 | Specify ssl_certificate_path and ssl_certificate_password if you need 349 | certificate verification 350 | 351 | [id="plugins-{type}s-{plugin}-ssl_certificate_password"] 352 | ===== `ssl_certificate_password` 353 | 354 | * Value type is <> 355 | * There is no default value for this setting. 356 | 357 | Password for the encrypted PKCS12 (.p12) certificate file specified in ssl_certificate_path 358 | 359 | [id="plugins-{type}s-{plugin}-ssl_certificate_path"] 360 | ===== `ssl_certificate_path` 361 | 362 | * Value type is <> 363 | * There is no default value for this setting. 364 | 365 | Path to an SSL certificate in PKCS12 (.p12) format used for verifying the remote host 366 | 367 | [id="plugins-{type}s-{plugin}-ssl_version"] 368 | ===== `ssl_version` 369 | 370 | * Value type is <> 371 | * Default value is `"TLSv1.2"` 372 | 373 | Version of the SSL protocol to use. 374 | 375 | [id="plugins-{type}s-{plugin}-subscription_retry_interval_seconds"] 376 | ===== `subscription_retry_interval_seconds` 377 | 378 | * This is a required setting. 379 | * Value type is <> 380 | * Default value is `5` 381 | 382 | Amount of time in seconds to wait after a failed subscription request 383 | before retrying. Subscribes can fail if the server goes away and then comes back. 384 | 385 | [id="plugins-{type}s-{plugin}-threads"] 386 | ===== `threads` 387 | 388 | * Value type is <> 389 | * Default value is `1` 390 | 391 | [id="plugins-{type}s-{plugin}-user"] 392 | ===== `user` 393 | 394 | * Value type is <> 395 | * Default value is `"guest"` 396 | 397 | RabbitMQ username 398 | 399 | [id="plugins-{type}s-{plugin}-vhost"] 400 | ===== `vhost` 401 | 402 | * Value type is <> 403 | * Default value is `"/"` 404 | 405 | The vhost (virtual host) to use. If you don't know what this 406 | is, leave the default. With the exception of the default 407 | vhost ("/"), names of vhosts should not begin with a forward 408 | slash. 409 | 410 | 411 | 412 | [id="plugins-{type}s-{plugin}-common-options"] 413 | include::{include_path}/{type}.asciidoc[] 414 | 415 | :default_codec!: 416 | --------------------------------------------------------------------------------