Instrument Apex
The instrument-apex skill adds Triton logging to an Apex class or trigger — transactions, catch blocks, DML and callout payloads — then optionally deploys it.
The instrument-apex skill reads an Apex class or trigger, works out what kind of code it is, and adds Triton 2.0 instrumentation that follows the same patterns you'd write by hand — the entry-point template, resume-first transactions, logNow in every catch, dmlResults on partial DML, callout payloads, and flush at the right boundaries. Then it shows you the diff and, if you want, deploys.
Usage
/pharos:instrument-apex <path> [--area <Area>] [--type <Type>] [--deploy]<path>
✅
The .cls or .trigger file to instrument. If omitted, the skill asks.
--area
The business/functional Area for these logs. If omitted, the skill infers it from the code and matches related code. Accepts any String — enum values are just examples.
--type
Technical classification. Defaults to Backend; inferred per method (e.g. BackendCall for callouts). Accepts any String.
--deploy
Deploy the file after you accept the diff. Without it, the skill asks at the end.
What it instruments
The skill classifies the file's structure first, because the transaction and flush pattern depends on it — a trigger handler, an @AuraEnabled controller, a batch, a queueable, an @future, and a plain service class each get treated differently. Test classes (@isTest) are left alone entirely.
From there it applies:
Entry-point template. At the start of the entry-point method (never a shared
static {}block — the template is a shared static and the last writer wins), it sets the template with the Category, Type, and Area, then creates each log fromTriton.t.Resume-first transactions. It always resumes an existing transaction before starting a new one, per the structure (see below).
Try/catch logging. Every catch gets a
Triton.logNow(...)with.exception(e)before the originalthrow— the flow is preserved, only logging is added.DML result logging. After partial-success DML (
allOrNone = false), it adds.dmlResults(results). A failure becomes aDMLResultlog at ERROR with an issue; when every row succeeded, nothing is emitted, so the skill never wraps it in anif.Callout / integration logging. HTTP calls get
.integrationPayload(req, res)andType.BackendCall— the category stays the defaultApex.System.debug→ Triton. EverySystem.debug(...)in non-test code becomes a bufferedTriton.debug(...). (Debug lines are safe in production — verbosity is governed by Log Level metadata.)Performance marks, where a span earns its place. For a stretch of work that should show up as a span rather than as one timed log, the skill wraps it in
Triton.startMark()/Triton.endMark()(closed in afinally), usingTriton.startTriggerMark()in trigger handlers. Everything logged inside nests under the mark, and names are derived from the stack trace, so no strings are hand-typed. Marks are not added to every entry point; a one-off duration on a single log is still timed by hand. See Performance Marks in Apex.Flush at boundaries.
Triton.flush()is added at the end of triggers, each batchexecutechunk andfinish,Queueable/Schedulableexecute,@future,@AuraEnabled, and REST/invocable methods — never inside a loop.Related objects. Record ids flowing through a method are attached with
.relatedObject(id)/.relatedObjects(ids), prioritized on error and DML logs.
Template initialization
The entry-point method opens with the template:
Transaction management (resume-first, per structure)
The goal is one correlated transaction across the whole call chain, so the skill always resumes first.
Synchronous entry points (@AuraEnabled, REST, @InvocableMethod) resume a caller-supplied id if there is one:
Other structures each get their own handling:
Trigger handlers guard the start (
if (Triton.TRANSACTION_ID == null) Triton.startTransaction();) so before/after and recursion don't reset the id, and log affected ids with.relatedObjects(...), built null-safely — see Trigger handlers below.Batchable classes implement
Database.Statefuland take the caller's id in the constructor, but only store it there: the constructor runs in the caller's transaction, sostartTransaction()there would overwrite the caller's id.startresumes that id (or starts one if there's none),resumeTransaction(this.txId)re-establishes it at the top of eachexecuteand infinish, and the class flushes perexecutechunk and infinish(each chunk is its own transaction).Queueable / Schedulable take a
String txIdconstructor arg, resume it at the top ofexecute, and flush before returning.@future takes a
String transactionIdparam (only primitives cross the boundary), resumes it, and flushes before returning.Async dispatch sites thread the current id through the hop:
Private / service methods never start, resume, or stop a transaction — they use the ambient one set by the entry point.
Triton.stopTransaction()is only ever called at a true end-of-chain boundary (batchfinish, top-level entry-point completion).
Try/catch — log and rethrow
.exception(e) sets the type, ERROR level, summary, stack trace, details, and flags issue creation — so no redundant .level(...). Downgrading it — Triton.warning(Triton.t.exception(e)) — keeps the exception detail but withdraws the issue; an explicit .createIssue() keeps it. A custom summary must chain after .exception(e), or it gets overwritten:
DML result logging
Callout / integration logging
Trigger handlers: logging never fails the write
A trigger runs inside someone else's DML. On a managed-package object — or any object a system or integration user writes to — a throw from your handler rolls back a write you don't own and may not be able to test as. Logging must never be the reason that happens.
What Triton guarantees. Triton.log and the level methods, logNow, flush, flushTop, the Flow Log action, and the LWC endpoint catch their own failures. If Pharos refuses a publish, those logs are dropped and the failure is written to the Apex debug log at ERROR; the business write goes ahead.
What the skill still takes care of in your code:
Null-safe builder arguments. The builder expression runs in your code before Triton is called, so Triton can't catch what throws there —
Trigger.newMap.keySet()in before insert,record.Parent__r.Nameon an unqueried relationship,e.getCause().getMessage()when there's no cause. Record ids are built like this:
Governor limits.
System.LimitExceptioncan't be caught by anyone. In a 200-record trigger the skill buffers withTriton.log(...)— neverlogNowin a loop — flushes once at the end of the handler, and keeps per-record details lean.No new
try/catcharound business logic. Where a catch already exists, it logs and keeps the existing throw or return.One publish on managed-package objects.
Triton.log(...)plus a singleTriton.flush()as the handler's last statement, rather thanlogNow. The audit flagslogNowandflushon these paths for review.
The test it adds. The skill adds a test that performs the triggering DML as a low-privilege user and asserts the write landed:
Know what this doesn't prove. System.runAs enforces record sharing only, not object or field permissions, and a user you can create in a test can't reproduce a hidden platform integration user. A passing test shows the handler survives a restricted human user; Triton's never-throw guarantee is what covers the rest. To prove your code survives a refused publish, see TritonTestFactory.failPublishing().
Review the diff
Before writing anything, the skill presents a summary — the structure it classified, the template it set, how it handled transactions, and counts of try/catch, DML, callout, System.debug, and flush changes (plus any sanctioned signature change) — followed by the key changes. You answer:
yes— apply and write the file.modify— give free-form corrections; the skill re-applies and asks again.skip— stop without writing.
Deploy
The instrumented content is written back to the original file (never a new one). Then, with --deploy or on confirmation:
A "status": "Succeeded" reports the deployed component; failures show the component-failures table and stop without reverting your file.
Where to go next
Instrument LWC — the front-end half of a stitched transaction.
Instrument Flows — the Flow leg of the same trace.
Instrument the Org — run this skill across every class and trigger at once.
Logging in Apex · Transaction Management — the hand-written patterns the skill automates.
Performance Marks in Apex — the mark API the skill wraps entry points in.
Spans & Distributed Tracing — how these correlated transactions map to OpenTelemetry.
Last updated