Phoenix.ReactServer.Telemetry (Phoenix.ReactServer NG v0.8.5)

Copy Markdown View Source

Comprehensive telemetry and metrics collection for Phoenix.ReactServer runtimes.

This module provides production-ready telemetry capabilities including performance metrics, health checks, runtime status monitoring, and comprehensive error tracking with structured logging.

Features

  • Performance Metrics: Track render times, success rates, and throughput
  • Health Checks: Monitor runtime process health and responsiveness
  • Error Tracking: Detailed error logging and categorization
  • Telemetry Integration: :telemetry events for observability
  • Runtime Status: Monitor JavaScript runtime process states
  • Cache Metrics: Track cache hit/miss ratios and performance
  • Structured Logging: Enhanced log formatting with render duration

Telemetry Events

The following telemetry events are emitted:

  • [:phoenix, :react, :render] - Fired after each component render

    • Measurements: %{duration: duration_ms}
    • Metadata: %{component: component, method: method, result: result, timestamp: timestamp}
  • [:phoenix, :react, :runtime_startup] - Fired when runtime starts

    • Metadata: %{runtime: runtime_name, port: port, timestamp: timestamp}
  • [:phoenix, :react, :runtime_shutdown] - Fired when runtime stops

    • Metadata: %{runtime: runtime_name, reason: reason, timestamp: timestamp}
  • [:phoenix, :react, :cache, :hit] - Fired on cache hits

    • Metadata: %{component: component, method: method, timestamp: timestamp}
  • [:phoenix, :react, :cache, :miss] - Fired on cache misses

    • Metadata: %{component: component, method: method, timestamp: timestamp}
  • [:phoenix, :react, :file_change] - Fired on component file changes

    • Metadata: %{path: path, action: action, timestamp: timestamp}
  • [:phoenix, :react, :build] - Fired after build operations

    • Measurements: %{duration: duration_ms}
    • Metadata: %{runtime: runtime_name, result: result, timestamp: timestamp}

Usage

Attach telemetry handlers to monitor performance:

:telemetry.attach_many(
  "phoenix-react-telemetry",
  [
    [:phoenix, :react, :render],
    [:phoenix, :react, :cache, :hit],
    [:phoenix, :react, :cache, :miss]
  ],
  &MyApp.Telemetry.handle_event/4,
  %{}
)

Health Checks

Use for health monitoring endpoints and alerts:

# Check runtime health
case Phoenix.ReactServer.Telemetry.health_check("Bun", 5225) do
  {:ok, metadata} -> :healthy
  {:error, reason} -> :unhealthy
end

Performance Monitoring

Track component performance over time:

  • Average render times per component
  • Error rates and failure patterns
  • Cache efficiency metrics
  • Runtime resource usage

Logging

The module provides structured logging for all operations:

  • Render duration: [Phoenix.ReactServer] Rendered <component> in <duration>ms (method: <method>, result: <result>)
  • Runtime events: [Phoenix.ReactServer] Runtime <runtime> started on port <port>
  • Cache events: [Phoenix.ReactServer] Cache <hit/miss> for <component>

Summary

Functions

Gets runtime statistics.

Performs a health check on the runtime.

Measures execution time of a function and records it with telemetry.

Records a build event with duration.

Records a cache hit event.

Records a cache miss event.

Records a file change event.

Records a render request with timing information and logs the render duration.

Records a runtime shutdown event.

Records a runtime startup event.

Functions

get_runtime_stats(runtime_name)

@spec get_runtime_stats(String.t()) :: map()

Gets runtime statistics.

health_check(runtime_name, port)

@spec health_check(String.t(), non_neg_integer()) :: {:ok, map()} | {:error, term()}

Performs a health check on the runtime.

measure(operation_name, telemetry_event, fun)

@spec measure(String.t(), [atom()], function()) :: any()

Measures execution time of a function and records it with telemetry.

This function wraps the execution of a given function, measures its duration, and emits telemetry events with the timing information.

Parameters

  • operation_name - Descriptive name for the operation being measured
  • telemetry_event - Telemetry event name (list of atoms)
  • fun - Zero-arity function to execute and measure

Returns

The result of executing fun

Examples

iex> Phoenix.ReactServer.Telemetry.measure("render_chart", [:phoenix, :react, :render], fn ->
...>   :timer.sleep(10)
...>   {:ok, "<div>Chart</div>"}
...> end)
{:ok, "<div>Chart</div>"}

record_build(runtime_name, duration_ms, result)

@spec record_build(String.t(), non_neg_integer(), :ok | :error) :: :ok

Records a build event with duration.

Parameters

  • runtime_name - Name of the runtime (e.g., "Bun", "Deno")
  • duration_ms - Build duration in milliseconds
  • result - Build result (:ok or :error)

record_cache_hit(component, method)

@spec record_cache_hit(String.t(), atom()) :: :ok

Records a cache hit event.

Parameters

  • component - Component name
  • method - Rendering method

record_cache_miss(component, method)

@spec record_cache_miss(String.t(), atom()) :: :ok

Records a cache miss event.

Parameters

  • component - Component name
  • method - Rendering method

record_file_change(path, action \\ "changed")

@spec record_file_change(String.t(), String.t()) :: :ok

Records a file change event.

record_render(component, method, duration_ms, result)

@spec record_render(String.t(), atom(), non_neg_integer(), :ok | :error) :: :ok

Records a render request with timing information and logs the render duration.

Parameters

  • component - Component name
  • method - Rendering method (:render_to_string, :render_to_static_markup, or :render_to_readable_stream)
  • duration_ms - Render duration in milliseconds
  • result - Result status (:ok or :error)

Examples

iex> Phoenix.ReactServer.Telemetry.record_render("chart", :render_to_string, 45, :ok)
:ok

record_runtime_shutdown(runtime_name, reason)

@spec record_runtime_shutdown(String.t(), term()) :: :ok

Records a runtime shutdown event.

Parameters

  • runtime_name - Name of the runtime (e.g., "Bun", "Deno")
  • reason - Shutdown reason

record_runtime_startup(runtime_name, port)

@spec record_runtime_startup(String.t(), non_neg_integer()) :: :ok

Records a runtime startup event.

Parameters

  • runtime_name - Name of the runtime (e.g., "Bun", "Deno")
  • port - Port number the runtime is listening on