Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Proxy json requests #88

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ If your Graphite composer is proteced by basic authentication, you have to ensur

See http://blog.rogeriopvl.com/archives/nginx-and-the-http-options-method/ for an Nginx example.

#### Alternatively, enable the built-in proxy to bypass the CORS requirement
By setting the `USE_PROXY` environment variable to `true` the clients will make ajax requests to Tasseo's backend and it will forward them on to Graphite or InfluxDB
```
USE_PROXY=true
```


## Alternate Backends

### Librato Metrics
Expand Down
4 changes: 2 additions & 2 deletions lib/tasseo/views/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@

var datasource;

var graphiteUrl = "#{ENV['GRAPHITE_URL']}";
var graphiteUrl = "#{ENV['GRAPHITE_URL'] && ENV['USE_PROXY'] ? '/proxy' : ENV['GRAPHITE_URL']}";
var graphiteAuth = "#{ENV['GRAPHITE_AUTH']}";
var libratoAuth = "#{ENV['LIBRATO_AUTH']}";
var influxDbUrl = "#{ENV['INFLUXDB_URL']}";
var influxDbUrl = "#{ENV['INFLUXDB_URL'] && ENV['USE_PROXY'] ? '/proxy' : ENV['INFLUXDB_URL']}";
var influxDbAuth = "#{ENV['INFLUXDB_AUTH']}";

if (libratoAuth != "") {
Expand Down
19 changes: 19 additions & 0 deletions lib/tasseo/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ def find_dashboards
{'status' => 'ok'}.to_json
end

is_influx = !!ENV["INFLUXDB_URL"]
data_source_url = is_influx ? ENV["INFLUXDB_URL"] : ENV["GRAPHITE_URL"]
auth_details = is_influx ? ENV["INFLUXDB_AUTH"] : ENV["GRAPHITE_AUTH"]
auth_details = auth_details.split(':') if auth_details
if ENV['USE_PROXY']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also check here to make sure we're not using Librato? Doesn't look like Librato is supported in your PR.

get %r{/proxy/(.*)} do
uri = URI("#{data_source_url}/#{params[:captures].first}?#{request.query_string}")
req = Net::HTTP::Get.new(uri)
if auth_details
req.basic_auth(auth_details[0], auth_details[1])
end
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
content_type :json
res.body
end
end

get %r{/([\S]+)} do
path = params[:captures].first
if dashboards.include?(path)
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.order = 'random'
ENV["USE_PROXY"]="true"
ENV["GRAPHITE_URL"]="http://echo.jsontest.com"
end
13 changes: 11 additions & 2 deletions spec/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def app
before do
header 'Accept', 'application/json'
end

context 'without dashboards' do
before do
app.any_instance.stub(:dashboards) { [] }
end

it 'should return a 204' do
get '/'
last_response.status.should eq(204)
Expand Down Expand Up @@ -89,6 +89,15 @@ def app
end
end

describe 'GET /proxy' do
context '/ JSON' do
it 'should return an array of dashboards' do
get '/proxy/foo/bar'
JSON.parse(last_response.body).should eq({'foo' => 'bar'})
end
end
end

describe 'GET *' do
context 'dashboard exists' do
it 'should be ok' do
Expand Down