From 7c29668886b51f76bc64db949337afd8b7b2e48b Mon Sep 17 00:00:00 2001 From: Brennen Smith Date: Mon, 8 Jan 2018 08:50:22 -1000 Subject: [PATCH 1/4] Add support for Redis unix sockets --- lib/logstash/inputs/redis.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/logstash/inputs/redis.rb b/lib/logstash/inputs/redis.rb index 203f854..c7b0aa3 100644 --- a/lib/logstash/inputs/redis.rb +++ b/lib/logstash/inputs/redis.rb @@ -29,6 +29,10 @@ module LogStash module Inputs class Redis < LogStash::Inputs::Threadable # The port to connect on. config :port, :validate => :number, :default => 6379 + # The unix socket path to connect on. Will override host and port if defined. + # There is no unix socket path by default. + config :path, :validate => :string + # The Redis database number. config :db, :validate => :number, :default => 0 @@ -68,7 +72,7 @@ def new_redis_instance end def register - @redis_url = "redis://#{@password}@#{@host}:#{@port}/#{@db}" + @redis_url = @path.nil? ? "redis://#{@password}@#{@host}:#{@port}/#{@db}" : "#{@password}@#{@path}/#{@db}" @redis_builder ||= method(:internal_redis_builder) @@ -114,13 +118,24 @@ def is_list_type? # private def redis_params - { - :host => @host, - :port => @port, + if @path.nil? + connectionParams = { + :host => @host, + :port => @port + } + else + connectionParams = { + :path => @path + } + end + + baseParams = { :timeout => @timeout, :db => @db, :password => @password.nil? ? nil : @password.value } + + return connectionParams.merge(baseParams) end # private From ab535a7d66f4edb03fbd0b0f1f6e34b21a1cd89d Mon Sep 17 00:00:00 2001 From: Brennen Smith Date: Tue, 9 Jan 2018 08:32:57 -1000 Subject: [PATCH 2/4] Make `path` and `host` mutually exclusive --- lib/logstash/inputs/redis.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/logstash/inputs/redis.rb b/lib/logstash/inputs/redis.rb index c7b0aa3..5306e7e 100644 --- a/lib/logstash/inputs/redis.rb +++ b/lib/logstash/inputs/redis.rb @@ -23,14 +23,17 @@ module LogStash module Inputs class Redis < LogStash::Inputs::Threadable default :codec, "json" - # The hostname of your Redis server. - config :host, :validate => :string, :default => "127.0.0.1" + # The hostname of your Redis server + # This defaults to 127.0.0.1 if not specified. + # This and :path are mutually exclusive. Will raise an ArgumentError if both are specified. + config :host, :validate => :string # The port to connect on. config :port, :validate => :number, :default => 6379 # The unix socket path to connect on. Will override host and port if defined. # There is no unix socket path by default. + # This and :host are mutually exclusive. Will raise an ArgumentError if both are specified. config :path, :validate => :string # The Redis database number. @@ -72,6 +75,14 @@ def new_redis_instance end def register + if !@host.nil? && !@path.nil? + raise ArgumentError.new( + "Host and Path cannot be specified simultaneously. These options are exclusive." + ) + elsif @host.nil? && @path.nil? + @host = "127.0.0.1" + end + @redis_url = @path.nil? ? "redis://#{@password}@#{@host}:#{@port}/#{@db}" : "#{@password}@#{@path}/#{@db}" @redis_builder ||= method(:internal_redis_builder) From d3b9b4e9dd2288888dd2fd28a3bc59b5d3b5a26c Mon Sep 17 00:00:00 2001 From: Brennen Smith Date: Thu, 11 Jan 2018 11:28:55 -1000 Subject: [PATCH 3/4] Add documentation updates --- docs/index.asciidoc | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/index.asciidoc b/docs/index.asciidoc index 9c95921..b6840c9 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -45,6 +45,7 @@ This plugin supports the following configuration options plus the <> |<>, one of `["list", "channel", "pattern_channel"]`|Yes | <> |<>|No | <> |<>|No +| <> |<>|No | <> |<>|Yes | <> |<>|No | <> |<>|No @@ -89,9 +90,19 @@ The Redis database number. * Value type is <> * Default value is `"127.0.0.1"` + * Path and Host are mutually exclusive - Logstash will throw an error if both are specified. The hostname of your Redis server. +id="plugins-{type}s-{plugin}-path"] +===== `path` + + * Value type is <> + * There is no default value for this setting. + * Path and Host are mutually exclusive - Logstash will throw an error if both are specified. + +The unix socket path of your Redis server. + [id="plugins-{type}s-{plugin}-key"] ===== `key` From e2742b03ca9720d37c69afb482ccfa3c83479c33 Mon Sep 17 00:00:00 2001 From: Brennen Smith Date: Wed, 17 Jan 2018 17:23:21 -0800 Subject: [PATCH 4/4] Revert logic to override rather than throw error if both path and host are specified. --- docs/index.asciidoc | 25 ++++++++++++------------- lib/logstash/inputs/redis.rb | 22 ++++++---------------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/docs/index.asciidoc b/docs/index.asciidoc index b6840c9..5fe1283 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -59,7 +59,7 @@ input plugins.   [id="plugins-{type}s-{plugin}-batch_count"] -===== `batch_count` +===== `batch_count` * Value type is <> * Default value is `125` @@ -67,7 +67,7 @@ input plugins. The number of events to return from Redis using EVAL. [id="plugins-{type}s-{plugin}-data_type"] -===== `data_type` +===== `data_type` * This is a required setting. * Value can be any of: `list`, `channel`, `pattern_channel` @@ -78,7 +78,7 @@ key. If `redis\_type` is `channel`, then we will SUBSCRIBE to the key. If `redis\_type` is `pattern_channel`, then we will PSUBSCRIBE to the key. [id="plugins-{type}s-{plugin}-db"] -===== `db` +===== `db` * Value type is <> * Default value is `0` @@ -86,25 +86,24 @@ If `redis\_type` is `pattern_channel`, then we will PSUBSCRIBE to the key. The Redis database number. [id="plugins-{type}s-{plugin}-host"] -===== `host` +===== `host` * Value type is <> * Default value is `"127.0.0.1"` - * Path and Host are mutually exclusive - Logstash will throw an error if both are specified. The hostname of your Redis server. id="plugins-{type}s-{plugin}-path"] -===== `path` +===== `path` * Value type is <> * There is no default value for this setting. - * Path and Host are mutually exclusive - Logstash will throw an error if both are specified. + * Path will override Host configuration if both specified. The unix socket path of your Redis server. [id="plugins-{type}s-{plugin}-key"] -===== `key` +===== `key` * This is a required setting. * Value type is <> @@ -113,7 +112,7 @@ The unix socket path of your Redis server. The name of a Redis list or channel. [id="plugins-{type}s-{plugin}-password"] -===== `password` +===== `password` * Value type is <> * There is no default value for this setting. @@ -121,7 +120,7 @@ The name of a Redis list or channel. Password to authenticate with. There is no authentication by default. [id="plugins-{type}s-{plugin}-port"] -===== `port` +===== `port` * Value type is <> * Default value is `6379` @@ -129,7 +128,7 @@ Password to authenticate with. There is no authentication by default. The port to connect on. [id="plugins-{type}s-{plugin}-threads"] -===== `threads` +===== `threads` * Value type is <> * Default value is `1` @@ -137,7 +136,7 @@ The port to connect on. [id="plugins-{type}s-{plugin}-timeout"] -===== `timeout` +===== `timeout` * Value type is <> * Default value is `5` @@ -147,4 +146,4 @@ Initial connection timeout in seconds. [id="plugins-{type}s-{plugin}-common-options"] -include::{include_path}/{type}.asciidoc[] \ No newline at end of file +include::{include_path}/{type}.asciidoc[] diff --git a/lib/logstash/inputs/redis.rb b/lib/logstash/inputs/redis.rb index 5306e7e..fe4f3af 100644 --- a/lib/logstash/inputs/redis.rb +++ b/lib/logstash/inputs/redis.rb @@ -23,17 +23,14 @@ module LogStash module Inputs class Redis < LogStash::Inputs::Threadable default :codec, "json" - # The hostname of your Redis server - # This defaults to 127.0.0.1 if not specified. - # This and :path are mutually exclusive. Will raise an ArgumentError if both are specified. - config :host, :validate => :string + # The hostname of your Redis server. + config :host, :validate => :string, :default => "127.0.0.1" # The port to connect on. config :port, :validate => :number, :default => 6379 - # The unix socket path to connect on. Will override host and port if defined. - # There is no unix socket path by default. - # This and :host are mutually exclusive. Will raise an ArgumentError if both are specified. + # The unix socket path to connect on. Will override host and port if defined. + # There is no unix socket path by default. config :path, :validate => :string # The Redis database number. @@ -75,14 +72,6 @@ def new_redis_instance end def register - if !@host.nil? && !@path.nil? - raise ArgumentError.new( - "Host and Path cannot be specified simultaneously. These options are exclusive." - ) - elsif @host.nil? && @path.nil? - @host = "127.0.0.1" - end - @redis_url = @path.nil? ? "redis://#{@password}@#{@host}:#{@port}/#{@db}" : "#{@password}@#{@path}/#{@db}" @redis_builder ||= method(:internal_redis_builder) @@ -129,12 +118,13 @@ def is_list_type? # private def redis_params - if @path.nil? + if @path.nil? connectionParams = { :host => @host, :port => @port } else + @logger.warn("Parameter 'path' is set, ignoring parameters: 'host' and 'port'") connectionParams = { :path => @path }