Skip to content

Commit

Permalink
migrate to Core transport WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 28, 2023
1 parent d8cf27f commit ed837c4
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 392 deletions.
122 changes: 22 additions & 100 deletions lib/datadog/ci/transport/http.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

require "net/http"
require "datadog/core/transport/http/adapters/net"
require "datadog/core/transport/http/env"
require "datadog/core/transport/request"

require_relative "gzip"
require_relative "../ext/transport"
Expand All @@ -26,129 +29,48 @@ def initialize(host:, timeout: DEFAULT_TIMEOUT, port: nil, ssl: true, compress:
@compress = compress.nil? ? false : compress
end

def request(path:, payload:, headers:, method: "post")
raise "Unknown method #{method}" unless respond_to?(method, true)

def request(path:, payload:, headers:, verb: "post")
if compress
headers[Ext::Transport::HEADER_CONTENT_ENCODING] = Ext::Transport::CONTENT_ENCODING_GZIP
payload = Gzip.compress(payload)
end

Datadog.logger.debug { "Sending #{method} request" }
Datadog.logger.debug { "Sending #{verb} request" }
Datadog.logger.debug { "host #{host}" }
Datadog.logger.debug { "port #{port}" }
Datadog.logger.debug { "ssl enabled #{ssl}" }
Datadog.logger.debug { "compression enabled #{compress}" }
Datadog.logger.debug { "path #{path}" }
Datadog.logger.debug { "payload size #{payload.size}" }

send(method, path: path, payload: payload, headers: headers)
ResponseDecorator.new(
adapter.call(
build_env(path: path, payload: payload, headers: headers, verb: verb)
)
)
end

private

def open(&block)
req = ::Net::HTTP.new(@host, @port)

req.use_ssl = @ssl
req.open_timeout = req.read_timeout = @timeout

req.start(&block)
def build_env(path:, payload:, headers:, verb:)
env = Datadog::Core::Transport::HTTP::Env.new(
Datadog::Core::Transport::Request.new
)
env.body = payload
env.path = path
env.headers = headers
env.verb = verb
env
end

def post(path:, headers:, payload:)
post = ::Net::HTTP::Post.new(path, headers)
post.body = payload

http_response = open do |http|
http.request(post)
end

Response.new(http_response)
rescue => e
Datadog.logger.debug("Unable to send events: #{e}")

InternalErrorResponse.new(e)
def adapter
@adapter ||= Datadog::Core::Transport::HTTP::Adapters::Net.new(host, port, timeout: timeout, ssl: ssl)
end

# Data structure for an HTTP Response
class Response
attr_reader :http_response

def initialize(http_response)
@http_response = http_response
end

def payload
http_response.body
end

def code
http_response.code.to_i
end

def ok?
code.between?(200, 299)
end

def unsupported?
code == 415
end

def not_found?
code == 404
end

def client_error?
code.between?(400, 499)
end

def server_error?
code.between?(500, 599)
end

def internal_error?
false
end

class ResponseDecorator < SimpleDelegator
def trace_count
0
end

def inspect
"#{self.class} ok?:#{ok?} unsupported?:#{unsupported?}, " \
"not_found?:#{not_found?}, client_error?:#{client_error?}, " \
"server_error?:#{server_error?}, internal_error?:#{internal_error?}, " \
"payload:#{payload}"
end
end

class InternalErrorResponse < Response
class DummyNetHTTPResponse
def body
""
end

def code
"-1"
end
end

attr_reader :error

def initialize(error)
super(DummyNetHTTPResponse.new)

@error = error
end

def internal_error?
true
end

def inspect
"#{super}, error_class:#{error.class}, error:#{error}"
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/ci/test_visibility/transport.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ module Datadog
?max_payload_size: Integer
) -> void

def send_traces: (Array[Datadog::Tracing::TraceSegment] traces) -> ::Array[Datadog::CI::Transport::HTTP::Response]
def send_traces: (Array[Datadog::Tracing::TraceSegment] traces) -> ::Array[Datadog::CI::Transport::HTTP::ResponseDecorator]

private

def send_payload: (String encoded_payload) -> Datadog::CI::Transport::HTTP::Response
def send_payload: (String encoded_payload) -> Datadog::CI::Transport::HTTP::ResponseDecorator
def pack_events: (Array[String] encoded_events) -> String
def encode_traces: (Array[Datadog::Tracing::TraceSegment] traces) -> ::Array[String]
def encode_span: (Datadog::Tracing::TraceSegment trace, Datadog::Tracing::Span span) -> String?
Expand Down
49 changes: 12 additions & 37 deletions sig/datadog/ci/transport/http.rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class SimpleDelegator
end

module Datadog
module CI
module Transport
class HTTP
@adapter: Datadog::Core::Transport::HTTP::Adapters::Net

attr_reader host: String
attr_reader port: Integer?
attr_reader ssl: bool
Expand All @@ -12,50 +17,20 @@ module Datadog

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

def request: (?method: String, payload: String, headers: Hash[String, String], path: String) -> Response
def request: (?verb: String, payload: String, headers: Hash[String, String], path: String) -> ResponseDecorator

private

def open: () { (::Net::HTTP) -> Net::HTTPResponse } -> Net::HTTPResponse

def post: (payload: String, headers: Hash[String, String], path: String) -> Response

class Response
attr_reader http_response: (Net::HTTPResponse | InternalErrorResponse::DummyNetHTTPResponse)

def initialize: ((Net::HTTPResponse | InternalErrorResponse::DummyNetHTTPResponse) http_response) -> void

def payload: () -> String

def code: () -> Integer

def ok?: () -> bool
def adapter: () -> Datadog::Core::Transport::HTTP::Adapters::Net

def unsupported?: () -> bool
def build_env: (payload: String, headers: Hash[String, String], path: String, verb: String) -> Datadog::Core::Transport::HTTP::Env

def not_found?: () -> bool

def client_error?: () -> bool

def server_error?: () -> bool

def internal_error?: () -> bool

def inspect: () -> ::String
end

class InternalErrorResponse < Response
class DummyNetHTTPResponse
def body: () -> ""
def code: () -> "-1"
end

attr_reader error: StandardError
@error: StandardError

def initialize: (StandardError error) -> void
class ResponseDecorator < ::SimpleDelegator
def initialize: (untyped anything) -> void
def trace_count: () -> Integer
end
end
end
end
end

Loading

0 comments on commit ed837c4

Please sign in to comment.