Skip to content

Commit

Permalink
Fix/add more validation (#5)
Browse files Browse the repository at this point in the history
* Add more validation on null connection

Signed-off-by: Daniela Bento <[email protected]>
  • Loading branch information
danifbento authored Jul 11, 2023
1 parent 1697e6b commit d7634a8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 55 deletions.
72 changes: 46 additions & 26 deletions lib/lua-nri/newrelic_agent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ local license_key = os.getenv("NEWRELIC_APP_LICENSE_KEY")
local app_name = os.getenv("NEWRELIC_APP_NAME")

local newrelic_application = nil

local newrelic_application_enabled = false

_M.enabled = ('string' == type(license_key) and
40 == #license_key) and
(newrelic.version() == newrelic.COMPATIBLE_VERSION)
Expand All @@ -30,28 +33,45 @@ _M.enable = function(configuration)
end

newrelic_application = newrelic.create_app(license_key, app_name, configuration)

newrelic_application_enabled = (newrelic_application ~= nil and _M.enabled)

local retry = 0
if newrelic_application_enabled == false and retry < 3 then
newrelic_application = newrelic.create_app(license_key, app_name, configuration)
retry = retry + 1
ngx.log(ngx.INFO, "Retrying create_app ... ".. tostring(retry) .. " times")
newrelic_application_enabled = (newrelic_application ~= nil and _M.enabled)
end
end

if newrelic_application_enabled == true then
ngx.log(ngx.INFO, 'Starting Newrelic Lua Agent for ' .. app_name)
else
ngx.log(ngx.ERR, 'Newrelic Lua Agent is not configured for ' .. (app_name or "invalid name"))
if retry == 3 then
ngx.log(ngx.ERR, 'All retries were used trying to create_app...')
end
end
end

_M.notice_error = function(priority, message, class)
if _M.enabled then
return newrelic.notice_error(ngx.ctx.nr_transaction_id, priority, message, class)
local transaction_id = ngx.ctx.nr_transaction_id
if newrelic_application_enabled and transaction_id then
return newrelic.notice_error(transaction_id, priority, message, class)
end
end

-- web transactions
_M.start_web_transaction = function()
if _M.enabled then
if newrelic_application_enabled then
ngx.ctx.nr_transaction_id = newrelic.start_web_transaction(newrelic_application, ngx.var.uri)
end
end

_M.end_web_transaction = function()
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.end_web_transaction(transaction_id)
end
end
Expand All @@ -60,84 +80,84 @@ end
-- generic segment
_M.start_segment = function(name, category)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.start_segment(transaction_id, name, category)
end
end

_M.end_segment = function(segment_id)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id and segment_id then
if newrelic_application_enabled and transaction_id and segment_id then
return newrelic.end_segment(transaction_id, segment_id)
end
end


-- non web transactions
_M.start_non_web_transaction = function(name)
if _M.enabled then
if newrelic_application_enabled then
ngx.ctx.nr_transaction_id_non_web = newrelic.start_non_web_transaction(newrelic_application, name)
end
end

_M.end_non_web_transaction = function()
local transaction_id = ngx.ctx.nr_transaction_id_non_web
if _M.enabled then
if newrelic_application_enabled and transaction_id then
return newrelic.end_non_web_transaction(transaction_id)
end
end

_M.ignore_transaction = function()
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.ignore_transaction(transaction_id)
end
end

-- attributes
_M.add_attribute = function(name, value)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.add_attribute(transaction_id, name, value)
end
end

-- custom events
_M.custom_event = function(type)
if _M.enabled then
if newrelic_application_enabled then
return newrelic.create_custom_event(type)
end
end

_M.record_custom_event = function(event)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.record_custom_event(transaction_id, event)
end
end

_M.discard_custom_event = function(event)
if _M.enabled and event then
if newrelic_application_enabled and event then
return newrelic.discard_custom_event(event)
end
end

_M.custom_event_add_attribute = function(custom_event, name, value)
if _M.enabled then
if newrelic_application_enabled then
return newrelic.custom_event_add_attribute(custom_event, name, value)
end
end

_M.record_custom_metric = function(name, milliseconds)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.record_custom_metric(transaction_id, name, milliseconds)
end
end

_M.start_datastore_segment = function(product, collection, operation, host, port_path_or_id, database_name, query)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.start_datastore_segment(
transaction_id,
product,
Expand All @@ -152,67 +172,67 @@ end

_M.start_external_segment = function(uri, procedure, library)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.start_external_segment(transaction_id, uri, procedure, library)
end
end

_M.set_transaction_name = function(name)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.set_transaction_name(transaction_id, name)
end
end

_M.set_transaction_timing = function(duration, start_time)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.set_transaction_timing(transaction_id, duration, start_time)
end
end

_M.set_segment_timing = function(segment, duration, start_time)
if _M.enabled and segment then
if newrelic_application_enabled and segment then
return newrelic.set_segment_timing(segment, duration, start_time)
end
end

_M.set_segment_parent_root = function(segment)
if _M.enabled and segment then
if newrelic_application_enabled and segment then
return newrelic.set_segment_parent_root(segment)
end
end

_M.set_segment_parent = function(segment, parent_segment)
if _M.enabled and segment and parent_segment then
if newrelic_application_enabled and segment and parent_segment then
return newrelic.set_segment_parent(segment, parent_segment)
end
end

_M.create_distributed_trace_payload = function(segment)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id and segment then
if newrelic_application_enabled and transaction_id and segment then
return newrelic.create_distributed_trace_payload(transaction_id, segment)
end
end

_M.create_distributed_trace_payload_httpsafe = function(segment)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id and segment then
if newrelic_application_enabled and transaction_id and segment then
return newrelic.create_distributed_trace_payload_httpsafe(transaction_id, segment)
end
end

_M.accept_distributed_trace_payload = function(payload, transport_type)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.accept_distributed_trace_payload(transaction_id, payload, transport_type)
end
end

_M.accept_distributed_trace_payload_httpsafe = function(payload, transport_type)
local transaction_id = ngx.ctx.nr_transaction_id
if _M.enabled and transaction_id then
if newrelic_application_enabled and transaction_id then
return newrelic.accept_distributed_trace_payload_httpsafe(transaction_id, payload, transport_type)
end
end
Expand Down
27 changes: 0 additions & 27 deletions lua-newrelic-integration-0.01-5.rockspec

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package = "lua-newrelic-integration"
version = "0.01-4"
version = "0.01-6"
source = {
url = "git://github.com/danifbento/lua-newrelic-integration",
tag = "v0.01-4"
tag = "v0.01-6"
}
description = {
summary = "Lua newrelic client library for OpenResty / ngx_lua.",
Expand Down

0 comments on commit d7634a8

Please sign in to comment.