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

#stats, #update_stats_from_response!

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



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

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

#change_api!(api_id) ⇒ Object

Raises:



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

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

#current_apiObject



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

def current_api
  apis[current_api_id]
end

#downgrade!Object

Raises:



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

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:



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

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
54
# 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
  stats.internal_error += 1
  stats.consecutive_errors += 1

  InternalErrorResponse.new(e)
end