-
-
Notifications
You must be signed in to change notification settings - Fork 539
fix: prevent SDK crash when SDK logging fails #2817
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 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
287f2e5
Add mise.toml to .gitignore
dingsdax a0626b6
fix: prevent SDK crash when SDK logging fails
dingsdax fc69fa2
Rubo👮❤️
dingsdax f5fce71
Update CHANGELOG.md
dingsdax 69cf109
use exception message instead of original message
dingsdax c270877
dry & add sanitized original msg in stderr output
dingsdax 8d9149c
more Rubo👮❤️
dingsdax 48f80a6
guard against any error in stderr logging
dingsdax 89dd377
Add stacktrace to stderr logging
dingsdax be4cb34
🧽 🧽 🧽
dingsdax 8057a97
Merge branch 'master' into sdk_logger_resiliency
dingsdax 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ node_modules | |
| .devcontainer/.env | ||
| vendor/gems | ||
| sentry-rails/Gemfile-*.lock | ||
| mise.toml | ||
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,122 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Sentry::LoggingHelper do | ||
| let(:string_io) { StringIO.new } | ||
| let(:logger) { Logger.new(string_io) } | ||
|
|
||
| let(:helper_class) do | ||
| Class.new do | ||
| include Sentry::LoggingHelper | ||
| attr_accessor :sdk_logger | ||
|
|
||
| def initialize(sdk_logger) | ||
| @sdk_logger = sdk_logger | ||
| end | ||
| end | ||
| end | ||
|
|
||
| let(:logger_helper) { helper_class.new(logger) } | ||
|
|
||
| describe "#log_error" do | ||
| it "logs exception message with description" do | ||
| exception = StandardError.new("Something went wrong") | ||
| logger_helper.log_error("test_error", exception) | ||
|
|
||
| expect(string_io.string).to include("test_error: Something went wrong") | ||
| end | ||
|
|
||
| it "includes backtrace when debug is true" do | ||
| exception = StandardError.new("Error") | ||
| exception.set_backtrace(["it_broke.rb:1"]) | ||
|
|
||
| logger_helper.log_error("test_error", exception, debug: true) | ||
|
|
||
| expect(string_io.string).to include("it_broke.rb:1") | ||
| end | ||
| end | ||
|
|
||
| describe "stderr fallback when logger fails" do | ||
| shared_examples "falls back to stderr" do |method_name, *args| | ||
| it "outputs to stderr with error class and message" do | ||
| broken_logger = Class.new do | ||
| def debug(*); raise IOError, "oops"; end | ||
| def warn(*); raise IOError, "oops"; end | ||
| end.new | ||
|
|
||
| helper = helper_class.new(broken_logger) | ||
|
|
||
| expect($stderr).to receive(:puts).with(/Sentry SDK logging failed \(IOError:/) | ||
| expect { helper.public_send(method_name, *args) }.not_to raise_error | ||
| end | ||
| end | ||
|
|
||
| context "#log_debug" do | ||
| include_examples "falls back to stderr", :log_debug, "Debug message" | ||
| end | ||
|
|
||
| context "#log_warn" do | ||
| include_examples "falls back to stderr", :log_warn, "Warning message" | ||
| end | ||
|
|
||
| it "includes backtrace in stderr output" do | ||
| broken_logger = Class.new do | ||
| def error(*) | ||
| error = IOError.new("oops") | ||
| error.set_backtrace([ | ||
| "logger.rb:42:in `write'", | ||
| "logger.rb:10:in `error'" | ||
| ]) | ||
|
|
||
| raise error | ||
| end | ||
| end.new | ||
|
|
||
| helper = helper_class.new(broken_logger) | ||
|
|
||
| stderr_output = nil | ||
| expect($stderr).to receive(:puts) { |msg| stderr_output = msg } | ||
|
|
||
| helper.log_error("Test message", StandardError.new("Error")) | ||
|
|
||
| expect(stderr_output).to include("IOError: oops") | ||
| expect(stderr_output).to include("logger.rb:42:in `write'") | ||
| expect(stderr_output).to include("logger.rb:10:in `error'") | ||
| end | ||
| end | ||
|
|
||
| describe "custom JSON logger with encoding errors" do | ||
| # Custom logger from GitHub issue #2805 | ||
| let(:json_logger) do | ||
| Class.new(::Logger) do | ||
| class JsonFormatter | ||
| def call(level, _, _, m) | ||
| { severity: level, message: m }.to_json << "\n" | ||
| end | ||
| end | ||
|
|
||
| def initialize(*) | ||
| super | ||
| self.formatter = JsonFormatter.new | ||
| end | ||
| end.new(StringIO.new) | ||
| end | ||
|
|
||
| let(:logger_helper) { helper_class.new(json_logger) } | ||
|
|
||
| it "scrubs invalid UTF-8 in stderr output when JSON logger fails on encoding" do | ||
| helper = helper_class.new(json_logger) | ||
|
|
||
| invalid_message = "a\x92b" | ||
| exception = StandardError.new("oops") | ||
|
|
||
| stderr_message = nil | ||
| expect($stderr).to receive(:puts) { |msg| stderr_message = msg } | ||
|
|
||
| expect { helper.log_error(invalid_message, exception) }.not_to raise_error | ||
|
|
||
| expect(stderr_message).to include("JSON::GeneratorError") | ||
| expect(stderr_message).to include("a<?>b") | ||
| expect(stderr_message).to include("oops") | ||
| end | ||
| end | ||
| end |
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.