-
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 #214 from 3scale/track-gateway-usage
send User-Agent with platform and system information
- Loading branch information
Showing
11 changed files
with
218 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
local ffi = require 'ffi' | ||
local module = require 'module' | ||
|
||
local setmetatable = setmetatable | ||
|
||
local _M = { | ||
_VERSION = '2.0' | ||
} | ||
|
||
function _M.deployment() | ||
return _M.threescale_deployment_env or 'unknown' | ||
end | ||
|
||
-- User-Agent: <product> / <product-version> <comment> | ||
-- User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions> | ||
|
||
function _M.call() | ||
return 'APIcast/' .. _M._VERSION .. ' (' .. _M.system_information() .. ') ' .. _M.platform() | ||
end | ||
|
||
function _M.system_information() | ||
return ffi.os .. '; ' .. ffi.arch .. '; env:' .. _M.deployment() | ||
end | ||
|
||
function _M.platform() | ||
local m = module.new() | ||
local table, err = m:require() | ||
local version = table and table._VERSION | ||
local name = table and table._NAME or m.name | ||
|
||
if not name then | ||
return nil, 'missing module name' | ||
end | ||
|
||
if not table and err then | ||
return nil, err | ||
end | ||
|
||
if version then | ||
return name ..'/'.. version | ||
else | ||
return name | ||
end | ||
end | ||
|
||
local mt = { | ||
__call = _M.call, | ||
__tostring = _M.call | ||
} | ||
|
||
function _M.reset() | ||
local env = { | ||
threescale_deployment_env = os.getenv('THREESCALE_DEPLOYMENT_ENV') | ||
} | ||
|
||
mt.__index = env | ||
|
||
_M.env = env | ||
|
||
mt.__call = _M.call | ||
mt.__tostring = _M.call | ||
end | ||
|
||
function _M.cache() | ||
_M.reset() | ||
|
||
local user_agent = _M.call() | ||
|
||
mt.__call = function() return user_agent end | ||
mt.__tostring = mt.__call | ||
end | ||
|
||
setmetatable(_M, mt) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,3 @@ | ||
#!/usr/bin/env sh | ||
|
||
exec resty -e "$(cat <<LUA | ||
if ngx ~= nil then | ||
ngx.exit = function()end | ||
end | ||
pcall(require, 'luarocks.loader') | ||
-- Busted command-line runner | ||
require 'busted.runner'({ standalone = false }) | ||
LUA | ||
)" | ||
exec resty bin/busted.lua "$@" |
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,8 @@ | ||
if ngx ~= nil then | ||
ngx.exit = function()end | ||
end | ||
|
||
pcall(require, 'luarocks.loader') | ||
|
||
-- Busted command-line runner | ||
require 'busted.runner'({ standalone = false }) |
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,71 @@ | ||
local user_agent = require 'user_agent' | ||
local ffi = require("ffi") | ||
|
||
describe('3scale', function() | ||
before_each(function() user_agent.reset() end) | ||
|
||
describe('.deployment', function() | ||
it('reads from environment', function() | ||
stub(os, 'getenv').on_call_with('THREESCALE_DEPLOYMENT_ENV').returns('foobar') | ||
|
||
user_agent.reset() | ||
|
||
assert.same('foobar', user_agent.deployment()) | ||
end) | ||
|
||
it('uses internal structure', function() | ||
user_agent.env.threescale_deployment_env = 'bar' | ||
|
||
assert.same('bar', user_agent.deployment()) | ||
end) | ||
end) | ||
|
||
describe('.user_agent', function() | ||
-- User-Agent: <product> / <product-version> <comment> | ||
-- User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions> | ||
|
||
it('matches common format', function() | ||
|
||
user_agent.env.threescale_deployment_env = 'production' | ||
|
||
assert.match('APIcast/' .. user_agent._VERSION, user_agent.call()) | ||
end) | ||
|
||
it('includes system information', function() | ||
assert.match('(' .. user_agent.system_information() .. ')', user_agent.call()) | ||
end) | ||
|
||
it('includes platform information', function() | ||
assert.match(' ' .. user_agent.platform(), user_agent.call()) | ||
end) | ||
|
||
it('works as tostring', function() | ||
assert.equal(user_agent.call(), tostring(user_agent)) | ||
end) | ||
|
||
it('works as function', function() | ||
assert.equal(user_agent.call(), user_agent()) | ||
end) | ||
end) | ||
|
||
|
||
describe('.system_information', function() | ||
it('includes os information', function() | ||
assert.match(ffi.os ..'; ' .. ffi.arch, user_agent.system_information()) | ||
end) | ||
|
||
it('includes deployment information', function() | ||
user_agent.env.threescale_deployment_env = 'foobar' | ||
assert.match(' env:foobar', user_agent.system_information()) | ||
end) | ||
end) | ||
|
||
describe('.platform', function() | ||
it('includes os information', function() | ||
local apicast = require('apicast') | ||
|
||
assert.same('APIcast/' .. apicast._VERSION, user_agent.platform()) | ||
end) | ||
end) | ||
|
||
end) |