fix(ottl): enforce valid types for setters in contexts (step 2) #44881
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Setters in OTTL contexts now validate that values are of the expected type and return descriptive errors when type mismatches occur, improving debugging experience. This is the continuation of work done in #43505, addressing remaining context such as datapoint, profile, resource, span, and span event.
Link to tracking issue
Completes the fix to #40198, part 1 was #43505.
Testing
Tests were adapted to conform to the new error statements. New tests were not added given the simplicity of where ctxutil.ExpectType is applied to and given ctxutil.ExpectType itself was added with proper tests. Introducing tests for each setter would just be a large amount of code duplication, testing the exact same code pattern over and over.
Note for reviewer
This PR is likely best reviewed without whitespace taken into consideration since most of the changes convert a block like
into
where any instruction or set of instructions will always be indented left after the refactor.
While it improves overall readability by putting the happy path logic outside a conditional, it makes reviewing the PR harder by touching a lot of code just for indentation.