class SignalFxClient
Constants
- API_ENDPOINT_SUFFIX
- HEADER_API_TOKEN_KEY
- HEADER_CONTENT_TYPE
- HEADER_USER_AGENT_KEY
- INGEST_ENDPOINT_SUFFIX
Public Class Methods
new(api_token, enable_aws_unique_id = false, ingest_endpoint = SignalFX::Config::DEFAULT_INGEST_ENDPOINT, api_endpoint = SignalFX::Config::DEFAULT_API_ENDPOINT, timeout = SignalFX::Config::DEFAULT_TIMEOUT, batch_size = SignalFX::Config::DEFAULT_BATCH_SIZE, user_agents = [])
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 18 def initialize(api_token, enable_aws_unique_id = false, ingest_endpoint = SignalFX::Config::DEFAULT_INGEST_ENDPOINT, api_endpoint = SignalFX::Config::DEFAULT_API_ENDPOINT, timeout = SignalFX::Config::DEFAULT_TIMEOUT, batch_size = SignalFX::Config::DEFAULT_BATCH_SIZE, user_agents = []) @api_token = api_token @ingest_endpoint = ingest_endpoint @api_endpoint = api_endpoint @timeout = timeout @batch_size = batch_size @user_agents = user_agents @aws_unique_id = nil @queue = Queue.new @async_running = false if enable_aws_unique_id retrieve_aws_unique_id { |request| if request != nil json_resp = JSON.parse(request.body) @aws_unique_id = json_resp['instanceId']+'_'+json_resp['region']+'_'+json_resp['accountId'] else puts('Failed to retrieve AWS unique ID.') end } end end
Public Instance Methods
send(cumulative_counters = nil, gauges = nil, counters = nil)
click to toggle source
Send the given metrics to SignalFx
synchronously. You can use this method to send data via reporters such as Codahale style libraries
Args:
cumulative_counters (list): a list of dictionaries representing the cumulative counters to report. gauges (list): a list of dictionaries representing the gauges to report. counters (list): a list of dictionaries representing the counters to report.
# File lib/signalfx/signal_fx_client.rb, line 54 def send(cumulative_counters = nil, gauges = nil, counters = nil) process_datapoint('cumulative_counter', cumulative_counters) process_datapoint('gauge', gauges) process_datapoint('counter', counters) data_points_list = [] while @queue.length > 0 && data_points_list.length < @batch_size data_points_list << @queue.shift end data_to_send = batch_data(data_points_list) begin post(data_to_send, @ingest_endpoint, INGEST_ENDPOINT_SUFFIX) ensure @async_running = false end end
send_async(cumulative_counters = nil, gauges = nil, counters = nil)
click to toggle source
Send the given metrics to SignalFx
asynchronously.
Args:
cumulative_counters (list): a list of dictionaries representing the cumulative counters to report. gauges (list): a list of dictionaries representing the gauges to report. counters (list): a list of dictionaries representing the counters to report.
# File lib/signalfx/signal_fx_client.rb, line 80 def send_async(cumulative_counters = nil, gauges = nil, counters = nil) process_datapoint('cumulative_counter', cumulative_counters) process_datapoint('gauge', gauges) process_datapoint('counter', counters) if @async_running return end data_points_list = [] while @queue.length > 0 && data_points_list.length < @batch_size data_points_list << @queue.shift end data_to_send = batch_data(data_points_list) @async_running = true Thread.abort_on_exception = true Thread.start { begin post(data_to_send, @ingest_endpoint, INGEST_ENDPOINT_SUFFIX){ @async_running = false } ensure @async_running = false end } end
send_event(event_type, dimensions = {}, properties = {})
click to toggle source
Send an event to SignalFx
.
Args:
event_type (string): the event type (name of the event time series). dimensions (dict): a map of event dimensions. properties (dict): a map of extra properties on that event.
# File lib/signalfx/signal_fx_client.rb, line 118 def send_event(event_type, dimensions = {}, properties = {}) data = { eventType: event_type, dimensions: dimensions, properties: properties } if @aws_unique_id data[:dimensions][SignalFX::Config::AWS_UNIQUE_ID_DIMENSION_NAME] = @aws_unique_id end post(data.to_json, @api_endpoint, API_ENDPOINT_SUFFIX, SignalFX::Config::JSON_HEADER_CONTENT_TYPE) end
Protected Instance Methods
add_to_queue(metric_type, datapoint)
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 142 def add_to_queue(metric_type, datapoint) raise 'Subclasses should implement this!' end
batch_data(data_point_list)
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 146 def batch_data(data_point_list) raise 'Subclasses should implement this!' end
get_queue()
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 134 def get_queue @queue end
header_content_type()
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 138 def header_content_type raise 'Subclasses should implement this!' end
Private Instance Methods
post(data_to_send, url, suffix, content_type = nil, &block)
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 152 def post(data_to_send, url, suffix, content_type = nil, &block) begin http_user_agents = '' if @user_agents != nil && @user_agents.length > 0 http_user_agents = ', ' + @user_agents.join(', ') end headers = {HEADER_CONTENT_TYPE => content_type != nil ? content_type : header_content_type, HEADER_API_TOKEN_KEY => @api_token, HEADER_USER_AGENT_KEY => Version::NAME + '/' + Version::VERSION + http_user_agents} RestClient::Request.execute( method: :post, url: url + '/' + suffix, headers: headers, payload: data_to_send, verify_ssl: OpenSSL::SSL::VERIFY_NONE, timeout: @timeout) { |response| case response.code when 200 if block block.call(response) end else puts "Failed to send datapoints. Response code: #{response.code}" if block block.call(nil) end end } rescue Exception => e puts "Failed to send datapoints. Error: #{e}" if block block.call(nil) end end end
process_datapoint(metric_type, data_points)
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 208 def process_datapoint(metric_type, data_points) if data_points != nil && data_points.kind_of?(Array) data_points.each { |datapoint| if @aws_unique_id if datapoint[:dimensions] == nil datapoint[:dimensions] = [] end datapoint[:dimensions] << {:key => SignalFX::Config::AWS_UNIQUE_ID_DIMENSION_NAME, :value => @aws_unique_id} end add_to_queue(metric_type, datapoint) } end end
retrieve_aws_unique_id(&block)
click to toggle source
# File lib/signalfx/signal_fx_client.rb, line 190 def retrieve_aws_unique_id(&block) begin RestClient::Request.execute(method: :get, url: SignalFX::Config::AWS_UNIQUE_ID_URL, timeout: 1) { |response| case response.code when 200 return block.call(response) else puts "Failed to retrieve AWS unique ID. Response code: #{response.code}" return block.call(nil) end } rescue Exception => e puts "Failed to retrieve AWS unique ID. Error: #{e}" block.call(nil) end end