Skip to main content

phoenix_channel_client/client/
tracing_support.rs

1use std::rc::Rc;
2
3use super::{ChannelEvent, SocketEvent, TelemetryEvent, TelemetryHook};
4
5/// Creates a telemetry hook that emits structured events through `tracing`.
6pub fn tracing_telemetry_hook() -> TelemetryHook {
7    Rc::new(|event| match event {
8        TelemetryEvent::Socket(socket) => match socket {
9            SocketEvent::Connected => {
10                tracing::info!(target: "phoenix_channels", "socket connected")
11            }
12            SocketEvent::Disconnected { reason } => {
13                tracing::warn!(target: "phoenix_channels", %reason, "socket disconnected")
14            }
15            SocketEvent::Connecting { attempt } => {
16                tracing::debug!(target: "phoenix_channels", attempt, "socket connecting")
17            }
18            SocketEvent::ReconnectScheduled { attempt, delay } => tracing::info!(
19                target: "phoenix_channels",
20                attempt,
21                delay_ms = delay.as_millis(),
22                "socket reconnect scheduled"
23            ),
24            SocketEvent::ReconnectStopped { attempt, reason } => tracing::warn!(
25                target: "phoenix_channels",
26                attempt,
27                %reason,
28                "socket reconnect stopped"
29            ),
30            SocketEvent::Closed => tracing::info!(target: "phoenix_channels", "socket closed"),
31            SocketEvent::Lagged { dropped } => tracing::warn!(
32                target: "phoenix_channels",
33                dropped,
34                "socket event subscriber lagged"
35            ),
36        },
37        TelemetryEvent::Channel { topic, event } => match event {
38            ChannelEvent::Lagged { dropped } => tracing::warn!(
39                target: "phoenix_channels",
40                topic,
41                dropped,
42                "channel event subscriber lagged"
43            ),
44            _ => tracing::debug!(
45                target: "phoenix_channels",
46                topic,
47                event = ?event,
48                "channel event"
49            ),
50        },
51        TelemetryEvent::FrameSent {
52            topic,
53            event,
54            binary,
55            bytes,
56        } => tracing::trace!(
57            target: "phoenix_channels",
58            topic,
59            event,
60            binary,
61            bytes,
62            "frame sent"
63        ),
64        TelemetryEvent::FrameReceived {
65            topic,
66            event,
67            binary,
68            bytes,
69        } => tracing::trace!(
70            target: "phoenix_channels",
71            topic,
72            event,
73            binary,
74            bytes,
75            "frame received"
76        ),
77        TelemetryEvent::ConnectionAttemptFinished {
78            attempt,
79            duration,
80            connected,
81        } => tracing::info!(
82            target: "phoenix_channels",
83            attempt,
84            duration_ms = duration.as_millis(),
85            connected,
86            "connection attempt finished"
87        ),
88        TelemetryEvent::CallCompleted {
89            topic,
90            event,
91            outcome,
92            duration,
93        } => tracing::debug!(
94            target: "phoenix_channels",
95            topic,
96            event,
97            outcome = ?outcome,
98            duration_ms = duration.as_millis(),
99            "channel call completed"
100        ),
101    })
102}