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

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

Overview

Adapter for Net::HTTP

Direct Known Subclasses

UnixSocket

Defined Under Namespace

Classes: Response, UnknownHTTPMethod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_settings) ⇒ Net

Returns a new instance of Net.



19
20
21
22
23
24
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 19

def initialize(agent_settings)
  @hostname = agent_settings.hostname
  @port = agent_settings.port
  @timeout = agent_settings.timeout_seconds
  @ssl = agent_settings.ssl
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



13
14
15
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 13

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 13

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



13
14
15
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 13

def ssl
  @ssl
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



13
14
15
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 13

def timeout
  @timeout
end

Class Method Details

.build(agent_settings) ⇒ Object



26
27
28
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 26

def self.build(agent_settings)
  new(agent_settings)
end

Instance Method Details

#call(env) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 42

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

#get(env) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 50

def get(env)
  get = ::Net::HTTP::Get.new(env.path, env.headers)

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

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

#open(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 30

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 62

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::Core::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



85
86
87
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 85

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