class SplunkTracing::Transport::HTTPJSON
HTTPJSON
is a transport that sends reports via HTTP in JSON format. It is thread-safe.
Constants
- ENCRYPTION_NONE
- ENCRYPTION_TLS
- HEADER_ACCESS_TOKEN
- REPORTS_API_ENDPOINT
- SPLUNK_HEC_HOST
- SPLUNK_HEC_PORT
Public Class Methods
Initialize the transport
@param host [String] host of the domain to the endpoint to push data @param port [Numeric] port on which to connect @param verbose [Numeric] verbosity level. Right now 0-3 are supported @param encryption [ENCRYPTION_TLS, ENCRYPTION_NONE
] kind of encryption to use @param access_token [String] access token for SplunkTracing
server @param ssl_verify_peer [Boolean] @param open_timeout [Integer] @param read_timeout [Integer] @param continue_timeout [Integer] @param keep_alive_timeout [Integer] @param logger [Logger]
# File lib/splunktracing/transport/http_json.rb, line 35 def initialize( host: SPLUNK_HEC_HOST, port: SPLUNK_HEC_PORT, verbose: 0, encryption: ENCRYPTION_TLS, access_token:, ssl_verify_peer: false, open_timeout: 20, read_timeout: 20, continue_timeout: nil, keep_alive_timeout: 2, logger: nil ) @host = host @port = port @verbose = verbose @encryption = encryption @ssl_verify_peer = ssl_verify_peer @open_timeout = open_timeout.to_i @read_timeout = read_timeout.to_i @continue_timeout = continue_timeout @keep_alive_timeout = keep_alive_timeout.to_i raise Tracer::ConfigurationError, 'access_token must be a string' unless access_token.is_a?(String) raise Tracer::ConfigurationError, 'access_token cannot be blank' if access_token.empty? @access_token = access_token @logger = logger || SplunkTracing.logger end
Public Instance Methods
Queue a report for sending
# File lib/splunktracing/transport/http_json.rb, line 67 def report(report) @logger.info report if @verbose >= 3 req = build_request(report) res = connection.request(req) @logger.info res.to_s if @verbose >= 3 nil end
Private Instance Methods
@param [String] report_string @return [Net::HTTP::Post]
# File lib/splunktracing/transport/http_json.rb, line 84 def build_request(report) gzip = Zlib::GzipWriter.new(StringIO.new) gzip << convert_report_data(report) req = Net::HTTP::Post.new(REPORTS_API_ENDPOINT) req[HEADER_ACCESS_TOKEN] = 'Splunk ' + @access_token req['Content-Type'] = 'application/json' req['Content-Encoding'] = 'gzip' req['Connection'] = 'keep-alive' req.body = gzip.close.string req end
@return [Net::HTTP]
# File lib/splunktracing/transport/http_json.rb, line 130 def connection unless @connection @connection = ::Net::HTTP.new(@host, @port) @connection.use_ssl = @encryption == ENCRYPTION_TLS @connection.verify_mode = ::OpenSSL::SSL::VERIFY_NONE unless @ssl_verify_peer @connection.open_timeout = @open_timeout @connection.read_timeout = @read_timeout @connection.continue_timeout = @continue_timeout @connection.keep_alive_timeout = @keep_alive_timeout end @connection end
@param [Hash] report @return [String] report_string
# File lib/splunktracing/transport/http_json.rb, line 100 def convert_report_data(report) report_obj_array = Array.new runtime_hash = report[:runtime] if report[:span_records].any? report[:span_records].each do |span| span_hash = {:time => span[:timestamp], :sourcetype => "splunktracing:span" } span_contents = span.merge(runtime_hash) log_array = span_contents.delete(:log_records) runtime_attrs = span_contents.delete(:attrs) span_contents[:tags].merge(runtime_attrs) span_hash["event"] = span_contents report_obj_array.push(span_hash.to_json) if log_array && log_array.any? span_contents.delete(:timestamp) span_contents.delete(:duration) log_array.each do |log| log_hash = {:time => log[:timestamp_micros]/1000000.0, :sourcetype => "splunktracing:log" , :event => log.merge(span_contents)} report_obj_array.push(log_hash.to_json) end end end end report_string = report_obj_array.join("\n") report_string end