@@ -24,29 +24,41 @@ class Span(object):
2424 __slots__ = (
2525 "trace_id" ,
2626 "span_id" ,
27- "ref " ,
28- "ref_type " ,
27+ "parent_span_id " ,
28+ "same_process_as_parent " ,
2929 "sampled" ,
3030 "transaction" ,
31+ "op" ,
32+ "description" ,
33+ "start_timestamp" ,
34+ "timestamp" ,
3135 "_tags" ,
36+ "_data" ,
3237 "_finished_spans" ,
33- "start" ,
34- "end" ,
3538 )
3639
3740 def __init__ (
38- self , trace_id , span_id , transaction = None , ref = None , ref_type = None , sampled = None
41+ self ,
42+ trace_id ,
43+ span_id ,
44+ parent_span_id = None ,
45+ same_process_as_parent = True ,
46+ sampled = None ,
47+ transaction = None ,
48+ op = None ,
49+ description = None ,
3950 ):
4051 self .trace_id = trace_id
4152 self .span_id = span_id
42- self .ref = ref
43- self .ref_type = ref_type
53+ self .parent_span_id = parent_span_id
54+ self .same_process_as_parent = same_process_as_parent
4455 self .sampled = sampled
4556 self .transaction = transaction
4657 self ._tags = {}
58+ self ._data = {}
4759 self ._finished_spans = []
48- self .start = datetime .now ()
49- self .end = None
60+ self .start_timestamp = datetime .now ()
61+ self .timestamp = None
5062
5163 def __repr__ (self ):
5264 return "<%s(transaction=%r, trace_id=%r, span_id=%r, ref=%r)>" % (
@@ -58,24 +70,18 @@ def __repr__(self):
5870 )
5971
6072 @classmethod
61- def start_trace (cls , transaction = None , sampled = None ):
62- return cls (
63- transaction = transaction ,
64- trace_id = uuid .uuid4 ().hex ,
65- span_id = uuid .uuid4 ().hex [16 :],
66- sampled = sampled ,
67- )
73+ def start_trace (cls , ** kwargs ):
74+ return cls (trace_id = uuid .uuid4 ().hex , span_id = uuid .uuid4 ().hex [16 :], ** kwargs )
6875
69- def new_span (self , ref_type = "child" ):
76+ def new_span (self , ** kwargs ):
7077 if self .trace_id is None :
7178 return Span .start_trace ()
7279
7380 rv = Span (
7481 trace_id = self .trace_id ,
7582 span_id = uuid .uuid4 ().hex [16 :],
76- ref = self ,
77- ref_type = ref_type ,
78- sampled = self .sampled ,
83+ parent_span_id = self .span_id ,
84+ ** kwargs
7985 )
8086 rv ._finished_spans = self ._finished_spans
8187 return rv
@@ -89,7 +95,7 @@ def continue_from_headers(cls, headers):
8995 parent = cls .from_traceparent (headers .get ("sentry-trace" ))
9096 if parent is None :
9197 return cls .start_trace ()
92- return parent .new_span ("follows_from" )
98+ return parent .new_span (same_process_as_parent = False )
9399
94100 def iter_headers (self ):
95101 yield "sentry-trace" , self .to_traceparent ()
@@ -110,31 +116,34 @@ def from_traceparent(cls, traceparent):
110116 if span_id is not None :
111117 span_id = int (span_id , 16 )
112118 if sampled is not None :
113- sampled = sampled == "1 "
119+ sampled = sampled != "0 "
114120
115121 return cls (trace_id = trace_id , span_id = span_id , sampled = sampled )
116122
117123 def to_traceparent (self ):
118- return "%s-%s-%s" % (
119- self .trace_id ,
120- self .span_id ,
121- "1" if not self .sampled else "0" ,
122- )
124+ return "%s-%s-%s" % (self .trace_id , self .span_id , "1" if self .sampled else "0" )
123125
124126 def set_tag (self , key , value ):
125127 self ._tags [key ] = value
126128
129+ def set_data (self , key , value ):
130+ self ._data [key ] = value
131+
127132 def finish (self ):
128- self .end = datetime .now ()
133+ self .timestamp = datetime .now ()
129134 self ._finished_spans .append (self )
130135
131136 def to_json (self ):
132137 return {
133138 "trace_id" : self .trace_id ,
134139 "span_id" : self .span_id ,
135- "ref_span_id " : self .ref and self . ref . span_id or None ,
140+ "parent_span_id " : self .parent_span_id ,
136141 "transaction" : self .transaction ,
137142 "tags" : self ._tags ,
138- "start" : self .start ,
139- "end" : self .end ,
143+ "data" : self ._data ,
144+ "start_timestamp" : self .start_timestamp ,
145+ "timestamp" : self .timestamp ,
140146 }
147+
148+ def get_trace_context (self ):
149+ return {"trace_id" : self .trace_id , "span_id" : self .span_id }
0 commit comments