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