@@ -109,13 +109,11 @@ def _span_flags(
109109 child_trace_flags : int , parent_span_context : Optional [SpanContext ]
110110) -> int :
111111 # Lower 8 bits: W3C TraceFlags
112- # Handle TraceFlags objects, regular ints, and test mocks
112+ # TraceFlags is an int subclass, but we handle Mock objects in tests
113113 try :
114- flags = (
115- int (child_trace_flags ) & PB2SpanFlags .SPAN_FLAGS_TRACE_FLAGS_MASK
116- )
117- except (TypeError , ValueError ):
118- # If conversion fails (e.g., Mock object), default to 0
114+ flags = child_trace_flags & PB2SpanFlags .SPAN_FLAGS_TRACE_FLAGS_MASK
115+ except TypeError :
116+ # If bitwise operation fails (e.g., Mock object in tests), default to 0
119117 flags = 0
120118 # Always indicate whether parent remote information is known
121119 flags |= PB2SpanFlags .SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
@@ -169,26 +167,14 @@ def _encode_links(links: Sequence[Link]) -> Sequence[PB2SPan.Link]:
169167 if links :
170168 pb2_links = []
171169 for link in links :
172- # For links, encode trace_flags and is_remote from the link's context
173- # Handle TraceFlags objects, regular ints, and test mocks
174- try :
175- flags = (
176- int (link .context .trace_flags )
177- & PB2SpanFlags .SPAN_FLAGS_TRACE_FLAGS_MASK
178- )
179- except (TypeError , ValueError ):
180- # If conversion fails (e.g., Mock object), default to 0
181- flags = 0
182- flags |= PB2SpanFlags .SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
183- if link .context .is_remote :
184- flags |= PB2SpanFlags .SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
185-
170+ # For links, we encode the link's own context (not treating it as parent-child)
171+ # The link context's is_remote indicates if the linked span is from a remote process
186172 encoded_link = PB2SPan .Link (
187173 trace_id = _encode_trace_id (link .context .trace_id ),
188174 span_id = _encode_span_id (link .context .span_id ),
189175 attributes = _encode_attributes (link .attributes ),
190176 dropped_attributes_count = link .dropped_attributes ,
191- flags = flags ,
177+ flags = _span_flags ( link . context . trace_flags , link . context ) ,
192178 )
193179 pb2_links .append (encoded_link )
194180 return pb2_links
0 commit comments