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 =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, port, options = {}) ⇒ Net

Returns a new instance of Net.



16
17
18
19
20
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 16

def initialize(hostname, port, options = {})
  @hostname = hostname
  @port = port
  @timeout = options[:timeout] || DEFAULT_TIMEOUT
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



9
10
11
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 9

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 9

def port
  @port
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



9
10
11
# File 'lib/ddtrace/transport/http/adapters/net.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#call(env) ⇒ Object



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

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

#openObject



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

def open
  # Open connection
  ::Net::HTTP.start(hostname, port, open_timeout: timeout, read_timeout: timeout) do |http|
    yield(http)
  end
end

#post(env) ⇒ Object



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

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

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

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