Skip to content

LoggingHandler does not respect OTEL_ATTRIBUTE_COUNT_LIMIT environment variable #4671

@giongto35

Description

@giongto35

Describe your environment

  • OpenTelemetry Python SDK version: 1.35.0.dev0
  • Python version: 3.10.0
  • Platform: Linux

What happened?

The LoggingHandler in the OpenTelemetry Python SDK does not respect the OTEL_ATTRIBUTE_COUNT_LIMIT environment variable. When this environment variable is set, it should limit the number of attributes in log records, but the LoggingHandler ignores this setting entirely and only use the default 128.
It means:

  • If we set to small count limit like 3, logging 10 attributes doesnt' get affected.
  • If we set to high count limit like 256, logging 200 attributes get attributes dropped.

Steps to Reproduce

  1. Set the OTEL_ATTRIBUTE_COUNT_LIMIT environment variable
  2. Use the LoggingHandler to emit log records with more attributes than the limit
  3. Observe that all attributes are preserved instead of being limited

Minimal reproduction code:

import os
import logging
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler, LogRecordProcessor, LogData

# Set attribute count limit to 3
os.environ['OTEL_ATTRIBUTE_COUNT_LIMIT'] = '3'

class TestProcessor(LogRecordProcessor):
    def __init__(self):
        self.log_data_emitted = []
    def on_emit(self, log_data: LogData):
        self.log_data_emitted.append(log_data)
    def shutdown(self): pass
    def force_flush(self, timeout_millis: int = 30000): pass

# Set up logging with LoggingHandler
logger_provider = LoggerProvider()
processor = TestProcessor()
logger_provider.add_log_record_processor(processor)

logger = logging.getLogger('test')
handler = LoggingHandler(level=logging.WARNING, logger_provider=logger_provider)
logger.addHandler(handler)

# Create log with many extra attributes (should be limited to 3)
extra_attrs = {f'custom_attr_{i}': f'value_{i}' for i in range(10)}
logger.warning('Test message', extra=extra_attrs)

# Check results
log_record = processor.log_data_emitted[0].log_record
print(f'Total attributes: {len(log_record.attributes)}')  # Should be 3
print(f'Dropped attributes: {log_record.dropped_attributes}')  # Should be 10

Expected Result

With OTEL_ATTRIBUTE_COUNT_LIMIT=3:

  • Total attributes: 3 (limited by the environment variable)
  • Dropped attributes: 10 (the excess attributes that were dropped)
  • Behavior: Only the first 3 attributes should be kept, the rest should be dropped

Actual Result

With OTEL_ATTRIBUTE_COUNT_LIMIT=3:

  • Total attributes: 13 (10 custom + 3 code attributes, no limit applied)
  • Dropped attributes: 0 (no attributes were dropped)
  • Behavior: All attributes are preserved, the environment variable is completely ignored

Additional context

The issue is in the LoggingHandler._translate() method in /opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py around line 650. When creating a LogRecord, the method doesn't pass any limits parameter:

return LogRecord(
    timestamp=timestamp,
    observed_timestamp=observered_timestamp,
    context=get_current() or None,
    severity_text=level_name,
    severity_number=severity_number,
    body=body,
    resource=logger.resource,
    attributes=attributes,
    # Missing: limits=LogLimits() that respects environment variables
)

This causes the LogRecord to use the default _UnsetLogLimits which doesn't enforce any attribute limits.

Beside

The early return doesn't allow attribute count to read from Environment when the value is unset

Would you like to implement a fix?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions