Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #7 from gorsuch/api_key

Support for Hosted Graphite API Keys
  • Loading branch information...
commit 29b985c5e96b9aea7e00cd486689b93c48595322 2 parents d4b45da + bfe91b6
@gorsuch gorsuch authored
View
4 README.md
@@ -52,6 +52,10 @@ RestClient.post("https://backstop.example.com/publish/note",
[{:metric => "foobar.release", :value => "v214", :measure_time => Time.now.to_i}].to_json)
```
+#### Using with Hosted Graphite
+
+Graphite hosting service [Hosted Graphite](https://www.hostedgraphite.com) requires metrics to be submitted with an API key prepended to the metric. To use their service, just define the `API_KEY` environment variable.
+
## Deployment
### Local
View
1  lib/backstop/config.rb
@@ -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
11 lib/backstop/publisher.rb
@@ -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
2  lib/backstop/web.rb
@@ -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
View
10 spec/backstop/publisher_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.