Skip to content

Align Virtual GenAI with the OpenTelemetry GenAI conventions on time to first token and span identification - #14273

Merged
wu-sheng merged 4 commits into
apache:masterfrom
peachisai:fix/genai-span-semconv-alignment
Nov 27, 2026
Merged

wu-sheng merged 4 commits into
apache:masterfrom
peachisai:fix/genai-span-semconv-alignment

Conversation

@peachisai

@peachisai peachisai commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fix Virtual GenAI reading the time to first token and identifying OTLP / Zipkin spans against the OpenTelemetry GenAI semantic conventions

  • Add a unit test to verify that the fix works.
  • Explain briefly why the bug exists and how to fix it.

This is the OAP side of apache/skywalking-java#851.

Time to first token. Virtual GenAI read only gen_ai.server.time_to_first_token, in milliseconds, from the span. The OpenTelemetry GenAI semantic conventions give that name to a metric; the span attribute they define is gen_ai.response.time_to_first_chunk, in seconds, so an OTLP or Zipkin span following the conventions reported no time to first token. The spans of a SkyWalking agent, OTLP spans and Zipkin spans are now read for gen_ai.response.time_to_first_chunk, converted to milliseconds. A span without it is still read for gen_ai.server.time_to_first_token, in milliseconds, which SkyWalking Java Agent 9.7 and 9.8 report, so nothing changes for released agents and this is not a breaking change. The tag is picked by whether the new one is present, then parsed in its own unit; both end as whole milliseconds in the same timeToFirstToken field, so gen_ai_model_ttft_avg and gen_ai_model_ttft_percentile keep their value and unit. The native path is the native-virtual-genai TAL rule, so TAL gets a parseSecondsAsMillisOrZero(text) function for the conversion; the OTLP and Zipkin path is GenAIMeterAnalyzer, behind GenAISpanListener.

Failed calls on OTLP / Zipkin spans. A span without gen_ai.response.model was skipped. A call that failed before the provider answered has no response model, so gen_ai_provider_sla and gen_ai_model_sla missed such failures. Such a span is now counted by its gen_ai.request.model.

Spans that are not a model call, on OTLP / Zipkin spans. Every span with gen_ai.response.model was counted as a model call. The conventions warn that the same tokens can be reported on several types of spans, and a span such as fetch_response can repeat the model and the token usage of an earlier call. A span whose gen_ai.operation.name is fetch_response, create_agent, execute_tool, plan, retrieval, user_speech, a memory operation, or starts with invoke_ is now skipped. A span with any other operation name, or with none, is counted as before.

E2E. With the old tag still read, the virtual-genai case passes with the agent image SW_AGENT_JAVA_COMMIT pins today, so this pull request does not wait for apache/skywalking-java#851.

  • If this pull request closes/resolves/fixes an existing issue, replace the issue number. Closes #.

  • Update the CHANGES log.

  • GraalVM distro impact:

    • None.
    • Impacted, by: a DSL engine (TAL), bundled rule files, test files the distro's tests reuse. TalGeneralFunctions gets the parseSecondsAsMillisOrZero(text) function, in the existing function class, no new provider. tal/native-virtual-genai.yaml reads gen_ai.response.time_to_first_chunk with it, and gen_ai.server.time_to_first_token with parseIntOrZero when the span has no time to first chunk. CheckerFixtures.java gets three GenAI fixtures for the new tag; the existing ones are unchanged.

…to first token and span identification

- Read the time to first token from gen_ai.response.time_to_first_chunk, in seconds, on
  native, OTLP and Zipkin spans. gen_ai.server.time_to_first_token, in milliseconds, is no
  longer read: the conventions give that name to a metric. TAL gets
  parseSecondsAsMillisOrZero(text) for the conversion.
- Count an OTLP or Zipkin span without gen_ai.response.model by its gen_ai.request.model,
  so a call that failed before the provider answered is no longer skipped.
- Skip OTLP and Zipkin spans whose gen_ai.operation.name is not a model call
  (fetch_response, create_agent, execute_tool, plan, retrieval, user_speech, the memory
  operations and every invoke_* name), which can repeat the token usage of other spans.
@wu-sheng wu-sheng added this to the 11.2.0 milestone Nov 26, 2026

@wu-sheng wu-sheng left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, the alignment looks right. I checked the skipped operation names against gen_ai.operation.name in semantic-conventions-genai, and OTLP double attributes reach the analyzer as 0.457 / 1.0E-4 text, which Double.parseDouble reads.

One request: please keep reading gen_ai.server.time_to_first_token as a fallback instead of dropping it. SkyWalking Java Agent 9.7.0 and 9.8.0, the latest release, report only that tag, and OAP 10.4.0 and later read it. Dropping it empties gen_ai_model_ttft_avg and gen_ai_model_ttft_percentile for every released agent until 9.9.0 ships and users upgrade. With the fallback, this PR is no longer a breaking change for them. The virtual-genai e2e also passes with the current SW_AGENT_JAVA_COMMIT pin, so this PR no longer has to wait for apache/skywalking-java#851.

The two tags have different units, seconds as a decimal and whole milliseconds, so pick the tag first and then its parser. Pick by whether the new tag is present, not by whether its value parsed to 0, so a malformed new value doesn't log a second warning for the old tag.

tal/native-virtual-genai.yaml:

// gen_ai.response.time_to_first_chunk: seconds, the span attribute of the OpenTelemetry GenAI conventions,
// reported by SkyWalking Java Agent 9.9.0 and later
def timeToFirstChunk = genAITags.get("gen_ai.response.time_to_first_chunk") as String
def timeToFirstToken = 0
if (!isBlank(timeToFirstChunk)) {
  timeToFirstToken = parseSecondsAsMillisOrZero(timeToFirstChunk)
} else {
  // SkyWalking Java Agent 9.7 and 9.8 report gen_ai.server.time_to_first_token instead, in milliseconds
  timeToFirstToken = parseIntOrZero(genAITags.get("gen_ai.server.time_to_first_token") as String)
}

GenAIMeterAnalyzer, one helper for extractMetricsFromSWSpan and extractMetrics:

/**
 * @return the time to first token in milliseconds: {@code gen_ai.response.time_to_first_chunk}, in seconds, the
 *         span attribute of the OpenTelemetry GenAI conventions; else {@code gen_ai.server.time_to_first_token}, in
 *         milliseconds, which SkyWalking Java Agent 9.7 and 9.8 report, as do OTLP / Zipkin producers that followed
 *         the earlier virtual-genai.md
 */
private int timeToFirstTokenMillis(final Map<String, String> tags) {
    final String timeToFirstChunk = tags.get(GenAISemanticAttributes.RESPONSE_TIME_TO_FIRST_CHUNK);
    if (StringUtil.isNotBlank(timeToFirstChunk)) {
        return parseSecondsAsMillis(timeToFirstChunk);
    }
    // SkyWalking Java Agent 9.7 and 9.8
    return parseSafeInt(tags.get(GenAISemanticAttributes.SERVER_TIME_TO_FIRST_TOKEN));
}

GEN_AI_KEYS needs both keys. The Zipkin path copies only the keys listed there, so without the old one Zipkin spans never see it:

GenAISemanticAttributes.RESPONSE_TIME_TO_FIRST_CHUNK,
// Read when a span has no time to first chunk: SkyWalking Java Agent 9.7 and 9.8, and the earlier virtual-genai.md
GenAISemanticAttributes.SERVER_TIME_TO_FIRST_TOKEN

GenAISemanticAttributes, keep the constant:

/**
 * Milliseconds, as an int. SkyWalking Java Agent 9.7 and 9.8 report the time to first token under this tag; later
 * agents report {@link #RESPONSE_TIME_TO_FIRST_CHUNK}. Read only when a span has no time to first chunk. The
 * OpenTelemetry GenAI conventions give this name to a metric, not to a span attribute.
 */
public static final String SERVER_TIME_TO_FIRST_TOKEN = "gen_ai.server.time_to_first_token";

Tests and docs that follow from this:

  • testOTLPSpanIgnoresTimeToFirstTokenInMillis becomes the fallback test ("100" reads as 100). Please add a case with both tags, where the new one wins.
  • Keep one CheckerFixtures GenAI fixture on the old tag, so the parity checker compares the fallback in the Java and TAL paths.
  • changes.md no longer needs Breaking Change. It can say the old tag, in milliseconds, is still read when a span has no gen_ai.response.time_to_first_chunk, for SkyWalking Java Agent 9.7 and 9.8. The same note goes in virtual-genai.md.

@wu-sheng wu-sheng added the enhancement Enhancement on performance or codes label Nov 27, 2026
…e to first chunk

SkyWalking Java Agent 9.7 and 9.8 report the time to first token under
gen_ai.server.time_to_first_token, in milliseconds. A span without
gen_ai.response.time_to_first_chunk is read for it, on native, OTLP and Zipkin spans, so
the change is no longer breaking for released agents. The tag is picked by whether the new
one is present, then parsed in its own unit; both end as whole milliseconds in the same
field.
@wu-sheng
wu-sheng merged commit 1d52e07 into apache:master Nov 27, 2026
546 of 549 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Enhancement on performance or codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants