Skip to content

Commit 25a7835

Browse files
committed
feat(logging): support for :origin attribute
1 parent 4c02988 commit 25a7835

5 files changed

Lines changed: 78 additions & 4 deletions

File tree

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: 5 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,14 +43,15 @@ 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

4950
SERIALIZERS = %i[
5051
attributes
5152
body
5253
level
54+
origin
5355
parent_span_id
5456
sdk_name
5557
sdk_version
@@ -82,6 +84,7 @@ def initialize(configuration: Sentry.configuration, **options)
8284
@template = @body if is_template?
8385
@attributes = options[:attributes] || DEFAULT_ATTRIBUTES
8486
@user = options[:user] || {}
87+
@origin = options[:origin]
8588
@contexts = {}
8689
end
8790

sentry-ruby/spec/sentry/log_event_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
expect(event.body).to eq("User John has logged in!")
2121
end
2222

23+
it "accepts origin parameter" do
24+
event = described_class.new(
25+
configuration: configuration,
26+
level: :info,
27+
body: "Database query executed",
28+
origin: "auto.db.rails"
29+
)
30+
31+
expect(event.origin).to eq("auto.db.rails")
32+
end
33+
2334
it "accepts attributes" do
2435
attributes = {
2536
"sentry.message.template" => "User %s has logged in!",
@@ -172,5 +183,30 @@
172183
expect(hash[:attributes]["user.name"]).to eq("john_doe")
173184
expect(hash[:attributes]["user.email"]).to eq("john@example.com")
174185
end
186+
187+
it "includes sentry.origin attribute when origin is set" do
188+
event = described_class.new(
189+
configuration: configuration,
190+
level: :info,
191+
body: "Database query executed",
192+
origin: "auto.db.rails"
193+
)
194+
195+
hash = event.to_hash
196+
197+
expect(hash[:attributes]["sentry.origin"]).to eq({ value: "auto.db.rails", type: "string" })
198+
end
199+
200+
it "does not include sentry.origin attribute when origin is nil" do
201+
event = described_class.new(
202+
configuration: configuration,
203+
level: :info,
204+
body: "Manual log message"
205+
)
206+
207+
hash = event.to_hash
208+
209+
expect(hash[:attributes]).not_to have_key("sentry.origin")
210+
end
175211
end
176212
end

sentry-ruby/spec/sentry_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,39 @@
399399
expect(log_event[:trace_id]).to_not be(nil)
400400
expect(log_event[:attributes]).to have_key("sentry.trace.parent_span_id")
401401
end
402+
403+
it "includes sentry.origin attribute when origin is provided" do
404+
expect do
405+
described_class.capture_log("Database query executed", level: :info, origin: "auto.logger.rails.log_subscriber")
406+
end.to_not change { sentry_events.count }
407+
408+
Sentry.get_current_client.flush
409+
410+
expect(sentry_envelopes.count).to eq(1)
411+
412+
log_event = sentry_logs.first
413+
414+
expect(log_event[:level]).to eq("info")
415+
expect(log_event[:body]).to eq("Database query executed")
416+
expect(log_event[:attributes]).to have_key("sentry.origin")
417+
expect(log_event[:attributes]["sentry.origin"]).to eq({ value: "auto.logger.rails.log_subscriber", type: "string" })
418+
end
419+
420+
it "does not include sentry.origin attribute when origin is not provided" do
421+
expect do
422+
described_class.capture_log("Manual log message", level: :info)
423+
end.to_not change { sentry_events.count }
424+
425+
Sentry.get_current_client.flush
426+
427+
expect(sentry_envelopes.count).to eq(1)
428+
429+
log_event = sentry_logs.first
430+
431+
expect(log_event[:level]).to eq("info")
432+
expect(log_event[:body]).to eq("Manual log message")
433+
expect(log_event[:attributes]).not_to have_key("sentry.origin")
434+
end
402435
end
403436

404437
describe ".start_transaction" do

0 commit comments

Comments
 (0)