Module: LogStash::Outputs::ElasticSearch::Common
- Included in:
- LogStash::Outputs::ElasticSearch
- Defined in:
- lib/logstash/outputs/elasticsearch/common.rb
Constant Summary collapse
- RETRYABLE_CODES =
[429, 503]
- SUCCESS_CODES =
[200, 201]
- VALID_HTTP_ACTIONS =
To be overidden by the -java version
["index", "delete", "create", "update"]
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
Instance Method Summary collapse
- #check_action_validity ⇒ Object
-
#event_action_params(event) ⇒ Object
get the action parameters for the given event.
-
#event_action_tuple(event) ⇒ Object
Convert the event into a 3-tuple of action, params, and event.
-
#get_event_type(event) ⇒ Object
Determine the correct value for the ‘type’ field for the given event.
- #install_template ⇒ Object
-
#multi_receive(events) ⇒ Object
Receive an array of events and immediately attempt to index them (no buffering).
- #next_sleep_interval(current_interval) ⇒ Object
- #register ⇒ Object
- #retrying_submit(actions) ⇒ Object
-
#safe_bulk(actions) ⇒ Object
Rescue retryable errors during bulk submission.
- #setup_hosts ⇒ Object
- #sleep_for_interval(sleep_interval) ⇒ Object
- #submit(actions) ⇒ Object
- #valid_actions ⇒ Object
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 5 def client @client end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
5 6 7 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 5 def hosts @hosts end |
Instance Method Details
#check_action_validity ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 50 def check_action_validity raise LogStash::ConfigurationError, "No action specified!" unless @action # If we're using string interpolation, we're good! return if @action =~ /%{.+}/ return if valid_actions.include?(@action) raise LogStash::ConfigurationError, "Action '#{@action}' is invalid! Pick one of #{valid_actions} or use a sprintf style statement" end |
#event_action_params(event) ⇒ Object
get the action parameters for the given event
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 138 def event_action_params(event) type = get_event_type(event) params = { :_id => @document_id ? event.sprintf(@document_id) : nil, :_index => event.sprintf(@index), :_type => type, :_routing => @routing ? event.sprintf(@routing) : nil } if @pipeline params[:pipeline] = event.sprintf(@pipeline) end if @parent params[:parent] = event.sprintf(@parent) end if @action == 'update' params[:_upsert] = LogStash::Json.load(event.sprintf(@upsert)) if @upsert != "" params[:_script] = event.sprintf(@script) if @script != "" params[:_retry_on_conflict] = @retry_on_conflict end params end |
#event_action_tuple(event) ⇒ Object
Convert the event into a 3-tuple of action, params, and event
32 33 34 35 36 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 32 def event_action_tuple(event) params = event_action_params(event) action = event.sprintf(@action) [action, params, event] end |
#get_event_type(event) ⇒ Object
Determine the correct value for the ‘type’ field for the given event
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 166 def get_event_type(event) # Set the 'type' value for the index. type = if @document_type event.sprintf(@document_type) else event.get("type") || "logs" end if !(type.is_a?(String) || type.is_a?(Numeric)) @logger.warn("Bad event type! Non-string/integer type value set!", :type_class => type.class, :type_value => type.to_s, :event => event) end type.to_s end |
#install_template ⇒ Object
46 47 48 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 46 def install_template TemplateManager.install_template(self) end |
#multi_receive(events) ⇒ Object
Receive an array of events and immediately attempt to index them (no buffering)
21 22 23 24 25 26 27 28 29 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 21 def multi_receive(events) if @flush_size events.each_slice(@flush_size) do |slice| (slice.map {|e| event_action_tuple(e) }) end else (events.map {|e| event_action_tuple(e)}) end end |
#next_sleep_interval(current_interval) ⇒ Object
103 104 105 106 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 103 def next_sleep_interval(current_interval) doubled = current_interval * 2 doubled > @retry_max_interval ? @retry_max_interval : doubled end |
#register ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 10 def register @stopping = Concurrent::AtomicBoolean.new(false) setup_hosts # properly sets @hosts build_client install_template check_action_validity @logger.info("New Elasticsearch output", :class => self.class.name, :hosts => @hosts) end |
#retrying_submit(actions) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 66 def (actions) # Initially we submit the full list of actions submit_actions = actions sleep_interval = @retry_initial_interval while submit_actions && submit_actions.length > 0 # We retry with whatever is didn't succeed begin submit_actions = submit(submit_actions) if submit_actions && submit_actions.size > 0 @logger.error("Retrying individual actions") submit_actions.each {|action| @logger.error("Action", action) } end rescue => e @logger.error("Encountered an unexpected error submitting a bulk request! Will retry.", :error_message => e., :class => e.class.name, :backtrace => e.backtrace) end # Everything was a success! break if !submit_actions || submit_actions.empty? # If we're retrying the action sleep for the recommended interval # Double the interval for the next time through to achieve exponential backoff Stud.stoppable_sleep(sleep_interval) { @stopping.true? } sleep_interval = next_sleep_interval(sleep_interval) end end |
#safe_bulk(actions) ⇒ Object
Rescue retryable errors during bulk submission
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 182 def safe_bulk(actions) sleep_interval = @retry_initial_interval begin es_actions = actions.map {|action_type, params, event| [action_type, params, event.to_hash]} response = @client.bulk(es_actions) response rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError => e # If we can't even connect to the server let's just print out the URL (:hosts is actually a URL) # and let the user sort it out from there @logger.error( "Attempted to send a bulk request to elasticsearch'"+ " but Elasticsearch appears to be unreachable or down!", :error_message => e., :class => e.class.name, :will_retry_in_seconds => sleep_interval ) @logger.debug("Failed actions for last bad bulk request!", :actions => actions) # We retry until there are no errors! Errors should all go to the retry queue sleep_interval = sleep_for_interval(sleep_interval) retry unless @stopping.true? rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::NoConnectionAvailableError => e @logger.error( "Attempted to send a bulk request to elasticsearch, but no there are no living connections in the connection pool. Perhaps Elasticsearch is unreachable or down?", :error_message => e., :class => e.class.name, :will_retry_in_seconds => sleep_interval ) Stud.stoppable_sleep(sleep_interval) { @stopping.true? } sleep_interval = next_sleep_interval(sleep_interval) retry unless @stopping.true? rescue ::LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError => e if RETRYABLE_CODES.include?(e.response_code) safe_url = ::LogStash::Outputs::ElasticSearch::SafeURL.without_credentials(e.url) log_hash = {:code => e.response_code, :url => safe_url} log_hash[:body] = e.body if @logger.debug? # Generally this is too verbose @logger.error("Attempted to send a bulk request to elasticsearch but received a bad HTTP response code!", log_hash) sleep_interval = sleep_for_interval(sleep_interval) retry unless @stopping.true? else @logger.error("Got a bad response code from server, but this code is not considered retryable. Request will be dropped", :code => e.response_code, :body => e.response.body) end rescue => e # Stuff that should never happen # For all other errors print out full connection issues @logger.error( "An unknown error occurred sending a bulk request to Elasticsearch. We will retry indefinitely", :error_message => e., :error_class => e.class.name, :backtrace => e.backtrace ) @logger.debug("Failed actions for last bad bulk request!", :actions => actions) # We retry until there are no errors! Errors should all go to the retry queue sleep_interval = sleep_for_interval(sleep_interval) retry unless @stopping.true? end end |
#setup_hosts ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 38 def setup_hosts @hosts = Array(@hosts) if @hosts.empty? @logger.info("No 'host' set in elasticsearch output. Defaulting to localhost") @hosts.replace(["localhost"]) end end |
#sleep_for_interval(sleep_interval) ⇒ Object
98 99 100 101 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 98 def sleep_for_interval(sleep_interval) Stud.stoppable_sleep(sleep_interval) { @stopping.true? } next_sleep_interval(sleep_interval) end |
#submit(actions) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 108 def submit(actions) bulk_response = safe_bulk(actions) # If the response is nil that means we were in a retry loop # and aborted since we're shutting down # If it did return and there are no errors we're good as well return if bulk_response.nil? || !bulk_response["errors"] actions_to_retry = [] bulk_response["items"].each_with_index do |response,idx| action_type, action_props = response.first status = action_props["status"] failure = action_props["error"] action = actions[idx] if SUCCESS_CODES.include?(status) next elsif RETRYABLE_CODES.include?(status) @logger.info "retrying failed action with response code: #{status} (#{failure})" actions_to_retry << action elsif !failure_type_logging_whitelist.include?(failure["type"]) @logger.warn "Failed action.", status: status, action: action, response: response end end actions_to_retry end |
#valid_actions ⇒ Object
62 63 64 |
# File 'lib/logstash/outputs/elasticsearch/common.rb', line 62 def valid_actions VALID_HTTP_ACTIONS end |