Class: SignalFxClient

Inherits:
Object
  • Object
show all
Defined in:
lib/signalfx/signal_fx_client.rb

Direct Known Subclasses

JsonSignalFx, ProtoBufSignalFx

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'
API_ENDPOINT_SUFFIX =
'v1/event'

Instance Method Summary collapse

Constructor Details

#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: []) ⇒ SignalFxClient

Returns a new instance of SignalFxClient.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

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.


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ 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.


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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: {}) ⇒ Object

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.


118
119
120
121
122
123
124
125
126
127
128
129
130
# 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