diff --git a/raven/init.lua b/raven/init.lua index 452f4f3..9e53b97 100644 --- a/raven/init.lua +++ b/raven/init.lua @@ -136,6 +136,9 @@ function _M.new(conf) logger = conf.logger or "root", tags = conf.tags or nil, extra = conf.extra or nil, + environment = conf.environment or nil, + release = conf.release or nil, + server_name = conf.server_name or nil } return setmetatable(obj, raven_mt) @@ -148,6 +151,21 @@ function _M.get_server_name() return "undefined" end +--- This method is reponsible to return the `request` field of an event. +-- The default implementation just returns `{}`, users are encouraged +-- to override this to something more sensible. +-- See [Sentry's docs](https://develop.sentry.dev/sdk/event-payloads/request/) +-- for the list of allowed properties. +function _M.get_request_data() + return {} +end + +--- This method can be used to tag a release in Sentry. +-- Typically you can use it with a commit hash. +function _M.get_release() + return nil +end + --- This table can be used to tune the message reporting. -- @field tags Tags for the message, they will be coalesced with the ones -- provided in the @{sentry_conf} table used in the constructor if any. In @@ -296,11 +314,12 @@ function raven_mt:send_report(json, conf) end end - json.event_id = event_id - json.timestamp = iso8601() - json.level = self.level - json.platform = "lua" - json.logger = self.logger + json.event_id = event_id + json.timestamp = iso8601() + json.level = self.level + json.platform = "lua" + json.logger = self.logger + json.environment = self.environment if conf then json.tags = merge_tables(conf.tags, self.tags) @@ -309,12 +328,20 @@ function raven_mt:send_report(json, conf) if conf.level then json.level = conf.level end + if conf.user then + json.user = merge_tables(conf.user, self.user) + end + if conf.contexts then + json.contexts = conf.contexts + end else json.tags = self.tags json.extra = self.extra end - json.server_name = _M.get_server_name() + json.request = _M.get_request_data() + json.server_name = self.server_name or _M.get_server_name() + json.release = self.release or _M.get_release() local json_str = json_encode(json) local ok, err = self.sender:send(json_str) diff --git a/raven/senders/luasocket.lua b/raven/senders/luasocket.lua index ded028c..dbb5fac 100644 --- a/raven/senders/luasocket.lua +++ b/raven/senders/luasocket.lua @@ -36,7 +36,7 @@ function mt:send(json_str) method = "POST", url = self.server, headers = { - ['Content-Type'] = 'applicaion/json', + ['Content-Type'] = 'application/json', ['User-Agent'] = "raven-lua-socket/" .. _VERSION, ['X-Sentry-Auth'] = generate_auth_header(self), ["Content-Length"] = tostring(#json_str), diff --git a/raven/senders/ngx.lua b/raven/senders/ngx.lua index 05bf470..9d63553 100644 --- a/raven/senders/ngx.lua +++ b/raven/senders/ngx.lua @@ -240,4 +240,66 @@ function _M.get_server_name() return "undefined" end +--- Returns the value of the `request_data` variable if possible. +-- Otherwise (wrong phase), this will return {}. +-- +-- It is intended to be used as a `get_request_data` override on the main raven +-- instance. +-- +-- @usage +-- local raven_ngx = require("raven.senders.ngx") +-- local rvn = raven.new(...) +-- rvn.get_request_data = raven_ngx.get_request_data +function _M.get_request_data() + local phase = ngx.get_phase() + -- the ngx.var.* API is not available in all contexts + if phase == "set" or + phase == "rewrite" or + phase == "access" or + phase == "content" or + phase == "header_filter" or + phase == "body_filter" or + phase == "log" + then + return { + caller = "nginx", + method = ngx.var.request_method or nil, + host = ngx.var.http_host or nil, + url = ngx.var.request_uri or nil, + query_string = ngx.var.query_string or nil, + env = { + -- some information can be only accessed if the request is at log phase + HOSTNAME = ngx.var.hostname, + METHOD = ngx.var.request_method or nil, + BODY_BYTES_SENT = ngx.var.body_bytes_sent or nil, + BYTES_RECEIVED = ngx.var.bytes_received or nil, + BYTES_SENT = ngx.var.bytes_sent or nil, + NGINX_VERSION = ngx.var.nginx_version or nil, + REMOTE_ADDR = ngx.var.remote_addr or nil, + REMOTE_PORT = ngx.var.remote_port or nil, + REQUEST_TIME = ngx.var.request_time or nil, + STATUS = ngx.var.status or nil, + USER_AGENT = ngx.var.http_user_agent or nil, + CONTENT_TYPE = ngx.var.http_content_type or nil, + CONTENT_LENGTH = ngx.var.http_content_length or nil, + -- proxy related information + PROXY_ADD_X_FORWARDED_FOR = ngx.var.proxy_add_x_forwarded_for or nil, + UPSTREAM_ADDR = ngx.var.upstream_add or nil, + UPSTREAM_BYTES_RECEIVED = ngx.var.upstream_bytes_received or nil, + UPSTREAM_BYTES_SENT = ngx.var.upstream_bytes_sent or nil, + UPSTREAM_CACHE_STATUS = ngx.var.upstream_cache_status or nil, + UPSTREAM_CONNECT_TIME = ngx.var.upstream_connect_time or nil, + UPSTREAM_FIRST_BYTE_TIME = ngx.var.upstream_first_byte_time or nil, + UPSTREAM_HEADER_TIME = ngx.var.upstream_header_time or nil, + UPSTREAM_RESPONSE_LENGTH = ngx.var.upstream_response_length or nil, + UPSTREAM_RESPONSE_TIME = ngx.var.upstream_response_time or nil, + UPSTREAM_STATUS = ngx.var.upstream_status or nil, + }, + } + end + return { + caller = "nginx", + } +end + return _M