Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Reorganize probes into integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Mar 21, 2017
2 parents 1cda724 + 69ee1a2 commit 672978f
Show file tree
Hide file tree
Showing 73 changed files with 1,076 additions and 994 deletions.
5 changes: 2 additions & 3 deletions lib/timber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require "timber/config"
require "timber/context"
require "timber/event"
require "timber/probe"
require "timber/integrator"
require "timber/util"
require "timber/version"

Expand All @@ -17,8 +17,7 @@
require "timber/log_devices"
require "timber/log_entry"
require "timber/logger"
require "timber/probes"
require "timber/rack_middlewares"
require "timber/integrations"

# Load frameworks
require "timber/frameworks"
Expand Down
5 changes: 0 additions & 5 deletions lib/timber/auth_check.rb

This file was deleted.

23 changes: 22 additions & 1 deletion lib/timber/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ def debug_logger
@debug_logger
end

# This method allows the delegation of the logger method to a given block. This has
# 2 purposes:
#
# 1. It allows us to delegate the logger call to methods like `::Rails.logger`. In
# a rails environment, this is generally the main logger and Timber respects it.
# 2. It allows Timber to call `Config.instance.logger` internally without having to
# worry about the environment. This way each framework can be responsible for setup.
#
# @private
def delegate_logger_to(&block)
@delegate_logger_to = block
end

# The target device that logs are written to. Must respond to #write and #close.
#
# Configure this with an environment variable. `TIMBER_LOG_DEVICE`, must be
Expand All @@ -49,7 +62,15 @@ def log_device
end

def logger
@logger ||= ::Logger.new(log_device)
if @delegate_logger_to
@delegate_logger_to.call
else
@logger ||= if defined?(::ActiveSupport::TaggedLogging)
::ActiveSupport::TaggedLogging.new(Logger.new(log_device))
else
Logger.new(log_device)
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/timber/contexts/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Contexts
# as join data across your logs, allowing you to query all logs for any attribute
# presented here. For example, viewing all logs for a given request_id.
#
# @note This context should be installed automatically through probes,
# such as the {Probes::RackHTTPContext} probe.
# @note This context should be installed automatically through integrations,
# such as the {Intregrations::Rack::HTTPContext} rack middleware.
class HTTP < Context
@keyspace = :http

Expand Down
2 changes: 1 addition & 1 deletion lib/timber/current_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def reset(*args)
# # ... anything logged here will include the context ...
# end
# # Be sure to checkout Timber::Contexts! These are officially supported and many of these
# # will be automatically included via Timber::Probes
# # will be automatically included via Timber::Integrations
#
# @example Adding multiple contexts
# Timber::CurrentContext.with(context1, context2) { ... }
Expand Down
8 changes: 8 additions & 0 deletions lib/timber/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def as_json(options = {})
raise NotImplementedError.new
end

# This ensures that Timber events get logged as messages if they are passed to
# the standard ::Logger.
#
# See: https://github.com/ruby/ruby/blob/f6e77b9d3555c1fbaa8aab1cdc0bd6bde95f62c6/lib/logger.rb#L615
def inspect
message
end

def to_json(options = {})
as_json.to_json(options)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/timber/events.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "timber/events/controller_call"
require "timber/events/custom"
require "timber/events/exception"
require "timber/events/http_client_request"
require "timber/events/http_client_response"
require "timber/events/http_server_request"
require "timber/events/http_server_response"
require "timber/events/sql_query"
Expand Down
17 changes: 13 additions & 4 deletions lib/timber/events/controller_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module Events
#
# Processing by PagesController#home as HTML
#
# @note This event should be installed automatically through probes,
# such as the {Probes::ActionControllerLogSubscriber} probe.
# @note This event should be installed automatically through integrations,
# such as the {Integrations::ActionController::LogSubscriber} integration.
class ControllerCall < Timber::Event
attr_reader :controller, :action, :params_json, :format
attr_reader :controller, :action, :params, :format

def initialize(attributes)
@controller = attributes[:controller] || raise(ArgumentError.new(":controller is required"))
@action = attributes[:action] || raise(ArgumentError.new(":action is required"))
@params_json = attributes[:params] && attributes[:params].to_json
@params = attributes[:params]
@format = attributes[:format]
end

Expand All @@ -35,6 +35,15 @@ def message
end
message
end

private
def params_json
@params_json ||= if params.nil? || params == {}
nil
else
params.to_json
end
end
end
end
end
7 changes: 4 additions & 3 deletions lib/timber/events/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Timber
module Events
# The exception event is used to track exceptions.
#
# @note This event should be installed automatically through probes,
# such as the {Probes::ActionDispatchDebugExceptions} probe.
# @note This event should be installed automatically through integrations,
# such as the {Integrations::ActionDispatch::DebugExceptions} integration.
class Exception < Timber::Event
attr_reader :name, :exception_message, :backtrace

Expand All @@ -16,7 +16,8 @@ def initialize(attributes)
raise(ArgumentError.new(":backtrace is required"))
end

@backtrace = backtrace.collect { |line| parse_backtrace_line(line) }
# 10 items max
@backtrace = backtrace[0..9].collect { |line| parse_backtrace_line(line) }
end

def to_hash
Expand Down
60 changes: 60 additions & 0 deletions lib/timber/events/http_client_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Timber
module Events
# The HTTP client request event tracks *outgoing* HTTP requests giving you structured insight
# into communication with external services.
#
# @note This event should be installed automatically through integrations,
# such as the {Integrations::NetHTTP} integration.
class HTTPClientRequest < Timber::Event
attr_reader :body, :headers, :host, :method, :path, :port, :query_string, :request_id,
:scheme, :service_name

def initialize(attributes)
@body = Util::HTTPEvent.normalize_body(attributes[:body])
@headers = Util::HTTPEvent.normalize_headers(attributes[:headers])
@host = attributes[:host] || raise(ArgumentError.new(":host is required"))
@method = Util::HTTPEvent.normalize_method(attributes[:method]) || raise(ArgumentError.new(":method is required"))
@path = attributes[:path] || raise(ArgumentError.new(":path is required"))
@port = attributes[:port]
@query_string = Util::HTTPEvent.normalize_query_string(attributes[:query_string])
@request_id = attributes[:request_id]
@scheme = attributes[:scheme] || raise(ArgumentError.new(":scheme is required"))
@service_name = attributes[:service_name]
end

def to_hash
{body: body, headers: headers, host: host, method: method, path: path, port: port,
query_string: query_string, request_id: request_id, scheme: scheme,
service_name: service_name}
end
alias to_h to_hash

def as_json(_options = {})
{:server_side_app => {:http_client_request => to_hash}}
end

def message
message = 'Outgoing HTTP request to '

if service_name
mesage << " #{service_name} [#{method}] #{full_path}"
else
message << " [#{method}] #{full_url}"
end
end

def status_description
Rack::Utils::HTTP_STATUS_CODES[status]
end

private
def full_path
Util::HTTPEvent.full_path(path, query_string)
end

def full_url
"#{scheme}#{host}#{full_path}"
end
end
end
end
46 changes: 46 additions & 0 deletions lib/timber/events/http_client_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Timber
module Events
# The HTTP client response event tracks responses for *outgoing* HTTP *requests*.
# This gives you structured insight into communication with external services.
#
# @note This event should be installed automatically through integrations,
# such as the {Integrations::NetHTTP} integration.
class HTTPClientResponse < Timber::Event
attr_reader :body, :headers, :request_id, :service_name, :status, :time_ms

def initialize(attributes)
@body = Util::HTTPEvent.normalize_body(attributes[:body])
@headers = Util::HTTPEvent.normalize_headers(attributes[:headers])
@request_id = attributes[:request_id]
@service_name = attributes[:service_name]
@status = attributes[:status] || raise(ArgumentError.new(":status is required"))
@time_ms = attributes[:time_ms] || raise(ArgumentError.new(":time_ms is required"))
@time_ms = @time_ms.round(6)
end

def to_hash
{body: body, headers: headers, request_id: request_id, service_name: service_name,
status: status, time_ms: time_ms}
end
alias to_h to_hash

def as_json(_options = {})
{:server_side_app => {:http_client_response => to_hash}}
end

def message
message = "Outgoing HTTP response"

if service_name
message << " from #{service_name}"
end

message << " #{status_description} in #{time_ms}ms"
end

def status_description
Rack::Utils::HTTP_STATUS_CODES[status]
end
end
end
end
21 changes: 12 additions & 9 deletions lib/timber/events/http_server_request.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
module Timber
module Events
# The HTTP request event tracks incoming HTTP requests.
# The HTTP server request event tracks incoming HTTP requests to your HTTP server.
# Such as unicorn, webrick, puma, etc.
#
# @note This event should be installed automatically through probes,
# such as the {Probes::ActionControllerLogSubscriber} probe.
# @note This event should be installed automatically through integrations,
# such as the {Integrations::ActionController::LogSubscriber} integration.
class HTTPServerRequest < Timber::Event
attr_reader :headers, :host, :method, :path, :port, :query_string, :request_id, :scheme
attr_reader :body, :headers, :host, :method, :path, :port, :query_string, :request_id,
:scheme

def initialize(attributes)
@headers = attributes[:headers]
@body = Util::HTTPEvent.normalize_body(attributes[:body])
@headers = Util::HTTPEvent.normalize_headers(attributes[:headers])
@host = attributes[:host] || raise(ArgumentError.new(":host is required"))
@method = attributes[:method] || raise(ArgumentError.new(":method is required"))
@method = Util::HTTPEvent.normalize_method(attributes[:method]) || raise(ArgumentError.new(":method is required"))
@path = attributes[:path] || raise(ArgumentError.new(":path is required"))
@port = attributes[:port]
@query_string = attributes[:query_string]
@request_id = attributes[:request_id]
@query_string = Util::HTTPEvent.normalize_query_string(attributes[:query_string])
@scheme = attributes[:scheme] || raise(ArgumentError.new(":scheme is required"))
@request_id = attributes[:request_id]
end

def to_hash
{headers: headers, host: host, method: method, path: path, port: port,
{body: body, headers: headers, host: host, method: method, path: path, port: port,
query_string: query_string, request_id: request_id, scheme: scheme}
end
alias to_h to_hash
Expand Down
19 changes: 10 additions & 9 deletions lib/timber/events/http_server_response.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
module Timber
module Events
# The HTTP response event tracks outgoing HTTP request responses.
# The HTTP server response event tracks outgoing HTTP responses that you send
# to clients.
#
# @note This event should be installed automatically through probes,
# such as the {Probes::ActionControllerLogSubscriber} probe.
# @note This event should be installed automatically through integrations,
# such as the {Integrations::ActionController::LogSubscriber} integration.
class HTTPServerResponse < Timber::Event
attr_reader :headers, :status, :time_ms
attr_reader :body, :headers, :request_id, :status, :time_ms

def initialize(attributes)
@body = Util::HTTPEvent.normalize_body(attributes[:body])
@headers = Util::HTTPEvent.normalize_headers(attributes[:headers])
@request_id = attributes[:request_id]
@status = attributes[:status] || raise(ArgumentError.new(":status is required"))
@time_ms = attributes[:time_ms] || raise(ArgumentError.new(":time_ms is required"))
@time_ms = @time_ms.round(6)
@additions = attributes[:additions]
end

def to_hash
{headers: headers, status: status, time_ms: time_ms}
{body: body, headers: headers, request_id: request_id, status: status, time_ms: time_ms}
end
alias to_h to_hash

Expand All @@ -24,9 +27,7 @@ def as_json(_options = {})
end

def message
message = "Completed #{status} #{status_description} in #{time_ms}ms"
message << " (#{additions.join(" | ".freeze)})" unless additions.empty?
message
"Completed #{status} #{status_description} in #{time_ms}ms"
end

def status_description
Expand Down
4 changes: 2 additions & 2 deletions lib/timber/events/sql_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Timber
module Events
# The SQL query event tracks sql queries to your database.
#
# @note This event should be installed automatically through probes,
# such as the {Probes::ActiveRecordLogSubscriber} probe.
# @note This event should be installed automatically through integrations,
# such as the {Integrations::ActiveRecord::LogSubscriber} integration.
class SQLQuery < Timber::Event
attr_reader :sql, :time_ms, :message

Expand Down
4 changes: 2 additions & 2 deletions lib/timber/events/template_render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Timber
module Events
# The template render event track template renderings and their performance.
#
# @note This event should be installed automatically through probes,
# such as the {Probes::ActionViewLogSubscriber} probe.
# @note This event should be installed automatically through integrations,
# such as the {Integrations::ActionView::LogSubscriber} integration.
class TemplateRender < Timber::Event
attr_reader :message, :name, :time_ms

Expand Down
Loading

0 comments on commit 672978f

Please sign in to comment.