-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add error reporters and JSON log formatter #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
04f78d0
feat(error-reporting): add configurable error reporters
mhenrixon 516209a
feat(logging): add JSON log formatter and structured logging
mhenrixon c180994
fix: address PR review feedback
mhenrixon cf28317
fix: add action key to executor error context for consistency
mhenrixon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Pgbus | ||
| # Central error reporting module. Iterates all configured error reporters | ||
| # and logs the error. Inspired by Sidekiq's error_handlers pattern. | ||
| # | ||
| # Usage: | ||
| # Pgbus::ErrorReporter.report(exception, { queue: "default" }) | ||
| # | ||
| # Configuration: | ||
| # Pgbus.configure do |c| | ||
| # c.error_reporters << ->(ex, ctx) { Appsignal.set_error(ex) { |t| t.set_tags(ctx) } } | ||
| # end | ||
| module ErrorReporter | ||
| module_function | ||
|
|
||
| def report(exception, context = {}, config: Pgbus.configuration) | ||
| log_error(exception, context, config: config) | ||
|
|
||
| config.error_reporters.each do |handler| | ||
| call_handler(handler, exception, context, config) | ||
| rescue Exception => e # rubocop:disable Lint/RescueException | ||
| config.logger.error { "[Pgbus] Error reporter raised: #{e.class}: #{e.message}" } | ||
| end | ||
| rescue Exception # rubocop:disable Lint/RescueException | ||
| # ErrorReporter must never raise — callers sit inside rescue blocks | ||
| # where an unexpected raise would break fault-tolerance invariants. | ||
| nil | ||
| end | ||
|
|
||
| def call_handler(handler, exception, context, config) | ||
| target = handler.is_a?(Proc) ? handler : handler.method(:call) | ||
| if target.arity == 3 || (target.arity.negative? && target.parameters.size >= 3) | ||
| handler.call(exception, context, config) | ||
| else | ||
| handler.call(exception, context) | ||
| end | ||
| end | ||
|
|
||
| def log_error(exception, context, config:) | ||
| config.logger.error do | ||
| msg = "[Pgbus] #{exception.class}: #{exception.message}" | ||
| msg += " (#{context.inspect})" unless context.empty? | ||
| msg | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,96 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "json" | ||
| require "logger" | ||
| require "time" | ||
|
|
||
| module Pgbus | ||
| # Log formatters for Pgbus, inspired by Sidekiq::Logger::Formatters. | ||
| # | ||
| # Usage: | ||
| # Pgbus.configure do |c| | ||
| # c.logger.formatter = Pgbus::LogFormatter::JSON.new | ||
| # end | ||
| # | ||
| # Or via the convenience config option: | ||
| # Pgbus.configure do |c| | ||
| # c.log_format = :json | ||
| # end | ||
| module LogFormatter | ||
| module_function | ||
|
|
||
| def tid | ||
| Thread.current[:pgbus_tid] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36) | ||
| end | ||
|
|
||
| # Thread-local context for structured logging. Works like | ||
| # Sidekiq::Context — any key/value pairs set via with_context | ||
| # appear in the JSON output under the "ctx" key. | ||
| def with_context(hash) | ||
| orig = current_context.dup | ||
| current_context.merge!(hash) | ||
| yield | ||
| ensure | ||
| Thread.current[:pgbus_log_context] = orig | ||
| end | ||
|
|
||
| def current_context | ||
| Thread.current[:pgbus_log_context] ||= {} | ||
| end | ||
|
|
||
| # Human-readable text formatter with Pgbus context. | ||
| # Output: "INFO 2024-01-15T10:30:00.000Z pid=1234 tid=abc queue=default: message\n" | ||
| class Text < ::Logger::Formatter | ||
| def call(severity, time, _progname, message) | ||
| "#{severity} #{time.utc.iso8601(3)} pid=#{::Process.pid} tid=#{LogFormatter.tid}#{format_context}: #{message}\n" | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def format_context | ||
| ctx = LogFormatter.current_context | ||
| return "" if ctx.empty? | ||
|
|
||
| " #{ctx.map { |k, v| "#{k}=#{v}" }.join(" ")}" | ||
| end | ||
| end | ||
|
|
||
| # JSON formatter for structured logging. Each log line is a single | ||
| # JSON object followed by a newline. Extracts the [Pgbus::Component] | ||
| # prefix from messages into a separate "component" field. | ||
| # | ||
| # Output fields: | ||
| # ts — ISO 8601 timestamp with milliseconds | ||
| # pid — process ID | ||
| # tid — thread ID (short hex) | ||
| # lvl — severity (DEBUG/INFO/WARN/ERROR/FATAL) | ||
| # msg — the log message (with component prefix stripped) | ||
| # component — extracted from [Pgbus] or [Pgbus::Foo] prefix (optional) | ||
| # ctx — thread-local context hash (optional, only when non-empty) | ||
| class JSON < ::Logger::Formatter | ||
| COMPONENT_PREFIX = /\A\[([^\]]+)\]\s*/ | ||
|
|
||
| def call(severity, time, _progname, message) | ||
| msg = message.to_s | ||
| hash = { | ||
| ts: time.utc.iso8601(3), | ||
| pid: ::Process.pid, | ||
| tid: LogFormatter.tid, | ||
| lvl: severity | ||
| } | ||
|
|
||
| if (match = msg.match(COMPONENT_PREFIX)) | ||
| hash[:component] = match[1] | ||
| msg = msg.sub(COMPONENT_PREFIX, "") | ||
| end | ||
|
|
||
| hash[:msg] = msg | ||
|
|
||
| ctx = LogFormatter.current_context | ||
| hash[:ctx] = ctx unless ctx.empty? | ||
|
|
||
| "#{::JSON.generate(hash)}\n" | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe Pgbus::Configuration do | ||
| subject(:config) { described_class.new } | ||
|
|
||
| describe "#error_reporters" do | ||
| it "defaults to an empty array" do | ||
| expect(config.error_reporters).to eq([]) | ||
| end | ||
|
|
||
| it "accepts callable objects" do | ||
| reporter = ->(ex, ctx) { [ex, ctx] } | ||
| config.error_reporters << reporter | ||
|
|
||
| expect(config.error_reporters).to contain_exactly(reporter) | ||
| end | ||
|
|
||
| it "can be replaced entirely" do | ||
| reporter = ->(ex, ctx) { [ex, ctx] } | ||
| config.error_reporters = [reporter] | ||
|
|
||
| expect(config.error_reporters).to contain_exactly(reporter) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.