Skip to content

Source gh-aw.effective_tokens from agent_usage.json fallback in conclusion spans#35932

Closed
Copilot wants to merge 4 commits into
mainfrom
copilot/otel-advisor-improve-effective-tokens
Closed

Source gh-aw.effective_tokens from agent_usage.json fallback in conclusion spans#35932
Copilot wants to merge 4 commits into
mainfrom
copilot/otel-advisor-improve-effective-tokens

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 30, 2026

sendJobConclusionSpan only read GH_AW_EFFECTIVE_TOKENS, which is not reliably present in post-step environments, so gh-aw.effective_tokens was silently missing from conclusion spans. This change makes the metric durable by sourcing it from /tmp/gh-aw/agent_usage.json when the env var is unavailable.

  • Token sourcing reliability

    • In actions/setup/js/send_otlp_span.cjs, conclusion-span token sourcing now:
      • prefers process.env.GH_AW_EFFECTIVE_TOKENS
      • falls back to /tmp/gh-aw/agent_usage.jsoneffective_tokens when env is absent
    • Keeps existing emission behavior (gh-aw.effective_tokens only when parsed value is a positive integer).
  • Precedence + fallback behavior codified in tests

    • In actions/setup/js/send_otlp_span.test.cjs, added focused coverage for:
      • artifact fallback when env var is absent
      • env var precedence when both env and artifact are present
      • omission case when neither source provides a usable value
    • Ensures the env-precedence path does not read agent_usage.json.
  • Implementation sketch

    const rawETFromEnv = process.env.GH_AW_EFFECTIVE_TOKENS || "";
    const usageFromArtifact = rawETFromEnv ? null : readJSONIfExists("/tmp/gh-aw/agent_usage.json");
    const rawET = rawETFromEnv || String((usageFromArtifact && usageFromArtifact.effective_tokens) || "");
    const effectiveTokens = rawET ? parseInt(rawET, 10) : NaN;

Copilot AI and others added 3 commits May 30, 2026 14:40
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve OTel instrumentation for gh-aw.effective_tokens Source gh-aw.effective_tokens from agent_usage.json fallback in conclusion spans May 30, 2026
Copilot AI requested a review from pelikhan May 30, 2026 14:47
@pelikhan pelikhan closed this May 30, 2026
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.

[otel-advisor] OTel improvement: source gh-aw.effective_tokens from agent_usage.json (native cost metric is on 0 spans)

2 participants