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'
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

Constructor Details

#initialize(api_token, enable_aws_unique_id: false, ingest_endpoint: RbConfig::DEFAULT_INGEST_ENDPOINT, stream_endpoint: RbConfig::DEFAULT_STREAM_ENDPOINT, timeout: RbConfig::DEFAULT_TIMEOUT, batch_size: RbConfig::DEFAULT_BATCH_SIZE, user_agents: [], logger: Logger.new(STDOUT, progname: "signalfx")) ⇒ SignalFxClient

Returns a new instance of SignalFxClient.



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
60
61
62
63
64
# File 'lib/signalfx/signal_fx_client.rb', line 31

def initialize(api_token,
               enable_aws_unique_id: false,
               ingest_endpoint: RbConfig::DEFAULT_INGEST_ENDPOINT,
               stream_endpoint: RbConfig::DEFAULT_STREAM_ENDPOINT,
               timeout: RbConfig::DEFAULT_TIMEOUT,
               batch_size: RbConfig::DEFAULT_BATCH_SIZE,
               user_agents: [],
               logger: Logger.new(STDOUT, progname: "signalfx"))

  @api_token = api_token
  @ingest_endpoint = ingest_endpoint
  @stream_endpoint = stream_endpoint
  @timeout = timeout
  @batch_size = batch_size
  @user_agents = user_agents
  @logger = logger

  @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']
        @logger.info("AWS Unique ID loaded: #{@aws_unique_id}")
      else
        @logger.warn('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.


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/signalfx/signal_fx_client.rb', line 74

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.


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/signalfx/signal_fx_client.rb', line 100

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.
timestamp (int64): a timestamp, by default is current time


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/signalfx/signal_fx_client.rb', line 139

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: 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

#signalflowSignalFlowClient

Create a new SignalFlow client. A single client can execute multiple computations that will be multiplexed over the same WebSocket connection.

Returns:

  • (SignalFlowClient)

    a newly instantiated client, configured with the api token and endpoints from this class



174
175
176
# File 'lib/signalfx/signal_fx_client.rb', line 174

def signalflow
  SignalFlowClient.new(@api_token, @stream_endpoint)
end