Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
millar committed Jul 10, 2019
0 parents commit 3d1234a
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: ruby

rvm:
- 2.3.7
- 2.4.4
- 2.5.1
- 2.6.3

gemfile:
- Gemfile

before_install:
- gem update bundler

script: bundle exec rake test
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in fluent-plugin-indicative.gemspec
gemspec
61 changes: 61 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
PATH
remote: .
specs:
fluent-plugin-indicative (0.1.0)
fluentd (>= 0.14.15, < 2)

GEM
remote: http://rubygems.org/
specs:
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
cool.io (1.5.4)
crack (0.4.3)
safe_yaml (~> 1.0.0)
dig_rb (1.0.1)
fluentd (1.6.0)
cool.io (>= 1.4.5, < 2.0.0)
dig_rb (~> 1.0.0)
http_parser.rb (>= 0.5.1, < 0.7.0)
msgpack (>= 0.7.0, < 2.0.0)
serverengine (>= 2.0.4, < 3.0.0)
sigdump (~> 0.2.2)
strptime (>= 0.2.2, < 1.0.0)
tzinfo (~> 1.0)
tzinfo-data (~> 1.0)
yajl-ruby (~> 1.0)
hashdiff (0.4.0)
http_parser.rb (0.6.0)
msgpack (1.3.0)
power_assert (1.1.4)
public_suffix (3.1.1)
rake (12.3.2)
safe_yaml (1.0.5)
serverengine (2.1.1)
sigdump (~> 0.2.2)
sigdump (0.2.4)
strptime (0.2.3)
test-unit (3.3.3)
power_assert
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
tzinfo-data (1.2019.2)
tzinfo (>= 1.0.0)
webmock (3.6.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
yajl-ruby (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
fluent-plugin-indicative!
rake
test-unit (>= 3.1.0)
webmock (>= 3.6.0, < 4)

BUNDLED WITH
1.16.1
7 changes: 7 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2019 Sam Millar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# fluent-plugin-indicative
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end

task :default => :test

22 changes: 22 additions & 0 deletions fluent-plugin-indicative.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = "fluent-plugin-indicative"
s.version = "0.1.0"
s.authors = ["Sam Millar"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/millar/fluent-plugin-indicative"
s.summary = %q{Fluentd output plugin to send events to Indicative}

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

# specify any dependencies here; for example:
s.add_development_dependency "rake"
s.add_development_dependency "test-unit", ">= 3.1.0"
s.add_development_dependency "webmock", ">= 3.6.0", "< 4"
s.add_runtime_dependency "fluentd", ">= 0.14.15", "< 2"
end
56 changes: 56 additions & 0 deletions lib/fluent/plugin/out_indicative.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'net/http'
require 'net/https'
require 'uri'

require 'fluent/plugin/output'

def flatten_hash(hash)
hash.each_with_object({}) do |(k, v), h|
if v.is_a? Hash
flatten_hash(v).map do |h_k, h_v|
h["#{k}.#{h_k}"] = h_v
end
else
h[k] = v
end
end
end


class Fluent::Plugin::IndicativeOutput < Fluent::Plugin::Output
Fluent::Plugin.register_output('indicative', self)

config_param :api_key, :string, secret: true
config_param :api_url, :string, default: 'https://api.indicative.com/service/event'
config_param :event_name_key, :string
config_param :event_time_key, :string
config_param :event_unique_id_keys, :array, value_type: :string

def process(tag, es)
es.each do |time, record|
send_event(record)
end
end

def send_event(data)
uri = URI.parse(@api_url)

headers = {'Content-Type' => 'application/json'}

unique_id_key = @event_unique_id_keys.find {|k| data[k]}

payload = {
apiKey: @api_key,
eventName: data[@event_name_key],
eventUniqueId: unique_id_key && data[unique_id_key],
properties: flatten_hash(data),
eventTime: data[@event_time_key]
}

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = payload.to_json
response = http.request(request)
end
end
11 changes: 11 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'bundler/setup'
require 'test/unit'

require 'webmock/test_unit'

$LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
$LOAD_PATH.unshift(__dir__)
require 'fluent/test'
require 'fluent/test/driver/output'

require 'fluent/plugin/out_indicative'
54 changes: 54 additions & 0 deletions test/plugin/test_out_indicative.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'helper'

class IndicativeOutputTest < Test::Unit::TestCase
def setup
Fluent::Test.setup
end

CONFIG = %[
api_key INDICATIVE_API_KEY
event_name_key event_name
event_time_key created_at
event_unique_id_keys user_id, session_id
]

def create_driver(conf=CONFIG)
Fluent::Test::Driver::Output.new(Fluent::Plugin::IndicativeOutput).configure(conf)
end

def test_configure
assert_raise(Fluent::ConfigError) {
d = create_driver('')
}
d = create_driver CONFIG
assert_equal 'INDICATIVE_API_KEY', d.instance.api_key
assert_equal 'event_name', d.instance.event_name_key
assert_equal 'created_at', d.instance.event_time_key
assert_equal ['user_id', 'session_id'], d.instance.event_unique_id_keys
end


def test_emit
d = create_driver(CONFIG)
stub_request(:any, d.instance.api_url)
d.run(default_tag: 'test') do
d.feed({'event_name' => 'screen_view', 'created_at' => '2015-01-01T00:00:00.000Z', 'session_id' => 'a3bd2', 'user_id' => nil, 'screen' => {'id' => 'index'}})
end
events = d.events
assert_equal 0, events.length
assert_requested :post, d.instance.api_url,
headers: {'Content-Type' => 'application/json'}, body: {
'apiKey' => 'INDICATIVE_API_KEY',
'eventName' => 'screen_view',
'eventUniqueId' => 'a3bd2',
'properties' => {
'event_name' => 'screen_view',
'created_at' => '2015-01-01T00:00:00.000Z',
'session_id' => 'a3bd2',
'user_id' => nil,
'screen.id' => 'index'
},
'eventTime' => '2015-01-01T00:00:00.000Z'
}.to_json, times: 1
end
end

0 comments on commit 3d1234a

Please sign in to comment.