lib/datadog/ci/transport/http.rb in datadog-ci-1.2.0 vs lib/datadog/ci/transport/http.rb in datadog-ci-1.3.0
- old
+ new
@@ -1,10 +1,12 @@
# frozen_string_literal: true
require "delegate"
require "socket"
+require "datadog/core/utils/time"
+
require_relative "gzip"
require_relative "adapters/net"
require_relative "../ext/transport"
module Datadog
@@ -37,32 +39,41 @@
verb: "post",
retries: MAX_RETRIES,
backoff: INITIAL_BACKOFF,
accept_compressed_response: false
)
- if compress
- headers[Ext::Transport::HEADER_CONTENT_ENCODING] = Ext::Transport::CONTENT_ENCODING_GZIP
- payload = Gzip.compress(payload)
- end
+ response = nil
- if accept_compressed_response
- headers[Ext::Transport::HEADER_ACCEPT_ENCODING] = Ext::Transport::CONTENT_ENCODING_GZIP
- end
+ duration_ms = Core::Utils::Time.measure(:float_millisecond) do
+ if compress
+ headers[Ext::Transport::HEADER_CONTENT_ENCODING] = Ext::Transport::CONTENT_ENCODING_GZIP
+ payload = Gzip.compress(payload)
+ end
- Datadog.logger.debug do
- "Sending #{verb} request: host=#{host}; port=#{port}; ssl_enabled=#{ssl}; " \
- "compression_enabled=#{compress}; path=#{path}; payload_size=#{payload.size}"
- end
+ if accept_compressed_response
+ headers[Ext::Transport::HEADER_ACCEPT_ENCODING] = Ext::Transport::CONTENT_ENCODING_GZIP
+ end
- response = ResponseDecorator.new(
- perform_http_call(path: path, payload: payload, headers: headers, verb: verb, retries: retries, backoff: backoff)
- )
+ Datadog.logger.debug do
+ "Sending #{verb} request: host=#{host}; port=#{port}; ssl_enabled=#{ssl}; " \
+ "compression_enabled=#{compress}; path=#{path}; payload_size=#{payload.size}"
+ end
- Datadog.logger.debug do
- "Received server response: #{response.inspect}"
+ response = perform_http_call(path: path, payload: payload, headers: headers, verb: verb, retries: retries, backoff: backoff)
+
+ Datadog.logger.debug do
+ "Received server response: #{response.inspect}"
+ end
end
+ # @type var response: Datadog::CI::Transport::Adapters::Net::Response
+ # @type var duration_ms: Float
+ # set some stats about the request
+ response.request_compressed = compress
+ response.request_size = payload.bytesize
+ response.duration_ms = duration_ms
+
response
end
private
@@ -79,23 +90,40 @@
perform_http_call(
path: path, payload: payload, headers: headers, verb: verb, retries: retries - 1, backoff: backoff * 2
)
else
Datadog.logger.error("Failed to send request after #{MAX_RETRIES} retries")
- raise e
+ ErrorResponse.new(e)
end
end
def adapter
@adapter ||= Datadog::CI::Transport::Adapters::Net.new(
hostname: host, port: port, ssl: ssl, timeout_seconds: timeout
)
end
- # adds compatibility with Datadog::Tracing transport
- class ResponseDecorator < ::SimpleDelegator
- def trace_count
- 0
+ class ErrorResponse < Adapters::Net::Response
+ def initialize(error)
+ @error = error
+ end
+
+ attr_reader :error
+
+ def payload
+ ""
+ end
+
+ def header(name)
+ nil
+ end
+
+ def code
+ nil
+ end
+
+ def inspect
+ "ErrorResponse error:#{error}"
end
end
end
end
end