Phoenix.SocketClient.Telemetry (phoenix_socket_client v0.8.0)
View SourceComprehensive Telemetry integration for Phoenix.SocketClient.
This module provides telemetry events with duration tracking for monitoring socket connections, channel joins/leaves, message handling, and connection lifecycle events.
Event Naming Convention
All events follow the pattern: [:phoenix, :socket_client, component, action]
Connection Events
[:phoenix, :socket_client, :connection, :start]- Connection attempt started[:phoenix, :socket_client, :connection, :stop]- Connection attempt completed[:phoenix, :socket_client, :connection, :error]- Connection attempt failed[:phoenix, :socket_client, :connection, :established, :start]- Connection established[:phoenix, :socket_client, :connection, :established, :stop]- Connection ended[:phoenix, :socket_client, :reconnection, :attempt]- Reconnection attempt
Channel Events
[:phoenix, :socket_client, :channel, :join, :start]- Channel join started[:phoenix, :socket_client, :channel, :join, :stop]- Channel join completed[:phoenix, :socket_client, :channel, :join, :error]- Channel join failed[:phoenix, :socket_client, :channel, :active, :start]- Channel became active[:phoenix, :socket_client, :channel, :active, :stop]- Channel became inactive[:phoenix, :socket_client, :channel, :leave]- Channel left[:phoenix, :socket_client, :channel, :push]- Message pushed to channel[:phoenix, :socket_client, :channel, :reply]- Reply received
Message Events
[:phoenix, :socket_client, :message, :sent]- Message sent[:phoenix, :socket_client, :message, :received]- Message received[:phoenix, :socket_client, :message, :encode, :start]- Message encoding started[:phoenix, :socket_client, :message, :encode, :stop]- Message encoding completed[:phoenix, :socket_client, :message, :decode, :start]- Message decoding started[:phoenix, :socket_client, :message, :decode, :stop]- Message decoding completed
System Events
[:phoenix, :socket_client, :heartbeat, :sent]- Heartbeat sent[:phoenix, :socket_client, :heartbeat, :timeout]- Heartbeat timeout[:phoenix, :socket_client, :error]- General error
Duration Tracking
Duration events use the span pattern with start/stop pairs:
:telemetry.span([:phoenix, :socket_client, :connection], %{socket_ref: ref, url: url}, fn ->
# Connection logic here
{:ok, %{status: :connected}}
end)This generates:
- Start event:
[:phoenix, :socket_client, :connection, :start] - Stop event:
[:phoenix, :socket_client, :connection, :stop]
Configuration
Configure telemetry behavior in your config:
config :phoenix_socket_client, :telemetry,
enabled: true,
default_handler: true,
track_durations: true,
log_levels: %{
connection: :info,
connection_established: :debug,
channel: :info,
channel_active: :debug,
message: :debug,
error: :error,
heartbeat: :debug
}Default Handler
Attach the default handler for Logger integration:
Phoenix.SocketClient.Telemetry.attach_default_handler()Custom Metrics Integration
Example with telemetry_metrics:
defmodule MyApp.Telemetry do
import Telemetry.Metrics
def metrics do
[
distribution("phoenix.socket_client.connection.duration",
unit: {:native, :millisecond},
tags: [:transport, :url]
),
distribution("phoenix.socket_client.channel.join.duration",
unit: {:native, :millisecond},
tags: [:topic, :status]
),
counter("phoenix.socket_client.connection.count",
tags: [:transport]
)
]
end
end
Summary
Functions
Attaches a debug handler that prints all telemetry events.
Attaches the default telemetry handler that converts events to Logger calls.
Emits channel active event.
Emits channel active start event.
Emits channel active stop event.
Emits channel join duration event.
Emits channel join error event.
Legacy: Emits channel join error event.
Emits channel join start event.
Emits channel join stop event.
Legacy: Emits channel join event.
Emits channel leave event.
Emits channel leave duration event.
Emits channel left event.
Legacy: Emits channel leave event.
Emits channel push event.
Emits channel reply event.
Emits channel status changed event.
Completes a duration context and emits summary events.
Gets the current telemetry configuration.
Emits connection error event.
Emits connection established start event.
Emits connection established stop event.
Emits connection start event.
Emits connection stop event.
Creates a duration measurement context for complex operations.
Emits debug-level event.
Detaches the debug handler.
Detaches the default telemetry handler.
Emits a telemetry event if telemetry is enabled and passes sampling/filters.
Checks if telemetry is enabled.
Emits general error event.
Legacy execute function for backward compatibility.
Gets a specific configuration value.
Emits heartbeat sent event.
Legacy: Emits heartbeat event.
Emits heartbeat timeout event.
Measures execution time of a function and emits start/stop events.
Measures a sub-operation within a duration context.
Emits message decode start event.
Emits message decode stop event.
Emits message decoded event.
Emits message encode start event.
Emits message encode stop event.
Emits message encoded event.
Emits message receive event.
Emits message received event.
Legacy: Emits message received event.
Emits message send event.
Emits message sent event.
Legacy: Emits message sent event.
Emits optimization-related event.
Emits binary pool hit optimization event.
Emits cache hit optimization event.
Emits cache miss optimization event.
Emits process hibernated optimization event.
Checks if an event passes the configured filters.
Legacy: Emits reconnection attempt event.
Emits reconnection attempt event.
Resets telemetry configuration to defaults.
Gets the current sampler rate.
Checks if an event should be sampled based on the sampler rate.
Legacy: Emits socket connection event.
Legacy: Emits socket connection attempt event.
Legacy: Emits socket connection duration event.
Legacy: Emits socket connection error event.
Legacy: Emits socket disconnection event.
Executes a function with telemetry span for duration tracking.
Starts tracking duration for a specific operation.
Stops duration tracking for a specific operation.
Checks if duration tracking is enabled.
Checks if memory usage tracking is enabled.
Checks if message size tracking is enabled.
Updates telemetry configuration.
Types
Functions
@spec attach_debug_handler() :: :ok
Attaches a debug handler that prints all telemetry events.
Useful for development and debugging.
@spec attach_default_handler(keyword()) :: :ok
Attaches the default telemetry handler that converts events to Logger calls.
The handler respects the configured log levels and formats duration measurements in human-readable format.
Example
Phoenix.SocketClient.Telemetry.attach_default_handler()You can also provide custom configuration:
Phoenix.SocketClient.Telemetry.attach_default_handler(
log_levels: %{connection: :debug, error: :warn}
)
@spec channel_active(metadata()) :: :ok
Emits channel active event.
@spec channel_active_start(metadata()) :: :ok
Emits channel active start event.
@spec channel_active_stop(metadata()) :: :ok
Emits channel active stop event.
@spec channel_join_duration(pid(), String.t(), non_neg_integer()) :: :ok
Emits channel join duration event.
@spec channel_join_error(metadata()) :: :ok
Emits channel join error event.
Legacy: Emits channel join error event.
@spec channel_join_start(metadata()) :: :ok
Emits channel join start event.
@spec channel_join_stop(metadata()) :: :ok
Emits channel join stop event.
Legacy: Emits channel join event.
@spec channel_leave(metadata()) :: :ok
Emits channel leave event.
@spec channel_leave_duration(pid(), String.t(), non_neg_integer()) :: :ok
Emits channel leave duration event.
@spec channel_left(metadata()) :: :ok
Emits channel left event.
Legacy: Emits channel leave event.
@spec channel_push(metadata()) :: :ok
Emits channel push event.
@spec channel_reply(metadata()) :: :ok
Emits channel reply event.
Emits channel status changed event.
@spec complete_duration_context(duration_context(), metadata()) :: :ok
Completes a duration context and emits summary events.
@spec config() :: map()
Gets the current telemetry configuration.
@spec connection_error(metadata()) :: :ok
Emits connection error event.
@spec connection_established_start(metadata()) :: :ok
Emits connection established start event.
@spec connection_established_stop(metadata()) :: :ok
Emits connection established stop event.
@spec connection_start(metadata()) :: :ok
Emits connection start event.
@spec connection_stop(metadata()) :: :ok
Emits connection stop event.
@spec create_duration_context(atom(), metadata()) :: duration_context()
Creates a duration measurement context for complex operations.
Returns a context that can be used to measure multiple sub-operations.
@spec debug(metadata()) :: :ok
Emits debug-level event.
@spec detach_debug_handler() :: :ok
Detaches the debug handler.
@spec detach_default_handler() :: :ok
Detaches the default telemetry handler.
@spec emit_event(event_name(), measurements(), metadata()) :: :ok
Emits a telemetry event if telemetry is enabled and passes sampling/filters.
@spec enabled?() :: boolean()
Checks if telemetry is enabled.
@spec error(metadata()) :: :ok
Emits general error event.
@spec execute(event_name(), measurements(), metadata()) :: :ok
Legacy execute function for backward compatibility.
Gets a specific configuration value.
@spec heartbeat_sent(metadata()) :: :ok
Emits heartbeat sent event.
Legacy: Emits heartbeat event.
@spec heartbeat_timeout(metadata()) :: :ok
Emits heartbeat timeout event.
Measures execution time of a function and emits start/stop events.
This is similar to span/3 but provides more explicit control over
the event names and metadata structure.
@spec measure_sub_operation(duration_context(), atom(), (-> result)) :: result when result: any()
Measures a sub-operation within a duration context.
@spec message_decode_start(metadata()) :: :ok
Emits message decode start event.
@spec message_decode_stop(metadata()) :: :ok
Emits message decode stop event.
@spec message_decoded(metadata()) :: :ok
Emits message decoded event.
@spec message_encode_start(metadata()) :: :ok
Emits message encode start event.
@spec message_encode_stop(metadata()) :: :ok
Emits message encode stop event.
@spec message_encoded(metadata()) :: :ok
Emits message encoded event.
@spec message_receive(metadata()) :: :ok
Emits message receive event.
@spec message_received(metadata()) :: :ok
Emits message received event.
Legacy: Emits message received event.
@spec message_send(metadata()) :: :ok
Emits message send event.
@spec message_sent(metadata()) :: :ok
Emits message sent event.
Legacy: Emits message sent event.
Emits optimization-related event.
@spec optimization_binary_pool_hit(metadata()) :: :ok
Emits binary pool hit optimization event.
@spec optimization_cache_hit(metadata()) :: :ok
Emits cache hit optimization event.
@spec optimization_cache_miss(metadata()) :: :ok
Emits cache miss optimization event.
@spec optimization_process_hibernated(metadata()) :: :ok
Emits process hibernated optimization event.
@spec passes_filters?(event_name(), measurements()) :: boolean()
Checks if an event passes the configured filters.
Legacy: Emits reconnection attempt event.
@spec reconnection_attempt(metadata()) :: :ok
Emits reconnection attempt event.
@spec reset_config() :: :ok
Resets telemetry configuration to defaults.
@spec sampler_rate() :: float()
Gets the current sampler rate.
@spec should_sample?() :: boolean()
Checks if an event should be sampled based on the sampler rate.
Legacy: Emits socket connection event.
Legacy: Emits socket connection attempt event.
@spec socket_connection_duration(pid(), String.t(), non_neg_integer()) :: :ok
Legacy: Emits socket connection duration event.
Legacy: Emits socket connection error event.
Legacy: Emits socket disconnection event.
@spec span(event_name(), metadata(), (-> span_result())) :: span_result()
Executes a function with telemetry span for duration tracking.
This is the preferred way to track durations as it ensures proper start/stop event pairing and handles errors gracefully.
Example
Phoenix.SocketClient.Telemetry.span(
[:phoenix, :socket_client, :connection],
%{socket_ref: ref, url: url},
fn -> connect_to_server() end
)
@spec start_duration(atom(), atom(), metadata()) :: duration_token()
Starts tracking duration for a specific operation.
Returns a tracking token that should be used with stop_duration/3.
@spec stop_duration(duration_token(), map()) :: :ok
Stops duration tracking for a specific operation.
Emits the stop event with duration measurements.
@spec track_durations?() :: boolean()
Checks if duration tracking is enabled.
@spec track_memory_usage?() :: boolean()
Checks if memory usage tracking is enabled.
@spec track_message_sizes?() :: boolean()
Checks if message size tracking is enabled.
@spec update_config(map()) :: :ok
Updates telemetry configuration.