Run in your codebase to get started:
npx restless initThis scans your project, figures out your framework, generates an OpenAPI spec, and automatically wires the SDK into your server.
restless-sdk
Capture your API traffic and send it to Restless.
One Rack middleware covers Rails, Sinatra, Hanami, Grape, Roda and anything else that speaks Rack. Ruby 2.6+. No dependencies.
Manual installation
If you'd rather wire it up by hand:
gem install restless-sdkor in a Gemfile:
gem "restless-sdk"Use
require "restless"
CLIENT = Restless.new(ENV["RESTLESS_KEY"])
CLIENT.setup do |request|
{
api_key: CLIENT.mask(request.header("Authorization")),
owner: {
# Permanent and immutable. A workspace id or database primary key,
# never an API key, email, or anything else that rotates: the
# dashboard pins a project's whole log history to this.
id: workspace_id_for(request),
# Runs once per owner id, then caches for an hour. Put the expensive
# lookup here, not in the fields above.
enrich: ->(owner_id) {
workspace = Workspace.find(owner_id)
{ label: workspace.name, email: workspace.admin_emails }
},
},
}
endThen mount it.
# config.ru (Sinatra, Roda, Hanami, plain Rack)
use CLIENT.rack
run App
# config/application.rb (Rails)
config.middleware.insert_before 0, CLIENT.rackCLIENT.rack returns a middleware factory: anything that calls .new(app)
on it works. Mount it as far out as you can, so it sees the real status
and body your app produced rather than an inner layer's.
What you get
-
Lazy owner enrichment. The
enrichcallback runs on the first request from each owner id and then caches, so 100 requests from one workspace do not mean 100 database lookups. The cached value still rides on every upload, so no request lands in the dashboard as unauthenticated. -
Safe by default.
Authorization,Cookie,password,token,ssnand friends are redacted before anything leaves your process. Bodies with nothing to redact are passed through byte for byte, so your payloads are never re-serialized on the way out. -
Error triage. Every response gets an
x-log-urlheader pointing at the captured log, anx-debugheader with thenpx api debug <id>command, and anx-api-feedbackheader with thenpx api feedback <id>command for filing feedback about that exact request. 4xx/5xx responses also get adebugblock in the JSON body. Browser callers can only read these headers if your API lists them inAccess-Control-Expose-Headers. If someone attaches a "next steps" message to an error in the dashboard, the SDK injects it asdebug.recovery- read synchronously from an in-process cache, never blocking the response on a network call. A 5xx that groups by its throw site also reports the key it would have grouped under otherwise, so a message you attached before that grouping existed keeps being injected. -
Blocking. Return
{ block: true }from the setup callback to reject a request before your handler runs, or{ block: { status: 402, message: "Payment required" } }to pick the status. - Sampling. Send less for noisy endpoints without losing your counts. See below.
Sampling
Each endpoint is "on" (the default), "off", "errors" (only status
= 400), or a rate such as
0.1: that fraction of successful requests is logged in full and the rest without bodies, so counts, latency and per-customer usage stay exact. Errors are never sampled.
CLIENT = Restless.new(ENV["RESTLESS_KEY"], sampling: {
default: "on",
routes: {
"GET /health" => "off",
"GET /status" => "errors",
"GET /pets" => 0.1,
"* /pets/{id}" => 0.5,
},
})| mode | successes (< 400) | errors (>= 400) |
|---|---|---|
"on" (default) |
full log | full log |
a rate 0 to 1
|
that fraction in full, the rest without bodies | full log |
"errors" |
nothing sent | full log |
"off" |
nothing sent | nothing sent |
Keys are a method (or * for any) and the route pattern your framework
matched (see Route patterns); {id} and :id are the
same, and HEAD does not fall back to GET. A request with no route
pattern uses default. Symbols work for the modes (:off, :errors).
The same sampling block can live under an API entry in
.restless/settings.json, and the option in code wins per route. A request
that sends nothing (an "off" route, or a success on an "errors" route)
also gets no x-request-id header and no debug headers, since there is no
log for them to point at. An invalid value never raises: it falls back to
"on" and says so under DEBUG=restless.
With sampling configured, the SDK tells Restless the configuration once, on
the first request it serves (never at boot), so the dashboard can show an
"off" endpoint as off rather than as idle.
The setup callback
It receives a small read-only request view and returns a hash:
| field | what |
|---|---|
api_key |
The masked end-user key. Always CLIENT.mask(raw_header) - never substitute "anonymous", whose last 4 characters would become the mask tail. |
owner[:id] |
The permanent, immutable workspace/tenant identifier. |
owner[:enrich] |
->(id) { ... } returning owner metadata (label, email, extras). The only channel for owner metadata; anything else inline on owner is dropped. |
block |
true, or { status:, message: }. |
| anything else | Carried through onto the log as-is. Keep it cheap - read straight off the request. Anything expensive belongs in enrich. |
The request view exposes header(name) (case-insensitive),
request_method, path, query_string, url, and the raw Rack env.
Route patterns
The middleware reads the matched route template from the framework so your handlers do not have to report it:
| framework | source | reported as |
|---|---|---|
| Sinatra |
env["sinatra.route"] (GET /pets/:id) |
/pets/{id} |
| Rails | env["action_dispatch.route_uri_pattern"] |
/pets/{id} |
| anything |
env["restless.route"], set by you |
as given |
The method prefix is stripped and :id-style params are rewritten to
{id}, so the same endpoint produces the same routePattern here as it
does in the Node, Python and Go SDKs. Override the whole thing with a
lambda:
use CLIENT.rack(route: ->(env) { env["my_router.pattern"] })Never breaks your API
Observability must not take down a production request path. Upload failures,
callback exceptions, unserializable bodies and malformed input are all caught
and swallowed (surfaced only under DEBUG=restless). Uploads happen on a
background thread and are fire-and-forget. Streaming responses
(text/event-stream) and bodies over 1 MiB are never buffered.
It also stays bounded when Restless is slow or busy. At most 4 uploads run at
once, each with a 10 s timeout; captures queue behind them (up to 1000, oldest
dropped first) and a freed slot sends up to 100 at a time to catch up. An
upload Restless turns away with 503 or 429 is retried up to twice, waiting
for its Retry-After (at most 10 s). Anything Restless may already have
stored, such as a 502, 504, timeout or dropped connection, is never resent,
so a log is never doubled. flush waits for all of it, retries included. A
worker forked by Puma or Unicorn starts with its own empty queue.
An exception your app raises is captured with its stack - so the error fingerprint keys on the method that raised, not on the middleware - and then re-raised untouched, leaving your own error handling completely unaffected.
Environment variables
| variable | purpose |
|---|---|
RESTLESS_KEY |
Your project API key, if you do not pass one explicitly. |
RESTLESS_BASE_URL |
Override the ingest URL (self-hosted or staging). |
RESTLESS_ENV / RACK_ENV / RAILS_ENV
|
production enables batching; anything else flushes every request. test disables uploads entirely. |
DEBUG=restless |
Print upload diagnostics to stderr. |
Conformance
This SDK implements version 1.4.0 of the Restless SDK Contract at level L2, and is verified against the shared cross-language conformance vectors and a differential fuzzer run against the reference implementation. See CONFORMANCE.md.
License
MIT