Skip to content

Commit 239a161

Browse files
authored
Add sentry.origin to log events (#2712)
* feat(logging): support for :origin attribute * fix(logging): add missing :origin to logger patch * fix(logging): add missing :origin to rails logging * Update CHANGELOG
1 parent 21d4d57 commit 239a161

File tree

13 files changed

+96
-8
lines changed

13 files changed

+96
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
## Unreleased
22

3+
### Features
4+
5+
- Support for `:origin` attribute in log events ([#2712](https://github.com/getsentry/sentry-ruby/pull/2712))
6+
37
### Bug Fixes
48

59
- Skip including `sentry.message.template` in the log event attributes if there are no interpolation parameters provided ([#2700](https://github.com/getsentry/sentry-ruby/pull/2700))
610
- Respect `log_level` when logging via `:std_lib_logger` patch ([#2709](https://github.com/getsentry/sentry-ruby/pull/2709))
11+
- Add `sentry.origin` attribute to log events ([#2712](https://github.com/getsentry/sentry-ruby/pull/2712))
712

813
## 5.27.0
914

10-
### Feature
15+
### Features
1116

1217
- Propagated sampling rates as specified in [Traces](https://develop.sentry.dev/sdk/telemetry/traces/#propagated-random-value) docs ([#2671](https://github.com/getsentry/sentry-ruby/pull/2671))
1318
- Support for Rails ActiveSupport log subscribers ([#2690](https://github.com/getsentry/sentry-ruby/pull/2690))

sentry-rails/lib/sentry/rails/log_subscriber.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module Rails
2727
# end
2828
# end
2929
class LogSubscriber < ActiveSupport::LogSubscriber
30+
ORIGIN = "auto.logger.rails.log_subscriber"
31+
3032
class << self
3133
if ::Rails.version.to_f < 6.0
3234
# Rails 5.x does not provide detach_from
@@ -51,8 +53,9 @@ def detach_from(namespace, notifications = ActiveSupport::Notifications)
5153
# @param message [String] The log message
5254
# @param level [Symbol] The log level (:trace, :debug, :info, :warn, :error, :fatal)
5355
# @param attributes [Hash] Additional structured attributes to include
54-
def log_structured_event(message:, level: :info, attributes: {})
55-
Sentry.logger.public_send(level, message, **attributes)
56+
# @param origin [String] The origin of the log event
57+
def log_structured_event(message:, level: :info, attributes: {}, origin: ORIGIN)
58+
Sentry.logger.public_send(level, message, **attributes, origin: origin)
5659
rescue => e
5760
# Silently handle any errors in logging to avoid breaking the application
5861
Sentry.configuration.sdk_logger.debug("Failed to log structured event: #{e.message}")

sentry-rails/spec/sentry/rails/log_subscribers/action_controller_subscriber_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
expect(log_event[:attributes][:method][:value]).to eq("GET")
3636
expect(log_event[:attributes][:path][:value]).to eq("/world")
3737
expect(log_event[:attributes][:format][:value]).to eq(:html)
38+
expect(log_event[:attributes]["sentry.origin"][:value]).to eq("auto.logger.rails.log_subscriber")
3839
end
3940

4041
it "logs bad requests appropriately" do

sentry-rails/spec/sentry/rails/log_subscribers/action_mailer_subscriber_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
expect(log_event[:attributes][:duration_ms][:value]).to be > 0
3939
expect(log_event[:attributes][:perform_deliveries][:value]).to be true
4040
expect(log_event[:attributes][:delivery_method][:value]).to eq(:test)
41+
expect(log_event[:attributes]["sentry.origin"][:value]).to eq("auto.logger.rails.log_subscriber")
4142
expect(log_event[:attributes][:date]).to be_present
4243
end
4344

sentry-rails/spec/sentry/rails/log_subscribers/active_job_subscriber_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
expect(log_event[:level]).to eq("info")
2727
expect(log_event[:attributes][:job_class][:value]).to eq("NormalJob")
2828
expect(log_event[:attributes][:duration_ms][:value]).to be > 0
29+
expect(log_event[:attributes]["sentry.origin"][:value]).to eq("auto.logger.rails.log_subscriber")
2930
end
3031

3132
it "logs job enqueue events when jobs are enqueued" do

sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
expect(log_event[:level]).to eq("info")
2626
expect(log_event[:attributes][:sql][:value]).to include("INSERT INTO")
2727
expect(log_event[:attributes][:duration_ms][:value]).to be > 0
28+
expect(log_event[:attributes]["sentry.origin"][:value]).to eq("auto.logger.rails.log_subscriber")
2829
end
2930

3031
it "logs SELECT queries with proper attributes" do

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ def capture_check_in(slug, status, **options)
500500
# @param [Hash] options Extra log event options
501501
# @option options [Symbol] level The log level (:trace, :debug, :info, :warn, :error, :fatal)
502502
# @option options [Integer] severity The severity number according to the Sentry Logs Protocol
503+
# @option options [String] origin The origin of the log event (e.g., "auto.db.rails", "manual")
503504
# @option options [Hash] Additional attributes to include with the log
504505
#
505506
# @example Direct usage (prefer using Sentry.logger instead)

sentry-ruby/lib/sentry/client.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ def event_from_check_in(
195195
def event_from_log(message, level:, **options)
196196
return unless configuration.sending_allowed?
197197

198-
attributes = options.reject { |k, _| k == :level || k == :severity }
198+
attributes = options.reject { |k, _| k == :level || k == :severity || k == :origin }
199+
origin = options[:origin]
199200

200-
LogEvent.new(level: level, body: message, attributes: attributes)
201+
LogEvent.new(level: level, body: message, attributes: attributes, origin: origin)
201202
end
202203

203204
# Initializes an Event object with the given Transaction object.

sentry-ruby/lib/sentry/log_event.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class LogEvent
2929
"sentry.address" => :server_name,
3030
"sentry.sdk.name" => :sdk_name,
3131
"sentry.sdk.version" => :sdk_version,
32-
"sentry.message.template" => :template
32+
"sentry.message.template" => :template,
33+
"sentry.origin" => :origin
3334
}
3435

3536
PARAMETER_PREFIX = "sentry.message.parameter"
@@ -42,7 +43,7 @@ class LogEvent
4243

4344
LEVELS = %i[trace debug info warn error fatal].freeze
4445

45-
attr_accessor :level, :body, :template, :attributes, :user
46+
attr_accessor :level, :body, :template, :attributes, :user, :origin
4647

4748
attr_reader :configuration, *(SERIALIZEABLE_ATTRIBUTES - %i[level body attributes])
4849

@@ -82,6 +83,7 @@ def initialize(configuration: Sentry.configuration, **options)
8283
@template = @body if is_template?
8384
@attributes = options[:attributes] || DEFAULT_ATTRIBUTES
8485
@user = options[:user] || {}
86+
@origin = options[:origin]
8587
@contexts = {}
8688
end
8789

sentry-ruby/lib/sentry/std_lib_logger.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module StdLibLogger
1212
4 => :fatal
1313
}.freeze
1414

15+
ORIGIN = "auto.logger.ruby.std_logger"
16+
1517
def add(severity, message = nil, progname = nil, &block)
1618
result = super
1719

@@ -35,7 +37,7 @@ def add(severity, message = nil, progname = nil, &block)
3537
message = message.to_s.strip
3638

3739
if !message.nil? && message != Sentry::Logger::PROGNAME && method = SEVERITY_MAP[severity]
38-
Sentry.logger.send(method, message)
40+
Sentry.logger.send(method, message, origin: ORIGIN)
3941
end
4042
end
4143

0 commit comments

Comments
 (0)