With OSSignposter, how do you measure how long a boat-rental app's Bluetooth dock unlock takes, and what goes wrong when two unlocks overlap under one signpost ID?
answer
- same subsystem and category as logs
- begin returns something to keep
- end with the same name
- the default ID is exclusive
- OSSignposter arrived in iOS 15
basics
~20 sCreate an OSSignposter, make a signpost ID per unlock, call beginInterval before sending the command and endInterval with the same name and returned state when the lock replies. Overlapping unlocks sharing the default .exclusive ID cannot be paired reliably.
solid answer
~40 s`OSSignposter` (iOS 15) emits signposts under a subsystem and category, often via `OSSignposter(logger:)`. For each unlock, call `makeSignpostID()`, then `beginInterval("Unlock", id: id)` before the Bluetooth command, keep the returned `OSSignpostIntervalState`, and call `endInterval("Unlock", state)` in the reply callback; `emitEvent(_:id:)` marks single moments in between. Names must be `StaticString`s and must match. The ID pairs begin with end: the default `.exclusive` suits only intervals that never overlap, so two concurrent unlocks sharing it become ambiguous and their durations untrustworthy. The interval state drives runtime checks (one end per state, same name, same log) that crash debug builds and annotate the closing signpost in production. `withIntervalSignpost` only wraps synchronous closures.
code
swift · 19 linesimport os
final class DockUnlocker {
private let signposter = OSSignposter(subsystem: "com.example.boatrental", category: "dock-unlock")
func unlock(dockNumber: Int, completion: @escaping (Bool) -> Void) {
let id = signposter.makeSignpostID()
let state = signposter.beginInterval("Unlock", id: id, "dock \(dockNumber)")
sendUnlockCommand(dockNumber) { [signposter] ok in
signposter.emitEvent("Lock replied", id: id)
signposter.endInterval("Unlock", state, "ok: \(ok)")
completion(ok)
}
}
private func sendUnlockCommand(_ dock: Int, reply: @escaping (Bool) -> Void) {
reply(true)
}
}go deeper
Recall that a signposted interval is a begin call and an end call, and that OSSignposter needs iOS 15.
Explain the returned interval state, why names must match as StaticString, and when emitEvent fits better than an interval.
Show you can measure async work correctly: one signpost ID per attempt, the end in the callback, and the debug-crash versus production-annotation behaviour.
Decide which user journeys the team signposts permanently, with shared names and categories, so measurements stay comparable across releases.
## What a signpost is A **signpost** is a log entry that marks a moment or the beginning or end of an interval, written with the same subsystems and categories you use for logging. `OSSignposter`, a struct in the `os` module available from iOS 15, is the Swift API for emitting them; older code uses the `os_signpost` functions available from iOS 12. Instruments' os_signpost instrument draws the intervals on a timeline, which is covered with Xcode tooling; this answer is about emitting them correctly. ## Creating the signposter - `OSSignposter(subsystem:category:)` takes the same strings as a `Logger`. - `OSSignposter(logger:)` reuses an existing logger's subsystem and category, which keeps a feature's log lines and signposts under one filter. - `isEnabled` tells you whether the signposter can currently emit signposts. ## An interval in four steps 1. Make a **signpost ID**: `let id = signposter.makeSignpostID()`, unique within that signposter's scope. 2. Call `beginInterval(_:id:)` just before the work. The name must be a `StaticString`, and you can add a message with the same interpolation and privacy options as `Logger`. 3. Keep the returned `OSSignpostIntervalState`. 4. Call `endInterval(_:_:)` with the **same name** and that state as soon as the work finishes, even when it finishes in a callback. ```swift import os final class DockUnlocker { private let signposter = OSSignposter(subsystem: "com.example.boatrental", category: "dock-unlock") func unlock(dockNumber: Int, completion: @escaping (Bool) -> Void) { let id = signposter.makeSignpostID() let state = signposter.beginInterval("Unlock", id: id, "dock \(dockNumber)") sendUnlockCommand(dockNumber) { [signposter] ok in signposter.emitEvent("Lock replied", id: id) signposter.endInterval("Unlock", state, "ok: \(ok)") completion(ok) } } private func sendUnlockCommand(_ dock: Int, reply: @escaping (Bool) -> Void) { reply(true) } } ``` `emitEvent(_:id:)` marks a single point of interest, here the moment the lock replies. `withIntervalSignpost(_:id:around:)` wraps a closure, but that closure is synchronous (`() throws -> T`), so it cannot span an unlock that completes later in a Bluetooth callback; use begin and end for that. ## Signpost IDs and overlapping intervals The ID pairs each begin with its end, which matters because intervals with the same name, subsystem and category can be in flight at once. | ID | When Apple says to use it | |---|---| | `.exclusive` (the default) | identical intervals can never overlap | | `OSSignpostID(_:)` | you have a value that uniquely identifies each instance | | `makeSignpostID(from:)` | an object identifies the pair; not across processes | | `makeSignpostID()` | otherwise | If a renter retries and two unlocks overlap while both use the default `.exclusive` ID, the tools can no longer tell which end belongs to which begin, and the measured durations cannot be trusted. A fresh `makeSignpostID()` per unlock keeps each interval distinct. ## Runtime assertions The interval state lets `OSSignposter` check that: - multiple end calls do not consume the same interval state; - the begin and end calls use the same name; - both calls emit to the same underlying log; - the `.exclusive` ID is not used for an interval that crosses process boundaries. In debug builds a failed assertion crashes the app; in production builds the app keeps running and the signposter attaches an error message to the interval's closing signpost. If you cannot hand the state to the code that ends the interval, `OSSignpostIntervalState.beginState(id:)` recreates it from the ID, but it bypasses these checks. The state is `Codable` when it has to travel between processes. ## Common mistakes - **Building the name at runtime.** Interval names must be `StaticString`, so an interpolated name such as `"Unlock dock \(dockNumber)"` will not compile; keep the name fixed and put the dock number in the interval's message. - **Forgetting the failure path.** If the unlock times out, the code must still call `endInterval`, ideally with a message saying it failed, or that attempt never gets an end and has no duration. - **Sharing one ID across retries.** A retry that starts before the first attempt ends needs its own ID. - **A separate subsystem for signposts.** Using the feature's logger keeps intervals and log lines under one filter. ## Reading signposts outside Instruments Signposts are log entries, so the `log` tool can show them, but not by default: `log show` and `log stream` include signpost events only with `--signpost`.
- What happens when endInterval is called twice with the same interval state?That breaks one of the interval-state assertions. In a debug build the app crashes at that call; in a production build it keeps running and the signposter attaches an error message to the interval's closing signpost, so the problem is recorded on the interval itself.
- Why not wrap the unlock in withIntervalSignpost(_:id:around:)?Its closure is synchronous, `() throws -> T`, so the interval ends when the closure returns. An unlock that completes later in a Bluetooth callback would be measured as the time to send the command, not the time to unlock.
- Can the code that ends the interval rebuild the state from the ID alone?Yes, `OSSignpostIntervalState.beginState(id:)` recreates it, but Apple warns this bypasses the consistency assertions between begin and end. Passing the original state, which is `Codable`, keeps the checks.
- The default .exclusive ID is fine for intervals that run concurrently
- endInterval may use a different name as long as the ID matches
- withIntervalSignpost can measure work that finishes in a later callback
- Signposts need a separate subsystem from the app's Logger lines
- A mismatched end crashes release builds as well as debug builds