PhoenixLiveCalendar.Utils.Telemetry (PhoenixLiveCalendar v0.1.0)

Copy Markdown View Source

Performance measurement and telemetry for PhoenixLiveCalendar.

Emits :telemetry events so consumers can attach their own handlers (LiveDashboard, Grafana, etc.) and logs warnings when operations exceed configurable thresholds.

Telemetry events

All events are prefixed with [:phoenix_live_calendar]:

EventMeasurementsMetadata
[:phoenix_live_calendar, :measure, :start]%{system_time: integer}%{label: atom} ++ custom
[:phoenix_live_calendar, :measure, :stop]%{duration: integer}%{label: atom} ++ custom
[:phoenix_live_calendar, :measure, :exception]%{duration: integer}%{label: atom, kind: atom, reason: term, stacktrace: list}
[:phoenix_live_calendar, :ingress]%{event_count: integer, estimated_bytes: integer}%{view: atom}

Configuration

config :phoenix_live_calendar,
  perf_warnings: true,
  perf_thresholds: %{
    group_events: 10,
    slot_layout: 5,
    filter: 5,
    overlap_layout: 5
  }

Set perf_warnings: false to silence all Logger warnings. Override individual thresholds via the perf_thresholds map.

Summary

Functions

Measures the execution time of fun, emits telemetry events, and logs a warning if the duration exceeds the configured threshold for label.

Measures and also checks duration against threshold, logging a warning if exceeded. Use this instead of measure/3 for hot paths where you want automatic threshold warnings.

Profiles an incoming event list at ingress time. Always runs exactly once per data update, regardless of dataset size.

Returns whether measurement should run for hot paths given the dataset size. Small datasets skip measurement to avoid overhead.

Returns the configured threshold in milliseconds for a given label.

Returns whether perf warnings are enabled.

Functions

measure(label, metadata \\ %{}, fun)

@spec measure(atom(), map(), (-> result)) :: result when result: var

Measures the execution time of fun, emits telemetry events, and logs a warning if the duration exceeds the configured threshold for label.

Returns the result of fun.

Examples

Telemetry.measure(:group_events, %{events: 200, dates: 42}, fn ->
  DateHelpers.group_events_by_date(events, dates)
end)

measure_and_warn(label, metadata \\ %{}, fun)

@spec measure_and_warn(atom(), map(), (-> result)) :: result when result: var

Measures and also checks duration against threshold, logging a warning if exceeded. Use this instead of measure/3 for hot paths where you want automatic threshold warnings.

Returns the result of fun.

profile_ingress(events, view \\ :unknown)

@spec profile_ingress([struct()], atom()) :: {non_neg_integer(), non_neg_integer()}

Profiles an incoming event list at ingress time. Always runs exactly once per data update, regardless of dataset size.

Emits a [:phoenix_live_calendar, :ingress] telemetry event and logs warnings for large datasets or high memory usage.

Returns {event_count, estimated_bytes} for the caller to use in downstream decisions (e.g. whether to measure hot paths).

should_measure?(event_count)

@spec should_measure?(non_neg_integer()) :: boolean()

Returns whether measurement should run for hot paths given the dataset size. Small datasets skip measurement to avoid overhead.

threshold(label)

@spec threshold(atom()) :: number()

Returns the configured threshold in milliseconds for a given label.

warnings_enabled?()

@spec warnings_enabled?() :: boolean()

Returns whether perf warnings are enabled.