Skip to content

Commit

Permalink
fix: review suggestions, renaming and except guard
Browse files Browse the repository at this point in the history
  • Loading branch information
claudinoac committed Aug 6, 2024
1 parent 3f73592 commit ee1468e
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions elasticsearch/_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@


class OpenTelemetry:
current_context: dict[str, str] = {}
context_carrier: dict[str, str] = {}

def __init__(
self,
Expand Down Expand Up @@ -80,8 +80,7 @@ def span(

span_name = endpoint_id or method
with self.tracer.start_as_current_span(span_name) as otel_span:
self.current_context = {}
TraceContextTextMapPropagator().inject(self.current_context)
TraceContextTextMapPropagator().inject(self.context_carrier)
otel_span.set_attribute("http.request.method", method)
otel_span.set_attribute("db.system", "elasticsearch")
if endpoint_id is not None:
Expand All @@ -98,12 +97,13 @@ def span(
@contextlib.contextmanager
def recover_parent_context(self) -> Generator[None, None, None]:
token = None
if self.current_context:
if self.context_carrier:
otel_parent_ctx = TraceContextTextMapPropagator().extract(
carrier=self.current_context
carrier=self.context_carrier
)
token = otel_context.attach(otel_parent_ctx)
yield

if token:
otel_context.detach(token)
try:
yield
finally:
if token:
otel_context.detach(token)

0 comments on commit ee1468e

Please sign in to comment.