99 # All spans created by this tracer are flat children of the Restate trace.
1010"""
1111
12- from opentelemetry .trace import INVALID_SPAN , use_span , Tracer , TracerProvider
12+ from typing import Optional , Iterator , Sequence
13+
14+ from opentelemetry import context as context_api
15+ from opentelemetry .trace import INVALID_SPAN , Span , SpanKind , Tracer , TracerProvider , use_span , Link
1316from opentelemetry .trace .propagation .tracecontext import TraceContextTextMapPropagator
17+ from opentelemetry .util import types
18+ from opentelemetry .util ._decorator import _agnosticcontextmanager
1419from restate .server_context import (
1520 current_context ,
1621 get_extension_data ,
@@ -58,15 +63,15 @@ def _get_root_context():
5863
5964 def start_span (
6065 self ,
61- name ,
62- context = None ,
63- kind = None ,
64- attributes = None ,
65- links = None ,
66- start_time = None ,
67- record_exception = True ,
68- set_status_on_exception = True ,
69- ):
66+ name : str ,
67+ context : Optional [ context_api . Context ] = None ,
68+ kind : SpanKind = SpanKind . INTERNAL ,
69+ attributes : types . Attributes = None ,
70+ links : Optional [ Sequence [ Link ]] = None ,
71+ start_time : Optional [ int ] = None ,
72+ record_exception : bool = True ,
73+ set_status_on_exception : bool = True ,
74+ ) -> Span :
7075 if restate_context_is_replaying .get (False ):
7176 return INVALID_SPAN
7277 root = self ._get_root_context ()
@@ -85,34 +90,38 @@ def start_span(
8590 self ._track_span (span )
8691 return span
8792
93+ @_agnosticcontextmanager
8894 def start_as_current_span (
8995 self ,
90- name ,
91- context = None ,
92- kind = None ,
93- attributes = None ,
94- links = None ,
95- start_time = None ,
96- record_exception = True ,
97- set_status_on_exception = True ,
98- end_on_exit = True ,
99- ):
96+ name : str ,
97+ context : Optional [ context_api . Context ] = None ,
98+ kind : SpanKind = SpanKind . INTERNAL ,
99+ attributes : types . Attributes = None ,
100+ links : Optional [ Sequence [ Link ]] = None ,
101+ start_time : Optional [ int ] = None ,
102+ record_exception : bool = True ,
103+ set_status_on_exception : bool = True ,
104+ end_on_exit : bool = True ,
105+ ) -> Iterator [ Span ] :
100106 if restate_context_is_replaying .get (False ):
101- return use_span (INVALID_SPAN , end_on_exit = False )
102- root = self ._get_root_context ()
103- if root is not None :
104- context = root
105- return self ._tracer .start_as_current_span (
106- name ,
107- context = context ,
108- kind = kind ,
109- attributes = attributes ,
110- links = links ,
111- start_time = start_time ,
112- record_exception = record_exception ,
113- set_status_on_exception = set_status_on_exception ,
114- end_on_exit = end_on_exit ,
115- )
107+ with use_span (INVALID_SPAN , end_on_exit = False ) as span :
108+ yield span
109+ else :
110+ root = self ._get_root_context ()
111+ if root is not None :
112+ context = root
113+ with self ._tracer .start_as_current_span (
114+ name ,
115+ context = context ,
116+ kind = kind ,
117+ attributes = attributes ,
118+ links = links ,
119+ start_time = start_time ,
120+ record_exception = record_exception ,
121+ set_status_on_exception = set_status_on_exception ,
122+ end_on_exit = end_on_exit ,
123+ ) as span :
124+ yield span
116125
117126 @staticmethod
118127 def _track_span (span ):
0 commit comments