Skip to content

Commit

Permalink
backend_client: report type of call
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Oct 3, 2018
1 parent 9982e6b commit 4aa47f3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
22 changes: 14 additions & 8 deletions gateway/src/apicast/backend_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local http_ng = require('resty.http_ng')
local user_agent = require('apicast.user_agent')
local resty_url = require('resty.url')
local resty_env = require('resty.env')
local threescale_backend_status_counters = require('apicast.metrics.3scale_backend_status')
local backend_calls_metrics = require('apicast.metrics.3scale_backend_calls')

local http_proxy = require('resty.http.proxy')
local http_ng_ngx = require('resty.http_ng.backend.ngx')
Expand Down Expand Up @@ -98,8 +98,8 @@ function _M:new(service, http_client)
}, mt)
end

local function inc_backend_status_metric(status)
threescale_backend_status_counters.inc(status)
local function inc_metrics(endpoint, status)
backend_calls_metrics.report(endpoint, status)
end

local function build_args(args)
Expand Down Expand Up @@ -138,8 +138,6 @@ local function call_backend_transaction(self, path, options, ...)

ngx.log(ngx.INFO, 'backend client uri: ', url, ' ok: ', res.ok, ' status: ', res.status, ' body: ', res.body, ' error: ', res.error)

inc_backend_status_metric(res.status)

return res
end

Expand Down Expand Up @@ -213,7 +211,11 @@ function _M:authrep(...)

local using_oauth = self.version == 'oauth'
local auth_uri = authrep_path(using_oauth)
return call_backend_transaction(self, auth_uri, authorize_options(using_oauth), ...)
local res = call_backend_transaction(self, auth_uri, authorize_options(using_oauth), ...)

inc_metrics('authrep', res.status)

return res
end

--- Call authorize (oauth_authorize) on backend.
Expand All @@ -226,7 +228,11 @@ function _M:authorize(...)

local using_oauth = self.version == 'oauth'
local auth_uri = auth_path(using_oauth)
return call_backend_transaction(self, auth_uri, authorize_options(using_oauth), ...)
local res = call_backend_transaction(self, auth_uri, authorize_options(using_oauth), ...)

inc_metrics('auth', res.status)

return res
end

function _M:report(reports_batch)
Expand All @@ -236,7 +242,7 @@ function _M:report(reports_batch)
local report_body = format_transactions(reports_batch)
local res = http_client.post(report_uri, report_body)

inc_backend_status_metric(res.status)
inc_metrics('report', res.status)

return res
end
Expand Down
39 changes: 38 additions & 1 deletion spec/backend_client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ local _M = require('apicast.backend_client')
local configuration = require('apicast.configuration')
local test_backend_client = require 'resty.http_ng.backend.test'
local ReportsBatch = require 'apicast.policy.3scale_batcher.reports_batch'
local backend_calls_metrics = require 'apicast.metrics.3scale_backend_calls'

describe('backend client', function()

local test_backend
local options_header_oauth = 'rejection_reason_header=1'
local options_header_no_oauth = 'rejection_reason_header=1&no_body=1'

before_each(function() test_backend = test_backend_client.new() end)
before_each(function()
test_backend = test_backend_client.new()
stub(backend_calls_metrics, 'report')
end)

describe('authrep', function()
it('works without params', function()
Expand Down Expand Up @@ -90,6 +94,17 @@ describe('backend client', function()

assert.equal(200, res.status)
end)

it('reports the call with the status', function()
local service = configuration.parse_service({ id = '42' })
local status = 200
test_backend.expect({}).respond_with({ status = status })
local backend_client = assert(_M:new(service, test_backend))

backend_client:authrep()

assert.stub(backend_calls_metrics.report).was_called_with('authrep', status)
end)
end)

describe('authorize', function()
Expand Down Expand Up @@ -171,6 +186,17 @@ describe('backend client', function()

assert.equal(200, res.status)
end)

it('reports the call with the status', function()
local service = configuration.parse_service({ id = '42' })
local status = 200
test_backend.expect({}).respond_with({ status = status })
local backend_client = assert(_M:new(service, test_backend))

backend_client:authorize()

assert.stub(backend_calls_metrics.report).was_called_with('auth', status)
end)
end)

describe('report', function()
Expand Down Expand Up @@ -269,6 +295,17 @@ describe('backend client', function()
assert.equal(200, res.status)
end)
end)

it('reports the call with the status', function()
local service = configuration.parse_service({ id = '42' })
local status = 200
test_backend.expect({}).respond_with({ status = status })
local backend_client = assert(_M:new(service, test_backend))

backend_client:report(ReportsBatch.new(service.id, {}))

assert.stub(backend_calls_metrics.report).was_called_with('report', status)
end)
end)

describe('store_oauth_token', function()
Expand Down

0 comments on commit 4aa47f3

Please sign in to comment.