lib/plain_apm/transport.rb in plain_apm-0.9.1 vs lib/plain_apm/transport.rb in plain_apm-0.9.2

- old
+ new

@@ -1,9 +1,10 @@ # frozen_string_literal: true require "net/http" require "json" +require "zlib" module PlainApm class Transport ## # HTTP transport class, mostly a wrapper for errors and timeout-handling. @@ -45,26 +46,23 @@ # TODO: our own CA bundle? http.use_ssl = uri.scheme == "https" http.open_timeout = HTTP_OPEN_TIMEOUT_SECONDS http.read_timeout = HTTP_READ_TIMEOUT_SECONDS + http.write_timeout = HTTP_WRITE_TIMEOUT_SECONDS - if RUBY_VERSION >= "2.6.0" - http.write_timeout = HTTP_WRITE_TIMEOUT_SECONDS - end - at_exit { shutdown } end ## # Performs the actual HTTP request. # # @param data [String] serialized payload to POST # @return [Array] [response, error, retriable] - def deliver(data) + def deliver(data, meta = {}) http_response do - http_request(http, uri.path, data) + http_request(http, uri.path, data, meta) end end private @@ -80,19 +78,24 @@ # Close the connection at exit. def shutdown http.finish if http.started? end - def http_request(http, path, body) - request = Net::HTTP::Post.new(path, http_headers) - http.request(request, body) + def http_request(http, path, body, meta) + request = Net::HTTP::Post.new(path, http_headers(meta)) + http.request(request, Zlib::Deflate.deflate(JSON.generate(body))) end - def http_headers + def http_headers(meta) + meta_headers = meta.map do |k, v| + ["X-PlainApm-#{k.to_s.split("_").map(&:capitalize).join("-")}", v.to_s] + end.to_h + { "Content-Type" => "application/json, charset=UTF-8", + "Content-Encoding" => "gzip", "X-PlainApm-Key" => app_key - } + }.merge(meta_headers) end def http_response # Opening the connection here allows us to rescue socket and SSL errors. # It'll be a NO-OP if already connected.