Class: Datadog::Tracing::Transport::IO::Client

Inherits:
Object
  • Object
show all
Includes:
Traces::Client, Statistics
Defined in:
lib/datadog/tracing/transport/io/client.rb

Overview

Encodes and writes tracer data to IO

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traces::Client

#send_traces

Methods included from Statistics

#metrics_for_exception, #metrics_for_response, #stats, #update_stats_from_exception!, #update_stats_from_response!

Constructor Details

#initialize(out, encoder, options = {}) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
# File 'lib/datadog/tracing/transport/io/client.rb', line 18

def initialize(out, encoder, options = {})
  @out = out
  @encoder = encoder

  @request_block = options.fetch(:request, method(:send_default_request))
  @encode_block = options.fetch(:encode, method(:encode_data))
  @write_block = options.fetch(:write, method(:write_data))
  @response_block = options.fetch(:response, method(:build_response))
end

Instance Attribute Details

#encoderObject (readonly)

Returns the value of attribute encoder.



14
15
16
# File 'lib/datadog/tracing/transport/io/client.rb', line 14

def encoder
  @encoder
end

#outObject (readonly)

Returns the value of attribute out.



14
15
16
# File 'lib/datadog/tracing/transport/io/client.rb', line 14

def out
  @out
end

Instance Method Details

#build_response(_request, _data, result) ⇒ Object



69
70
71
# File 'lib/datadog/tracing/transport/io/client.rb', line 69

def build_response(_request, _data, result)
  IO::Response.new(result)
end

#encode_data(encoder, request) ⇒ Object



61
62
63
# File 'lib/datadog/tracing/transport/io/client.rb', line 61

def encode_data(encoder, request)
  request.parcel.encode_with(encoder)
end

#send_request(request) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/datadog/tracing/transport/io/client.rb', line 28

def send_request(request)
  # Write data to IO
  # If block is given, allow it to handle writing
  # Otherwise do a standard encode/write/response.
  response = if block_given?
               yield(out, request)
             else
               @request_block.call(out, request)
             end

  # Update statistics
  update_stats_from_response!(response)

  # Return response
  response
rescue StandardError => e
  message =
    "Internal error during IO transport request. Cause: #{e.class.name} #{e.message} " \
      "Location: #{Array(e.backtrace).first}"

  # Log error
  if stats.consecutive_errors > 0
    Datadog.logger.debug(message)
  else
    Datadog.logger.error(message)
  end

  # Update statistics
  update_stats_from_exception!(e)

  InternalErrorResponse.new(e)
end

#write_data(out, data) ⇒ Object



65
66
67
# File 'lib/datadog/tracing/transport/io/client.rb', line 65

def write_data(out, data)
  out.puts(data)
end