Welcome to the final chapter of our architecture deep dive!
In the previous chapter, Resource & Cost Accounting, we learned how to track the immediate "bill" for the user. We know how much money was spent during the session.
But what if we want to answer bigger questions later?
The user doesn't need to see this on their screen, but the developers (us!) need this data to improve the software. This is Telemetry Infrastructure.
Imagine you are building an airplane.
In bootstrap, the Telemetry layer serves as this "Black Box." It provides standardized slots in the Global Application State where different tools can plug in and report statistics without knowing where that data goes.
We rely on a standard industry framework called OpenTelemetry. You don't need to know how it works internally, just that it gives us three tools:
This measures numeric values. It answers questions like "How many?" or "How fast?"
total_errors).A raw number often isn't enough. We don't just want to know "5 lines of code were changed." We want to know:
type: "added" (vs. removed)
In bootstrap, we use specific AttributedCounter objects stored in the state, such as locCounter (Lines of Code) and costCounter.
Let's say you are writing a new tool that deletes files. You want to track how many files are deleted across all users.
You don't need to set up a database connection. You just grab the counter from the global state and add to it.
When the agent writes code, it calculates the difference (diff) and reports it.
import { getLocCounter } from './state.js'
function reportChanges(added: number, removed: number) {
const counter = getLocCounter()
// Safety check: Telemetry might be disabled
if (counter) {
// Record lines added
counter.add(added, { type: 'added' })
// Record lines removed
counter.add(removed, { type: 'removed' })
}
}
While Resource & Cost Accounting shows the cost to the user, we also report it to our telemetry system so we can analyze global trends.
import { getCostCounter } from './state.js'
function trackApiCost(cost: number, model: string) {
const counter = getCostCounter()
if (counter) {
// We tag the cost with the model name
counter.add(cost, { model: model })
}
}
How does a number get from a function call to a graph on a dashboard?
MeterProvider (the engine).sessionCounter) and store them in STATE..add().
Let's look at how state.ts manages these recorders.
In the State object, we hold references to the generic OpenTelemetry interfaces (Meter, LoggerProvider) and our specific custom counters.
// state.ts
type State = {
// The main engine
meter: Meter | null
// Specific counters ready for use
sessionCounter: AttributedCounter | null
locCounter: AttributedCounter | null
costCounter: AttributedCounter | null
// ... others like prCounter, tokenCounter
// ... other state fields
}
We have a setup function setMeter. This is called during application startup (bootstrapping). It creates the counters once and saves them into the global state so they can be reused everywhere.
// state.ts
export function setMeter(
meter: Meter,
createCounter: (name: string, opts: any) => AttributedCounter
): void {
STATE.meter = meter
// Define the 'Lines of Code' counter
STATE.locCounter = createCounter('claude_code.lines_of_code.count', {
description: 'Count of lines modified',
})
// Define the 'Cost' counter
STATE.costCounter = createCounter('claude_code.cost.usage', {
unit: 'USD',
})
}
We provide simple getters. Note that they return null if telemetry hasn't been initialized yet (or if it's disabled). This prevents the app from crashing just because the monitoring system isn't ready.
// state.ts
export function getLocCounter(): AttributedCounter | null {
return STATE.locCounter
}
export function getCostCounter(): AttributedCounter | null {
return STATE.costCounter
}
Telemetry is the observer that watches all other systems:
STATE singleton from Global Application State.sessionId from Session Lifecycle Management so we can trace a specific user's journey.costCounter for long-term analysis.In this chapter, we learned:
Meter and AttributedCounter.locCounter and costCounter are stored in the Global State.Congratulations! You have navigated the core architecture of the Bootstrap project.
With these five pillars, you understand how the application maintains stability, tracks history, and manages resources. You are now ready to dive into the codebase and build your own tools!
Generated by Code IQ