Class: Librato::Metrics::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/librato/metrics/connection.rb

Constant Summary collapse

DEFAULT_API_ENDPOINT =
'https://metrics-api.librato.com'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection



18
19
20
21
22
# File 'lib/librato/metrics/connection.rb', line 18

def initialize(options={})
  @client = options[:client]
  @api_endpoint = options[:api_endpoint]
  @adapter = options[:adapter]
end

Instance Method Details

#api_endpointObject

API endpoint that will be used for requests.



26
27
28
# File 'lib/librato/metrics/connection.rb', line 26

def api_endpoint
  @api_endpoint || DEFAULT_API_ENDPOINT
end

#transportObject

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/librato/metrics/connection.rb', line 30

def transport
  raise(NoClientProvided, "No client provided.") unless @client
  @transport ||= Faraday::Connection.new(
    :url => api_endpoint + "/v1/",
    :request => {:open_timeout => 20, :timeout => 30}) do |f|

    f.use Librato::Metrics::Middleware::RequestBody
    f.use Librato::Metrics::Middleware::Retry
    f.use Librato::Metrics::Middleware::CountRequests
    f.use Librato::Metrics::Middleware::ExpectsStatus

    f.adapter @adapter || Metrics.faraday_adapter
  end.tap do |transport|
    transport.headers[:user_agent] = user_agent
    transport.headers[:content_type] = 'application/json'
    transport.basic_auth @client.email, @client.api_key
  end
end

#user_agentObject

User-agent used when making requests.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/librato/metrics/connection.rb', line 51

def user_agent
  return @client.custom_user_agent if @client.custom_user_agent
  ua_chunks = []
  agent_identifier = @client.agent_identifier
  if agent_identifier && !agent_identifier.empty?
    ua_chunks << agent_identifier
  end
  ua_chunks << "librato-metrics/#{Metrics::VERSION}"
  ua_chunks << "(#{ruby_engine}; #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})"
  ua_chunks << "direct-faraday/#{Faraday::VERSION}"
  # TODO: include adapter information
  #ua_chunks << "(#{transport_adapter})"
  ua_chunks.join(' ')
end