Skip to content

Commit

Permalink
DEBUG-2334 repair DI transport (#4087)
Browse files Browse the repository at this point in the history
* DEBUG-2334 repair DI transport

"input" endpoint requires a JSON payload, not a
multipart form post.

A better test is included to verify this functionality.
  • Loading branch information
p-datadog authored Nov 7, 2024
1 parent 2224fe4 commit c8bd1e4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
25 changes: 16 additions & 9 deletions lib/datadog/di/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,38 @@ def send_diagnostics(payload)
StringIO.new(JSON.dump(payload)), 'application/json', 'event.json'
)
payload = {'event' => event_payload}
send_request('Probe status submission', DIAGNOSTICS_PATH, payload)
# Core transport unconditionally specifies headers to underlying
# Net::HTTP client, ends up passing 'nil' as headers if none are
# specified by us, which then causes Net::HTTP to die with an exception.
send_request('Probe status submission',
path: DIAGNOSTICS_PATH, form: payload, headers: {})
end

def send_input(payload)
send_request('Probe snapshot submission', INPUT_PATH, payload,
send_request('Probe snapshot submission',
path: INPUT_PATH, body: payload.to_s,
headers: {'content-type' => 'application/json'},)
end

private

attr_reader :client

def send_request(desc, path, payload, headers: {})
def send_request(desc, **options)
# steep:ignore:start
env = OpenStruct.new(
path: path,
form: payload,
headers: headers,
)
env = OpenStruct.new(**options)
# steep:ignore:end
response = client.post(env)
unless response.ok?
raise Error::AgentCommunicationError, "#{desc} failed: #{response.code}: #{response.payload}"
end
rescue IOError, SystemCallError => exc
# Datadog::Core::Transport does not perform any exception mapping,
# therefore we could have any exception here from failure to parse
# agent URI for example.
# If we ever implement retries for network errors, we should distinguish
# actual network errors from non-network errors that are raised by
# transport code.
rescue => exc
raise Error::AgentCommunicationError, "#{desc} failed: #{exc.class}: #{exc}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/di/transport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Datadog

attr_reader client: untyped

def send_request: (String desc, String path, Hash[untyped,untyped] payload, ?headers: ::Hash[untyped, untyped]) -> void
def send_request: (String desc, path: String, ?body: String, ?form: Hash[untyped,untyped], ?headers: ::Hash[untyped, untyped]) -> void
end
end
end
31 changes: 25 additions & 6 deletions spec/datadog/di/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,33 @@
end

describe '.send_input' do
let(:payload) do
{}
context 'empty payload' do
let(:payload) do
{}
end

it 'does not raise exceptions' do
expect do
client.send_input(payload)
end.not_to raise_exception
end
end

it 'does not raise exceptions' do
expect do
client.send_input(payload)
end.not_to raise_exception
context 'partial DI payload' do
let(:payload) do
{
service: 'rspec',
"debugger.snapshot": {
id: '1234',
},
}
end

it 'does not raise exceptions' do
expect do
client.send_input(payload)
end.not_to raise_exception
end
end
end

Expand Down

0 comments on commit c8bd1e4

Please sign in to comment.