Class: Datadog::Transport::HTTP::Client

Inherits:
Object
  • Object
show all
Includes:
Statistics
Defined in:
lib/ddtrace/transport/http/client.rb

Overview

Routes, encodes, and sends tracer data to the trace agent via HTTP.

Defined Under Namespace

Classes: NoDowngradeAvailableError, UnknownApiVersionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Statistics

included

Constructor Details

#initialize(apis, current_api_id) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
# File 'lib/ddtrace/transport/http/client.rb', line 15

def initialize(apis, current_api_id)
  @apis = apis

  # Activate initial API
  change_api!(current_api_id)
end

Instance Attribute Details

#apisObject (readonly)

Returns the value of attribute apis.



11
12
13
# File 'lib/ddtrace/transport/http/client.rb', line 11

def apis
  @apis
end

#current_api_idObject (readonly)

Returns the value of attribute current_api_id.



11
12
13
# File 'lib/ddtrace/transport/http/client.rb', line 11

def current_api_id
  @current_api_id
end

Instance Method Details

#build_env(request) ⇒ Object



55
56
57
# File 'lib/ddtrace/transport/http/client.rb', line 55

def build_env(request)
  Env.new(request)
end

#change_api!(api_id) ⇒ Object



68
69
70
71
# File 'lib/ddtrace/transport/http/client.rb', line 68

def change_api!(api_id)
  raise UnknownApiVersionError, api_id unless apis.key?(api_id)
  @current_api_id = api_id
end

#current_apiObject



64
65
66
# File 'lib/ddtrace/transport/http/client.rb', line 64

def current_api
  apis[current_api_id]
end

#downgrade!Object



73
74
75
76
77
# File 'lib/ddtrace/transport/http/client.rb', line 73

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) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/ddtrace/transport/http/client.rb', line 59

def downgrade?(response)
  return false unless apis.fallbacks.key?(current_api_id)
  response.not_found? || response.unsupported?
end

#send_request(request, &block) ⇒ Object



22
23
24
25
26
27
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
# File 'lib/ddtrace/transport/http/client.rb', line 22

def send_request(request, &block)
  # Build request into env
  env = build_env(request)

  # Get response from API
  response = yield(current_api, env)

  # Update statistics
  update_stats_from_response!(response)

  # If API should be downgraded, downgrade and try again.
  if downgrade?(response)
    downgrade!
    response = send_request(request, &block)
  end

  response
rescue StandardError => e
  message = "Internal error during HTTP transport request. Cause: #{e.message} Location: #{e.backtrace.first}"

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

  # Update statistics
  update_stats_from_exception!(e)

  InternalErrorResponse.new(e)
end