Skip to content

LogRecord#attributes= setter does not update @total_recorded_attributes #2194

@ltickett

Description

@ltickett

Description of the bug

When mutating a LogRecord's attributes after initialization (e.g. in a custom LogRecordProcessor#on_emit), the attributes= setter updates the attributes hash but does not recalculate @total_recorded_attributes. This means to_log_record_data reports a stale count from initialization time.

There is no public API to update @total_recorded_attributes — the only workaround is instance_variable_set(:@total_recorded_attributes, attrs.size), which is fragile and depends on SDK internals.

Expected behavior: Either attributes= should automatically update @total_recorded_attributes, or a public method should be provided to recalculate it.

Relevant code: logs/lib/opentelemetry/sdk/logs/log_record.rb (https://github.com/open-telemetry/opentelemetry-ruby/blob/main/logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb) — @total_recorded_attributes is set once in initialize and never updated when attributes is reassigned via the attr_accessor.

Here's the merge request/project where I encountered this issue: https://gitlab.com/gitlab-org/developer-relations/contributor-success/contributors-gitlab-com/-/merge_requests/2325

Share details about your runtime

  • Operating system details: Linux, Debian 12 (bookworm), aarch64
  • RUBY_ENGINE: "ruby"
  • RUBY_VERSION: "4.0.5"
  • RUBY_DESCRIPTION: "ruby 4.0.5 (2026-05-20 revision 64336ffd0e) +PRISM aarch64-linux"

Share a simplified reproduction if possible

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  gem 'opentelemetry-api'
  gem 'opentelemetry-sdk'
  gem 'opentelemetry-logs-sdk'
end

require 'opentelemetry-api'
require 'opentelemetry-sdk'
require 'opentelemetry/sdk/logs'

log_record = OpenTelemetry::SDK::Logs::LogRecord.new(
  body: 'test message',
  attributes: { 'key1' => 'value1' }
)

puts "Initial attributes count: #{log_record.instance_variable_get(:@total_recorded_attributes)}"
# => 1

log_record.attributes = { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }

puts "Attributes after reassignment: #{log_record.attributes.size}"
# => 3
puts "total_recorded_attributes (stale): #{log_record.instance_variable_get(:@total_recorded_attributes)}"
# => 1 (BUG: should be 3)

data = log_record.to_log_record_data
puts "Reported in log_record_data: #{data.total_recorded_attributes}"
# => 1 (stale)

Tip: React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions