lib/datadog/ci/transport/adapters/net.rb in datadog-ci-1.2.0 vs lib/datadog/ci/transport/adapters/net.rb in datadog-ci-1.3.0

- old
+ new

@@ -1,11 +1,13 @@ # frozen_string_literal: true require "datadog/core/transport/response" require "datadog/core/transport/ext" +require_relative "net_http_client" require_relative "../gzip" +require_relative "../../ext/telemetry" require_relative "../../ext/transport" module Datadog module CI module Transport @@ -24,11 +26,11 @@ @timeout = timeout_seconds @ssl = ssl end def open(&block) - req = net_http_client.new(hostname, port) + req = NetHttpClient.original_net_http.new(hostname, port) req.use_ssl = ssl req.open_timeout = req.read_timeout = timeout req.start(&block) @@ -61,10 +63,12 @@ class Response include Datadog::Core::Transport::Response attr_reader :http_response + # Stats for telemetry + attr_accessor :duration_ms, :request_compressed, :request_size def initialize(http_response) @http_response = http_response end @@ -75,20 +79,27 @@ Datadog.logger.debug("Decompressing gzipped response payload") @decompressed_payload = Gzip.decompress(http_response.body) end + def response_size + http_response.body.bytesize + end + def header(name) http_response[name] end def code http_response.code.to_i end def ok? - code.between?(200, 299) + http_code = code + return false if http_code.nil? + + http_code.between?(200, 299) end def unsupported? code == 415 end @@ -96,15 +107,21 @@ def not_found? code == 404 end def client_error? - code.between?(400, 499) + http_code = code + return false if http_code.nil? + + http_code.between?(400, 499) end def server_error? - code.between?(500, 599) + http_code = code + return false if http_code.nil? + + http_code.between?(500, 599) end def gzipped_content? header(Ext::Transport::HEADER_CONTENT_ENCODING) == Ext::Transport::CONTENT_ENCODING_GZIP end @@ -117,20 +134,34 @@ return false if first_bytes.nil? || first_bytes.empty? first_bytes.b == Ext::Transport::GZIP_MAGIC_NUMBER end - def inspect - "#{super}, http_response:#{http_response}" + def error + nil end - end - private + def telemetry_error_type + return nil if ok? - def net_http_client - return ::Net::HTTP unless defined?(WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP) + case error + when nil + Ext::Telemetry::ErrorType::STATUS_CODE + when Timeout::Error + Ext::Telemetry::ErrorType::TIMEOUT + else + Ext::Telemetry::ErrorType::NETWORK + end + end - WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP + # compatibility with Datadog::Tracing transport layer + def trace_count + 0 + end + + def inspect + "#{super}, http_response:#{http_response}" + end end end end end end