Skip to content

Commit a25022c

Browse files
committed
fix(handlers): address code-review feedback on lines_of_code semantics
- handleSessionError: read sessionTotals before deleting, so the AGENT_NAME attribute on the error span is actually populated (pre-existing bug the reviewer surfaced in this diff's adjacent block). - handleSessionDiff: write sessionDiffTotals through setBoundedMap, matching the repo convention for sessionTotals / sessionSpans / pendingToolSpans (MAX_PENDING = 500 cap against unbounded growth). - otel.ts / README.md: spell out the counter's "sums gross additions across non-revert deltas" semantics and direct readers to lines_of_code.total as the authoritative live cumulative; note that intra-message reverts are not captured.
1 parent 9eaefc7 commit a25022c

4 files changed

Lines changed: 7 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ An [opencode](https://opencode.ai) plugin that exports telemetry via OpenTelemet
3131
| `opencode.session.count` | Counter | Incremented on each `session.created` event |
3232
| `opencode.token.usage` | Counter | Per token type: `input`, `output`, `reasoning`, `cacheRead`, `cacheCreation` |
3333
| `opencode.cost.usage` | Counter | USD cost per completed assistant message |
34-
| `opencode.lines_of_code.count` | Counter | Net lines added/removed in the session. The handler emits only the positive delta since the previous `session.diff`, so summing the counter yields the true cumulative total without double-counting. |
35-
| `opencode.lines_of_code.total` | Gauge | Current cumulative lines added/removed for the session, refreshed on every `session.diff`. Drops back to `0` if opencode reports a revert to baseline. |
34+
| `opencode.lines_of_code.count` | Counter | Emits only the positive delta since the previous `session.diff`, so summing the counter avoids the old double-counting. **Caveats:** when opencode reports a cumulative drop (a revert), the negative delta is skipped — so summing the counter reflects gross additions across non-revert deltas and can overstate net after a revert. Rewrites that happen and are reverted *within a single message* aren't captured at all (opencode publishes the per-message cumulative, not intra-message churn). Use `opencode.lines_of_code.total` for the authoritative live total. |
35+
| `opencode.lines_of_code.total` | Gauge | Current cumulative lines added/removed for the session, refreshed on every `session.diff`. Drops back to `0` if opencode reports a revert to baseline. Authoritative for "what does this session currently amount to". |
3636
| `opencode.commit.count` | Counter | Git commits detected via bash tool |
3737
| `opencode.tool.duration` | Histogram | Tool execution time in milliseconds |
3838
| `opencode.cache.count` | Counter | Cache activity per message: `type=cacheRead` or `type=cacheCreation` |

src/handlers/activity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SeverityNumber } from "@opentelemetry/api-logs"
22
import type { EventSessionDiff, EventCommandExecuted } from "@opencode-ai/sdk"
3-
import { isMetricEnabled } from "../util.ts"
3+
import { isMetricEnabled, setBoundedMap } from "../util.ts"
44
import type { HandlerContext } from "../types.ts"
55

66
/**
@@ -25,7 +25,7 @@ export function handleSessionDiff(e: EventSessionDiff, ctx: HandlerContext) {
2525
const prev = ctx.sessionDiffTotals.get(sessionID) ?? { additions: 0, deletions: 0 }
2626
const deltaAdded = totalAdded - prev.additions
2727
const deltaRemoved = totalRemoved - prev.deletions
28-
ctx.sessionDiffTotals.set(sessionID, { additions: totalAdded, deletions: totalRemoved })
28+
setBoundedMap(ctx.sessionDiffTotals, sessionID, { additions: totalAdded, deletions: totalRemoved })
2929

3030
const baseAttrs = { ...ctx.commonAttrs, "session.id": sessionID }
3131

src/handlers/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export function handleSessionError(e: EventSessionError, ctx: HandlerContext) {
145145
const rawID = e.properties.sessionID
146146
const sessionID = rawID ?? "unknown"
147147
const error = errorSummary(e.properties.error)
148+
const totals = rawID ? ctx.sessionTotals.get(rawID) : undefined
148149
if (rawID) {
149150
ctx.sessionTotals.delete(rawID)
150151
ctx.sessionDiffTotals.delete(rawID)
@@ -154,7 +155,6 @@ export function handleSessionError(e: EventSessionError, ctx: HandlerContext) {
154155
if (rawID) {
155156
const sessionSpan = ctx.sessionSpans.get(rawID)
156157
if (sessionSpan) {
157-
const totals = ctx.sessionTotals.get(rawID)
158158
if (totals) sessionSpan.setAttribute(AGENT_NAME, totals.agent)
159159
sessionSpan.setStatus({ code: SpanStatusCode.ERROR, message: error })
160160
sessionSpan.setAttribute("error", error)

src/otel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ export function createInstruments(prefix: string): Instruments {
147147
}),
148148
linesCounter: meter.createCounter(`${prefix}lines_of_code.count`, {
149149
unit: "{line}",
150-
description: "Per-event delta of lines added/removedsum over a session yields the net cumulative total.",
150+
description: "Positive per-event delta of lines added/removed; sum reflects gross additions across non-revert deltas and may overstate net after a revert. Use lines_of_code.total for the authoritative live cumulative value.",
151151
}),
152152
linesTotalGauge: meter.createGauge(`${prefix}lines_of_code.total`, {
153153
unit: "{line}",
154-
description: "Cumulative lines added/removed in the current session, refreshed on every session.diff.",
154+
description: "Authoritative live cumulative lines added/removed for the current session, refreshed on every session.diff; drops back to 0 if opencode reports a revert to baseline.",
155155
}),
156156
commitCounter: meter.createCounter(`${prefix}commit.count`, {
157157
unit: "{commit}",

0 commit comments

Comments
 (0)