Permalink
Browse files

allow for an optional api key

  • Loading branch information...
1 parent d4b45da commit a87a77130e5492e9f9546f79d95bc4774e117169 @gorsuch gorsuch committed Dec 27, 2012
Showing with 20 additions and 4 deletions.
  1. +1 −0 lib/backstop/config.rb
  2. +8 −3 lib/backstop/publisher.rb
  3. +1 −1 lib/backstop/web.rb
  4. +10 −0 spec/backstop/publisher_spec.rb
View
@@ -8,5 +8,6 @@ def self.deploy; env!('DEPLOY'); end
def self.port; env!('PORT').to_i; end
def self.carbon_urls; env!('CARBON_URLS').split(','); end
def self.prefixes; env!('PREFIXES').split(','); end
+ def self.api_key; ENV['API_KEY']; end
end
end
View
@@ -1,14 +1,19 @@
module Backstop
class Publisher
- attr_reader :connections
+ attr_reader :connections, :api_key
- def initialize(urls)
+ def initialize(urls, opts={})
@connections = []
@connections = urls.map { |u| URI.parse(u) }.map { |u| TCPSocket.new(u.host, u.port) }
+ @api_key = opts[:api_key]
+ end
+
+ def metric_name(name)
+ api_key ? "#{api_key}.#{name}" : name
end
def publish(name, value, time=Time.now.to_i)
- connections.sample.puts("#{name} #{value} #{time}")
+ connections.sample.puts("#{metric_name(name)} #{value} #{time}")
end
end
end
View
@@ -13,7 +13,7 @@ class Application < Sinatra::Base
helpers do
def publisher
- @@publisher ||= Backstop::Publisher.new(Config.carbon_urls)
+ @@publisher ||= Backstop::Publisher.new(Config.carbon_urls, :api_key => Config.api_key)
end
end
@@ -29,4 +29,14 @@
socket_double.should_receive(:puts).with('foo 1 12345')
b.publish('foo', 1)
end
+
+ it 'should apply an api key if you provide one' do
+ urls = ['tcp://10.0.0.1:5000']
+ socket_double = double('TCPSocket')
+ TCPSocket.should_receive(:new).with('10.0.0.1', 5000) { socket_double }
+ b = Backstop::Publisher.new(urls, :api_key => '12345')
+
+ socket_double.should_receive(:puts).with('12345.foo 1 1')
+ b.publish('foo', 1, 1)
+ end
end

0 comments on commit a87a771

Please sign in to comment.