Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@
<modules>
<module>worker-workflow</module>
<module>worker-workflow-container</module>
<module>worker-workflow-otel-extension</module>
<module>worker-workflow-restclients</module>
<module>worker-workflow-testing</module>
</modules>
Expand Down
5 changes: 5 additions & 0 deletions worker-workflow-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>standard-worker-container</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.github.cafdataprocessing.workers.workflow</groupId>
<artifactId>worker-workflow-otel-extension</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.github.cafdataprocessing.workers.workflow</groupId>
<artifactId>worker-workflow</artifactId>
Expand Down
11 changes: 10 additions & 1 deletion worker-workflow-container/worker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ then
fi

cd /maven
exec java $CAF_WORKER_JAVA_OPTS \

# Locate the OTel body-capture extension agent JAR. It is always loaded; when the OTel Java agent
# is absent the agent self-disables (the classloader guard in OtelRabbitBodyAgent skips
# instrumentation if io.opentelemetry.api.trace.Span is not resolvable).
OTEL_EXTENSION_JAR=$(ls /maven/worker-workflow-otel-extension-*.jar 2>/dev/null | head -1)
if [ -n "$OTEL_EXTENSION_JAR" ]; then
OTEL_EXTENSION_AGENT_ARG="-javaagent:${OTEL_EXTENSION_JAR}"
fi

exec java $OTEL_EXTENSION_AGENT_ARG $CAF_WORKER_JAVA_OPTS \
-Dpolyglot.engine.WarnInterpreterOnly=false \
-cp "*" \
com.github.workerframework.core.WorkerApplication \
Expand Down
126 changes: 126 additions & 0 deletions worker-workflow-otel-extension/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2017-2026 Open Text.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.cafdataprocessing.workers.workflow</groupId>
<artifactId>worker-workflow-aggregator</artifactId>
<version>8.6.0-SNAPSHOT</version>
</parent>

<artifactId>worker-workflow-otel-extension</artifactId>
<name>worker-workflow-otel-extension</name>
<description>
Java premain agent that captures the raw RabbitMQ message body on the OTel consumer span.
Instruments DefaultRabbitConsumer.getDeliverEvent(), which is called synchronously inside
the TracedDelegatingConsumer.handleDelivery() window while the consumer span is active.
</description>

<dependencies>
<!--
ByteBuddy is used to instrument DefaultRabbitConsumer.getDeliverEvent() at agent load
time. It is shaded into the fat JAR to avoid version conflicts with the app classpath.
-->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
</dependency>

<!--
OTel API: provided at runtime by the OTel Java agent (bootstrap classloader).
Not included in the fat JAR.
-->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<version>1.64.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!--
Build a self-contained fat JAR with ByteBuddy shaded to avoid classpath conflicts.
The Premain-Class manifest entry makes this usable as a -javaagent.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>net.bytebuddy</pattern>
<shadedPattern>
com.github.cafdataprocessing.workers.workflow.otel.shaded.bytebuddy
</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Premain-Class>
com.github.cafdataprocessing.workers.workflow.otel.OtelRabbitBodyAgent
</Premain-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>net.bytebuddy:byte-buddy</artifact>
<!--
The jdk5 artifact includes multi-release entries targeting
Java 24 (class file major version 68). The shade plugin
cannot process these, so exclude them — the base classes
contain all functionality needed here.
-->
<excludes>
<exclude>META-INF/versions/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2017-2026 Open Text.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.cafdataprocessing.workers.workflow.otel;

import java.lang.instrument.Instrumentation;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import static net.bytebuddy.matcher.ElementMatchers.named;

/**
* Java premain agent that captures the raw RabbitMQ message body onto the OTel consumer span.
* <p>
* The CAF worker framework uses raw {@code com.rabbitmq.client.Consumer} with an asynchronous
* event-queue pattern. The OTel Java agent wraps the registered consumer in a
* {@code TracedDelegatingConsumer}, which creates and ends the consumer span entirely within
* {@code handleDelivery()}. The framework's {@code RabbitConsumer.handleDelivery()} is
* {@code final} and just enqueues a {@code ConsumerDeliverEvent}; actual processing happens on
* a separate thread long after the span has ended.
* <p>
* The only synchronous call-site inside the live consumer span is
* {@code DefaultRabbitConsumer.getDeliverEvent(Envelope, byte[], Map)}, which receives the raw
* body bytes and is called from within {@code RabbitConsumer.handleDelivery()}, which is itself
* called from within {@code TracedDelegatingConsumer.handleDelivery()} while the span is active.
* <p>
* This agent instruments {@code getDeliverEvent} via ByteBuddy advice to read {@code body} and
* set {@code messaging.rabbitmq.message.body} on {@code Span.current()} before the method body
* executes (and before the span ends).
*/
public final class OtelRabbitBodyAgent
{
private OtelRabbitBodyAgent()
{
}

public static void premain(final String args, final Instrumentation inst)
{
new AgentBuilder.Default()
.type(named("com.github.workerframework.util.rabbitmq.DefaultRabbitConsumer"))
.transform((builder, typeDescription, classLoader, module, protectionDomain) -> {
// Guard: only instrument if the OTel API Span class is resolvable from the
// target classloader. If the OTel Java agent is absent, Span is not on the
// bootstrap classloader and the instrumented bytecode would fail class
// verification with NoClassDefFoundError.
final ClassLoader effectiveLoader = classLoader != null
? classLoader
: ClassLoader.getSystemClassLoader();
try {
Class.forName("io.opentelemetry.api.trace.Span", false, effectiveLoader);
} catch (final ClassNotFoundException ignored) {
return builder; // OTel API not available — skip instrumentation
}
return builder.visit(
Advice.to(RabbitBodyCaptureAdvice.class)
.on(named("getDeliverEvent"))
);
})
.installOn(inst);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2017-2026 Open Text.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.cafdataprocessing.workers.workflow.otel;

import io.opentelemetry.api.trace.Span;
import java.nio.charset.StandardCharsets;
import net.bytebuddy.asm.Advice;

/**
* ByteBuddy advice for {@code DefaultRabbitConsumer.getDeliverEvent(Envelope, byte[], Map)}.
* <p>
* This advice is inlined at instrumentation time — the advice class is not referenced at runtime.
* The inlined code runs at method-enter and uses {@code Span.current()}, which resolves to the
* active OTel consumer span because the method is called from within
* {@code TracedDelegatingConsumer.handleDelivery()} while the span scope is open.
* <p>
* The body is truncated to 4096 bytes before conversion to a UTF-8 string to keep attribute
* size reasonable for a POC. No masking or redaction is applied.
*/
public final class RabbitBodyCaptureAdvice
{
private RabbitBodyCaptureAdvice()
{
}

@Advice.OnMethodEnter
public static void onEnter(@Advice.Argument(1) final byte[] body)
{
if (body == null || body.length == 0) {
return;
}
final int len = Math.min(body.length, 4096);
final String payload = new String(body, 0, len, StandardCharsets.UTF_8);
Span.current().setAttribute("messaging.rabbitmq.message.body", payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public void checkHealth(final HealthMonitor healthMonitor)
@Override
public void processDocument(final Document document) throws DocumentWorkerTransientException
{
addMdcLoggingData(document.getTask());
final Task task = document.getTask();
addMdcLoggingData(task);

// Get the workflow specification passed in
final String customDataWorkflowName = document.getCustomData("workflowName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.github.cafapi.common.api.ConfigurationException;
import com.github.cafdataprocessing.workers.document.model.Document;
import com.github.cafdataprocessing.workers.document.model.Task;
import com.github.cafdataprocessing.workers.document.testing.CustomDataBuilder;
import com.github.cafdataprocessing.workers.document.testing.DocumentBuilder;
import com.github.cafdataprocessing.workers.workflow.restclients.settings_service.api.SettingsApi;
Expand All @@ -28,7 +29,6 @@

import com.github.workerframework.api.WorkerException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down