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

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
25
26
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 19

def initialize(agent_settings)
  @hostname = agent_settings.hostname
  @port = agent_settings.port
  # @hostname = '10.0.13.176'
  # @port = 18386
  @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



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

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

Instance Method Details

#call(env) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 45

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

#get(env) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 53

def get(env)
  get = ::Net::HTTP::Get.new(net_http_path_from_env(env), 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

#net_http_path_from_env(env) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 102

def net_http_path_from_env(env)
  path = env.path
  case query = env.query
  when Hash
    path = path + '?' + URI.encode_www_form(query)
  when String
    path = path + '?' + query
  when nil
    # Nothing
  else
    raise ArgumentError, "Invalid type for query: #{query}"
  end
  path
end

#open(&block) ⇒ Object



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

def open(&block)
  # DEV Initializing +Net::HTTP+ directly help us avoid expensive
  # options processing done in +Net::HTTP.start+:
  # debug cloudwise upload request
  # 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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 65

def post(env)
  post = nil

  if env.form.nil? || env.form.empty?
    post = ::Net::HTTP::Post.new(net_http_path_from_env(env), 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

  # Debug log: response result
  if defined?(Datadog.logger)
    Datadog.logger.debug do
      response_body = http_response.body.to_s
      # 截断过长的响应体
      truncated_body = (response_body.length > 500) ? "#{response_body[0..500]}..." : response_body
      "[Trace Transport] Flushing trace response: POST.uri =#{net_http_path_from_env(env)}, body=#{truncated_body}"
    end
  end

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

#urlObject



98
99
100
# File 'lib/datadog/core/transport/http/adapters/net.rb', line 98

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