fix AttributePropagatingSpanProcessor with NonRecordingSpan as parent#744
fix AttributePropagatingSpanProcessor with NonRecordingSpan as parent#744zhongkechen wants to merge 3 commits into
Conversation
|
I partially am okay with these changes, though I don't agree with this span processor working with NonRecording Spans since there's no SpanKind. |
There was a problem hiding this comment.
instead of isinstance(parent_span, ReadableSpan), could we use parent_span.is_recording() instead?
The OTel spec defines that NonRecordingSpan.is_recording() must return False, so it's a cleaner check that doesn't require importing ReadableSpan and aligns directly with the spec's contract:
elif parent_span and parent_span.is_recording() and _is_server_kind(parent_span):
propagation_data = self._propagation_data_extractor(parent_span)
elif parent_span and parent_span.is_recording():
propagation_data = parent_span.attributes.get(self._propagation_data_key)
|
@zhongkechen Please add unit tests for this change as well and a |
I'm using I thought I should keep the check logic consistent at least in the same method |
I see though I'd still suggest using what is OTel spec aligned instead of using |
|
I don't think Since this is an SDK plugin it should be fine for it to introspect API spans to get at the underlying SDK spans, which is where |
I'm okay with the changes then, @zhongkechen please add unit tests and a changelog entry and we can help merge this |
0ac5569 to
c6b79ce
Compare
|
added a unit test case |
| from amazon.opentelemetry.distro._aws_attribute_keys import AWS_CONSUMER_PARENT_SPAN_KIND, AWS_SDK_DESCENDANT, \ | ||
| AWS_TRACE_FLAG_SAMPLED | ||
| from amazon.opentelemetry.distro._aws_span_processing_util import get_ingress_operation | ||
| from amazon.opentelemetry.distro.attribute_propagating_span_processor import AttributePropagatingSpanProcessor |
There was a problem hiding this comment.
Bug: NonRecordingSpan is defined in opentelemetry.trace, not opentelemetry.sdk.trace. This import will raise an ImportError at runtime. The correct import should add NonRecordingSpan to the opentelemetry.trace import line instead (line 11 in the base file).
| self.assertEqual(grand_child_span.attributes.get(SpanAttributes.CLOUD_RESOURCE_ID), cloud_resource_id) | ||
|
|
||
| def test_attributes_propagation_with_non_recording_span(self): | ||
| parent_span: Span = NonRecordingSpan(SpanContext(1, 2, True, TraceFlags.SAMPLED, TraceState.get_default())) |
There was a problem hiding this comment.
The test assertion only checks AWS_TRACE_FLAG_SAMPLED, which is unconditionally set on every span (line 87 of the source) regardless of parent type. This means the test would pass even without the bug fix. To properly validate the fix, consider asserting that the propagation-data attribute is NOT set on the child (since a NonRecordingSpan parent cannot provide it), e.g.: self.assertIsNone(child_span.attributes.get(_SPAN_NAME_KEY)). This directly tests the code path that was broken before the fix.
| if not _is_server_kind(span): | ||
| propagation_data = self._propagation_data_extractor(span) | ||
| elif parent_span and _is_server_kind(parent_span): | ||
| elif parent_span and isinstance(parent_span, ReadableSpan) and _is_server_kind(parent_span): |
There was a problem hiding this comment.
The fix is correct. Minor suggestion: line 49 assigns parent_span with the type annotation ReadableSpan, but get_current_span() can actually return any Span (including NonRecordingSpan). Consider changing the annotation to Span to make it obvious why the isinstance checks are necessary.
Issue #, if available:
Fixes: #743
Description of changes:
aws-otel-python-instrumentation/aws-opentelemetry-distro/src/amazon/opentelemetry/distro/attribute_propagating_span_processor.py
Lines 80 to 81 in 28fc34e
When the
parent_spanisn't aReadableSpan(e.g. aNonRecordingSpan),_is_server_kindwould throw an attribute exception because the other types of span may not have thekindattribute.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.