Profiler Reference
Lookup material for the built-in profiler: every setting, what a captured span holds, the export formats, and where runs are stored and served. The profiling guide covers turning it on and using it.
Configuration
| Variable | Default | Description |
|---|---|---|
PROFILER_ENABLED |
false |
Master switch. true loads the profiler. |
PROFILER_AUTH_TOKEN |
(unset) | The header trigger's token, and the bearer token for the /__profiler/* routes. Empty means no token is required: any non-empty header value passes. |
PROFILER_SAMPLE_RATE |
0.0 |
Share of requests profiled at random, from 0.0 to 1.0. |
PROFILER_EXCLUDE_PATHS |
(unset) | Comma-separated glob patterns (PHP_DENY_PATHS syntax) left out of sampling, such as /_profiler,/_profiler/**,/_wdt/**. The header trigger still profiles them. |
PROFILER_MAX_SPANS |
50000 |
Most calls observed per request. Past it no span is recorded, and the run is flagged truncated. |
PROFILER_MAX_DEPTH |
0 (no cap) |
Deepest nesting of recorded calls. The effective maximum is 32; see The depth limit. |
PROFILER_OUTPUT_DIR |
/tmp/oxphp-profiles |
Absolute path, writable by www-data. |
PROFILER_OUTPUT_FORMATS |
xhprof,collapsed |
Comma-separated subset of xhprof and collapsed. Empty turns disk output off. |
PROFILER_RETENTION_COUNT |
100 |
Runs kept, on disk and in memory. Trimmed every 5 seconds. |
PROFILER_DISK_MAX_PER_SEC |
10 |
Most runs written to disk per second. The rest are dropped and counted in oxphp_profiler_disk_drops_total. |
PROFILER_EXPORT_URL |
(unset) | URL each captured run is POSTed to. |
PROFILER_EXPORT_FORMAT |
xhprof |
Body of each push: xhprof or collapsed. |
PROFILER_EXPORT_AUTH_TOKEN |
(unset) | Bearer token sent to the push target. |
Never commit PROFILER_AUTH_TOKEN to the repository.
PROFILER_MAX_SPANS is also the size of the tree when there is no depth limit. Under PROFILER_MAX_DEPTH it is not: a call the depth limit discards still counts here first, so a truncated run can reach disk with a fraction of this many spans.
PROFILER_OUTPUT_FORMATS skips an unknown name with a startup warning, and a list with no known name writes both formats. An empty value writes no files and no index.json line, and /__profiler/runs then answers {"runs":[],"total":0,"limit":0,"offset":0,"error":"disk storage disabled"}. A PROFILER_EXPORT_URL, if set, still receives every run.
PROFILER_EXPORT_FORMAT falls back to xhprof, with a startup warning, on an unknown value.
PROFILER_INTERNAL, PROFILER_EXPORT_XHGUI, PROFILER_EXPORT_BUGGREGATOR, PROFILER_EXPORT_APP_NAME and PROFILER_EXPORT_TAGS were removed in 0.16.0 and are ignored if still set. See Migration from 0.15.0.
The depth limit
A call nested deeper than PROFILER_MAX_DEPTH is not recorded, and normally neither is anything it calls: that subtree is absent from the profile rather than flattened into it. Its time is still counted. The call at the limit spans everything it did, and with nothing recorded underneath, that duration reads as its own exclusive time. A frame at the boundary therefore looks hot in itself rather than hot in what it called.
- The effective maximum is
32, the depth to which the engine observer tracks open calls. A larger value is accepted and clamped, and withPROFILER_ENABLED=trueit is reported as clamped at startup. - Deep self-recursion is the exception. Past 32 frames of a function calling itself, a return can be attributed to a shallower frame of the same function. A call deeper than the limit can then still be recorded, and the durations around it should not be trusted.
- It bounds the profile, not the cost of producing it. The engine hook still runs on the deeper frames, and the calls it drops still count against
PROFILER_MAX_SPANS. - Spans an application pushes through the APM functions are not subject to it.
Example production config
environment:
PROFILER_ENABLED: "true"
PROFILER_AUTH_TOKEN: "${PROFILER_TOKEN_FROM_VAULT}"
PROFILER_SAMPLE_RATE: "0.001" # ~0.1% of traffic
PROFILER_OUTPUT_DIR: /var/lib/oxphp/profiles
PROFILER_OUTPUT_FORMATS: "xhprof,collapsed"
PROFILER_RETENTION_COUNT: "500"
PROFILER_DISK_MAX_PER_SEC: "20"
PROFILER_EXPORT_URL: "http://profile-collector.monitoring.svc.cluster.local/ingest"
PROFILER_EXPORT_FORMAT: "xhprof"What a captured span contains
Each span is one PHP call:
| Field | What it holds |
|---|---|
span_id, parent_span_id |
W3C-compatible ids. span_id is a deterministic counter. |
trace_id |
The trace id, shared with APM. |
name |
Fully qualified PHP function or method name. |
start_ns, end_ns |
Wall-clock time, in nanoseconds since the profiler's epoch. |
cpu_ns |
Thread CPU time, or 0 where the platform does not provide it. |
memory_start, memory_end |
PHP's memory usage on entry and on exit. |
attributes |
Key-value pairs, from metric() and APM. |
events |
Timestamped events, each with a kind, a label and attributes. |
status_code, status_message |
0 unset, 1 ok, 2 error. |
leaked |
true when the span was closed by force at the end of the request, because an exception unwound past it. |
Event kinds:
| Kind | Emitted by | oxphp.event.kind |
|---|---|---|
Mark |
mark(), #[Mark] |
mark |
Slow |
#[SlowThreshold] |
slow |
MemorySpike |
#[MemoryThreshold] |
memory_spike |
Exception |
APM's exception capture | exception |
Custom |
oxphp_apm_event() |
custom |
Events and attributes are in neither file format. A run's event_count in index.json counts its events. They leave the server only through APM's OTLP export, on the spans APM exports, with the kind in the event attribute oxphp.event.kind.
Export formats
Files live under PROFILER_OUTPUT_DIR, named <run_id>.<ext>, where run_id = <ts_ms>-<req_id_prefix>-<rand4> (for example 1713600000000-a1b2c3d4-0f5e). Every run is named this way, whatever turned profiling on: the header, the sampling draw, or OxPHP\Profile\start() from PHP.
req_id_prefix is the first eight characters of the request id and is not a key on its own: in the id the server mints, those characters are the epoch seconds, shared by every request of that second. Use the request_id field of the index.json entry instead. It carries the id in full and is what the access log prints.
collapsed
Extension: .collapsed
- One line per distinct call stack:
main();func;child <microseconds>. The number is the wall time of every call on that stack minus the calls they made, so the widths in a flamegraph add up. - Opens in speedscope.app: drop the file on the page and pick Left Heavy to put the heaviest stacks first.
- The usual input for SVG flamegraphs with
flamegraph.pl:
curl -H "Authorization: Bearer dev-secret" \
http://localhost:9090/__profiler/runs/<run_id>.collapsed \
| flamegraph.pl --title "Checkout" > flame.svgxhprof
Extension: .xhprof.json
- The pair map
xhprof_disable()returns:{"caller==>callee": {"ct", "wt", "cpu", "mu", "pmu"}}. - Suits scripts. Hunting a memory leak has a
jqone-liner over it. - A viewer with its own import format needs an adapter in front of it.
Storage and cleanup
/tmp/oxphp-profiles/
├── index.json # NDJSON, one record per line
├── 1713600000000-a1b2c3d4-0f5e.xhprof.json
├── 1713600000000-a1b2c3d4-0f5e.collapsed
└── 1713600001234-b2c3d4e5-4a2b.xhprof.jsonindex.json entry schema
{
"run_id": "1713600000000-a1b2c3d4-0f5e",
"request_id": "a1b2c3d4e5f67890",
"trace_id": "0af7651916cd43dd8448eb211c80319c",
"timestamp_ms": 1713600000000,
"duration_ms": 123,
"method": "GET",
"url": "/checkout",
"status": 200,
"user_agent": "Mozilla/5.0 …",
"client_ip": "10.0.0.42",
"source": "Header", // Header | SampleRate | Sdk
"span_count": 4821,
"event_count": 7,
"error_count": 0,
"leaked_count": 0,
"truncated": false, // true: went past PROFILER_MAX_SPANS
"oxphp_version": "0.16.0",
"formats": ["xhprof.json", "collapsed"]
}source names what admitted the run. Header is the header trigger and SampleRate the sampling draw. Sdk covers every run no trigger admitted: a request that turned profiling on for itself with OxPHP\Profile\start(), and also a plugin that selected full profiling on its own.
user_agent is the request's User-Agent header, cut to 512 bytes (a …(truncated) marker included) when longer, with bytes that are not valid UTF-8 replaced with U+FFFD. It is null when the request sent none.
/__profiler/runs reads index.json, sorts it newest first and pages it with ?limit=N&offset=M.
Retention
- Every 5 seconds, a background task deletes the runs past
PROFILER_RETENTION_COUNTand rewritesindex.jsonatomically. PROFILER_DISK_MAX_PER_SECprotects the disk: a run over that rate is not written, andoxphp_profiler_disk_drops_totalgoes up.- Runs stored by 0.15.0 stay listed. Retention deletes their
.speedscope.jsonand.pproffiles as usual, and asking for those two formats over HTTP answers404.
Internal HTTP routes
With INTERNAL_ADDR=0.0.0.0:9090, the profiler serves four routes under /__profiler/. When a token is set, each one requires Authorization: Bearer <PROFILER_AUTH_TOKEN>, compared in constant time; a wrong token answers 401.
| Route | Method | Purpose |
|---|---|---|
/__profiler/runs |
GET | JSON array of runs. ?limit=N&offset=M. |
/__profiler/runs/{id} |
GET | JSON metadata for one run. |
/__profiler/runs/{id}.{format} |
GET | Raw profile bytes. format is xhprof.json or collapsed. |
/__profiler/config |
GET | Current profiler configuration, tokens redacted. |
A run or a format that is not on disk answers 404.
Example scripts
# The 5 slowest of the last 20 runs
curl -s -H "Authorization: Bearer $TOK" \
"http://localhost:9090/__profiler/runs?limit=20" \
| jq '.runs | sort_by(.duration_ms) | reverse | .[:5]'
# All profiles for one URL
curl -s -H "Authorization: Bearer $TOK" \
"http://localhost:9090/__profiler/runs?limit=500" \
| jq '.runs[] | select(.url == "/checkout")'HTTP push
To send each run to a remote collector:
environment:
PROFILER_EXPORT_URL: "http://profile-collector/ingest"
PROFILER_EXPORT_FORMAT: "xhprof"
PROFILER_EXPORT_AUTH_TOKEN: "shared-secret" # optional- The request body is exactly the
PROFILER_EXPORT_FORMAToutput, with no wrapper. A collector that expects its own envelope, such as xhgui or Buggregator, needs an adapter in front of it. - A failed push is retried: 3 attempts with backoff of 100, 200 and 400 ms, within 5 seconds in all.
- Each failure increments
oxphp_profiler_http_push_failures_totaland logs a warning with therun_idand the HTTP status.
Prometheus metrics
Exposed on /metrics:
oxphp_profiler_runs_total{source="header"|"sample"|"sdk"}
oxphp_profiler_spans_collected_total
oxphp_profiler_bytes_written_total{format="xhprof"|"collapsed"}
oxphp_profiler_disk_drops_total
oxphp_profiler_http_push_failures_total
oxphp_profiler_truncated_total
oxphp_profiler_in_memory_runsStarter Prometheus alerts:
- alert: ProfilerDiskDrops
expr: rate(oxphp_profiler_disk_drops_total[5m]) > 0
annotations:
summary: "Profiler is dropping runs on disk: check PROFILER_DISK_MAX_PER_SEC"
- alert: ProfilerPushFailing
expr: rate(oxphp_profiler_http_push_failures_total[5m]) > 0
annotations:
summary: "Profile collector is unreachable"
- alert: ProfilerTruncatingTrees
expr: rate(oxphp_profiler_truncated_total[5m]) > 0
annotations:
summary: "Requests exceed PROFILER_MAX_SPANS: raise the cap or investigate"Command cheatsheet
# Profile one request
curl -H "X-OxPHP-Profile: $TOK" http://localhost/endpoint
# List runs, the 10 slowest first
curl -sH "Authorization: Bearer $TOK" http://localhost:9090/__profiler/runs \
| jq '.runs | sort_by(.duration_ms) | reverse | .[:10]'
# The newest run's id
RUN_ID=$(curl -sH "Authorization: Bearer $TOK" http://localhost:9090/__profiler/runs \
| jq -r '.runs[0].run_id')
# Download for speedscope.app (drop the file on the page)
curl -sH "Authorization: Bearer $TOK" \
http://localhost:9090/__profiler/runs/$RUN_ID.collapsed > run.collapsed
# Download as xhprof
curl -sH "Authorization: Bearer $TOK" \
http://localhost:9090/__profiler/runs/$RUN_ID.xhprof.json > run.xhprof.json
# flamegraph.pl
curl -sH "Authorization: Bearer $TOK" \
http://localhost:9090/__profiler/runs/$RUN_ID.collapsed \
| flamegraph.pl > flame.svg
# Metrics
curl -s http://localhost:9090/metrics | grep oxphp_profiler_
# Current profiler config (tokens are redacted)
curl -sH "Authorization: Bearer $TOK" http://localhost:9090/__profiler/config | jqSee also
- Profiling PHP code: the guide, with the triggers, the PHP SDK, workflows and troubleshooting
- Profiling Examples: complete PHP scenarios
- Internal Server:
INTERNAL_ADDRand the server's other admin routes - Prometheus Metrics: the server's other metrics