Skip to content

Commit 3f2ce8d

Browse files
tmckeonclaude
authored andcommitted
Add OpenTelemetry tracing instrumentation to API layer
Add OTel annotations and API dependencies to enable distributed tracing of CloudStack API requests. The @WithSpan annotation on handleRequest() tags each trace with the API command name (e.g. listVirtualMachines), making it possible to filter and analyze per-command latency. The OTel agent auto-instruments JDBC and HTTP calls, providing full request waterfalls with no additional code changes. All instrumentation is inert without the OTel Java agent attached: @WithSpan is ignored, Span.current() returns a no-op, and the opentelemetry-api calls return immediately with zero overhead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ddcc0c8 commit 3f2ce8d

4 files changed

Lines changed: 24 additions & 0 deletions

File tree

api/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
<artifactId>cloud-framework-direct-download</artifactId>
7272
<version>${project.version}</version>
7373
</dependency>
74+
<dependency>
75+
<groupId>io.opentelemetry.instrumentation</groupId>
76+
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
77+
<version>2.16.0</version>
78+
</dependency>
7479
</dependencies>
7580
<build>
7681
<plugins>

server/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@
197197
<version>4.22.1.0-SNAPSHOT</version>
198198
<scope>compile</scope>
199199
</dependency>
200+
<dependency>
201+
<groupId>io.opentelemetry.instrumentation</groupId>
202+
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
203+
<version>2.16.0</version>
204+
</dependency>
205+
<dependency>
206+
<groupId>io.opentelemetry</groupId>
207+
<artifactId>opentelemetry-api</artifactId>
208+
<version>1.51.0</version>
209+
</dependency>
200210
</dependencies>
201211
<build>
202212
<plugins>

server/src/main/java/com/cloud/api/ApiServer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
import javax.servlet.http.HttpServletResponse;
5959
import javax.servlet.http.HttpSession;
6060

61+
import io.opentelemetry.api.trace.Span;
62+
import io.opentelemetry.instrumentation.annotations.WithSpan;
63+
6164
import com.cloud.cluster.ManagementServerHostVO;
6265
import com.cloud.cluster.dao.ManagementServerHostDao;
6366
import com.cloud.user.Account;
@@ -619,6 +622,7 @@ public void checkCharacterInkParams(final Map params) {
619622
}
620623

621624
@Override
625+
@WithSpan("ApiServer.handleRequest")
622626
@SuppressWarnings("rawtypes")
623627
public String handleRequest(final Map params, final String responseType, final StringBuilder auditTrailSb) throws ServerApiException {
624628
checkCharacterInkParams(params);
@@ -628,6 +632,10 @@ public String handleRequest(final Map params, final String responseType, final S
628632

629633
try {
630634
command = (String[])params.get("command");
635+
if (command != null && command.length > 0) {
636+
Span.current().updateName("ApiServer.handleRequest " + command[0]);
637+
Span.current().setAttribute("api.command", command[0]);
638+
}
631639
if (command == null) {
632640
logger.error("invalid request, no command sent");
633641
if (logger.isTraceEnabled()) {

tools/docker/supervisord.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ command=/bin/bash -c "mvn -pl client jetty:run -Dsimulator -Dorg.eclipse.jetty.a
1212
directory=/root
1313
stdout_logfile=/dev/stdout
1414
stdout_logfile_maxbytes=0
15+
redirect_stderr=true
1516
user=root
1617

1718
[program:cloudstack-ui]

0 commit comments

Comments
 (0)