-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEBUG-3535 use core transport for DI to support unix domain sockets (#…
- Loading branch information
Showing
26 changed files
with
1,044 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../core/transport/parcel' | ||
require_relative 'http/client' | ||
|
||
module Datadog | ||
module DI | ||
module Transport | ||
module Diagnostics | ||
class EncodedParcel | ||
include Datadog::Core::Transport::Parcel | ||
end | ||
|
||
class Request < Datadog::Core::Transport::Request | ||
end | ||
|
||
class Transport | ||
attr_reader :client, :apis, :default_api, :current_api_id | ||
|
||
def initialize(apis, default_api) | ||
@apis = apis | ||
|
||
@client = HTTP::Client.new(current_api) | ||
end | ||
|
||
def current_api | ||
@apis[HTTP::API::DIAGNOSTICS] | ||
end | ||
|
||
def send_diagnostics(payload) | ||
json = JSON.dump(payload) | ||
parcel = EncodedParcel.new(json) | ||
request = Request.new(parcel) | ||
|
||
response = @client.send_diagnostics_payload(request) | ||
unless response.ok? | ||
# TODO Datadog::Core::Transport::InternalErrorResponse | ||
# does not have +code+ method, what is the actual API of | ||
# these response objects? | ||
raise Error::AgentCommunicationError, "send_diagnostics failed: #{begin | ||
response.code | ||
rescue | ||
"???" | ||
end}: #{response.payload}" | ||
end | ||
rescue Error::AgentCommunicationError | ||
raise | ||
# 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, "send_diagnostics failed: #{exc.class}: #{exc}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'uri' | ||
|
||
require_relative '../../core/environment/container' | ||
require_relative '../../core/environment/ext' | ||
require_relative '../../core/transport/ext' | ||
require_relative '../../core/transport/http/adapters/net' | ||
require_relative '../../core/transport/http/adapters/test' | ||
require_relative '../../core/transport/http/adapters/unix_socket' | ||
require_relative 'diagnostics' | ||
require_relative 'input' | ||
require_relative 'http/api' | ||
require_relative '../../core/transport/http/builder' | ||
require_relative '../../../datadog/version' | ||
|
||
module Datadog | ||
module DI | ||
module Transport | ||
# Namespace for HTTP transport components | ||
module HTTP | ||
module_function | ||
|
||
# Builds a new Transport::HTTP::Client | ||
def new(klass, &block) | ||
Core::Transport::HTTP::Builder.new( | ||
api_instance_class: API::Instance, &block | ||
).to_transport(klass) | ||
end | ||
|
||
# Builds a new Transport::HTTP::Client with default settings | ||
# Pass a block to override any settings. | ||
def diagnostics( | ||
agent_settings:, | ||
**options | ||
) | ||
new(DI::Transport::Diagnostics::Transport) do |transport| | ||
transport.adapter(agent_settings) | ||
transport.headers default_headers | ||
|
||
apis = API.defaults | ||
|
||
transport.api API::DIAGNOSTICS, apis[API::DIAGNOSTICS] | ||
|
||
# Apply any settings given by options | ||
unless options.empty? | ||
transport.default_api = options[:api_version] if options.key?(:api_version) | ||
transport.headers options[:headers] if options.key?(:headers) | ||
end | ||
|
||
# Call block to apply any customization, if provided | ||
yield(transport) if block_given? | ||
end | ||
end | ||
|
||
# Builds a new Transport::HTTP::Client with default settings | ||
# Pass a block to override any settings. | ||
def input( | ||
agent_settings:, | ||
**options | ||
) | ||
new(DI::Transport::Input::Transport) do |transport| | ||
transport.adapter(agent_settings) | ||
transport.headers default_headers | ||
|
||
apis = API.defaults | ||
|
||
transport.api API::INPUT, apis[API::INPUT] | ||
|
||
# Apply any settings given by options | ||
unless options.empty? | ||
transport.default_api = options[:api_version] if options.key?(:api_version) | ||
transport.headers options[:headers] if options.key?(:headers) | ||
end | ||
|
||
# Call block to apply any customization, if provided | ||
yield(transport) if block_given? | ||
end | ||
end | ||
|
||
def default_headers | ||
{ | ||
Datadog::Core::Transport::Ext::HTTP::HEADER_CLIENT_COMPUTED_TOP_LEVEL => '1', | ||
Datadog::Core::Transport::Ext::HTTP::HEADER_META_LANG => Datadog::Core::Environment::Ext::LANG, | ||
Datadog::Core::Transport::Ext::HTTP::HEADER_META_LANG_VERSION => Datadog::Core::Environment::Ext::LANG_VERSION, | ||
Datadog::Core::Transport::Ext::HTTP::HEADER_META_LANG_INTERPRETER => | ||
Datadog::Core::Environment::Ext::LANG_INTERPRETER, | ||
Datadog::Core::Transport::Ext::HTTP::HEADER_META_LANG_INTERPRETER_VENDOR => Core::Environment::Ext::LANG_ENGINE, | ||
Datadog::Core::Transport::Ext::HTTP::HEADER_META_TRACER_VERSION => | ||
Datadog::Core::Environment::Ext::GEM_DATADOG_VERSION | ||
}.tap do |headers| | ||
# Add container ID, if present. | ||
container_id = Datadog::Core::Environment::Container.container_id | ||
headers[Datadog::Core::Transport::Ext::HTTP::HEADER_CONTAINER_ID] = container_id unless container_id.nil? | ||
# Pretend that stats computation are already done by the client | ||
if Datadog.configuration.appsec.standalone.enabled | ||
headers[Datadog::Core::Transport::Ext::HTTP::HEADER_CLIENT_COMPUTED_STATS] = 'yes' | ||
end | ||
end | ||
end | ||
|
||
def default_adapter | ||
Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER | ||
end | ||
|
||
# Add adapters to registry | ||
Datadog::Core::Transport::HTTP::Builder::REGISTRY.set( | ||
Datadog::Core::Transport::HTTP::Adapters::Net, | ||
Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER | ||
) | ||
Datadog::Core::Transport::HTTP::Builder::REGISTRY.set(Datadog::Core::Transport::HTTP::Adapters::Test, Datadog::Core::Transport::Ext::Test::ADAPTER) | ||
Datadog::Core::Transport::HTTP::Builder::REGISTRY.set( | ||
Datadog::Core::Transport::HTTP::Adapters::UnixSocket, | ||
Datadog::Core::Transport::Ext::UnixSocket::ADAPTER | ||
) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.