Skip to content

Commit a3742d6

Browse files
committed
Sync otel updates
1 parent 49489e0 commit a3742d6

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

src/libtmux/common.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
from ._compat import LooseVersion
2323
from ._internal import trace as libtmux_trace
2424

25+
try:
26+
from .otel import start_span
27+
except Exception: # pragma: no cover - optional dependency
28+
def start_span(name: str, **fields):
29+
return libtmux_trace.span(name, **fields)
30+
2531
if t.TYPE_CHECKING:
2632
from collections.abc import Callable
2733

@@ -41,11 +47,27 @@
4147

4248
_RUST_BACKEND = os.getenv("LIBTMUX_BACKEND") == "rust"
4349
_RUST_SERVER_CACHE: dict[
44-
tuple[str | None, str | None, int | None, str | None, str | None, bool | None],
50+
tuple[
51+
str | None,
52+
str | None,
53+
int | None,
54+
str | None,
55+
str | None,
56+
bool | None,
57+
str | None,
58+
],
4559
t.Any,
4660
] = {}
4761
_RUST_SERVER_CONFIG: dict[
48-
tuple[str | None, str | None, int | None, str | None, str | None, bool | None],
62+
tuple[
63+
str | None,
64+
str | None,
65+
int | None,
66+
str | None,
67+
str | None,
68+
bool | None,
69+
str | None,
70+
],
4971
set[str],
5072
] = {}
5173

@@ -88,7 +110,7 @@ def _rust_run_with_config(
88110
cmd_parts: list[str],
89111
cmd_list: list[str],
90112
) -> tuple[list[str], list[str], int, list[str]]:
91-
with libtmux_trace.span(
113+
with start_span(
92114
"rust_run_with_config",
93115
layer="tmux-bin",
94116
cmd=" ".join(cmd_parts),
@@ -182,13 +204,17 @@ def _rust_server(
182204
connection_kind = os.getenv("LIBTMUX_RUST_CONNECTION_KIND")
183205
server_kind = os.getenv("LIBTMUX_RUST_SERVER_KIND")
184206
control_autostart = _env_bool("LIBTMUX_RUST_CONTROL_AUTOSTART")
207+
mux_server_bin = os.getenv("LIBTMUX_RUST_MUX_SERVER_BIN") or os.getenv(
208+
"MUX_SERVER_BIN"
209+
)
185210
key = (
186211
socket_name,
187212
socket_path,
188213
colors,
189214
connection_kind,
190215
server_kind,
191216
control_autostart,
217+
mux_server_bin,
192218
)
193219
server = _RUST_SERVER_CACHE.get(key)
194220
if server is None:
@@ -201,14 +227,17 @@ def _rust_server(
201227
kwargs["server_kind"] = server_kind
202228
if control_autostart is not None:
203229
kwargs["control_autostart"] = control_autostart
204-
with libtmux_trace.span(
230+
if mux_server_bin:
231+
kwargs["mux_server_bin"] = mux_server_bin
232+
with start_span(
205233
"rust_server_init",
206234
layer="python",
207235
socket_name=socket_name,
208236
socket_path=socket_path,
209237
connection_kind=connection_kind,
210238
server_kind=server_kind,
211239
control_autostart=control_autostart,
240+
mux_server_bin=mux_server_bin,
212241
):
213242
server = rust_backend.Server(
214243
socket_path=socket_path,
@@ -249,7 +278,10 @@ def _rust_cmd_result(
249278
connection_kind = os.getenv("LIBTMUX_RUST_CONNECTION_KIND")
250279
server_kind = os.getenv("LIBTMUX_RUST_SERVER_KIND")
251280
control_autostart = _env_bool("LIBTMUX_RUST_CONTROL_AUTOSTART")
252-
with libtmux_trace.span(
281+
mux_server_bin = os.getenv("LIBTMUX_RUST_MUX_SERVER_BIN") or os.getenv(
282+
"MUX_SERVER_BIN"
283+
)
284+
with start_span(
253285
"rust_cmd_result",
254286
layer="python",
255287
cmd=" ".join(cmd_parts),
@@ -259,6 +291,7 @@ def _rust_cmd_result(
259291
connection_kind=connection_kind,
260292
server_kind=server_kind,
261293
control_autostart=control_autostart,
294+
mux_server_bin=mux_server_bin,
262295
):
263296
if connection_kind in {"bin", "tmux-bin"} and config_file:
264297
cmd_parts = ["-f", config_file, *cmd_parts]
@@ -272,11 +305,12 @@ def _rust_cmd_result(
272305
connection_kind,
273306
server_kind,
274307
control_autostart,
308+
mux_server_bin,
275309
)
276310
if config_file:
277311
loaded = _RUST_SERVER_CONFIG.setdefault(key, set())
278312
if config_file not in loaded:
279-
with libtmux_trace.span("rust_server_is_alive", layer="rust"):
313+
with start_span("rust_server_is_alive", layer="rust"):
280314
server_alive = bool(server.is_alive())
281315
if not server_alive:
282316
stdout_lines, stderr_lines, exit_code, cmd_args = (
@@ -293,7 +327,7 @@ def _rust_cmd_result(
293327
return stdout_lines, stderr_lines, exit_code, cmd_args
294328
quoted = shlex.quote(config_file)
295329
try:
296-
with libtmux_trace.span(
330+
with start_span(
297331
"rust_server_source_file",
298332
layer="rust",
299333
config_file=config_file,
@@ -310,7 +344,7 @@ def _rust_cmd_result(
310344

311345
cmd_line = " ".join(shlex.quote(part) for part in cmd_parts)
312346
try:
313-
with libtmux_trace.span(
347+
with start_span(
314348
"rust_server_cmd",
315349
layer="rust",
316350
cmd=cmd_line,
@@ -557,7 +591,7 @@ class tmux_cmd:
557591

558592
def __init__(self, *args: t.Any) -> None:
559593
if _RUST_BACKEND:
560-
with libtmux_trace.span("tmux_cmd", layer="python", backend="rust"):
594+
with start_span("tmux_cmd", layer="python", backend="rust"):
561595
stdout, stderr, returncode, cmd = _rust_cmd_result(args)
562596
self.cmd = cmd
563597
self.returncode = returncode
@@ -576,7 +610,7 @@ def __init__(self, *args: t.Any) -> None:
576610
self.cmd = cmd
577611

578612
try:
579-
with libtmux_trace.span(
613+
with start_span(
580614
"tmux_cmd",
581615
layer="tmux-bin",
582616
backend="tmux-bin",

src/libtmux/server.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
from libtmux import exc, formats
1818
from libtmux._internal import trace as libtmux_trace
19+
20+
try:
21+
from libtmux.otel import start_span
22+
except Exception: # pragma: no cover - optional dependency
23+
def start_span(name: str, **fields):
24+
return libtmux_trace.span(name, **fields)
25+
1926
from libtmux._internal.query_list import QueryList
2027
from libtmux.common import tmux_cmd
2128
from libtmux.constants import OptionScope
@@ -207,7 +214,7 @@ def is_alive(self) -> bool:
207214
>>> assert not tmux.is_alive()
208215
"""
209216
if os.getenv("LIBTMUX_BACKEND") == "rust":
210-
with libtmux_trace.span(
217+
with start_span(
211218
"server_is_alive",
212219
layer="python",
213220
backend="rust",
@@ -221,7 +228,7 @@ def is_alive(self) -> bool:
221228
else self.socket_path
222229
)
223230
server = _rust_server(self.socket_name, socket_path, self.colors)
224-
with libtmux_trace.span(
231+
with start_span(
225232
"rust_server_is_alive",
226233
layer="rust",
227234
):
@@ -245,7 +252,7 @@ def raise_if_dead(self) -> None:
245252
<class 'subprocess.CalledProcessError'>
246253
"""
247254
if os.getenv("LIBTMUX_BACKEND") == "rust":
248-
with libtmux_trace.span(
255+
with start_span(
249256
"server_raise_if_dead",
250257
layer="python",
251258
backend="rust",
@@ -266,7 +273,7 @@ def raise_if_dead(self) -> None:
266273
else self.socket_path
267274
)
268275
server = _rust_server(self.socket_name, socket_path, self.colors)
269-
with libtmux_trace.span(
276+
with start_span(
270277
"rust_server_require",
271278
layer="rust",
272279
):

0 commit comments

Comments
 (0)