Class: SignalFxClient
- Inherits:
-
Object
- Object
- SignalFxClient
- Defined in:
- lib/signalfx/signal_fx_client.rb
Direct Known Subclasses
Constant Summary collapse
- HEADER_API_TOKEN_KEY =
'X-SF-Token'- HEADER_USER_AGENT_KEY =
'User-Agent'- HEADER_CONTENT_TYPE =
'Content-Type'- INGEST_ENDPOINT_SUFFIX =
'v2/datapoint'- EVENT_ENDPOINT_SUFFIX =
'v2/event'- EVENT_CATEGORIES =
{ USER_DEFINED: 'USER_DEFINED', ALERT: 'ALERT', AUDIT: 'AUDIT', JOB: 'JOB', COLLECTD: 'COLLECTD', SERVICE_DISCOVERY: 'SERVICE_DISCOVERY', EXCEPTION: 'EXCEPTION' }
Instance Method Summary collapse
-
#initialize(api_token, enable_aws_unique_id: false, ingest_endpoint: RbConfig::DEFAULT_INGEST_ENDPOINT, timeout: RbConfig::DEFAULT_TIMEOUT, batch_size: RbConfig::DEFAULT_BATCH_SIZE, user_agents: []) ⇒ SignalFxClient
constructor
A new instance of SignalFxClient.
-
#send(cumulative_counters: nil, gauges: nil, counters: nil) ⇒ Object
Send the given metrics to SignalFx synchronously.
-
#send_async(cumulative_counters: nil, gauges: nil, counters: nil) ⇒ Object
Send the given metrics to SignalFx asynchronously.
-
#send_event(event_type, event_category: , dimensions: {}, properties: {}, timestamp: (Time.now.to_i * 1000).to_i) ⇒ Object
Send an event to SignalFx.
Constructor Details
#initialize(api_token, enable_aws_unique_id: false, ingest_endpoint: RbConfig::DEFAULT_INGEST_ENDPOINT, timeout: RbConfig::DEFAULT_TIMEOUT, batch_size: RbConfig::DEFAULT_BATCH_SIZE, user_agents: []) ⇒ SignalFxClient
Returns a new instance of SignalFxClient.
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 55 56 57 58 59 |
# File 'lib/signalfx/signal_fx_client.rb', line 30 def initialize(api_token, enable_aws_unique_id: false, ingest_endpoint: RbConfig::DEFAULT_INGEST_ENDPOINT, timeout: RbConfig::DEFAULT_TIMEOUT, batch_size: RbConfig::DEFAULT_BATCH_SIZE, user_agents: []) @api_token = api_token @ingest_endpoint = ingest_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'] puts("AWS Unique ID loaded: #{@aws_unique_id}") else puts('Failed to retrieve AWS unique ID.') end } end end |
Instance Method Details
#send(cumulative_counters: nil, gauges: nil, counters: nil) ⇒ Object
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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/signalfx/signal_fx_client.rb', line 69 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) ⇒ Object
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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/signalfx/signal_fx_client.rb', line 95 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, event_category: , dimensions: {}, properties: {}, timestamp: (Time.now.to_i * 1000).to_i) ⇒ Object
Send an event to SignalFx.
Args:
event_type (string): the event type (name of the event time series).
event_category (string): the category of event. Choose one from EVENT_CATEGORIES list
dimensions (dict): a map of event dimensions.
properties (dict): a map of extra properties on that event.
(int64): a , by default is current time
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/signalfx/signal_fx_client.rb', line 134 def send_event(event_type, event_category: EVENT_CATEGORIES[:USER_DEFINED], dimensions: {}, properties: {}, timestamp: (Time.now.to_i * 1000).to_i) if event_type.blank? raise 'Type of event should not be empty!' end event_cat = event_category if event_category.blank? event_cat = EVENT_CATEGORIES[:USER_DEFINED] end if !event_cat.blank? and !EVENT_CATEGORIES.has_value?(event_cat) raise 'Unsupported event category: ' + event_cat end data = { category: event_cat, eventType: event_type, dimensions: dimensions, properties: properties, timestamp: } if @aws_unique_id data[:dimensions][RbConfig::AWS_UNIQUE_ID_DIMENSION_NAME] = @aws_unique_id end post(build_event(data), @ingest_endpoint, EVENT_ENDPOINT_SUFFIX) end |