Skip to content

[pdata] Add AsReadOnly function to pcommon.Map and pcommon.Slice#12995

Closed
edmocosta wants to merge 4 commits into
open-telemetry:mainfrom
edmocosta:add-as-read-only-pcommon-func
Closed

[pdata] Add AsReadOnly function to pcommon.Map and pcommon.Slice#12995
edmocosta wants to merge 4 commits into
open-telemetry:mainfrom
edmocosta:add-as-read-only-pcommon-func

Conversation

@edmocosta
Copy link
Copy Markdown

Description

This PR adds the function AsReadOnly to the pcommon.Map and pcommon.Slice, which goal is to allow API users to create read-only shallow copies of those objects.

This utility is specially useful when those types are passed around, and no modification is expected to be performed on them. For example, a few OTTL functions are currently leveraging those objects mutability to set their values (see), causing problems and unexpected behavior. Entirely copying them could be an alternative to avoid that, but it's too expensive.

Testing

Unit tests

@edmocosta edmocosta requested review from a team, bogdandrutu and dmitryax as code owners May 8, 2025 10:29
@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.48%. Comparing base (8568c97) to head (79d1eaf).
⚠️ Report is 185 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #12995   +/-   ##
=======================================
  Coverage   91.48%   91.48%           
=======================================
  Files         506      506           
  Lines       28557    28563    +6     
=======================================
+ Hits        26125    26131    +6     
  Misses       1917     1917           
  Partials      515      515           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@edmocosta
Copy link
Copy Markdown
Author

edmocosta commented May 19, 2025

it seems the contrib-tests (processor/deltatocumulativeprocessor) are hanging as it uses some equality check that does not handle infinite recursion (see here). It basically goes through all exported methods of the struct, finds the new AsReadOnly function, and starts an infinite looping trying to reach the end of the struct hierarchy, calling AsReadOnly over and over again.

@edmocosta
Copy link
Copy Markdown
Author

Do you have any thoughts on this @open-telemetry/collector-approvers? If you agree with this feature, I can manage pinging processor/deltatocumulativeprocessor codeowners so we can fix that failing test I mentioned here.

Copy link
Copy Markdown
Member

@bogdandrutu bogdandrutu left a comment

Choose a reason for hiding this comment

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

I don't see a good enough reason here to open this functionality to any sub-type and to possibly limit our future development of this feature because of this.

@edmocosta
Copy link
Copy Markdown
Author

edmocosta commented Jun 9, 2025

I don't see a good enough reason here to open this functionality to any sub-type and to possibly limit our future development of this feature because of this.

I don't see how this functionality would limit future development, it's leveraring what's already implemented, and I think any behavior changes on that API would be considered a breaking change anyway.

I'm fine if you folks disagree on adding it, the goal was avoiding coping/wrapping maps & slices over and over again when read-only instances are desired (probably the same use-case that led implementing the existing logic). IMO, those data structures, especially maps, already have some overhead for accessing specific values, and adding another layer for copying it before accessing them would make it even slower. Anyway, that's an OTTL issue, and if this feature is not added, we will need to find another alternative for that @TylerHelmuth @evan-bradley.

Thanks.

@github-actions
Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions Bot added Stale and removed Stale labels Jun 26, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions Bot added the Stale label Jul 12, 2025
@github-actions
Copy link
Copy Markdown
Contributor

Closed as inactive. Feel free to reopen if this PR is still being worked on.

@github-actions github-actions Bot closed this Jul 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants