-
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.
[prometheus] create prometheus exporter skeleton
- Loading branch information
Showing
10 changed files
with
100 additions
and
3 deletions.
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
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
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
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,23 @@ | ||
local prometheus = require('nginx.prometheus') | ||
local assert = assert | ||
local dict = 'prometheus_metrics' | ||
|
||
if ngx.shared[dict] then | ||
local init = prometheus.init(dict) | ||
|
||
local metrics = { } | ||
local __call = function(_, type, name, ...) | ||
local metric_name = assert(name, 'missing metric name') | ||
|
||
if not metrics[metric_name] then | ||
metrics[metric_name] = init[assert(type, 'missing metric type')](init, metric_name, ...) | ||
end | ||
|
||
return metrics[metric_name] | ||
end | ||
|
||
return setmetatable({ }, { __call = __call, __index = init }) | ||
else | ||
local noop = function() end | ||
return setmetatable({ collect = noop }, { __call = noop }) | ||
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
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,41 @@ | ||
|
||
describe('prometheus', function() | ||
before_each(function() package.loaded['apicast.prometheus'] = nil end) | ||
|
||
describe('shared dictionary is missing', function() | ||
before_each(function() ngx.shared.prometheus_metrics = nil end) | ||
|
||
it('can be called', function() | ||
assert.is_nil(require('apicast.prometheus')()) | ||
end) | ||
|
||
it('can be collected', function() | ||
assert.is_nil(require('apicast.prometheus'):collect()) | ||
end) | ||
end) | ||
|
||
describe('shared dictionary is there', function() | ||
before_each(function() | ||
ngx.shared.prometheus_metrics = { | ||
set = function() end, | ||
get_keys = function() return {} end | ||
} | ||
end) | ||
|
||
local prometheus | ||
|
||
before_each(function() | ||
prometheus = assert(require('apicast.prometheus')) | ||
end) | ||
|
||
it('can be called', function() | ||
assert.is_table(prometheus('counter', 'some_metric')) | ||
end) | ||
|
||
it('can be collected', function() | ||
ngx.header = { } | ||
assert.is_nil(require('apicast.prometheus'):collect()) | ||
end) | ||
end) | ||
|
||
end) |