Skip to content

Conversation

giongto35
Copy link
Contributor

@giongto35 giongto35 commented Jul 10, 2025

Description

Fixes #4671

This PR fixes a bug where 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 was ignoring this setting entirely and only use the default value 128.
It means:

  • If we set to small count limit like 3, logging 10 attributes still works without any attributes dropped.
  • If we set to high count limit like 256, logging 200 attributes has (200-128= 72) attributes dropped.

Changes Made

Fixed the Root Cause Bug

The core issue was that LogRecord was created with _UnsetLogLimits which used UNSET values, and the LogLimits._from_env_if_absent method had a bug where it returned None immediately when value == cls.UNSET, never checking environment variables.

# Before (buggy): _UnsetLogLimits = LogLimits(max_attributes=LogLimits.UNSET, ...) # This caused _from_env_if_absent to return None immediately without reading env vars # After (fixed): # Removed _UnsetLogLimits entirely, now use None as default limits = limits or LogLimits() # LogLimits() correctly reads from env vars

Implementation Benefits

This implementation allows users to:

  • Rely on environment variables for configuration (OTEL_ATTRIBUTE_COUNT_LIMIT)
  • Use a cleaner, more intuitive API without special UNSET values

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Code refactoring (improves code quality without changing functionality)

How Has This Been Tested?

You can run the tests using pytest:

# Install the package in development mode pip install -e . # Run all the LoggingHandler tests pytest opentelemetry-sdk/tests/logs/test_handler.py -v # Run all logging tests pytest opentelemetry-sdk/tests/logs/ -v # Run only the attribute count limit tests pytest opentelemetry-sdk/tests/logs/test_handler.py::TestLoggingHandler -k "otel_attribute_count_limit" -v

Test Coverage

Added comprehensive tests to verify the fix works correctly:

  1. test_otel_attribute_count_limit_respected_in_logging_handler - Tests that when OTEL_ATTRIBUTE_COUNT_LIMIT=3, only 3 attributes are kept and the rest are dropped
  2. test_otel_attribute_count_limit_includes_code_attributes - Tests that when OTEL_ATTRIBUTE_COUNT_LIMIT=5, only 5 attributes are kept and the limit applies to all attribute types including code attributes
  3. test_logging_handler_without_env_var_uses_default_limit - Tests that without the environment variable, the default limit of 128 is applied

All tests pass, confirming the fix works correctly across all these scenarios.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated
  • Fixed the original bug with OTEL_ATTRIBUTE_COUNT_LIMIT
  • Refactored code for better maintainability
  • Removed deprecated/unnecessary code patterns
  • All existing tests continue to pass
@giongto35 giongto35 requested a review from a team as a code owner July 10, 2025 02:37
Copy link

linux-foundation-easycla bot commented Jul 10, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@giongto35 giongto35 force-pushed the otel-attribute-count-not-respected branch from 889a3ec to d2ac2c3 Compare July 10, 2025 05:09
Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

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

@giongto35
Copy link
Contributor Author

Hi @pmcollins , can this change be merged so we can patch our code?

@pmcollins
Copy link
Member

Hi @pmcollins , can this change be merged so we can patch our code?

Hi @giongto35 -- we'll need one of the maintainers to review the change. Once a maintainer has approved, then the change can be merged.

In the meantime, I have approved the workflows to run.

@giongto35
Copy link
Contributor Author

Hi @aabmass @ocelotl @lzchen @xrmx , can anyone of you from maintainers can help me merge this change? Appreciate it.

@yurishkuro
Copy link
Member

@giongto35 avoid merging main unless there are conflicts, doing so just resets the CI checks and wastes maintainers' time as they need to re-approve CI runs.

@giongto35
Copy link
Contributor Author

giongto35 commented Jul 24, 2025

I see some failures in the checks https://github.com/open-telemetry/opentelemetry-python/pull/4677/checks . Is there anything I need to do to get it merged
"opentelemetry-sdk/tests/logs/test_handler.py:39:0: R0904: Too many public methods (21/20) (too-many-public-methods)"
Seems I needs to reduce number of tests

and @aabmass @ocelotl @lzchen @xrmx please help merge the change if it is ready, it is breaking our production.

@giongto35
Copy link
Contributor Author

giongto35 commented Aug 3, 2025

Ping @aabmass @ocelotl @lzchen @xrmx . It is breaking our production so we hope the issue is addressed ASAP. What is the ETA for this fix to be merged?

@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Aug 18, 2025
@xrmx
Copy link
Contributor

xrmx commented Sep 9, 2025

@giongto35 please fix linting failures (fine to add annotation to ignore the pylint error) and add a changelog entry

@xrmx xrmx moved this from Ready for review to Reviewed PR that needs fixing in @xrmx's Python PR digest Sep 9, 2025
@xrmx xrmx moved this from Reviewed PR that needs fixing to Approved PRs that need fixing in @xrmx's Python PR digest Sep 10, 2025
@xrmx xrmx enabled auto-merge (squash) September 11, 2025 07:29
@xrmx xrmx merged commit 712ed5d into open-telemetry:main Sep 11, 2025
370 of 371 checks passed
@github-project-automation github-project-automation bot moved this from Approved PRs that need fixes to Done in @xrmx's Python PR digest Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

7 participants