Class: Datadog::Transport::HTTP::Traces::API::Endpoint

Inherits:
API::Endpoint
  • Object
show all
Defined in:
lib/ddtrace/transport/http/traces.rb

Overview

Endpoint for submitting trace data

Constant Summary collapse

HEADER_CONTENT_TYPE =
'Content-Type'.freeze
HEADER_TRACE_COUNT =
'X-Datadog-Trace-Count'.freeze
SERVICE_RATE_KEY =
'rate_by_service'.freeze

Instance Attribute Summary collapse

Attributes inherited from API::Endpoint

#path, #verb

Instance Method Summary collapse

Constructor Details

#initialize(path, encoder, options = {}) ⇒ Endpoint

Returns a new instance of Endpoint.



95
96
97
98
99
# File 'lib/ddtrace/transport/http/traces.rb', line 95

def initialize(path, encoder, options = {})
  super(:post, path)
  @encoder = encoder
  @service_rates = options.fetch(:service_rates, false)
end

Instance Attribute Details

#encoderObject (readonly)

Returns the value of attribute encoder.



92
93
94
# File 'lib/ddtrace/transport/http/traces.rb', line 92

def encoder
  @encoder
end

Instance Method Details

#call(env, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ddtrace/transport/http/traces.rb', line 105

def call(env, &block)
  # Add trace count header
  env.headers[HEADER_TRACE_COUNT] = env.request.parcel.count.to_s

  # Encode body & type
  env.headers[HEADER_CONTENT_TYPE] = encoder.content_type
  env.body = env.request.parcel.encode_with(encoder)

  # Query for response
  http_response = super(env, &block)

  # Process the response
  response_options = {}.tap do |options|
    # Parse service rates, if configured to do so.
    if service_rates? && !http_response.payload.to_s.empty?
      body = JSON.parse(http_response.payload)
      if body.is_a?(Hash) && body.key?(SERVICE_RATE_KEY)
        options[:service_rates] = body[SERVICE_RATE_KEY]
      end
    end
  end

  # Build and return a trace response
  Traces::Response.new(http_response, response_options)
end

#service_rates?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/ddtrace/transport/http/traces.rb', line 101

def service_rates?
  @service_rates == true
end