fix: no method error caused when there is a missmatch in context object#507
Merged
Conversation
Signed-off-by: Arjun Rajappa <arjun.rajappa@ibm.com>
6b234a9 to
c0c1ba2
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Fix NoMethodError for undefined valid? method on OpenTelemetry::Context
Problem
The application was crashing with an uncaught NoMethodError during span creation in the Rack middleware:
This error occurred in the request handling pipeline, blocking span creation and potentially causing application failures in production environments.
Root Cause
The code was calling valid?() directly on parent_span_context without first checking if the object responds to this method. In certain edge cases, the parent_span_context object may not have the valid? method available, leading to the NoMethodError.
Solution
Added a defensive guard using respond_to?(:valid?) before calling the valid? method:
Before:
if parent_span_context&.valid?After:
if parent_span_context&.respond_to?(:valid?) && parent_span_context.valid?This ensures the method exists on the object before attempting to call it, preventing the NoMethodError from being raised.
Impact
Prevents application crashes during span creation in the Rack instrumentation layer
Maintains backward compatibility with different OpenTelemetry context implementations
Graceful degradation when encountering unexpected context objects
Testing Notes
Since the exact conditions to reproduce this error could not be reliably replicated, this defensive programming approach provides the safest fix to prevent crashes in client applications while maintaining existing functionality.