|
| 1 | +import { TelemetryProvider } from "./provider" |
| 2 | +import { SpanKind, SpanStatusCode } from "@opentelemetry/api" |
| 3 | + |
| 4 | +export namespace LocTelemetry { |
| 5 | + export interface FileChange { |
| 6 | + filePath: string |
| 7 | + additions: number |
| 8 | + deletions: number |
| 9 | + toolName: string |
| 10 | + changeType?: "add" | "update" | "delete" | "move" |
| 11 | + } |
| 12 | + |
| 13 | + export function recordFileChange(change: FileChange, sessionID: string): void { |
| 14 | + const tracer = TelemetryProvider.getTracer() |
| 15 | + if (!tracer) return |
| 16 | + |
| 17 | + const span = tracer.startSpan("file.change", { |
| 18 | + kind: SpanKind.INTERNAL, |
| 19 | + attributes: { |
| 20 | + "opencode.session.id": sessionID, |
| 21 | + "opencode.tool.name": change.toolName, |
| 22 | + "opencode.file.path": change.filePath, |
| 23 | + "opencode.loc.additions": change.additions, |
| 24 | + "opencode.loc.deletions": change.deletions, |
| 25 | + "opencode.loc.net_change": change.additions - change.deletions, |
| 26 | + }, |
| 27 | + }) |
| 28 | + |
| 29 | + if (change.changeType) { |
| 30 | + span.setAttribute("opencode.file.change_type", change.changeType) |
| 31 | + } |
| 32 | + |
| 33 | + span.setStatus({ code: SpanStatusCode.OK }) |
| 34 | + span.end() |
| 35 | + } |
| 36 | + |
| 37 | + export function recordBatchChanges(changes: FileChange[], sessionID: string, toolName: string): void { |
| 38 | + const tracer = TelemetryProvider.getTracer() |
| 39 | + if (!tracer) return |
| 40 | + |
| 41 | + if (changes.length === 0) return |
| 42 | + |
| 43 | + let totalAdditions = 0 |
| 44 | + let totalDeletions = 0 |
| 45 | + const fileDetails = changes.map((change) => { |
| 46 | + totalAdditions += change.additions |
| 47 | + totalDeletions += change.deletions |
| 48 | + return { |
| 49 | + path: change.filePath, |
| 50 | + additions: change.additions, |
| 51 | + deletions: change.deletions, |
| 52 | + type: change.changeType, |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + const span = tracer.startSpan("file.batch_change", { |
| 57 | + kind: SpanKind.INTERNAL, |
| 58 | + attributes: { |
| 59 | + "opencode.session.id": sessionID, |
| 60 | + "opencode.tool.name": toolName, |
| 61 | + "opencode.batch.file_count": changes.length, |
| 62 | + "opencode.batch.total_additions": totalAdditions, |
| 63 | + "opencode.batch.total_deletions": totalDeletions, |
| 64 | + "opencode.batch.net_change": totalAdditions - totalDeletions, |
| 65 | + "opencode.batch.files": JSON.stringify(fileDetails), |
| 66 | + }, |
| 67 | + }) |
| 68 | + |
| 69 | + span.setStatus({ code: SpanStatusCode.OK }) |
| 70 | + span.end() |
| 71 | + } |
| 72 | +} |
0 commit comments