Class: Aws::Xray::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/xray/client.rb

Overview

Own the responsibility of holding destination address and sending segments.

Class Method Summary collapse

Class Method Details

.send_payload(payload) ⇒ Object

Will be called in other threads.

Parameters:

  • payload (String)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aws/xray/client.rb', line 34

def send_payload(payload)
  Aws::Xray.config.logger.debug("#{Thread.current}: Client#send_payload")
  sock = Aws::Xray.config.client_options[:sock] || UDPSocket.new
  host, port = Aws::Xray.config.client_options[:host], Aws::Xray.config.client_options[:port]

  begin
    len = sock.send(payload, Socket::MSG_DONTWAIT, host, port)
    raise CanNotSendAllByteError.new(payload.bytesize, len) if payload.bytesize != len
    Aws::Xray.config.logger.debug("#{Thread.current}: Client#send_payload successfully sent payload, len=#{len}")
    len
  rescue SystemCallError, SocketError, CanNotSendAllByteError => e
    begin
      Aws::Xray.config.segment_sending_error_handler.call(e, payload, host: host, port: port)
    rescue Exception => e
      $stderr.puts("Error handler `#{Aws::Xray.config.segment_sending_error_handler}` raised an error: #{e}\n#{e.backtrace.join("\n")}")
    end
  ensure
    sock.close
  end
end

.send_segment(segment) ⇒ Object

Parameters:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/aws/xray/client.rb', line 10

def send_segment(segment)
  Aws::Xray.config.logger.debug("#{Thread.current}: Client.send_segment started")
  payload = %!{"format": "json", "version": 1}\n#{segment.to_json}\n!

  begin
    if Aws::Xray.config.client_options[:sock] # test env or not aws-xray is not enabled
      send_payload(payload)
      Aws::Xray.config.logger.debug("#{Thread.current}: Client.send_segment called #send_payload in the same thread")
    else # production env
      Worker.post(payload)
      Aws::Xray.config.logger.debug("#{Thread.current}: Client.send_segment posted a job to worker")
    end
  rescue QueueIsFullError => e
    begin
      host, port = Aws::Xray.config.client_options[:host], Aws::Xray.config.client_options[:port]
      Aws::Xray.config.segment_sending_error_handler.call(e, payload, host: host, port: port)
    rescue Exception => e
      $stderr.puts("Error handler `#{Aws::Xray.config.segment_sending_error_handler}` raised an error: #{e}\n#{e.backtrace.join("\n")}")
    end
  end
end