From 96d9187e1d0f0894c7ee700e3c2fe267b33ef0d2 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Fri, 6 Jun 2025 09:06:13 +0000 Subject: [PATCH 01/10] Simplify LogEvent --- sentry-ruby/lib/sentry/log_event.rb | 17 +++++++++++++---- sentry-ruby/lib/sentry/scope.rb | 14 ++++++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index a66828531..f03da1efa 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -4,7 +4,7 @@ module Sentry # Event type that represents a log entry with its attributes # # @see https://develop.sentry.dev/sdk/telemetry/logs/#log-envelope-item-payload - class LogEvent < Event + class LogEvent TYPE = "log" DEFAULT_PARAMETERS = [].freeze @@ -15,8 +15,12 @@ class LogEvent < Event level body timestamp + environment + release + server_name trace_id attributes + contexts ] SENTRY_ATTRIBUTES = { @@ -33,15 +37,20 @@ class LogEvent < Event attr_accessor :level, :body, :template, :attributes - def initialize(configuration: Sentry.configuration, **options) - super(configuration: configuration) + attr_reader :configuration, *SERIALIZEABLE_ATTRIBUTES + def initialize(configuration: Sentry.configuration, **options) + @configuration = configuration @type = TYPE + @server_name = configuration.server_name + @environment = configuration.environment + @release = configuration.release + @timestamp = Sentry.utc_now.iso8601 @level = options.fetch(:level) @body = options[:body] @template = @body if is_template? @attributes = options[:attributes] || DEFAULT_ATTRIBUTES - @contexts = DEFAULT_CONTEXT + @contexts = {} end def to_hash diff --git a/sentry-ruby/lib/sentry/scope.rb b/sentry-ruby/lib/sentry/scope.rb index 68dfc079c..235cbf4b5 100644 --- a/sentry-ruby/lib/sentry/scope.rb +++ b/sentry-ruby/lib/sentry/scope.rb @@ -46,7 +46,7 @@ def clear # @param hint [Hash] the hint data that'll be passed to event processors. # @return [Event] def apply_to_event(event, hint = nil) - unless event.is_a?(CheckInEvent) + unless event.is_a?(CheckInEvent) || event.is_a?(LogEvent) event.tags = tags.merge(event.tags) event.user = user.merge(event.user) event.extra = extra.merge(event.extra) @@ -54,7 +54,7 @@ def apply_to_event(event, hint = nil) event.transaction = transaction_name if transaction_name event.transaction_info = { source: transaction_source } if transaction_source event.fingerprint = fingerprint - event.level = level unless event.is_a?(LogEvent) + event.level = level event.breadcrumbs = breadcrumbs event.rack_env = rack_env if rack_env event.attachments = attachments @@ -62,10 +62,16 @@ def apply_to_event(event, hint = nil) if span event.contexts[:trace] ||= span.get_trace_context - event.dynamic_sampling_context ||= span.get_dynamic_sampling_context + + if event.respond_to?(:dynamic_sampling_context) + event.dynamic_sampling_context ||= span.get_dynamic_sampling_context + end else event.contexts[:trace] ||= propagation_context.get_trace_context - event.dynamic_sampling_context ||= propagation_context.get_dynamic_sampling_context + + if event.respond_to?(:dynamic_sampling_context) + event.dynamic_sampling_context ||= propagation_context.get_dynamic_sampling_context + end end all_event_processors = self.class.global_event_processors + @event_processors From 4af3b00f23a7cc90c98301a2d169f3d08e3a6463 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Fri, 6 Jun 2025 12:46:56 +0000 Subject: [PATCH 02/10] Optimize value_type --- sentry-ruby/lib/sentry/log_event.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index f03da1efa..d466f96f5 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -123,17 +123,15 @@ def attribute_hash(value) { value: value, type: value_type(value) } end + VALUE_TYPES = Hash.new("string").merge!({ + Integer => "integer", + TrueClass => "boolean", + FalseClass => "boolean", + Float => "double" + }).freeze + def value_type(value) - case value - when Integer - "integer" - when TrueClass, FalseClass - "boolean" - when Float - "double" - else - "string" - end + VALUE_TYPES[value.class] end def parameters From fb442ee9631e7d9309a4a3a0795a3ab79f2935a0 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Fri, 6 Jun 2025 12:57:40 +0000 Subject: [PATCH 03/10] Optimize serialize --- sentry-ruby/lib/sentry/log_event.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index d466f96f5..b96fa231f 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -39,6 +39,17 @@ class LogEvent attr_reader :configuration, *SERIALIZEABLE_ATTRIBUTES + SERIALIZERS = %i[ + attributes + body + level + parent_span_id + sdk_name + sdk_version + timestamp + trace_id + ].map { |name| [name, :"serialize_#{name}"] }.to_h + def initialize(configuration: Sentry.configuration, **options) @configuration = configuration @type = TYPE @@ -62,9 +73,9 @@ def to_hash private def serialize(name) - serializer = :"serialize_#{name}" + serializer = SERIALIZERS[name] - if respond_to?(serializer, true) + if serializer __send__(serializer) else public_send(name) From fe99701fc1a186cdb5c3cba016a54ca7752c7348 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Fri, 6 Jun 2025 13:03:30 +0000 Subject: [PATCH 04/10] Optimize serialize_attributes --- sentry-ruby/lib/sentry/log_event.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index b96fa231f..102f54015 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -117,8 +117,10 @@ def serialize_body end def serialize_attributes - hash = attributes.each_with_object({}) do |(key, value), memo| - memo[key] = attribute_hash(value) + hash = {} + + attributes.each do |key, value| + hash[key] = attribute_hash(value) end SENTRY_ATTRIBUTES.each do |key, name| From 4740c23b1e879c7e95b5811e0f10c7e50abce7ef Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 9 Jun 2025 11:33:14 +0000 Subject: [PATCH 05/10] Stop creating intermediate iso timestamps --- sentry-ruby/lib/sentry/log_event.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index 102f54015..7fbae41ad 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -56,7 +56,7 @@ def initialize(configuration: Sentry.configuration, **options) @server_name = configuration.server_name @environment = configuration.environment @release = configuration.release - @timestamp = Sentry.utc_now.iso8601 + @timestamp = Sentry.utc_now @level = options.fetch(:level) @body = options[:body] @template = @body if is_template? @@ -95,7 +95,7 @@ def serialize_sdk_version end def serialize_timestamp - Time.parse(timestamp).to_f + timestamp.to_f end def serialize_trace_id From 83aecd2999b74d432dd96b8bee71f144030c0aff Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 9 Jun 2025 11:50:46 +0000 Subject: [PATCH 06/10] Fix double-timestamping --- sentry-ruby/lib/sentry/client.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sentry-ruby/lib/sentry/client.rb b/sentry-ruby/lib/sentry/client.rb index 9e1b93098..27dff3f92 100644 --- a/sentry-ruby/lib/sentry/client.rb +++ b/sentry-ruby/lib/sentry/client.rb @@ -195,12 +195,7 @@ def event_from_log(message, level:, **options) attributes = options.reject { |k, _| k == :level || k == :severity } - LogEvent.new( - level: level, - body: message, - timestamp: Time.now.to_f, - attributes: attributes - ) + LogEvent.new(level: level, body: message, attributes: attributes) end # Initializes an Event object with the given Transaction object. From 151976ea681f7beee0da75820812705f7e7503d0 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 9 Jun 2025 11:56:31 +0000 Subject: [PATCH 07/10] Move VALUE_TYPES up --- sentry-ruby/lib/sentry/log_event.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index 7fbae41ad..358f47430 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -50,6 +50,13 @@ class LogEvent trace_id ].map { |name| [name, :"serialize_#{name}"] }.to_h + VALUE_TYPES = Hash.new("string").merge!({ + TrueClass => "boolean", + FalseClass => "boolean", + Integer => "integer", + Float => "double" + }).freeze + def initialize(configuration: Sentry.configuration, **options) @configuration = configuration @type = TYPE @@ -136,13 +143,6 @@ def attribute_hash(value) { value: value, type: value_type(value) } end - VALUE_TYPES = Hash.new("string").merge!({ - Integer => "integer", - TrueClass => "boolean", - FalseClass => "boolean", - Float => "double" - }).freeze - def value_type(value) VALUE_TYPES[value.class] end From ecfe433edc2cdb55fdc556b6972fb4f7cc8cde53 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 9 Jun 2025 11:57:37 +0000 Subject: [PATCH 08/10] Move TOKEN_REGEXP up --- sentry-ruby/lib/sentry/log_event.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index 358f47430..aa43d0cce 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -57,6 +57,8 @@ class LogEvent Float => "double" }).freeze + TOKEN_REGEXP = /%\{(\w+)\}/ + def initialize(configuration: Sentry.configuration, **options) @configuration = configuration @type = TYPE @@ -166,8 +168,6 @@ def parameters end end - TOKEN_REGEXP = /%\{(\w+)\}/ - def template_tokens @template_tokens ||= body.scan(TOKEN_REGEXP).flatten.map(&:to_sym) end From dc906a92ed5e4a94148a2ddad75f38bb139dbb62 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 9 Jun 2025 12:01:32 +0000 Subject: [PATCH 09/10] Remove unused DEFAULT_CONTEXT constant --- sentry-ruby/lib/sentry/log_event.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/sentry-ruby/lib/sentry/log_event.rb b/sentry-ruby/lib/sentry/log_event.rb index aa43d0cce..e7981379c 100644 --- a/sentry-ruby/lib/sentry/log_event.rb +++ b/sentry-ruby/lib/sentry/log_event.rb @@ -9,7 +9,6 @@ class LogEvent DEFAULT_PARAMETERS = [].freeze DEFAULT_ATTRIBUTES = {}.freeze - DEFAULT_CONTEXT = {}.freeze SERIALIZEABLE_ATTRIBUTES = %i[ level From cafd4cdcd3a99fbef6cb849db9d72afbc0841e33 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Mon, 9 Jun 2025 12:07:37 +0000 Subject: [PATCH 10/10] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8ddfb3d0..4a7c168cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - Support for `before_send_log` ([#2634](https://github.com/getsentry/sentry-ruby/pull/2634)) +### Bug Fixes + +- Structured logging consumes way less memory now ([#2643](https://github.com/getsentry/sentry-ruby/pull/2643)) + ## 5.24.0 ### Features