Class: SemanticLogger::Appender::SplunkHttp

Inherits:
Http show all
Defined in:
lib/semantic_logger/appender/splunk_http.rb

Constant Summary collapse

CONFIGURATION_CODES =

The status codes in the body of a 400 Bad Request from the HTTP Event Collector that reject the request for the collector's configuration or the appender's, whatever entries it holds:

7 Incorrect index
10 Data channel is missing
11 Invalid data channel
14 ACK is disabled
16 Query string authorization is not enabled
21 Invalid token
22 Token disabled

The others it answers with a 400 are caused by the entries, so a batch is split up to find them: 5 No data, 6 Invalid data format, 12 Event field is required, 13 Event field cannot be blank, and 15 Error in handling indexed fields.

See "Troubleshoot HTTP Event Collector", Possible error codes:

https://help.splunk.com/en/splunk-enterprise/get-started/get-data-in/10.4/get-data-with-http-event-collector/troubleshoot-http-event-collector
[7, 10, 11, 14, 16, 21, 22].freeze

Constants inherited from Http

Http::MAX_LOGGED_RESPONSE_BODY, Http::NOT_SENT_ERRORS, Http::URL_FORMAT_HINT

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Http

#compress, #continue_timeout, #header, #http, #open_timeout, #path, #port, #proxy_url, #query, #read_timeout, #server, #ssl_options, #url, #username

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Instance Method Summary collapse

Methods inherited from Http

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #close, #inspect_details, #log, #message_error?, #not_sent?, #redacted_url, #reopen, #wrote_nothing?

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #close, #console_output?, #console_stream, #default_formatter, #flush, #inspect, #inspect_details, #log, #message_error?, #not_sent?, #should_log?, #wrote_nothing?

Constructor Details

#initialize(token: nil, source_type: nil, index: nil, compress: true, **args) ⇒ SplunkHttp

Create Splunk appender over persistent HTTP(S)

Parameters:

token: [String]
Token created in Splunk for this HTTP Appender
Mandatory.

source_type: [String]
Optional: Source type to display in Splunk

index: [String]
Optional: Name of a valid index for this message in Splunk.

url: [String]
Valid URL to post to.
  Example: http://example.com
To enable SSL include https in the URL.
  Example: https://example.com
  verify_mode will default: OpenSSL::SSL::VERIFY_PEER

application: [String]
Name of this application to appear in log messages.
Default: SemanticLogger.application

host: [String]
Name of this host to appear in log messages.
Default: SemanticLogger.host

compress: [true|false]
Splunk supports HTTP Compression, enable by default.
Default: true

ssl: [Hash]
Specific SSL options: For more details see NET::HTTP.start
  ca_file, ca_path, cert, cert_store, ciphers, key, open_timeout, read_timeout, ssl_timeout,
  ssl_version, use_ssl, verify_callback, verify_depth and verify_mode.

level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: none (:trace), so it writes every entry that a logger emits; each logger
filters by its own level.

formatter: [Object|Proc]
An instance of a class that implements #call, or a Proc to be used to format
the output from this appender
Default: Use the built-in formatter (See: #call)

filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied.
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
      The Proc must return true or false.


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/semantic_logger/appender/splunk_http.rb', line 101

def initialize(token: nil,
               source_type: nil,
               index: nil,
               compress: true,
               **args,
               &)
  @source_type = source_type
  @index       = index

  super(compress: compress, **args, &)

  # Put splunk auth token in the header of every HTTP post.
  replace_header("Authorization", "Splunk #{token}")
end

Instance Attribute Details

#index ⇒ Object

Returns the value of attribute index.



47
48
49
# File 'lib/semantic_logger/appender/splunk_http.rb', line 47

def index
  @index
end

#source_type ⇒ Object

Returns the value of attribute source_type.



47
48
49
# File 'lib/semantic_logger/appender/splunk_http.rb', line 47

def source_type
  @source_type
end

Instance Method Details

#batch(logs) ⇒ Object

Send a batch of log messages to the HTTP Event Collector in a single request, as event objects stacked one after the other, the batch format of "Format events for HTTP Event Collector":

https://help.splunk.com/en/splunk-enterprise/get-started/get-data-in/10.6/get-data-with-http-event-collector/format-events-for-http-event-collector

Each event is what #log sends for that entry, formatted by #format_log. The formatter must format each entry as exactly one event object, as the default one, the appender itself (see #call), does, since the collector names a rejected event by its position in the request (see #rejected_entry). Every entry is formatted before the request is sent, so a format error sends nothing, and the batch is split up to find the entry at fault (see Http#batch_all_or_nothing?).



153
154
155
156
157
# File 'lib/semantic_logger/appender/splunk_http.rb', line 153

def batch(logs)
  message = logs.map { |log| format_log(log) }.join
  logger.trace(message)
  post(message)
end

#call(log, logger) ⇒ Object

Returns [String] JSON to send to Splunk.

For splunk format requirements see:

https://docs.splunk.com/Documentation/Splunk/latest/Data/FormateventsforHTTPEventCollector


120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/semantic_logger/appender/splunk_http.rb', line 120

def call(log, logger)
  h = SemanticLogger::Formatters::Raw.new(time_format: :seconds).call(log, logger)
  h.delete(:host)
  message = {
    source: logger.application,
    host:   logger.host,
    time:   h.delete(:time),
    event:  h
  }
  message[:sourcetype]  = source_type if source_type
  message[:index]       = index if index
  Utils.to_json(message)
end

#fails_any_batch?(exception) ⇒ Boolean

Whether the exception would fail any batch, whatever entries it holds (see Http#fails_any_batch?), including a 400 Bad Request whose body holds one of the collector's CONFIGURATION_CODES, such as for a wrong index:. So the batch is discarded at once, and reported once, with the collector's text, rather than split up and each entry sent on its own.

Returns:

  • (Boolean)


138
139
140
141
# File 'lib/semantic_logger/appender/splunk_http.rb', line 138

def fails_any_batch?(exception)
  super || (exception.is_a?(ResponseError) && exception.rejected? &&
    CONFIGURATION_CODES.include?(collector_response(exception.body)["code"]))
end

#rejected_entry(exception) ⇒ Object

Returns [Integer|nil] the position in the batch of the event that the HTTP Event Collector rejected, from the invalid-event-number in the body of its 400 Bad Request, such as data format","code":6,"invalid-event-number":2, or nil when it holds none (see Subscriber#rejected_entry).

The collector reads the events of a batch in order, and indexes those before the one it rejects. Splunk's documentation does not say so: "Troubleshoot HTTP Event Collector" and "Format events for HTTP Event Collector" describe neither invalid-event-number nor what happens to the rest of a batch. Users report it, for example https://github.com/moby/moby/issues/35862, where a batch of 10 whose last event is malformed, sent again and again, had "the first 1-9 ... accepted by Splunk over and over again".

So splitting such a batch to find the event at fault would index the events before it twice. Instead the events before it are counted as written, it is discarded, and those after it, which the collector never read, are sent again. A 400 without the number, such as a proxy's, is split as for Http, and so is any other status, which the collector does not answer with an event number. The number counts events, so a formatter must format each entry as one event object, as the default one does.



177
178
179
180
181
# File 'lib/semantic_logger/appender/splunk_http.rb', line 177

def rejected_entry(exception)
  return unless exception.is_a?(ResponseError) && exception.code == 400

  collector_response(exception.body)["invalid-event-number"]
end