class Datadog::Transport::Traces::Transport

Sends traces based on transport API configuration.

This class initializes the HTTP client, breaks down large batches of traces into smaller chunks and handles API version downgrade handshake.

Attributes

apis[R]
client[R]
current_api_id[R]
default_api[R]

Public Class Methods

new(apis, default_api) click to toggle source
# File lib/ddtrace/transport/traces.rb, line 107
def initialize(apis, default_api)
  @apis = apis
  @default_api = default_api

  change_api!(default_api)
end

Public Instance Methods

current_api() click to toggle source
# File lib/ddtrace/transport/traces.rb, line 152
def current_api
  apis[@current_api_id]
end
send_traces(traces) click to toggle source
# File lib/ddtrace/transport/traces.rb, line 114
def send_traces(traces)
  encoder = current_api.encoder
  chunker = Datadog::Transport::Traces::Chunker.new(encoder)

  responses = chunker.encode_in_chunks(traces.lazy).map do |encoded_traces, trace_count|
    request = Request.new(EncodedParcel.new(encoded_traces, trace_count))

    client.send_payload(request).tap do |response|
      if downgrade?(response)
        downgrade!
        return send_traces(traces)
      end
    end
  end

  # Force resolution of lazy enumerator.
  #
  # The "correct" method to call here would be `#force`,
  # as this method was created to force the eager loading
  # of a lazy enumerator.
  #
  # Unfortunately, JRuby < 9.2.9.0 erroneously eagerly loads
  # the lazy Enumerator during intermediate steps.
  # This forces us to use `#to_a`, as this method works for both
  # lazy and regular Enumerators.
  # Using `#to_a` can mask the fact that we expect a lazy
  # Enumerator.
  responses = responses.to_a

  Datadog.health_metrics.transport_chunked(responses.size)

  responses
end
stats() click to toggle source
# File lib/ddtrace/transport/traces.rb, line 148
def stats
  @client.stats
end

Private Instance Methods

change_api!(api_id) click to toggle source
# File lib/ddtrace/transport/traces.rb, line 171
def change_api!(api_id)
  raise UnknownApiVersionError, api_id unless apis.key?(api_id)

  @current_api_id = api_id
  @client = HTTP::Client.new(current_api)
end
downgrade!() click to toggle source
# File lib/ddtrace/transport/traces.rb, line 164
def downgrade!
  downgrade_api_id = apis.fallbacks[@current_api_id]
  raise NoDowngradeAvailableError, @current_api_id if downgrade_api_id.nil?

  change_api!(downgrade_api_id)
end
downgrade?(response) click to toggle source
# File lib/ddtrace/transport/traces.rb, line 158
def downgrade?(response)
  return false unless apis.fallbacks.key?(@current_api_id)

  response.not_found? || response.unsupported?
end