Skip to content

Commit

Permalink
feat(analytics) Real socket address and environment
Browse files Browse the repository at this point in the history
- Add the real analytics socket
- Add the `environment` value to ALF format and plugin configuration
  • Loading branch information
thibaultcha committed Jun 25, 2015
1 parent f2ee9e5 commit 6627910
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions kong/plugins/analytics/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ local DELAYED_LOCK = false -- careful: this will only work when lua_code_cache i
local LATEST_CALL

local ANALYTICS_SOCKET = {
host = "localhost", -- socket.analytics.mashape.com
port = 58000,
path = "/alf_1.0.0"
host = "socket.analytics.mashape.com",
port = 80,
path = "/1.0.0/single"
}

local function send_batch(premature, conf, alf)
Expand All @@ -34,7 +34,7 @@ local function send_batch(premature, conf, alf)
return
end

local message = alf:to_json_string(conf.service_token)
local message = alf:to_json_string(conf.service_token, conf.environment)

local ok, err
local client = http:new()
Expand Down
1 change: 1 addition & 0 deletions kong/plugins/analytics/schema.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
return {
fields = {
service_token = { type = "string", required = true },
environment = { type = "string" },
batch_size = { type = "number", default = 100 },
log_body = { type = "boolean", default = false },
delay = { type = "number", default = 10 }
Expand Down
7 changes: 5 additions & 2 deletions kong/plugins/log_serializers/alf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ function alf_mt:new_alf()
local ALF = {
version = "1.0.0",
serviceToken = "", -- will be filled by to_json_string()
environment = "",
har = {
log = {
version = "1.2",
creator = {
name = "kong-mashape-analytics-plugin",
name = "mashape-analytics-agent-kong",
version = "1.0.0"
},
entries = {}
Expand Down Expand Up @@ -175,13 +176,15 @@ function alf_mt:add_entry(ngx)
return table.getn(self.har.log.entries)
end

function alf_mt:to_json_string(token)
function alf_mt:to_json_string(token, environment)
if not token then
error("Mashape Analytics serviceToken required", 2)
end

-- inject token
self.serviceToken = token
-- inject environment (left empty if nil)
self.environment = environment

local str = json.encode(self)
return str:gsub("\""..EMPTY_ARRAY_PLACEHOLDER.."\"", ""):gsub("\\/", "/")
Expand Down
3 changes: 1 addition & 2 deletions spec/plugins/logging_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local IO = require "kong.tools.io"
local yaml = require "yaml"
local uuid = require "uuid"
local cjson = require "cjson"
local stringy = require "stringy"
Expand Down Expand Up @@ -72,7 +71,7 @@ describe("Logging Plugins", function()
local log_message = cjson.decode(res)
assert.are.same("127.0.0.1", log_message.client_ip)
end)

it("should log proper latencies", function()
local http_thread = spec_helper.start_http_server(HTTP_DELAY_PORT) -- Starting the mock TCP server
local tcp_thread = spec_helper.start_tcp_server(TCP_PORT) -- Starting the mock TCP server
Expand Down

0 comments on commit 6627910

Please sign in to comment.