Skip to content

Commit

Permalink
Close spans when process shuts down before the exporter shuts down an…
Browse files Browse the repository at this point in the history
…d drops them (#108)
  • Loading branch information
alexmojaki authored May 3, 2024
1 parent 70192d5 commit 73a1314
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions logfire/_internal/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import atexit
import sys
import traceback
import typing
import warnings
from functools import cached_property
from functools import cached_property, partial
from time import time
from types import TracebackType
from typing import TYPE_CHECKING, Any, Callable, ContextManager, Iterable, Literal, Sequence, TypeVar, Union, cast
Expand Down Expand Up @@ -1105,16 +1106,19 @@ def shutdown(self, timeout_millis: int = 30_000, flush: bool = True) -> bool: #
class FastLogfireSpan:
"""A simple version of `LogfireSpan` optimized for auto-tracing."""

__slots__ = ('_span', '_token')
__slots__ = ('_span', '_token', '_atexit')

def __init__(self, span: trace_api.Span) -> None:
self._span = span
self._token = context_api.attach(trace_api.set_span_in_context(self._span))
self._atexit = partial(self.__exit__, None, None, None)
atexit.register(self._atexit)

def __enter__(self) -> FastLogfireSpan:
return self

def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: Any) -> None:
atexit.unregister(self._atexit)
context_api.detach(self._token)
_exit_span(self._span, exc_value)
self._span.end()
Expand All @@ -1139,6 +1143,7 @@ def __init__(
self._token: None | object = None
self._span: None | trace_api.Span = None
self.end_on_exit = True
self._atexit: Callable[[], None] | None = None

if not TYPE_CHECKING: # pragma: no branch

Expand All @@ -1154,12 +1159,19 @@ def __enter__(self) -> LogfireSpan:
)
if self._token is None: # pragma: no branch
self._token = context_api.attach(trace_api.set_span_in_context(self._span))

self._atexit = partial(self.__exit__, None, None, None)
atexit.register(self._atexit)

return self

def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: Any) -> None:
if self._token is None: # pragma: no cover
return

if self._atexit: # pragma: no branch
atexit.unregister(self._atexit)

context_api.detach(self._token)
self._token = None

Expand Down

0 comments on commit 73a1314

Please sign in to comment.