diff --git a/logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb b/logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb index d2d6f0f544..840cb47175 100644 --- a/logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb +++ b/logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb @@ -18,7 +18,6 @@ class LogRecord < OpenTelemetry::Logs::LogRecord :severity_text, :severity_number, :body, - :attributes, :event_name, :trace_id, :span_id, @@ -26,6 +25,8 @@ class LogRecord < OpenTelemetry::Logs::LogRecord :resource, :instrumentation_scope + attr_reader :attributes + # Creates a new {LogRecord}. # # @param [optional Time] timestamp Time when the event occurred. @@ -81,7 +82,6 @@ def initialize( @severity_text = severity_text @severity_number = severity_number @body = body - @attributes = attributes&.to_h # We need a mutable copy of attributes @event_name = event_name @trace_id = trace_id @span_id = span_id @@ -89,6 +89,18 @@ def initialize( @resource = resource @instrumentation_scope = instrumentation_scope @log_record_limits = log_record_limits || LogRecordLimits::DEFAULT + self.attributes = attributes + end + + # Sets the attributes for this {LogRecord}, recalculating the total + # recorded attribute count and reapplying the configured attribute + # limits. + # + # @param [optional Hash{String => String, Numeric, Boolean, + # Array}] new_attributes Attributes to + # associate with the {LogRecord}. + def attributes=(new_attributes) + @attributes = new_attributes&.to_h # We need a mutable copy of attributes @total_recorded_attributes = @attributes&.size || 0 trim_attributes(@attributes) @@ -115,9 +127,7 @@ def to_log_record_data private def to_integer_nanoseconds(timestamp) - return unless timestamp.is_a?(Time) - - (timestamp.to_r * (10**9)).to_i + (timestamp.to_r * (10**9)).to_i if timestamp.is_a?(Time) end def trim_attributes(attributes) diff --git a/logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb b/logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb index dff217b520..d718a2b9ca 100644 --- a/logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb +++ b/logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb @@ -154,6 +154,36 @@ end end + describe '#attributes=' do + it 'updates total_recorded_attributes when reassigned' do + log_record = Logs::LogRecord.new(attributes: { 'key1' => 'value1' }) + assert_equal(1, log_record.instance_variable_get(:@total_recorded_attributes)) + + log_record.attributes = { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' } + + assert_equal(3, log_record.instance_variable_get(:@total_recorded_attributes)) + assert_equal(3, log_record.to_log_record_data.total_recorded_attributes) + end + + it 'sets total_recorded_attributes to 0 when set to nil' do + log_record = Logs::LogRecord.new(attributes: { 'key1' => 'value1' }) + + log_record.attributes = nil + + assert_equal(0, log_record.instance_variable_get(:@total_recorded_attributes)) + assert_nil(log_record.attributes) + end + + it 'applies attribute count limits when reassigned' do + limits = Logs::LogRecordLimits.new(attribute_count_limit: 1) + log_record = Logs::LogRecord.new(log_record_limits: limits, attributes: { 'a' => 'a' }) + + log_record.attributes = { 'old' => 'old', 'new' => 'new' } + + assert_equal({ 'new' => 'new' }, log_record.attributes) + end + end + describe 'attribute value limit' do it 'truncates the values that are too long' do length_limit = 32