Class: Datadog::Core::Telemetry::Http::Adapters::Net

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

Overview

Class defining methods to make http requests via NET

Defined Under Namespace

Classes: Response

Constant Summary collapse

DEFAULT_TIMEOUT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname:, port: nil, timeout: DEFAULT_TIMEOUT, ssl: true) ⇒ Net

Returns a new instance of Net.



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

def initialize(hostname:, port: nil, timeout: DEFAULT_TIMEOUT, ssl: true)
  @hostname = hostname
  @port = port
  @timeout = timeout
  @ssl = ssl.nil? ? true : ssl
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



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

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#sslObject (readonly)

Returns the value of attribute ssl.



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

def ssl
  @ssl
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#open(&block) ⇒ Object



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

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

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

  req.start(&block)
end

#post(env) ⇒ Object



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

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

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

  Response.new(http_response)
rescue StandardError => e
  Datadog.logger.debug("Unable to send telemetry event to agent: #{e}")
  Telemetry::Http::InternalErrorResponse.new(e)
end