Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class LogRecord < OpenTelemetry::Logs::LogRecord
:severity_text,
:severity_number,
:body,
:attributes,
:event_name,
:trace_id,
:span_id,
:trace_flags,
:resource,
:instrumentation_scope

attr_reader :attributes

# Creates a new {LogRecord}.
#
# @param [optional Time] timestamp Time when the event occurred.
Expand Down Expand Up @@ -81,14 +82,25 @@ 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
@trace_flags = trace_flags
@resource = resource
@instrumentation_scope = instrumentation_scope
@log_record_limits = log_record_limits || LogRecordLimits::DEFAULT
self.attributes = attributes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting! Why go with self.attributes here? Just want to make sure I'm following the flow.

Comment thread
kaylareopelle marked this conversation as resolved.
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<String, Numeric, Boolean>}] 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
Comment thread
kaylareopelle marked this conversation as resolved.

trim_attributes(@attributes)
Expand All @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down