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_(segment) ⇒ Object

Will be called in other threads.

Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aws/xray/client.rb', line 29

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

  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_ 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
# File 'lib/aws/xray/client.rb', line 10

def send_segment(segment)
  begin
    if Aws::Xray.config.client_options[:sock] # test env or not aws-xray is not enabled
      send_(segment)
    else # production env
      Worker.post(segment)
    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, '', 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