CodexWrapper.Telemetry (CodexWrapper v0.4.0)

Copy Markdown View Source

:telemetry events emitted by CodexWrapper.

The synchronous command and session events use :telemetry.span/3. Streaming events follow the same start/stop/exception convention while keeping the span open for the lifetime of lazy enumeration.

Events

execute_json/2 uses the corresponding execute/2 path and therefore emits the same exec or review event rather than a second JSON-specific event.

Measurements

  • :start%{monotonic_time: integer(), system_time: integer()}
  • :stop%{monotonic_time: integer(), duration: integer()}
  • :exception%{monotonic_time: integer(), duration: integer()}

Metadata

Every event includes:

  • :command — one of :exec, :exec_resume, :review, :session_exec, or :session_resume
  • :session_id — the session identifier when known
  • :sandbox_mode — the configured sandbox mode when present
  • :approval_policy — the configured approval policy when present

Synchronous stop events additionally include :exit_code when the wrapped call returns a %CodexWrapper.Result{}. Stream events do not include an exit code because the lazy Runner contract does not expose one. Exception metadata follows the standard span shape with :kind, :reason, and :stacktrace.

Example

:telemetry.attach_many(
  "codex-wrapper-logger",
  [
    [:codex_wrapper, :exec, :stop],
    [:codex_wrapper, :stream, :stop],
    [:codex_wrapper, :review, :stop],
    [:codex_wrapper, :session, :turn, :stop]
  ],
  fn event, measurements, metadata, _config ->
    require Logger

    Logger.info(
      "#{inspect(event)} duration=#{measurements.duration} " <>
        "metadata=#{inspect(metadata)}"
    )
  end,
  nil
)

Summary

Functions

Build metadata for an Exec or ExecResume command.

Build metadata for a Review command.

Build metadata for a session turn.

Run a synchronous function inside a telemetry span.

Instrument the full lifecycle of a lazy enumerable.

Types

event_name()

@type event_name() :: [atom(), ...]

metadata()

@type metadata() :: %{optional(atom()) => term()}

Functions

exec_metadata(command, builder)

@spec exec_metadata(
  atom(),
  struct()
) :: metadata()

Build metadata for an Exec or ExecResume command.

review_metadata(command, builder)

@spec review_metadata(
  atom(),
  struct()
) :: metadata()

Build metadata for a Review command.

session_turn_metadata(command, builder, session_id)

@spec session_turn_metadata(atom(), struct(), String.t() | nil) :: metadata()

Build metadata for a session turn.

span(event_prefix, start_metadata, fun)

@spec span(event_name(), metadata(), (-> result)) :: result when result: var

Run a synchronous function inside a telemetry span.

The function's return value is preserved. When it contains a %CodexWrapper.Result{}, its exit code is added to stop metadata.

span_stream(event_prefix, start_metadata, stream_fun)

@spec span_stream(event_name(), metadata(), (-> Enumerable.t())) :: Enumerable.t()

Instrument the full lifecycle of a lazy enumerable.

No event is emitted and stream_fun is not called until the returned enumerable is first reduced. The stop event is emitted after the producer finishes or the consumer halts it. Producer and consumer exceptions emit an exception event and are re-raised without a stop event.