Skip to content

Commit 6186151

Browse files
sgdesmetgeoand
authored andcommitted
Trace tool calls
1 parent cbf57ca commit 6186151

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/config/TracingConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,16 @@ public interface TracingConfig {
1515
*/
1616
@WithDefault("false")
1717
Boolean includeCompletion();
18+
19+
/**
20+
* If enabled, tool call arguments are included on the generated spans
21+
*/
22+
@WithDefault("false")
23+
Boolean includeToolArguments();
24+
25+
/**
26+
* If enabled, tool call results are included on the generated spans
27+
*/
28+
@WithDefault("false")
29+
Boolean includeToolResult();
1830
}

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/tool/ToolSpanWrapper.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,52 @@
44

55
import jakarta.inject.Inject;
66

7+
import org.eclipse.microprofile.config.inject.ConfigProperty;
8+
79
import dev.langchain4j.agent.tool.ToolExecutionRequest;
810
import io.opentelemetry.api.trace.Span;
11+
import io.opentelemetry.api.trace.SpanKind;
912
import io.opentelemetry.api.trace.Tracer;
1013
import io.opentelemetry.context.Scope;
1114

1215
public class ToolSpanWrapper implements QuarkusToolExecutor.Wrapper {
1316

1417
private final Tracer tracer;
18+
private final boolean includeArguments;
19+
private final boolean includeResult;
1520

1621
@Inject
17-
public ToolSpanWrapper(Tracer tracer) {
22+
public ToolSpanWrapper(Tracer tracer,
23+
@ConfigProperty(name = "quarkus.langchain4j.tracing.include-tool-arguments") boolean includeArguments,
24+
@ConfigProperty(name = "quarkus.langchain4j.tracing.include-tool-result") boolean includeResult) {
25+
1826
this.tracer = tracer;
27+
this.includeArguments = includeArguments;
28+
this.includeResult = includeResult;
1929
}
2030

2131
@Override
2232
public String wrap(ToolExecutionRequest toolExecutionRequest, Object memoryId,
2333
BiFunction<ToolExecutionRequest, Object, String> fun) {
24-
Span span = tracer.spanBuilder("langchain4j.tools." + toolExecutionRequest.name()).startSpan();
34+
35+
// from https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-spans.md#execute-tool-span
36+
Span span = tracer.spanBuilder("langchain4j.tools." + toolExecutionRequest.name())
37+
.setSpanKind(SpanKind.INTERNAL)
38+
.setAttribute("gen_ai.operation.name", "execute_tool")
39+
.setAttribute("gen_ai.tool.call.id", toolExecutionRequest.id())
40+
.setAttribute("gen_ai.tool.name", toolExecutionRequest.name())
41+
.setAttribute("gen_ai.tool.type", "function")
42+
.startSpan();
43+
if (includeArguments) {
44+
span.setAttribute("gen_ai.tool.call.arguments", toolExecutionRequest.arguments());
45+
}
2546
try (Scope scope = span.makeCurrent()) {
2647
// TODO Handle async method here.
27-
return fun.apply(toolExecutionRequest, memoryId);
48+
var result = fun.apply(toolExecutionRequest, memoryId);
49+
if (includeResult) {
50+
span.setAttribute("gen_ai.tool.call.result", result);
51+
}
52+
return result;
2853
} catch (Throwable t) {
2954
span.recordException(t);
3055
throw t;

docs/modules/ROOT/pages/includes/quarkus-langchain4j-core.adoc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,48 @@ endif::add-copy-button-to-env-var[]
309309
|boolean
310310
|`false`
311311

312+
a| [[quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-arguments]] [.property-path]##link:#quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-arguments[`quarkus.langchain4j.tracing.include-tool-arguments`]##
313+
ifdef::add-copy-button-to-config-props[]
314+
config_property_copy_button:+++quarkus.langchain4j.tracing.include-tool-arguments+++[]
315+
endif::add-copy-button-to-config-props[]
316+
317+
318+
[.description]
319+
--
320+
If enabled, tool call arguments are included on the generated spans
321+
322+
323+
ifdef::add-copy-button-to-env-var[]
324+
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_ARGUMENTS+++[]
325+
endif::add-copy-button-to-env-var[]
326+
ifndef::add-copy-button-to-env-var[]
327+
Environment variable: `+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_ARGUMENTS+++`
328+
endif::add-copy-button-to-env-var[]
329+
--
330+
|boolean
331+
|`false`
332+
333+
a| [[quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-result]] [.property-path]##link:#quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-result[`quarkus.langchain4j.tracing.include-tool-result`]##
334+
ifdef::add-copy-button-to-config-props[]
335+
config_property_copy_button:+++quarkus.langchain4j.tracing.include-tool-result+++[]
336+
endif::add-copy-button-to-config-props[]
337+
338+
339+
[.description]
340+
--
341+
If enabled, tool call results are included on the generated spans
342+
343+
344+
ifdef::add-copy-button-to-env-var[]
345+
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_RESULT+++[]
346+
endif::add-copy-button-to-env-var[]
347+
ifndef::add-copy-button-to-env-var[]
348+
Environment variable: `+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_RESULT+++`
349+
endif::add-copy-button-to-env-var[]
350+
--
351+
|boolean
352+
|`false`
353+
312354
h|[[quarkus-langchain4j-core_section_quarkus-langchain4j]] [.section-name.section-level0]##link:#quarkus-langchain4j-core_section_quarkus-langchain4j[Default model config]##
313355
h|Type
314356
h|Default

docs/modules/ROOT/pages/includes/quarkus-langchain4j-core_quarkus.langchain4j.adoc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,48 @@ endif::add-copy-button-to-env-var[]
309309
|boolean
310310
|`false`
311311

312+
a| [[quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-arguments]] [.property-path]##link:#quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-arguments[`quarkus.langchain4j.tracing.include-tool-arguments`]##
313+
ifdef::add-copy-button-to-config-props[]
314+
config_property_copy_button:+++quarkus.langchain4j.tracing.include-tool-arguments+++[]
315+
endif::add-copy-button-to-config-props[]
316+
317+
318+
[.description]
319+
--
320+
If enabled, tool call arguments are included on the generated spans
321+
322+
323+
ifdef::add-copy-button-to-env-var[]
324+
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_ARGUMENTS+++[]
325+
endif::add-copy-button-to-env-var[]
326+
ifndef::add-copy-button-to-env-var[]
327+
Environment variable: `+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_ARGUMENTS+++`
328+
endif::add-copy-button-to-env-var[]
329+
--
330+
|boolean
331+
|`false`
332+
333+
a| [[quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-result]] [.property-path]##link:#quarkus-langchain4j-core_quarkus-langchain4j-tracing-include-tool-result[`quarkus.langchain4j.tracing.include-tool-result`]##
334+
ifdef::add-copy-button-to-config-props[]
335+
config_property_copy_button:+++quarkus.langchain4j.tracing.include-tool-result+++[]
336+
endif::add-copy-button-to-config-props[]
337+
338+
339+
[.description]
340+
--
341+
If enabled, tool call results are included on the generated spans
342+
343+
344+
ifdef::add-copy-button-to-env-var[]
345+
Environment variable: env_var_with_copy_button:+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_RESULT+++[]
346+
endif::add-copy-button-to-env-var[]
347+
ifndef::add-copy-button-to-env-var[]
348+
Environment variable: `+++QUARKUS_LANGCHAIN4J_TRACING_INCLUDE_TOOL_RESULT+++`
349+
endif::add-copy-button-to-env-var[]
350+
--
351+
|boolean
352+
|`false`
353+
312354
h|[[quarkus-langchain4j-core_section_quarkus-langchain4j]] [.section-name.section-level0]##link:#quarkus-langchain4j-core_section_quarkus-langchain4j[Default model config]##
313355
h|Type
314356
h|Default

0 commit comments

Comments
 (0)