Class: Datadog::Transport::HTTP::Adapters::Net

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/transport/http/adapters/net.rb

Overview

Adapter for Net::HTTP

Direct Known Subclasses

UnixSocket

Defined Under Namespace

Classes: Response, UnknownHTTPMethod

Constant Summary collapse

DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname = nil, port = nil, **options) ⇒ Net

Deprecated.

Positional parameters are deprecated. Use named parameters instead.

Returns a new instance of Net.



20
21
22
23
24
25
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 20

def initialize(hostname = nil, port = nil, **options)
  @hostname = hostname || options.fetch(:hostname)
  @port = port || options.fetch(:port)
  @timeout = options[:timeout] || DEFAULT_TIMEOUT
  @ssl = options.key?(:ssl) ? options[:ssl] == true : false
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



11
12
13
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 11

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 11

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



11
12
13
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 11

def ssl
  @ssl
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



11
12
13
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 11

def timeout
  @timeout
end

Class Method Details

.build(agent_settings) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 27

def self.build(agent_settings)
  new(
    hostname: agent_settings.hostname,
    port: agent_settings.port,
    timeout: agent_settings.timeout_seconds,
    ssl: agent_settings.ssl
  )
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 48

def call(env)
  if respond_to?(env.verb)
    send(env.verb, env)
  else
    raise UnknownHTTPMethod, env
  end
end

#open(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 36

def open(&block)
  # DEV Initializing +Net::HTTP+ directly help us avoid expensive
  # options processing done in +Net::HTTP.start+:
  # https://github.com/ruby/ruby/blob/b2d96abb42abbe2e01f010ffc9ac51f0f9a50002/lib/net/http.rb#L614-L618
  req = ::Net::HTTP.new(hostname, port, nil)

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

  req.start(&block)
end

#post(env) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 56

def post(env)
  post = nil

  if env.form.nil? || env.form.empty?
    post = ::Net::HTTP::Post.new(env.path, env.headers)
    post.body = env.body
  else
    post = ::Datadog::Vendor::Net::HTTP::Post::Multipart.new(
      env.path,
      env.form,
      env.headers
    )
  end

  # Connect and send the request
  http_response = open do |http|
    http.request(post)
  end

  # Build and return response
  Response.new(http_response)
end

#urlObject



79
80
81
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 79

def url
  "http://#{hostname}:#{port}?timeout=#{timeout}"
end