diff --git a/pom.xml b/pom.xml index 7abe982..1678c66 100644 --- a/pom.xml +++ b/pom.xml @@ -1026,6 +1026,7 @@ worker-workflow worker-workflow-container + worker-workflow-otel-extension worker-workflow-restclients worker-workflow-testing diff --git a/worker-workflow-container/pom.xml b/worker-workflow-container/pom.xml index a20c241..21ad04f 100644 --- a/worker-workflow-container/pom.xml +++ b/worker-workflow-container/pom.xml @@ -50,6 +50,11 @@ standard-worker-container pom + + com.github.cafdataprocessing.workers.workflow + worker-workflow-otel-extension + ${project.version} + com.github.cafdataprocessing.workers.workflow worker-workflow diff --git a/worker-workflow-container/worker.sh b/worker-workflow-container/worker.sh index 2935a48..0724468 100644 --- a/worker-workflow-container/worker.sh +++ b/worker-workflow-container/worker.sh @@ -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 \ diff --git a/worker-workflow-otel-extension/pom.xml b/worker-workflow-otel-extension/pom.xml new file mode 100644 index 0000000..be561ff --- /dev/null +++ b/worker-workflow-otel-extension/pom.xml @@ -0,0 +1,126 @@ + + + + 4.0.0 + + + com.github.cafdataprocessing.workers.workflow + worker-workflow-aggregator + 8.6.0-SNAPSHOT + + + worker-workflow-otel-extension + worker-workflow-otel-extension + + 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. + + + + + + net.bytebuddy + byte-buddy + + + + + io.opentelemetry + opentelemetry-api + 1.64.0 + provided + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.6.0 + + + package + + shade + + + false + false + + + net.bytebuddy + + com.github.cafdataprocessing.workers.workflow.otel.shaded.bytebuddy + + + + + + + + com.github.cafdataprocessing.workers.workflow.otel.OtelRabbitBodyAgent + + true + true + + + + + + net.bytebuddy:byte-buddy + + + META-INF/versions/** + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + diff --git a/worker-workflow-otel-extension/src/main/java/com/github/cafdataprocessing/workers/workflow/otel/OtelRabbitBodyAgent.java b/worker-workflow-otel-extension/src/main/java/com/github/cafdataprocessing/workers/workflow/otel/OtelRabbitBodyAgent.java new file mode 100644 index 0000000..e8b6278 --- /dev/null +++ b/worker-workflow-otel-extension/src/main/java/com/github/cafdataprocessing/workers/workflow/otel/OtelRabbitBodyAgent.java @@ -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. + *

+ * 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. + *

+ * 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. + *

+ * 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); + } +} diff --git a/worker-workflow-otel-extension/src/main/java/com/github/cafdataprocessing/workers/workflow/otel/RabbitBodyCaptureAdvice.java b/worker-workflow-otel-extension/src/main/java/com/github/cafdataprocessing/workers/workflow/otel/RabbitBodyCaptureAdvice.java new file mode 100644 index 0000000..1af4abc --- /dev/null +++ b/worker-workflow-otel-extension/src/main/java/com/github/cafdataprocessing/workers/workflow/otel/RabbitBodyCaptureAdvice.java @@ -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)}. + *

+ * 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. + *

+ * 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); + } +} diff --git a/worker-workflow/src/main/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorker.java b/worker-workflow/src/main/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorker.java index adf55fb..9d7ade7 100644 --- a/worker-workflow/src/main/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorker.java +++ b/worker-workflow/src/main/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorker.java @@ -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"); diff --git a/worker-workflow/src/test/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorkerTest.java b/worker-workflow/src/test/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorkerTest.java index 3a595f7..2c8feba 100644 --- a/worker-workflow/src/test/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorkerTest.java +++ b/worker-workflow/src/test/java/com/github/cafdataprocessing/workers/workflow/WorkflowWorkerTest.java @@ -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; @@ -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;