refrakt/telemetry

Basic telemetry and request metrics.

Experimental. This module is lightly used and may change or move to its own package; see https://github.com/raskell-io/refrakt/issues/28.

Tracks request counts, response times, and error rates. Use as middleware to collect metrics automatically.

Usage

import refrakt/telemetry

// Start the metrics collector
let assert Ok(metrics) = telemetry.start()

// Add to middleware
fn middleware(req, next) {
  use <- telemetry.track(req, metrics)
  next(req)
}

// Get current metrics
let stats = telemetry.get_stats(metrics)
// stats.total_requests, stats.avg_response_ms, etc.

Types

A running telemetry collector.

pub type Metrics =
  process.Subject(MetricsMessage)
pub opaque type MetricsMessage

Collected statistics.

pub type Stats {
  Stats(
    total_requests: Int,
    total_errors: Int,
    status_counts: dict.Dict(Int, Int),
    avg_response_ms: Int,
  )
}

Constructors

  • Stats(
      total_requests: Int,
      total_errors: Int,
      status_counts: dict.Dict(Int, Int),
      avg_response_ms: Int,
    )

Values

pub fn get_stats(
  metrics: process.Subject(MetricsMessage),
) -> Stats

Get current metrics statistics.

pub fn start() -> Result(
  process.Subject(MetricsMessage),
  actor.StartError,
)

Start the telemetry collector.

pub fn track(
  req: request.Request(wisp.Connection),
  metrics: process.Subject(MetricsMessage),
  next: fn() -> response.Response(wisp.Body),
) -> response.Response(wisp.Body)

Middleware that tracks request duration and status.

✨ Search Document