Repository navigation
Align Virtual GenAI with the OpenTelemetry GenAI conventions on time to first token and span identification - #14273
Conversation
…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
left a comment
There was a problem hiding this comment.
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_TOKENGenAISemanticAttributes, 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:
testOTLPSpanIgnoresTimeToFirstTokenInMillisbecomes the fallback test ("100"reads as 100). Please add a case with both tags, where the new one wins.- Keep one
CheckerFixturesGenAI fixture on the old tag, so the parity checker compares the fallback in the Java and TAL paths. changes.mdno longer needs Breaking Change. It can say the old tag, in milliseconds, is still read when a span has nogen_ai.response.time_to_first_chunk, for SkyWalking Java Agent 9.7 and 9.8. The same note goes invirtual-genai.md.
…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.
Fix Virtual GenAI reading the time to first token and identifying OTLP / Zipkin spans against the OpenTelemetry GenAI semantic conventions
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 isgen_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 forgen_ai.response.time_to_first_chunk, converted to milliseconds. A span without it is still read forgen_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 sametimeToFirstTokenfield, sogen_ai_model_ttft_avgandgen_ai_model_ttft_percentilekeep their value and unit. The native path is thenative-virtual-genaiTAL rule, so TAL gets aparseSecondsAsMillisOrZero(text)function for the conversion; the OTLP and Zipkin path isGenAIMeterAnalyzer, behindGenAISpanListener.Failed calls on OTLP / Zipkin spans. A span without
gen_ai.response.modelwas skipped. A call that failed before the provider answered has no response model, sogen_ai_provider_slaandgen_ai_model_slamissed such failures. Such a span is now counted by itsgen_ai.request.model.Spans that are not a model call, on OTLP / Zipkin spans. Every span with
gen_ai.response.modelwas counted as a model call. The conventions warn that the same tokens can be reported on several types of spans, and a span such asfetch_responsecan repeat the model and the token usage of an earlier call. A span whosegen_ai.operation.nameisfetch_response,create_agent,execute_tool,plan,retrieval,user_speech, a memory operation, or starts withinvoke_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-genaicase passes with the agent imageSW_AGENT_JAVA_COMMITpins 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
CHANGESlog.GraalVM distro impact:
TalGeneralFunctionsgets theparseSecondsAsMillisOrZero(text)function, in the existing function class, no new provider.tal/native-virtual-genai.yamlreadsgen_ai.response.time_to_first_chunkwith it, andgen_ai.server.time_to_first_tokenwithparseIntOrZerowhen the span has no time to first chunk.CheckerFixtures.javagets three GenAI fixtures for the new tag; the existing ones are unchanged.