Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Record attachment operation counts in telemetry - #14360

Merged
BagToad merged 1 commit into
trunkfrom
bagtoad/attachment-operation-telemetry
Sep 9, 2026
Merged

Record attachment operation counts in telemetry#14360
BagToad merged 1 commit into
trunkfrom
bagtoad/attachment-operation-telemetry

Conversation

@BagToad

@BagToad BagToad commented Sep 4, 2026

Copy link
Copy Markdown
Member

Follow-up to #14327, which added attachment invocation counts.

Description

Attachment telemetry records how many --attach flags a command received, but not how successful uploads changed the command's Markdown.

This adds append_ops_count and replace_ops_count measures to the existing attachment_invocation event. An attachment that replaces several textual references counts as one replacement.

The event is registered before argument validation and resolved when telemetry flushes. It still reports attach_count for failed invocations, including authentication and validation failures, while successful uploads can add their operation counts to the same event.

How did you test this change?

I built the local gh binary and exercised a disposable test issue. I attached two PNGs, referenced the first twice in the comment body, and left the second unreferenced.

The posted comment rewrote both references to the first uploaded URL and appended the second attachment. With GH_TELEMETRY=log, I saw one attachment_invocation event with attach_count set to 2 and replace_ops_count and append_ops_count each set to 1, plus the paired command_invocation event.

I also ran the command with a missing attachment. It failed validation and logged attach_count as 1, with both operation counts set to 0.

The recordings project the raw log through jq so they show only event type, command, flags, and measures.

Mixed append and replace

A mixed attachment invocation records one replacement and one append operation

Validation failure

A failed attachment validation retains the attachment count and records zero operations

Key points

  • attachment_invocation uses dimensions {command} and measures {attach_count, append_ops_count, replace_ops_count}. It is sampled at 100%.
  • After --attach is parsed, authentication, positional argument, and command validation failures retain attach_count and report zero operations.
  • Partial failures count only uploads completed before the first failure. A final issue, pull request, or comment write failure does not erase completed Markdown operation counts.
  • The event contains no paths, alt text, body content, repository information, or asset data.
  • Generic command telemetry remains unaware of attachment behavior.

Notes for reviewers

Before and after code diagram for deferred attachment telemetry

Start with the telemetry recorder interface and service implementation. The important new pattern is RecordDeferred: a caller registers an event early, with its original recorded timestamp, but supplies a callback that resolves the final event during Flush. Sampling and disabled-telemetry checks still happen before the callback is evaluated. The existing immediate Record path is unchanged.

Then review InvocationTelemetry, which references the parsed attachment flag but owns the recorder and mutable event. Each command options struct stores it beside AttachFlag. It registers the deferred event from Cobra's argument validator, after flags are parsed but before positional argument validation. Successful uploads update this telemetry object before the process-level flush, producing exactly one attachment_invocation across subsequent validation and execution paths.

The telemetry service test covers flush-time resolution. The rooted attachment command test covers failure in a parent persistent pre-run hook, the lifecycle edge case that motivated the deferred recorder.

Authorship and follow-up

Who wrote this:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @BagToad will read and reply directly. Name the account.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

@BagToad
BagToad requested a review from a team as a code owner September 4, 2026 21:51
@BagToad
BagToad requested review from tidy-dev and a balanced review from Copilot September 4, 2026 21:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The deferred lifecycle, operation accounting, command integrations, and failure paths are consistently implemented and covered by focused tests.

Review tier: Balanced
Findings: None

What changed in this PR

Adds attachment Markdown operation counts to issue and pull request telemetry while preserving failed-invocation reporting.

Changes:

  • Tracks upload, append, and replacement outcomes.
  • Adds flush-time deferred telemetry resolution.
  • Integrates and tests telemetry across six attachment-enabled commands.
File Description
internal/​attachments/​attach.go Returns detailed upload results.
internal/​attachments/​attach_test.go Tests operation counting.
internal/​attachments/​doc.go Updates uploader documentation.
internal/​attachments/​flags.go Removes immediate telemetry recording.
internal/​attachments/​flags_test.go Tests deferred invocation telemetry.
internal/​attachments/​references.go Counts rewritten attachments.
internal/​attachments/​telemetry.go Implements invocation telemetry lifecycle.
internal/​attachments/​test.go Adds telemetry test helpers.
internal/​gh/​ghtelemetry/​telemetry.go Extends the recorder interface.
internal/​telemetry/​fake.go Supports deferred events in the spy.
internal/​telemetry/​telemetry.go Resolves deferred events during flush.
internal/​telemetry/​telemetry_test.go Verifies flush-time resolution.
pkg/​cmd/​issue/​comment/​comment.go Registers deferred attachment telemetry.
pkg/​cmd/​issue/​comment/​comment_test.go Tests validation-failure telemetry.
pkg/​cmd/​issue/​create/​create.go Records creation attachment operations.
pkg/​cmd/​issue/​create/​create_test.go Tests creation operation counts.
pkg/​cmd/​issue/​edit/​edit.go Records edit attachment operations.
pkg/​cmd/​issue/​edit/​edit_test.go Tests edit operation counts.
pkg/​cmd/​pr/​comment/​comment.go Registers deferred attachment telemetry.
pkg/​cmd/​pr/​comment/​comment_test.go Tests validation-failure telemetry.
pkg/​cmd/​pr/​create/​create.go Records creation attachment operations.
pkg/​cmd/​pr/​create/​create_test.go Tests creation operation counts.
pkg/​cmd/​pr/​edit/​edit.go Records edit attachment operations.
pkg/​cmd/​pr/​edit/​edit_test.go Tests edit operation counts.
pkg/​cmd/​pr/​shared/​commentable.go Records shared comment upload operations.
pkg/​cmd/​pr/​shared/​commentable_test.go Tests comment operation telemetry.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@tidy-dev
tidy-dev removed their request for review September 8, 2026 13:49
@BagToad
BagToad merged commit ae1ba32 into trunk Sep 9, 2026
19 checks passed
@BagToad
BagToad deleted the bagtoad/attachment-operation-telemetry branch September 9, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants