Skip to content

Commit 411c6e3

Browse files
romank0Roman Konoval
authored andcommitted
Merge branch 'main' into baggage-propagation-fix
2 parents f03ddc2 + 9bee5ea commit 411c6e3

File tree

3 files changed

+367
-36
lines changed

3 files changed

+367
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ temporalio/bridge/temporal_sdk_bridge*
1010
/sdk-python.iml
1111
/.zed
1212
*.DS_Store
13+
tags

temporalio/contrib/opentelemetry.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
cast,
1818
)
1919

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+
2030
import opentelemetry.baggage.propagation
2131
import opentelemetry.context
2232
import opentelemetry.context.context
@@ -26,18 +36,7 @@
2636
import opentelemetry.trace.propagation.tracecontext
2737
import opentelemetry.util.types
2838
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
4140

4241
# OpenTelemetry dynamically, lazily chooses its context implementation at
4342
# runtime. When first accessed, they use pkg_resources.iter_entry_points + load.
@@ -173,29 +172,37 @@ def _start_as_current_span(
173172
kind: opentelemetry.trace.SpanKind,
174173
context: Optional[Context] = None,
175174
) -> 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+
)
196201
)
197-
)
198-
raise
202+
raise
203+
finally:
204+
if token:
205+
opentelemetry.context.detach(token)
199206

200207
def _completed_workflow_span(
201208
self, params: _CompletedWorkflowSpanParams

0 commit comments

Comments
 (0)