22# This product includes software developed at Datadog (https://www.datadoghq.com/).
33# Copyright 2021 Datadog, Inc.
44import pytest
5- from utils import weblog , interfaces , scenarios , features , logger
6- from utils .dd_types import DataDogAgentSpan , AgentTraceFormat
5+ from utils import weblog , interfaces , scenarios , features
6+ from utils .dd_types import DataDogAgentSpan
77from utils ._context .header_tag_vars import (
88 CONFIG_COLON_LEADING ,
99 CONFIG_COLON_TRAILING ,
@@ -426,79 +426,6 @@ def test_trace_header_tags(self):
426426 assert span_meta [RESPONSE_PREFIX + key .lower ()] == response_headers [key ]
427427
428428
429- # The Datadog specific tracecontext flags to mark flags are set
430- TRACECONTEXT_FLAGS_SET = 1 << 31
431-
432-
433- class SpanLink :
434- def __init__ (self , data : dict , trace_id : str , trace_id_low : int , trace_id_high : int ):
435- self ._data = data
436- self .trace_id = trace_id
437- self .trace_id_low = trace_id_low
438- self .trace_id_high = trace_id_high
439-
440- self .attributes : dict [str , str ] | None = data .get ("attributes" )
441- self .trace_state : str | None = data .get ("tracestate" , data .get ("trace_state" ))
442- self .flags : int = (data ["flags" ] | TRACECONTEXT_FLAGS_SET ) if "flags" in data else 0
443-
444- if "span_id" in self ._data : # span_id is a string on base 16
445- self .span_id = int (data ["span_id" ], base = 16 )
446- elif "spanID" in self ._data : # spanID is a string on base 10
447- self .span_id = int (data ["spanID" ])
448- else :
449- raise ValueError (f"No span id exists in span link: { data } " )
450-
451- @staticmethod
452- def from_span_links (data : dict ) -> "SpanLink" :
453- return SpanLink (
454- data , trace_id = data ["traceID" ], trace_id_high = int (data ["traceIDHigh" ]), trace_id_low = int (data ["traceID" ])
455- )
456-
457- @staticmethod
458- def from_efficient_trace_payload_format (data : dict ) -> "SpanLink" :
459- trace_id = data ["traceID" ]
460-
461- return SpanLink (
462- data ,
463- trace_id = trace_id ,
464- trace_id_high = (int (trace_id , 16 ) >> 64 ) & 0xFFFFFFFFFFFFFFFF ,
465- trace_id_low = int (trace_id , 16 ) & 0xFFFFFFFFFFFFFFFF ,
466- )
467-
468- @staticmethod
469- def from_legacy_format (data : dict ) -> "SpanLink" :
470- trace_id = data ["trace_id" ]
471-
472- return SpanLink (
473- data ,
474- trace_id = trace_id ,
475- trace_id_high = int (trace_id [:16 ], base = 16 ),
476- trace_id_low = int (trace_id [- 16 :], base = 16 ),
477- )
478-
479- def __str__ (self ) -> str :
480- return str (self ._data )
481-
482- def __repr__ (self ) -> str :
483- return repr (self ._data )
484-
485-
486- def get_span_links (span : DataDogAgentSpan ) -> list [SpanLink ]:
487- if span .get ("spanLinks" ) is not None :
488- logger .info ("Span links are stored inside span.spanLinks" )
489- return [SpanLink .from_span_links (data ) for data in span .get ("spanLinks" )]
490-
491- if span .trace .format == AgentTraceFormat .efficient_trace_payload_format and span .get ("links" ) is not None :
492- logger .info ("Span links are stored inside span.links and trace format is v1" )
493- return [SpanLink .from_efficient_trace_payload_format (data ) for data in span .get ("links" )]
494-
495- logger .info ("Span links are stored inside span.meta['_dd.span_links'] and trace format is legacy" )
496- raw = span .meta .get ("_dd.span_links" , [])
497- raw_deserilialized = json .loads (raw ) if isinstance (raw , (str , bytes , bytearray )) else raw
498-
499- return [SpanLink .from_legacy_format (data ) for data in raw_deserilialized ]
500-
501-
502429@scenarios .default
503430@features .context_propagation_extract_behavior
504431class Test_ExtractBehavior_Default :
@@ -528,7 +455,7 @@ def test_single_tracecontext(self):
528455 span = spans [0 ]
529456 assert trace_id == 1
530457 assert span .get ("parentID" ) == "1"
531- span_links = get_span_links (span )
458+ span_links = span . get_span_links ()
532459 assert len (span_links ) == 0 , "Expected span links to be absent"
533460
534461 # Test the next outbound span context
@@ -568,7 +495,7 @@ def test_multiple_tracecontexts(self):
568495 assert span .get ("parentID" ) == "2"
569496
570497 # Test the extracted span links: One span link per conflicting trace context
571- span_links = get_span_links (span )
498+ span_links = span . get_span_links ()
572499 assert len (span_links ) == 1 , "Expected span links to be present"
573500
574501 link = span_links [0 ]
@@ -619,7 +546,7 @@ def test_single_tracecontext(self):
619546 # Test the extracted span links: One span link for the incoming (Datadog trace context).
620547 # In the case that span links are generated for conflicting trace contexts, those span links
621548 # are not included in the new trace context
622- span_links = get_span_links (span )
549+ span_links = span . get_span_links ()
623550 assert len (span_links ) == 1 , "Expected span links to be present"
624551
625552 # Assert the Datadog (restarted) span link
@@ -670,7 +597,7 @@ def test_multiple_tracecontexts(self):
670597 # Test the extracted span links: One span link for the incoming (Datadog trace context).
671598 # In the case that span links are generated for conflicting trace contexts, those span links
672599 # are not included in the new trace context
673- span_links = get_span_links (span )
600+ span_links = span . get_span_links ()
674601 assert len (span_links ) == 1 , "Expected span links to be present"
675602
676603 # Assert the Datadog (restarted) span link
@@ -720,7 +647,7 @@ def test_multiple_tracecontexts_with_overrides(self):
720647 # Test the extracted span links: One span link for the incoming (Datadog trace context).
721648 # In the case that span links are generated for conflicting trace contexts, those span links
722649 # are not included in the new trace context
723- span_links = get_span_links (span )
650+ span_links = span . get_span_links ()
724651 assert len (span_links ) == 1 , "Expected span links to be present"
725652
726653 # Assert the Datadog (restarted) span link
@@ -756,7 +683,7 @@ def _get_otel_span(self, spans: list) -> DataDogAgentSpan:
756683 return span
757684 # Fallback: return whichever span has span links
758685 for span in spans :
759- if get_span_links (span ):
686+ if span . get_span_links ():
760687 return span
761688 return spans [0 ]
762689
@@ -783,7 +710,7 @@ def test_single_tracecontext(self):
783710 assert span .get ("traceID" ) != "1"
784711 assert span .get ("parentID" ) is None
785712
786- span_links = get_span_links (span )
713+ span_links = span . get_span_links ()
787714 assert len (span_links ) == 1 , "Expected span links to be present"
788715
789716 link = span_links [0 ]
@@ -822,7 +749,7 @@ def test_multiple_tracecontexts(self):
822749 assert span .get ("traceID" ) != "8687463697196027922"
823750 assert span .get ("parentID" ) is None
824751
825- span_links = get_span_links (span )
752+ span_links = span . get_span_links ()
826753 assert len (span_links ) == 1 , "Expected span links to be present"
827754
828755 link = span_links [0 ]
@@ -860,7 +787,7 @@ def test_multiple_tracecontexts_with_overrides(self):
860787 assert span .get ("traceID" ) != "1"
861788 assert span .get ("parentID" ) is None
862789
863- span_links = get_span_links (span )
790+ span_links = span . get_span_links ()
864791 assert len (span_links ) == 1 , "Expected span links to be present"
865792
866793 link = span_links [0 ]
@@ -904,7 +831,7 @@ def test_single_tracecontext(self):
904831 span = spans [0 ]
905832 assert span .get ("traceID" ) != "1"
906833 assert span .get ("parentID" ) is None
907- span_links = get_span_links (span )
834+ span_links = span . get_span_links ()
908835 assert len (span_links ) == 0 , "Expected span links to be absent"
909836
910837 # Test the next outbound span context
@@ -944,7 +871,7 @@ def test_multiple_tracecontexts(self):
944871 span .get ("traceID" ) != "8687463697196027922" # Lower 64-bits of traceparent
945872 )
946873 assert span .get ("parentID" ) is None
947- span_links = get_span_links (span )
874+ span_links = span . get_span_links ()
948875 assert len (span_links ) == 0 , "Expected span links to be absent"
949876
950877 # Test the next outbound span context
@@ -988,7 +915,7 @@ def test_single_tracecontext(self):
988915 # Test the extracted span links: One span link for the incoming (Datadog trace context).
989916 # In the case that span links are generated for conflicting trace contexts, those span links
990917 # are not included in the new trace context
991- span_links = get_span_links (span )
918+ span_links = span . get_span_links ()
992919 assert len (span_links ) == 1 , "Expected span links to be present"
993920
994921 # Assert the Datadog (restarted) span link
@@ -1039,7 +966,7 @@ def test_multiple_tracecontexts(self):
1039966 # Test the extracted span links: One span link for the incoming (Datadog trace context).
1040967 # In the case that span links are generated for conflicting trace contexts, those span links
1041968 # are not included in the new trace context
1042- span_links = get_span_links (span )
969+ span_links = span . get_span_links ()
1043970 assert len (span_links ) == 1 , "Expected span links to be present"
1044971
1045972 # Assert the Datadog (restarted) span link
0 commit comments