Skip to content

Commit

Permalink
Updated client library to use ingest v2/event end point for all events
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremetat committed Feb 2, 2016
1 parent 5982f0c commit eaef4c7
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
signalfx (0.1.0)
signalfx (1.0.0)
protobuf (~> 3.5.1, >= 3.5.1)
rest-client (~> 1.8)

Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ See `examples/generic_usecase.py` for a complete code example for Reporting data

Events can be sent to SignalFx via the `send_event` function. The
event type must be specified, and dimensions and extra event properties
can be supplied as well.

can be supplied as well. Also please specify event category: for that get
option from dictionary `EVENT_CATEGORIES`. Different categories of events are supported.
Available categories of events are `USER_DEFINED`, `ALERT`, `AUDIT`, `JOB`,
`COLLECTD`, `SERVICE_DISCOVERY`, `EXCEPTION`.

```ruby
require('signalfx')
Expand All @@ -123,13 +125,15 @@ client = SignalFx.new 'MY_SIGNALFX_TOKEN'

client.send_event(
'[event_type]',
'[event_category]',
{
host: 'myhost',
service: 'myservice',
instance: 'myinstance'
},
properties={
version: 'event_version'})
version: 'event_version'},
timestamp)
```

See `examples/generic_usecase.py` for a complete code example for Reporting data.
Expand Down
4 changes: 2 additions & 2 deletions examples/generic_usecase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
instance: 'myinstance'}
properties = {version: version}


client.send_event(event_type, dimensions: dimensions, properties: properties)
event_category = SignalFxClient::EVENT_CATEGORIES[:ALERT]
client.send_event(event_type, event_category: event_category, dimensions: dimensions, properties: properties)
end

counter +=1
Expand Down
38 changes: 38 additions & 0 deletions lib/proto/signal_fx_protocol_buffers.pb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class MetricType < ::Protobuf::Enum
define :CUMULATIVE_COUNTER, 3
end

class EventCategory < ::Protobuf::Enum
define :USER_DEFINED, 1000000
define :ALERT, 100000
define :AUDIT, 200000
define :JOB, 300000
define :COLLECTD, 400000
define :SERVICE_DISCOVERY, 500000
define :EXCEPTION, 700000
end


##
# Message Classes
Expand All @@ -29,6 +39,10 @@ class Dimension < ::Protobuf::Message; end
class DataPoint < ::Protobuf::Message; end
class DataPointUploadMessage < ::Protobuf::Message; end
class PointValue < ::Protobuf::Message; end
class Property < ::Protobuf::Message; end
class PropertyValue < ::Protobuf::Message; end
class Event < ::Protobuf::Message; end
class EventUploadMessage < ::Protobuf::Message; end


##
Expand Down Expand Up @@ -63,6 +77,30 @@ class PointValue
optional ::Com::Signalfx::Metrics::Protobuf::Datum, :value, 4
end

class Property
optional :string, :key, 1
optional ::Com::Signalfx::Metrics::Protobuf::PropertyValue, :value, 2
end

class PropertyValue
optional :string, :strValue, 1
optional :double, :doubleValue, 2
optional :int64, :intValue, 3
optional :bool, :boolValue, 4
end

class Event
required :string, :eventType, 1
repeated ::Com::Signalfx::Metrics::Protobuf::Dimension, :dimensions, 2
repeated ::Com::Signalfx::Metrics::Protobuf::Property, :properties, 3
optional ::Com::Signalfx::Metrics::Protobuf::EventCategory, :category, 4
optional :int64, :timestamp, 5
end

class EventUploadMessage
repeated ::Com::Signalfx::Metrics::Protobuf::Event, :events, 1
end

end

end
Expand Down
6 changes: 3 additions & 3 deletions lib/signalfx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ module SignalFx
# @param batch_size - number
# @param user_agents - array
def self.new(api_token, enable_aws_unique_id: false, ingest_endpoint: Config::DEFAULT_INGEST_ENDPOINT,
api_endpoint: Config::DEFAULT_API_ENDPOINT, timeout: Config::DEFAULT_TIMEOUT,
timeout: Config::DEFAULT_TIMEOUT,
batch_size: Config::DEFAULT_BATCH_SIZE, user_agents: [])
begin
require_relative './proto/signal_fx_protocol_buffers.pb'
ProtoBufSignalFx.new(api_token, enable_aws_unique_id: enable_aws_unique_id, ingest_endpoint: ingest_endpoint,
api_endpoint: api_endpoint, timeout: timeout,
timeout: timeout,
batch_size: batch_size, user_agents: user_agents)

rescue Exception => e
puts "Protocol Buffers not installed properly. Switch to JSON.
#{e}"
JsonSignalFx.new(api_token, enable_aws_unique_id: enable_aws_unique_id, ingest_endpoint: ingest_endpoint,
api_endpoint: api_endpoint, timeout: timeout,
timeout: timeout,
batch_size: batch_size, user_agents: user_agents)
end

Expand Down
1 change: 0 additions & 1 deletion lib/signalfx/conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Config
# Default Parameters
DEFAULT_INGEST_ENDPOINT = 'https://ingest.signalfx.com'
DEFAULT_API_ENDPOINT = 'https://api.signalfx.com'
DEFAULT_BATCH_SIZE = 300 # Will wait for this many requests before posting
DEFAULT_TIMEOUT = 1

Expand Down
6 changes: 6 additions & 0 deletions lib/signalfx/json_signal_fx_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ def batch_data(data_point_list)

data.to_json
end

def build_event(event)
event_list = []
event_list << event
event_list.to_json
end
end
51 changes: 51 additions & 0 deletions lib/signalfx/protobuf_signal_fx_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,55 @@ def batch_data(data_point_list)
data_point_list.each { |datapoint| dpum.datapoints << datapoint }
dpum.to_s
end

def build_event(event)
protobuf_event = Com::Signalfx::Metrics::Protobuf::Event.new
if event[:eventType]
protobuf_event.eventType = event[:eventType]
end

if event[:category]
protobuf_event.category = Com::Signalfx::Metrics::Protobuf::EventCategory.const_get(event[:category].upcase)
end

if event[:timestamp]
protobuf_event.timestamp = event[:timestamp];
end

#set datapoint dimensions
dimensions = Array.new
if event[:dimensions] != nil
event[:dimensions].each {
|key, value| dimensions.push(
Com::Signalfx::Metrics::Protobuf::Dimension.new :key => key, :value => value)
}
end
protobuf_event.dimensions = dimensions

# assign value type
protobuf_event.properties = []
event[:properties].each { |prop_key, prop_value |
property = Com::Signalfx::Metrics::Protobuf::Property.new
property.key = prop_key
if prop_value.kind_of?(String)
property.value = Com::Signalfx::Metrics::Protobuf::PropertyValue.new :strValue => prop_value
else
if prop_value.kind_of?(Float)
property.value = Com::Signalfx::Metrics::Protobuf::PropertyValue.new :doubleValue => prop_value
else
if prop_value.kind_of?(Fixnum)
property.value = Com::Signalfx::Metrics::Protobuf::PropertyValue.new :intValue => prop_value
else
throw TypeError('Invalid Value ' + prop_value);
end
end
end
protobuf_event.properties << property
}

event_msg = Com::Signalfx::Metrics::Protobuf::EventUploadMessage.new
event_msg[:events] = Array.new
event_msg[:events] << protobuf_event
event_msg.to_s
end
end
53 changes: 42 additions & 11 deletions lib/signalfx/signal_fx_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@ class SignalFxClient
HEADER_USER_AGENT_KEY = 'User-Agent'
HEADER_CONTENT_TYPE = 'Content-Type'
INGEST_ENDPOINT_SUFFIX = 'v2/datapoint'
API_ENDPOINT_SUFFIX = 'v1/event'
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'
}

$event_categories = EVENT_CATEGORIES

def initialize(api_token, enable_aws_unique_id: false, ingest_endpoint: Config::DEFAULT_INGEST_ENDPOINT,
api_endpoint: Config::DEFAULT_API_ENDPOINT, timeout: Config::DEFAULT_TIMEOUT,
timeout: Config::DEFAULT_TIMEOUT,
batch_size: 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
Expand Down Expand Up @@ -101,7 +112,7 @@ def send_async(cumulative_counters: nil, gauges: nil, counters: nil)
Thread.start {
begin

post(data_to_send, @ingest_endpoint, INGEST_ENDPOINT_SUFFIX){
post(data_to_send, @ingest_endpoint, INGEST_ENDPOINT_SUFFIX) {
@async_running = false
}
ensure
Expand All @@ -115,22 +126,38 @@ def send_async(cumulative_counters: nil, gauges: nil, counters: nil)
#
#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.
def send_event(event_type, dimensions: {}, properties: {})
# timestamp (int64): a timestamp, by default is current time
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
properties: properties,
timestamp: timestamp
}

if @aws_unique_id
data[:dimensions][Config::AWS_UNIQUE_ID_DIMENSION_NAME] = @aws_unique_id
end

puts(data)

post(data.to_json, @api_endpoint, API_ENDPOINT_SUFFIX, Config::JSON_HEADER_CONTENT_TYPE)
post(build_event(data), @ingest_endpoint, EVENT_ENDPOINT_SUFFIX)
end

protected
Expand All @@ -151,16 +178,20 @@ def batch_data(data_point_list)
raise 'Subclasses should implement this!'
end

def build_event(event)
raise 'Subclasses should implement this!'
end

private

def post(data_to_send, url, suffix, content_type = nil, &block)
def post(data_to_send, url, suffix, &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,
headers = {HEADER_CONTENT_TYPE => header_content_type,
HEADER_API_TOKEN_KEY => @api_token,
HEADER_USER_AGENT_KEY => Version::NAME + '/' + Version::VERSION + http_user_agents}

Expand Down
2 changes: 1 addition & 1 deletion lib/signalfx/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (C) 2015 SignalFx, Inc. All rights reserved.

module Version
VERSION = '0.1.0'
VERSION = '1.0.0'
NAME = 'signalfx-ruby-client'
end
Loading

0 comments on commit eaef4c7

Please sign in to comment.