Module: Fiveruns::Dash::Store::HTTP

Included in:
Update
Defined in:
lib/fiveruns/dash/store/http.rb

Defined Under Namespace

Classes: Multipart

Instance Method Summary collapse

Instance Method Details

#add_path_to(uri) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fiveruns/dash/store/http.rb', line 94

def add_path_to(uri)
  new_uri = uri.dup
  path = case payload
  when Fiveruns::Dash::PingPayload
    ::File.join('/apps', app_token, "ping")
  when Fiveruns::Dash::InfoPayload
    ::File.join('/apps', app_token, "processes.json")
  when Fiveruns::Dash::DataPayload
    ::File.join('/apps', app_token, "metrics.json")
  when Fiveruns::Dash::TracePayload
    ::File.join('/apps', app_token, "traces.json")
  when Fiveruns::Dash::ExceptionsPayload
    ::File.join('/apps', app_token, "exceptions.json")
  else
    raise ArgumentError, 'Unknown payload type: #{payload.class}'
  end
  new_uri.path = path
  new_uri
end

#app_tokenObject



127
128
129
# File 'lib/fiveruns/dash/store/http.rb', line 127

def app_token
  ::Fiveruns::Dash.configuration.options[:app]
end

#check_response_of(response) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fiveruns/dash/store/http.rb', line 64

def check_response_of(response)
  unless response
    Fiveruns::Dash.logger.debug "Received no response from Dash service"
    return false
  end
  case response.code.to_i
  when 201
    # data = Fiveruns::JSON.load(response.body)
    # set_trace_contexts(data)
    true
  when 400..499
    Fiveruns::Dash.logger.warn "Could not access Dash service (#{response.code.to_i}, #{response.body.inspect})"
    false
  else
    Fiveruns::Dash.logger.debug "Received unknown response from Dash service (#{response.inspect})"
    false
  end
rescue Fiveruns::JSON::ParserError => e
  puts response.body
  Fiveruns::Dash.logger.error "Received non-FiverunsJSON response (#{response.inspect})"
  false
end

#extra_params_for(payload) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/fiveruns/dash/store/http.rb', line 114

def extra_params_for(payload)
  case payload
  when Fiveruns::Dash::ExceptionsPayload
    {:app_id => app_token}
  else
    Hash.new
  end
end

#normalize_key(key) ⇒ Object



123
124
125
# File 'lib/fiveruns/dash/store/http.rb', line 123

def normalize_key(key)
  key.to_a.flatten.map { |k| k.to_s }.sort
end

#resolved_hostname(hostname) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/fiveruns/dash/store/http.rb', line 18

def resolved_hostname(hostname)
  if resolved_hostnames[hostname] && Time.now < resolved_hostnames[hostname].next_update
    ip = resolved_hostnames[hostname].ip
  else
    ip = hostname == 'localhost' ? '127.0.0.1' : IPSocket.getaddress(hostname)
    ip_struct = OpenStruct.new(:ip => ip, :next_update => Time.now + (23 * 60 * 60) + (rand(60) * 60))
    resolved_hostnames[hostname] = ip_struct
  end
  ip
end

#resolved_hostnamesObject



13
14
15
16
# File 'lib/fiveruns/dash/store/http.rb', line 13

def resolved_hostnames
  Thread.current[:resolved_hostnames] ||= {}
  Thread.current[:resolved_hostnames]
end

#safelyObject



56
57
58
59
60
61
62
# File 'lib/fiveruns/dash/store/http.rb', line 56

def safely
  yield
rescue Exception => e
  Fiveruns::Dash.logger.error "Could not access Dash service: #{e.message}"
  Fiveruns::Dash.logger.error e.backtrace.join("\n\t")
  false
end

#set_trace_contexts(data) ⇒ Object



87
88
89
90
91
92
# File 'lib/fiveruns/dash/store/http.rb', line 87

def set_trace_contexts(data)
  trace_contexts = data['traces']
  if trace_contexts.is_a?(Array)
    Fiveruns::Dash.trace_contexts = trace_contexts
  end
end

#store_http(*uris) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/fiveruns/dash/store/http.rb', line 30

def store_http(*uris)
  Fiveruns::Dash.logger.info "Attempting to send #{payload.class}"
  if (uri = uris.detect { |u| transmit_to(add_path_to(u)) })
    Fiveruns::Dash.logger.info "Sent #{payload.class} to #{uri}"
    uri
  else
    Fiveruns::Dash.logger.warn "Could not send #{payload.class}"
    false
  end
end

#transmit_to(uri) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fiveruns/dash/store/http.rb', line 41

def transmit_to(uri)
  response = nil
  safely do
    http = Net::HTTP.new(resolved_hostname(uri.host), uri.port)
    http.use_ssl = true if uri.scheme == 'https'
    http.open_timeout = 10
    http.read_timeout = 10
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    extra_params = extra_params_for(payload)
    multipart = Multipart.new(payload.io, payload.params.merge(extra_params))
    response = http.post(uri.request_uri, multipart.to_s, "Content-Type" => multipart.content_type, "Host" => uri.host) 
  end
  check_response_of response
end