Perplexed.Telemetry (Perplexed v0.2.0)

Copy Markdown View Source

:telemetry events Perplexed emits. Attach handlers to track spend or to be told when a provider needs attention.

Searches and scrapes are spans: each emits :start, then :stop (or :exception if it raised), with :telemetry.span/3's standard duration (native time units) on stop.

EventMeasurementsMetadata
[:perplexed, :search, :start]system_timeprovider, query
[:perplexed, :search, :stop]duration, cost, hitsprovider, query, result
[:perplexed, :scrape, :start]system_timeurl
[:perplexed, :scrape, :stop]duration, cost, browser_msurl, tier, attempts, result
[:perplexed, :model]token counts, cost (when priced)model, phase
[:perplexed, :search, :attempt]duration (ms), costprovider, outcome, billable
[:perplexed, :scrape, :attempt]duration (ms), costurl, tier, outcome, billable
[:perplexed, :searxng, :attempt]duration (ms), hitsinstance, outcome, retry_after_ms
[:perplexed, :searxng, :circuit]count (always 1)instance, from, to, cooldown_ms
[:perplexed, :searxng, :instances]count (live instances)result
[:perplexed, :provider, :alert]count (always 1)provider, reason, status, message

result is :ok or :error. On an alert, status and message are present when the provider answered with an HTTP error. [:perplexed, :search, :attempt] is emitted for each step of a fallback chain (a list of providers); a single provider emits just the span.

An alert fires when a provider answers that credits or quota ran out (:quota_exhausted), that the credentials were rejected (:authentication), or that requests are being throttled (:rate_limited). A missing credential does not alert; unconfigured providers are expected.

Summary

Functions

Emits [:perplexed, kind, :attempt] for one step of a search chain or scrape ladder: duration (ms) and cost, with metadata plus billable, true when the step cost anything.

Emits [:perplexed | event] with measurements and metadata.

Emits a provider alert when reason is one that needs a person's attention.

Runs fun as a [:perplexed | event] span. fun returns {result, stop_measurements, stop_metadata}; the span returns result.

Functions

attempt(kind, duration_ms, cost, metadata)

@spec attempt(atom(), non_neg_integer(), float(), map()) :: :ok

Emits [:perplexed, kind, :attempt] for one step of a search chain or scrape ladder: duration (ms) and cost, with metadata plus billable, true when the step cost anything.

emit(event, measurements, metadata)

@spec emit([atom()], map(), map()) :: :ok

Emits [:perplexed | event] with measurements and metadata.

maybe_alert(provider, reason, metadata)

@spec maybe_alert(atom(), atom(), map()) :: :ok

Emits a provider alert when reason is one that needs a person's attention.

span(event, metadata, fun)

@spec span([atom()], map(), (-> {result, map(), map()})) :: result when result: term()

Runs fun as a [:perplexed | event] span. fun returns {result, stop_measurements, stop_metadata}; the span returns result.