|
17 | 17 | cast, |
18 | 18 | ) |
19 | 19 |
|
| 20 | +import temporalio.activity |
| 21 | +import temporalio.api.common.v1 |
| 22 | +import temporalio.client |
| 23 | +import temporalio.converter |
| 24 | +import temporalio.exceptions |
| 25 | +import temporalio.worker |
| 26 | +import temporalio.workflow |
| 27 | +from temporalio.exceptions import ApplicationError, ApplicationErrorCategory |
| 28 | +from typing_extensions import Protocol, TypeAlias, TypedDict |
| 29 | + |
20 | 30 | import opentelemetry.baggage.propagation |
21 | 31 | import opentelemetry.context |
22 | 32 | import opentelemetry.context.context |
|
26 | 36 | import opentelemetry.trace.propagation.tracecontext |
27 | 37 | import opentelemetry.util.types |
28 | 38 | from opentelemetry.context import Context |
29 | | -from opentelemetry.trace import Span, SpanKind, Status, StatusCode, _Links |
30 | | -from opentelemetry.util import types |
31 | | -from typing_extensions import Protocol, TypeAlias, TypedDict |
32 | | - |
33 | | -import temporalio.activity |
34 | | -import temporalio.api.common.v1 |
35 | | -import temporalio.client |
36 | | -import temporalio.converter |
37 | | -import temporalio.exceptions |
38 | | -import temporalio.worker |
39 | | -import temporalio.workflow |
40 | | -from temporalio.exceptions import ApplicationError, ApplicationErrorCategory |
| 39 | +from opentelemetry.trace import Status, StatusCode |
41 | 40 |
|
42 | 41 | # OpenTelemetry dynamically, lazily chooses its context implementation at |
43 | 42 | # runtime. When first accessed, they use pkg_resources.iter_entry_points + load. |
@@ -173,29 +172,37 @@ def _start_as_current_span( |
173 | 172 | kind: opentelemetry.trace.SpanKind, |
174 | 173 | context: Optional[Context] = None, |
175 | 174 | ) -> Iterator[None]: |
176 | | - with self.tracer.start_as_current_span( |
177 | | - name, |
178 | | - attributes=attributes, |
179 | | - kind=kind, |
180 | | - context=context, |
181 | | - set_status_on_exception=False, |
182 | | - ) as span: |
183 | | - if input: |
184 | | - input.headers = self._context_to_headers(input.headers) |
185 | | - try: |
186 | | - yield None |
187 | | - except Exception as exc: |
188 | | - if ( |
189 | | - not isinstance(exc, ApplicationError) |
190 | | - or exc.category != ApplicationErrorCategory.BENIGN |
191 | | - ): |
192 | | - span.set_status( |
193 | | - Status( |
194 | | - status_code=StatusCode.ERROR, |
195 | | - description=f"{type(exc).__name__}: {exc}", |
| 175 | + if context: |
| 176 | + token = opentelemetry.context.attach(context) |
| 177 | + else: |
| 178 | + token = None |
| 179 | + try: |
| 180 | + with self.tracer.start_as_current_span( |
| 181 | + name, |
| 182 | + attributes=attributes, |
| 183 | + kind=kind, |
| 184 | + context=context, |
| 185 | + set_status_on_exception=False, |
| 186 | + ) as span: |
| 187 | + if input: |
| 188 | + input.headers = self._context_to_headers(input.headers) |
| 189 | + try: |
| 190 | + yield None |
| 191 | + except Exception as exc: |
| 192 | + if ( |
| 193 | + not isinstance(exc, ApplicationError) |
| 194 | + or exc.category != ApplicationErrorCategory.BENIGN |
| 195 | + ): |
| 196 | + span.set_status( |
| 197 | + Status( |
| 198 | + status_code=StatusCode.ERROR, |
| 199 | + description=f"{type(exc).__name__}: {exc}", |
| 200 | + ) |
196 | 201 | ) |
197 | | - ) |
198 | | - raise |
| 202 | + raise |
| 203 | + finally: |
| 204 | + if token: |
| 205 | + opentelemetry.context.detach(token) |
199 | 206 |
|
200 | 207 | def _completed_workflow_span( |
201 | 208 | self, params: _CompletedWorkflowSpanParams |
|
0 commit comments