instrumental_pubnub_example 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec2b173fb8e13dae02e158f14b75dbf50d3db55b
4
+ data.tar.gz: 4317fe833f317e275497a2aa5dacb9e57d8e29f3
5
+ SHA512:
6
+ metadata.gz: 57fef30bdb80e4407d52d4eb1c5c9734b04ef2a78dd63089fa61a12a1c59a6554890c63d680debb004f4c90de271af216838754a2ae193fb8221f856bd15980c
7
+ data.tar.gz: 2cb3f12086e19b7d54d5c7564910c07a60e89a9734abadd9fda841d68b529208c7ebdd7379b6732a7213688959530e59956a55434d440260f6bc5df414a61b78
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
35
+ *.log
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ instrumental_pubnub_example (0.0.1)
5
+ instrumental_agent (>= 0.12.7)
6
+ pubnub (>= 3.6.9)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ eventmachine (1.0.4)
12
+ instrumental_agent (0.12.7)
13
+ json (1.8.1)
14
+ net-http-persistent (2.9.4)
15
+ pubnub (3.6.9)
16
+ eventmachine (~> 1.0)
17
+ json (~> 1.8)
18
+ net-http-persistent (~> 2.9)
19
+ rake (10.4.2)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ instrumental_pubnub_example!
26
+ rake
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ An example Instrumental relay using data received via PubNub. Run via command line, `instrumental_pubnub_relay`.
2
+
3
+ Will receive messages from your PubNub channel and try to coerce them to Instrumental measurements. By default, each metric
4
+ will be prefixed by the hostname of the machine you are running the relay on. You can specify a different prefix via the `-p`
5
+ command.
6
+
7
+ Metrics will be suffixed with the index that they appear in if they are in a Hash or Array. That is to say, if your PubNub message looks like this:
8
+
9
+ ```
10
+ { "foo": 20.0, "baz": [10.0, 5.0] }
11
+ ```
12
+
13
+ the metrics sent will look like this:
14
+
15
+ ```
16
+ hostname.foo
17
+ hostname.baz.0
18
+ hostname.baz.1
19
+ ```
20
+
21
+ ## Command line usage
22
+
23
+ From help:
24
+ ```sh
25
+ Usage: instrumental_pubnub_relay [options]
26
+ -k, --pubnub-subscribe-key KEY Your PubNub subscribe key
27
+ -c, --pubnub-channel CHANNEL Your PubNub channel
28
+ -i API_KEY, Your Instrumental API key
29
+ --instrumental-api-key
30
+ -p, --key-prefix PREFIX String to prefix all received metrics with (default: my.machine)
31
+ -l, --log-level LEVEL Log level for output, valid values are: ["debug", "warn", "info", "fatal"]
32
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'instrumental/pub_nub/relay'
4
+ require 'logger'
5
+ require 'optparse'
6
+ require 'socket'
7
+
8
+ pubnub_subscribe = nil
9
+ pubnub_channel = nil
10
+ instrumental_key = nil
11
+ log_level = 'warn'
12
+ log_levels = %w{debug warn info fatal}
13
+ key_prefix = Socket.gethostname
14
+
15
+ OptionParser.new do |opts|
16
+ opts.on("-k", "--pubnub-subscribe-key KEY", "Your PubNub subscribe key") do |k|
17
+ pubnub_subscribe = k
18
+ end
19
+ opts.on("-c", "--pubnub-channel CHANNEL", "Your PubNub channel") do |c|
20
+ pubnub_channel = c
21
+ end
22
+ opts.on("-i", "--instrumental-api-key API_KEY", "Your Instrumental API key") do |i|
23
+ instrumental_key = i
24
+ end
25
+ opts.on("-p", "--key-prefix PREFIX", "String to prefix all received metrics with (default: #{key_prefix})") do |p|
26
+ key_prefix = p
27
+ end
28
+ opts.on("-l", "--log-level LEVEL", log_levels, "Log level for output, valid values are: #{log_levels.inspect}") do |l|
29
+ log_level = l
30
+ end
31
+ end.parse!
32
+
33
+ if pubnub_subscribe.to_s.strip.empty?
34
+ raise "You must provide a PubNub subscribe key"
35
+ end
36
+
37
+ if pubnub_channel.to_s.strip.empty?
38
+ raise "You must provide a PubNub channel name"
39
+ end
40
+
41
+ if instrumental_key.to_s.strip.empty?
42
+ raise "You must provide an Instrumental API key"
43
+ end
44
+
45
+ if key_prefix.to_s.strip.empty?
46
+ raise "You must provide a non-empty key prefix"
47
+ end
48
+
49
+ logger = Logger.new(STDOUT)
50
+ logger.level = Logger.const_get(log_level.upcase)
51
+ relay = Instrumental::PubNub::Relay.new(pubnub_subscribe,
52
+ pubnub_channel,
53
+ instrumental_key,
54
+ key_prefix,
55
+ logger)
56
+
57
+ trap('INT') do
58
+ relay.disconnect!
59
+ end
60
+
61
+ relay.run!
@@ -0,0 +1,19 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "instrumental/pub_nub/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "instrumental_pubnub_example"
6
+ s.version = Instrumental::PubNub::VERSION
7
+ s.authors = ["Christopher Zelenak"]
8
+ s.email = ["support@instrumentalapp.com"]
9
+ s.homepage = "http://github.com/expectedbehavior/instrumental_pubnub_example"
10
+ s.summary = %q{Relay PubNub data to instrumentalapp.com}
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+ s.add_dependency(%q<pubnub>, [">= 3.6.9"])
17
+ s.add_dependency(%q<instrumental_agent>, [">= 0.12.7"])
18
+ s.add_development_dependency(%q<rake>, [">= 0"])
19
+ end
@@ -0,0 +1,117 @@
1
+ require 'instrumental_agent'
2
+ require 'pubnub'
3
+
4
+ module Instrumental
5
+ module PubNub
6
+ class Relay
7
+
8
+ def initialize(pubnub_subscribe_key, pubnub_channel_name, instrumental_api_key, key_prefix, logger)
9
+ @logger = logger
10
+ @agent = Instrumental::Agent.new(instrumental_api_key)
11
+ @pubnub = Pubnub.new(:subscribe_key => pubnub_subscribe_key,
12
+ :error_callback => method(:on_error),
13
+ :connect_callback => method(:on_connect))
14
+ @channel = pubnub_channel_name
15
+ @prefix = key_prefix
16
+ @disconnecting = false
17
+ end
18
+
19
+ def run!
20
+ @pubnub.subscribe(:channel => @channel, :callback => method(:on_message))
21
+ block_for_messages
22
+ end
23
+
24
+ def disconnect!
25
+ @disconnecting = true
26
+ @pubnub.leave(:channel => @channel, :callback => method(:on_leave))
27
+ end
28
+
29
+ def on_leave(msg)
30
+ shutdown
31
+ end
32
+
33
+ def on_message(msg)
34
+ data = msg.message
35
+ submitted_metrics = case data
36
+ when Hash
37
+ data.flat_map do |k, v|
38
+ gauge_array([@prefix, k].join("."), Array(v))
39
+ end
40
+ when Array
41
+ gauge_array(@prefix, data)
42
+ else
43
+ if data.to_s.size > 0
44
+ gauge_metric(@prefix, data.to_s)
45
+ end
46
+ end
47
+ submitted_metrics = Array(submitted_metrics).compact
48
+ if submitted_metrics.size > 0
49
+ info "Sent #{submitted_metrics.size} metrics"
50
+ end
51
+ end
52
+
53
+ def gauge_array(name, val)
54
+ if val.size != 1
55
+ val.map.with_index do |measurement, i|
56
+ gauge_metric([name, i].join("."), measurement)
57
+ end
58
+ else
59
+ gauge_metric(name, val.first)
60
+ end
61
+ end
62
+
63
+ def gauge_metric(name, value)
64
+ case value
65
+ when Numeric
66
+ debug "Gauge metric %s with value %s" % [name.inspect, value.inspect]
67
+ @agent.gauge(name, value)
68
+ name
69
+ else
70
+ warn "Not measuring metric %s, not a numeric value (%s)" % [name.inspect, value.inspect]
71
+ nil
72
+ end
73
+ end
74
+
75
+ def on_error(msg)
76
+ error "%s (%s)" % [msg.message, msg.response.inspect]
77
+ if @disconnecting
78
+ shutdown
79
+ end
80
+ end
81
+
82
+ def on_connect(msg)
83
+ info "Connected"
84
+ end
85
+
86
+ private
87
+
88
+ %w{warn debug info fatal error}.each do |level|
89
+ define_method(level.to_sym) do |msg|
90
+ @logger.send(level.to_sym, msg)
91
+ end
92
+ end
93
+
94
+ def force_shutdown_after_wait(wait_time = 10)
95
+ EM.add_timer(wait_time) do
96
+ info "Forcing shutdown"
97
+ shutdown
98
+ exit 1
99
+ end
100
+ end
101
+
102
+ def eventmachine_running?
103
+ EM.reactor_running? && EM.reactor_thread
104
+ end
105
+
106
+ def block_for_messages
107
+ sleep 1 while !eventmachine_running?
108
+ EM.reactor_thread.join
109
+ end
110
+
111
+ def shutdown
112
+ EM.stop_event_loop
113
+ end
114
+
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,5 @@
1
+ module Instrumental
2
+ module PubNub
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instrumental_pubnub_example
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Zelenak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pubnub
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.6.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.6.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: instrumental_agent
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - support@instrumentalapp.com
58
+ executables:
59
+ - instrumental_pubnub_relay
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".ruby-version"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - bin/instrumental_pubnub_relay
70
+ - instrumental-pubnub-example.gemspec
71
+ - lib/instrumental/pub_nub/relay.rb
72
+ - lib/instrumental/pub_nub/version.rb
73
+ homepage: http://github.com/expectedbehavior/instrumental_pubnub_example
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.5
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Relay PubNub data to instrumentalapp.com
96
+ test_files: []