Skip to content

Commit

Permalink
add request stats to HTTP Response object
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jul 25, 2024
1 parent 5161d98 commit 4418efc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
7 changes: 7 additions & 0 deletions lib/datadog/ci/transport/adapters/net.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Response
include Datadog::Core::Transport::Response

attr_reader :http_response
# Stats for telemetry
attr_accessor :request_compressed, :request_size

def initialize(http_response)
@http_response = http_response
Expand Down Expand Up @@ -124,6 +126,11 @@ def error
nil
end

# compatibility with Datadog::Tracing transport layer
def trace_count
0
end

def inspect
"#{super}, http_response:#{http_response}"
end
Expand Down
14 changes: 4 additions & 10 deletions lib/datadog/ci/transport/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ def request(
"compression_enabled=#{compress}; path=#{path}; payload_size=#{payload.size}"
end

response = ResponseDecorator.new(
perform_http_call(path: path, payload: payload, headers: headers, verb: verb, retries: retries, backoff: backoff)
)
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

# set some stats about the request
response.request_compressed = compress
response.request_size = payload.size
response
end

Expand Down Expand Up @@ -91,13 +92,6 @@ def adapter
)
end

# adds compatibility with Datadog::Tracing transport
class ResponseDecorator < ::SimpleDelegator
def trace_count
0
end
end

class ErrorResponse < Adapters::Net::Response
def initialize(error)
@error = error
Expand Down
8 changes: 8 additions & 0 deletions sig/datadog/ci/transport/adapters/net.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ module Datadog

attr_reader http_response: ::Net::HTTPResponse

attr_reader error: StandardError?

attr_accessor request_compressed: bool

attr_accessor request_size: Integer

def initialize: (::Net::HTTPResponse http_response) -> void

def payload: () -> String
Expand All @@ -56,6 +62,8 @@ module Datadog

def gzipped_content?: () -> bool

def trace_count: () -> 0

def inspect: () -> ::String
end
end
Expand Down
11 changes: 1 addition & 10 deletions sig/datadog/ci/transport/http.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,15 @@ module Datadog

def initialize: (host: String, port: Integer, ?ssl: bool, ?timeout: Integer, ?compress: bool) -> void

def request: (?verb: String, payload: String, headers: Hash[String, String], path: String, ?retries: Integer, ?backoff: Integer, ?accept_compressed_response: bool) -> ResponseDecorator
def request: (?verb: String, payload: String, headers: Hash[String, String], path: String, ?retries: Integer, ?backoff: Integer, ?accept_compressed_response: bool) -> Datadog::CI::Transport::Adapters::Net::Response

private

def adapter: () -> Datadog::CI::Transport::Adapters::Net

def perform_http_call: (payload: String, headers: Hash[String, String], path: String, verb: String, ?retries: Integer, ?backoff: Integer) -> Datadog::CI::Transport::Adapters::Net::Response

class ResponseDecorator < ::SimpleDelegator
include Datadog::Core::Transport::Response

def initialize: (untyped anything) -> void
def trace_count: () -> Integer
end

class ErrorResponse < Datadog::CI::Transport::Adapters::Net::Response
attr_reader error: StandardError?

def initialize: (StandardError error) -> void
end
end
Expand Down
15 changes: 10 additions & 5 deletions spec/datadog/ci/transport/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
let(:request_options) { {accept_compressed_response: false} }

let(:response_payload) { "sample payload" }
let(:http_response) { double("http_response", code: 200, payload: response_payload) }
let(:net_http_response) { double("Net::HTTP::Response", code: 200, body: response_payload, "[]": nil) }
let(:http_response) { Datadog::CI::Transport::Adapters::Net::Response.new(net_http_response) }

subject(:response) { transport.request(path: path, payload: payload, headers: headers, **request_options) }

Expand All @@ -112,17 +113,19 @@
end

it "produces a response" do
is_expected.to be_a_kind_of(described_class::ResponseDecorator)
is_expected.to be_a_kind_of(Datadog::CI::Transport::Adapters::Net::Response)

expect(response.code).to eq(200)
expect(response.payload).to eq("sample payload")
expect(response.request_compressed).to eq(false)
expect(response.request_size).to eq(payload.size)
end

context "when accepting gzipped response" do
let(:expected_headers) { {"Content-Type" => "application/json", "Accept-Encoding" => "gzip"} }
let(:request_options) { {accept_compressed_response: true} }

it { is_expected.to be_a_kind_of(described_class::ResponseDecorator) }
it { is_expected.to be_a_kind_of(Datadog::CI::Transport::Adapters::Net::Response) }
end
end

Expand All @@ -147,9 +150,11 @@
end

it "produces a response" do
is_expected.to be_a_kind_of(described_class::ResponseDecorator)
is_expected.to be_a_kind_of(Datadog::CI::Transport::Adapters::Net::Response)

expect(response.code).to eq(200)
expect(response.request_compressed).to eq(true)
expect(response.request_size).to eq(expected_payload.size)
end
end

Expand All @@ -163,7 +168,7 @@
end

it "produces a response" do
is_expected.to be_a_kind_of(described_class::ResponseDecorator)
is_expected.to be_a_kind_of(Datadog::CI::Transport::Adapters::Net::Response)

expect(response.code).to eq(200)
end
Expand Down

0 comments on commit 4418efc

Please sign in to comment.