-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #918 from 3scale/upstream-metrics
Add Prometheus metrics for the upstream
- Loading branch information
Showing
5 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local tonumber = tonumber | ||
|
||
local prometheus = require('apicast.prometheus') | ||
|
||
local _M = {} | ||
|
||
local upstream_status_codes = prometheus( | ||
'counter', | ||
'upstream_status', | ||
'HTTP status from upstream servers', | ||
{ 'status' } | ||
) | ||
|
||
local upstream_resp_times = prometheus( | ||
'histogram', | ||
'upstream_resp_times', | ||
'Response times from upstream servers' | ||
) | ||
|
||
local function inc_status_codes_counter(status) | ||
if tonumber(status) and upstream_status_codes then | ||
upstream_status_codes:inc(1, { status }) | ||
end | ||
end | ||
|
||
local function add_resp_time(response_time) | ||
local time = tonumber(response_time) | ||
|
||
if time and upstream_resp_times then | ||
upstream_resp_times:observe(time) | ||
end | ||
end | ||
|
||
function _M.report(status, response_time) | ||
inc_status_codes_counter(status) | ||
add_resp_time(response_time) | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
describe('upstream metrics', function() | ||
describe('report', function() | ||
local upstream_metrics | ||
local test_counter = { inc = function() end } | ||
local test_histogram = { observe = function() end } | ||
|
||
before_each(function() | ||
-- Make Prometheus return stubbed counters and histograms | ||
stub(test_counter, 'inc') | ||
stub(test_histogram, 'observe') | ||
|
||
local Prometheus = require('apicast.prometheus') | ||
getmetatable(Prometheus).__call = function(_, type) | ||
if type == 'counter' then | ||
return test_counter | ||
elseif type == 'histogram' then | ||
return test_histogram | ||
end | ||
end | ||
|
||
package.loaded['apicast.metrics.upstream'] = nil | ||
upstream_metrics = require('apicast.metrics.upstream') | ||
end) | ||
|
||
after_each(function() | ||
package.loaded['apicast.prometheus'] = nil | ||
require('apicast.prometheus') | ||
|
||
package.loaded['apicast.metrics.upstream'] = nil | ||
require('apicast.metrics.upstream') | ||
end) | ||
|
||
it('increases the counter of status codes', function() | ||
upstream_metrics.report(200, 0.1) | ||
assert.stub(test_counter.inc).was_called_with(test_counter, 1, { 200 }) | ||
end) | ||
|
||
it('adds the latency to the histogram', function() | ||
upstream_metrics.report(200, 0.1) | ||
assert.stub(test_histogram.observe).was_called_with(test_histogram, 0.1) | ||
end) | ||
|
||
describe('when the status is nil or empty', function() | ||
it('does not increase the counter of status codes', function() | ||
upstream_metrics.report(nil, 0.1) | ||
upstream_metrics.report('', 0.1) | ||
assert.stub(test_counter.inc).was_not_called() | ||
end) | ||
end) | ||
|
||
describe('when the latency is nil or empty', function() | ||
it('does not add the latency to the histogram', function() | ||
upstream_metrics.report(200, nil) | ||
upstream_metrics.report(200, '') | ||
assert.stub(test_histogram.observe).was_not_called() | ||
end) | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters