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

Constant Summary collapse

DEFAULT_TIMEOUT =

in seconds

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.



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

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.



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



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

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



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

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

#get(env) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 59

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



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

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 71

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



94
95
96
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 94

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