Monitorex.EventHandler (monitorex v0.6.0)

Copy Markdown

Handles telemetry events from Tesla, Finch, Req, and Phoenix, transforming them into Monitorex.Event structs.

Each handler function follows the Telemetry handler callback signature: (event_name, measurements, metadata, config).

Summary

Functions

Handles a Finch telemetry event ([:finch, :request, :stop]).

Handles a Phoenix telemetry event ([:phoenix, :router_dispatch, :stop]).

Handles a Req telemetry event from req_telemetry ([:req, :request, :pipeline, :stop]).

Handles a Tesla telemetry event ([:tesla, :request, :stop]).

Post-processes an Event to tag it as slow if its duration exceeds the threshold.

Functions

handle_finch_event(arg1, measurements, metadata, config)

@spec handle_finch_event(
  event_name :: [atom()],
  measurements :: map(),
  metadata :: map(),
  config :: keyword()
) :: Monitorex.Event.t() | nil

Handles a Finch telemetry event ([:finch, :request, :stop]).

Parses the metadata and measurements into a Monitorex.Event struct with source :finch and direction :outbound.

Telemetry metadata shape

%{
  url:  %URI{} | String.t(),
  method: :get | "GET" | ,
  status: 200,
  pid: pid,
  monotonic_time: integer
}

The url field may be a URI.t() struct or a string; both are handled. The method field may be an atom or string; both are normalised.

handle_phoenix_event(arg1, measurements, metadata, config)

@spec handle_phoenix_event(
  event_name :: [atom()],
  measurements :: map(),
  metadata :: map(),
  config :: keyword()
) :: Monitorex.Event.t() | nil

Handles a Phoenix telemetry event ([:phoenix, :router_dispatch, :stop]).

Parses the metadata and measurements into a Monitorex.Event struct with source :phoenix and direction :inbound.

The consumer is extracted via ConsumerIdentifier.identify/1.

If the application config key :inbound_path_prefixes is set to a list of path prefixes, only requests whose path starts with one of the prefixes produce an event. Returns nil (i.e. the event is filtered) when no prefix matches.

Telemetry metadata shape

%{
  conn: %Plug.Conn{}
}

handle_req_event(arg1, measurements, metadata, config)

@spec handle_req_event(
  event_name :: [atom()],
  measurements :: map(),
  metadata :: map(),
  config :: keyword()
) :: Monitorex.Event.t() | nil

Handles a Req telemetry event from req_telemetry ([:req, :request, :pipeline, :stop]).

Parses the metadata and measurements into a Monitorex.Event struct with source :req and direction :outbound.

Telemetry metadata shape (req_telemetry pipeline events)

%{
  url: %URI{},
  method: :get,
  status: 200,
  resp_headers: [{name, value}],
  error: %RuntimeError{},
  ref: term(),
  metadata: %{}
}

Duration is in measurements.duration (native time units).

handle_tesla_event(arg1, measurements, metadata, config)

@spec handle_tesla_event(
  event_name :: [atom()],
  measurements :: map(),
  metadata :: map(),
  config :: keyword()
) :: Monitorex.Event.t() | nil

Handles a Tesla telemetry event ([:tesla, :request, :stop]).

Parses the metadata and measurements into a Monitorex.Event struct with source :tesla and direction :outbound.

The URL is normalised via UrlNormalizer.normalize/1 and sensitive query parameters are redacted via URLRedactor.redact/1.

Telemetry metadata shape

%{
  url:  %URI{},
  method: :get | :post | ,
  status: 200,
  pid: pid,
  monotonic_time: integer
}

The dedup_key is set to {pid, monotonic_time} for deduplication.

tag_slow_request(event, metadata \\ %{})

@spec tag_slow_request(Monitorex.Event.t() | nil, any()) :: Monitorex.Event.t() | nil

Post-processes an Event to tag it as slow if its duration exceeds the threshold.

When a request is slow, full request/response bodies are captured even if body storage is globally disabled. This provides debugging data for slow requests without the memory overhead of storing all bodies.

The threshold is configured via:

config :monitorex, :slow_request_threshold_ms, 2_000

Set to nil or 0 to disable slow request tracing.

Returns

Modified %Event{} with :slow set and bodies populated (if applicable).