diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/groovy/SpringBatchTest.groovy b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/groovy/SpringBatchTest.groovy deleted file mode 100644 index d91d38ca6ba9..000000000000 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/groovy/SpringBatchTest.groovy +++ /dev/null @@ -1,346 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification -import org.springframework.batch.core.JobParameter -import org.springframework.context.ConfigurableApplicationContext -import org.springframework.context.annotation.AnnotationConfigApplicationContext -import org.springframework.context.support.ClassPathXmlApplicationContext - -import static io.opentelemetry.api.trace.SpanKind.INTERNAL -import static io.opentelemetry.api.trace.StatusCode.ERROR -import static java.util.Collections.emptyMap - -abstract class SpringBatchTest extends AgentInstrumentationSpecification { - - abstract runJob(String jobName, Map params = emptyMap()) - - def "should trace tasklet job+step"() { - when: - runJob("taskletJob") - - then: - assertTraces(1) { - trace(0, 3) { - span(0) { - name "BatchJob taskletJob" - kind INTERNAL - attributes { - "job.system" "spring_batch" - } - } - span(1) { - name "BatchJob taskletJob.step" - kind INTERNAL - childOf span(0) - attributes {} - } - span(2) { - name "BatchJob taskletJob.step.Tasklet" - kind INTERNAL - childOf span(1) - attributes {} - } - } - } - } - - def "should handle exception in tasklet job+step"() { - when: - runJob("taskletJob", ["fail": new JobParameter(1)]) - - then: - assertTraces(1) { - trace(0, 3) { - span(0) { - name "BatchJob taskletJob" - kind INTERNAL - attributes { - "job.system" "spring_batch" - } - } - span(1) { - name "BatchJob taskletJob.step" - kind INTERNAL - childOf span(0) - attributes {} - } - span(2) { - name "BatchJob taskletJob.step.Tasklet" - kind INTERNAL - childOf span(1) - status ERROR - errorEvent IllegalStateException, "fail" - attributes {} - } - } - } - } - - def "should trace chunked items job"() { - when: - runJob("itemsAndTaskletJob") - - then: - assertTraces(1) { - trace(0, 7) { - span(0) { - name "BatchJob itemsAndTaskletJob" - kind INTERNAL - attributes { - "job.system" "spring_batch" - } - } - span(1) { - name "BatchJob itemsAndTaskletJob.itemStep" - kind INTERNAL - childOf span(0) - attributes {} - } - span(2) { - name "BatchJob itemsAndTaskletJob.itemStep.Chunk" - kind INTERNAL - childOf span(1) - attributes {} - } - span(3) { - name "BatchJob itemsAndTaskletJob.itemStep.Chunk" - kind INTERNAL - childOf span(1) - attributes {} - } - span(4) { - name "BatchJob itemsAndTaskletJob.itemStep.Chunk" - kind INTERNAL - childOf span(1) - attributes {} - } - span(5) { - name "BatchJob itemsAndTaskletJob.taskletStep" - kind INTERNAL - childOf span(0) - attributes {} - } - span(6) { - name "BatchJob itemsAndTaskletJob.taskletStep.Tasklet" - kind INTERNAL - childOf span(5) - attributes {} - } - } - } - } - - def "should trace flow job"() { - when: - runJob("flowJob") - - then: - assertTraces(1) { - trace(0, 5) { - span(0) { - name "BatchJob flowJob" - kind INTERNAL - attributes { - "job.system" "spring_batch" - } - } - span(1) { - name "BatchJob flowJob.flowStep1" - kind INTERNAL - childOf span(0) - attributes {} - } - span(2) { - name "BatchJob flowJob.flowStep1.Tasklet" - kind INTERNAL - childOf span(1) - attributes {} - } - span(3) { - name "BatchJob flowJob.flowStep2" - kind INTERNAL - childOf span(0) - attributes {} - } - span(4) { - name "BatchJob flowJob.flowStep2.Tasklet" - kind INTERNAL - childOf span(3) - attributes {} - } - } - } - } - - def "should trace split flow job"() { - when: - runJob("splitJob") - - then: - assertTraces(1) { - trace(0, 5) { - span(0) { - name "BatchJob splitJob" - kind INTERNAL - attributes { - "job.system" "spring_batch" - } - } - span(1) { - name ~/BatchJob splitJob\.splitFlowStep[12]/ - kind INTERNAL - childOf span(0) - attributes {} - } - span(2) { - name ~/BatchJob splitJob\.splitFlowStep[12]\.Tasklet/ - kind INTERNAL - childOf span(1) - attributes {} - } - span(3) { - name ~/BatchJob splitJob\.splitFlowStep[12]/ - kind INTERNAL - childOf span(0) - attributes {} - } - span(4) { - name ~/BatchJob splitJob\.splitFlowStep[12]\.Tasklet/ - kind INTERNAL - childOf span(3) - attributes {} - } - } - } - } - - def "should trace job with decision"() { - when: - runJob("decisionJob") - - then: - assertTraces(1) { - trace(0, 5) { - span(0) { - name "BatchJob decisionJob" - kind INTERNAL - attributes { - "job.system" "spring_batch" - } - } - span(1) { - name "BatchJob decisionJob.decisionStepStart" - kind INTERNAL - childOf span(0) - attributes {} - } - span(2) { - name "BatchJob decisionJob.decisionStepStart.Tasklet" - kind INTERNAL - childOf span(1) - attributes {} - } - span(3) { - name "BatchJob decisionJob.decisionStepLeft" - kind INTERNAL - childOf span(0) - attributes {} - } - span(4) { - name "BatchJob decisionJob.decisionStepLeft.Tasklet" - kind INTERNAL - childOf span(3) - attributes {} - } - } - } - } - - def "should trace partitioned job"() { - when: - runJob("partitionedJob") - - then: - assertTraces(1) { - trace(0, 8) { - span(0) { - name "BatchJob partitionedJob" - kind INTERNAL - attributes { - "job.system" "spring_batch" - } - } - span(1) { - def stepName = hasPartitionManagerStep() ? "partitionManagerStep" : "partitionWorkerStep" - name "BatchJob partitionedJob.$stepName" - kind INTERNAL - childOf span(0) - attributes {} - } - span(2) { - name ~/BatchJob partitionedJob.partitionWorkerStep:partition[01]/ - kind INTERNAL - childOf span(1) - attributes {} - } - span(3) { - name ~/BatchJob partitionedJob.partitionWorkerStep:partition[01].Chunk/ - kind INTERNAL - childOf span(2) - attributes {} - } - span(4) { - name ~/BatchJob partitionedJob.partitionWorkerStep:partition[01].Chunk/ - kind INTERNAL - childOf span(2) - attributes {} - } - span(5) { - name ~/BatchJob partitionedJob.partitionWorkerStep:partition[01]/ - kind INTERNAL - childOf span(1) - attributes {} - } - span(6) { - name ~/BatchJob partitionedJob.partitionWorkerStep:partition[01].Chunk/ - kind INTERNAL - childOf span(5) - attributes {} - } - span(7) { - name ~/BatchJob partitionedJob.partitionWorkerStep:partition[01].Chunk/ - kind INTERNAL - childOf span(5) - attributes {} - } - } - } - } - - protected boolean hasPartitionManagerStep() { - true - } -} - -class JavaConfigBatchJobTest extends SpringBatchTest implements ApplicationConfigTrait { - @Override - ConfigurableApplicationContext createApplicationContext() { - new AnnotationConfigApplicationContext(SpringBatchApplication) - } -} - -class XmlConfigBatchJobTest extends SpringBatchTest implements ApplicationConfigTrait { - @Override - ConfigurableApplicationContext createApplicationContext() { - new ClassPathXmlApplicationContext("spring-batch.xml") - } -} - -class JsrConfigBatchJobTest extends SpringBatchTest implements JavaxBatchConfigTrait { - protected boolean hasPartitionManagerStep() { - false - } -} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/JavaConfigBatchJobTest.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/JavaConfigBatchJobTest.java new file mode 100644 index 000000000000..1401a587fa7c --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/JavaConfigBatchJobTest.java @@ -0,0 +1,23 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.basic; + +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner.ApplicationConfigRunner; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner.SpringBatchApplication; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +class JavaConfigBatchJobTest extends SpringBatchTest { + + @RegisterExtension + static final ApplicationConfigRunner runner = + new ApplicationConfigRunner( + () -> new AnnotationConfigApplicationContext(SpringBatchApplication.class)); + + public JavaConfigBatchJobTest() { + super(runner); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/JsrConfigBatchJobTest.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/JsrConfigBatchJobTest.java new file mode 100644 index 000000000000..c2cc24315545 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/JsrConfigBatchJobTest.java @@ -0,0 +1,22 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.basic; + +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner.JavaxBatchConfigRunner; +import org.junit.jupiter.api.extension.RegisterExtension; + +class JsrConfigBatchJobTest extends SpringBatchTest { + @Override + protected boolean hasPartitionManagerStep() { + return false; + } + + @RegisterExtension static final JavaxBatchConfigRunner runner = new JavaxBatchConfigRunner(); + + public JsrConfigBatchJobTest() { + super(runner); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/SpringBatchTest.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/SpringBatchTest.java new file mode 100644 index 000000000000..1465fda06559 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/SpringBatchTest.java @@ -0,0 +1,279 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.basic; + +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static java.util.Collections.singletonMap; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner.JobRunner; +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; +import io.opentelemetry.sdk.testing.assertj.TraceAssert; +import io.opentelemetry.sdk.trace.data.StatusData; +import java.util.function.Consumer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.batch.core.JobParameter; + +abstract class SpringBatchTest { + + private final JobRunner runner; + + @RegisterExtension + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + public SpringBatchTest(JobRunner runner) { + this.runner = runner; + } + + @Test + void shouldTraceTaskletJobStep() { + runner.runJob("taskletJob"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("BatchJob taskletJob") + .hasKind(SpanKind.INTERNAL) + .hasAttribute(AttributeKey.stringKey("job.system"), "spring_batch"), + span -> + span.hasName("BatchJob taskletJob.step") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + span -> + span.hasName("BatchJob taskletJob.step.Tasklet") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(1)))); + } + + @Test + void shouldHandleExceptionInTaskletJobStep() { + runner.runJob("taskletJob", singletonMap("fail", new JobParameter(1L))); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("BatchJob taskletJob") + .hasKind(SpanKind.INTERNAL) + .hasAttribute(AttributeKey.stringKey("job.system"), "spring_batch"), + span -> + span.hasName("BatchJob taskletJob.step") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + span -> + span.hasName("BatchJob taskletJob.step.Tasklet") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(1)) + .hasStatus(StatusData.error()) + .hasTotalAttributeCount(0) + .hasException(new IllegalStateException("fail")))); + } + + @Test + void shouldTraceChunkedItemsJob() { + runner.runJob("itemsAndTaskletJob"); + + testing.waitAndAssertTraces( + trace -> { + Consumer chunk = + span -> + span.hasName("BatchJob itemsAndTaskletJob.itemStep.Chunk") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(1)); + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("BatchJob itemsAndTaskletJob") + .hasKind(SpanKind.INTERNAL) + .hasAttribute(AttributeKey.stringKey("job.system"), "spring_batch"), + span -> + span.hasName("BatchJob itemsAndTaskletJob.itemStep") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + chunk, + chunk, + chunk, + span -> + span.hasName("BatchJob itemsAndTaskletJob.taskletStep") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + span -> + span.hasName("BatchJob itemsAndTaskletJob.taskletStep.Tasklet") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(5))); + }); + } + + @Test + void shouldTraceFlowJob() { + runner.runJob("flowJob"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("BatchJob flowJob") + .hasKind(SpanKind.INTERNAL) + .hasAttribute(AttributeKey.stringKey("job.system"), "spring_batch"), + span -> + span.hasName("BatchJob flowJob.flowStep1") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + span -> + span.hasName("BatchJob flowJob.flowStep1.Tasklet") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(1)), + span -> + span.hasName("BatchJob flowJob.flowStep2") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + span -> + span.hasName("BatchJob flowJob.flowStep2.Tasklet") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(3)))); + } + + @Test + void shouldTraceSplitFlowJob() { + runner.runJob("splitJob"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("BatchJob splitJob") + .hasKind(SpanKind.INTERNAL) + .hasAttribute(AttributeKey.stringKey("job.system"), "spring_batch"), + span -> + span.satisfies( + spanData -> + assertThat(spanData.getName()) + .matches("BatchJob splitJob.splitFlowStep[12]")) + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(0)), + span -> + span.satisfies( + spanData -> + assertThat(spanData.getName()) + .matches("BatchJob splitJob.splitFlowStep[12].Tasklet")) + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(1)), + span -> + span.satisfies( + spanData -> + assertThat(spanData.getName()) + .matches("BatchJob splitJob.splitFlowStep[12]")) + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + span -> + span.satisfies( + spanData -> + assertThat(spanData.getName()) + .matches("BatchJob splitJob.splitFlowStep[12].Tasklet")) + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(3)))); + } + + @Test + void shouldTraceJobWithDecision() { + runner.runJob("decisionJob"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("BatchJob decisionJob") + .hasKind(SpanKind.INTERNAL) + .hasAttribute(AttributeKey.stringKey("job.system"), "spring_batch"), + span -> + span.hasName("BatchJob decisionJob.decisionStepStart") + .hasKind(SpanKind.INTERNAL) + .hasTotalAttributeCount(0) + .hasParent(trace.getSpan(0)), + span -> + span.hasName("BatchJob decisionJob.decisionStepStart.Tasklet") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(1)), + span -> + span.hasName("BatchJob decisionJob.decisionStepLeft") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(0)), + span -> + span.hasName("BatchJob decisionJob.decisionStepLeft.Tasklet") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(3)))); + } + + @Test + void shouldTracePartitionedJob() { + runner.runJob("partitionedJob"); + + testing.waitAndAssertTraces( + trace -> { + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("BatchJob partitionedJob") + .hasKind(SpanKind.INTERNAL) + .hasAttribute(AttributeKey.stringKey("job.system"), "spring_batch"), + span -> + span.hasName( + hasPartitionManagerStep() + ? "BatchJob partitionedJob.partitionManagerStep" + : "BatchJob partitionedJob.partitionWorkerStep") + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(0)), + span -> + span.satisfies( + spanData -> + assertThat(spanData.getName()) + .matches( + "BatchJob partitionedJob.partitionWorkerStep:partition[01]")) + .hasKind(SpanKind.INTERNAL) + .hasParent(trace.getSpan(1)), + span -> partitionChunk(trace, span, 2), + span -> partitionChunk(trace, span, 2), + span -> + span.satisfies( + spanData -> + assertThat(spanData.getName()) + .matches( + "BatchJob partitionedJob.partitionWorkerStep:partition[01]")) + .hasParent(trace.getSpan(1)), + span -> partitionChunk(trace, span, 5), + span -> partitionChunk(trace, span, 5)); + }); + } + + private static void partitionChunk(TraceAssert trace, SpanDataAssert span, int index) { + span.satisfies( + spanData -> + assertThat(spanData.getName()) + .matches("BatchJob partitionedJob.partitionWorkerStep:partition[01].Chunk")) + .hasParent(trace.getSpan(index)); + } + + protected boolean hasPartitionManagerStep() { + return true; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/XmlConfigBatchJobTest.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/XmlConfigBatchJobTest.java new file mode 100644 index 000000000000..19a395594619 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/basic/XmlConfigBatchJobTest.java @@ -0,0 +1,21 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.basic; + +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner.ApplicationConfigRunner; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +class XmlConfigBatchJobTest extends SpringBatchTest { + + @RegisterExtension + static final ApplicationConfigRunner runner = + new ApplicationConfigRunner(() -> new ClassPathXmlApplicationContext("spring-batch.xml")); + + public XmlConfigBatchJobTest() { + super(runner); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventChunkListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventChunkListener.java new file mode 100644 index 000000000000..d0c421d968ae --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventChunkListener.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import io.opentelemetry.api.trace.Span; +import javax.batch.api.chunk.listener.ChunkListener; + +public class CustomEventChunkListener implements ChunkListener { + @Override + public void beforeChunk() { + Span.current().addEvent("chunk.before"); + } + + @Override + public void onError(Exception e) { + Span.current().addEvent("chunk.error"); + } + + @Override + public void afterChunk() { + Span.current().addEvent("chunk.after"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemProcessListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemProcessListener.java new file mode 100644 index 000000000000..95a37221ae21 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemProcessListener.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import io.opentelemetry.api.trace.Span; +import javax.batch.api.chunk.listener.ItemProcessListener; + +class CustomEventItemProcessListener implements ItemProcessListener { + @Override + public void beforeProcess(Object o) { + Span.current().addEvent("item.process.before"); + } + + @Override + public void afterProcess(Object o, Object o1) { + Span.current().addEvent("item.process.after"); + } + + @Override + public void onProcessError(Object o, Exception e) { + Span.current().addEvent("item.process.error"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemReadListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemReadListener.java new file mode 100644 index 000000000000..72d64e28f308 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemReadListener.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import io.opentelemetry.api.trace.Span; +import javax.batch.api.chunk.listener.ItemReadListener; + +class CustomEventItemReadListener implements ItemReadListener { + @Override + public void beforeRead() { + Span.current().addEvent("item.read.before"); + } + + @Override + public void afterRead(Object o) { + Span.current().addEvent("item.read.after"); + } + + @Override + public void onReadError(Exception e) { + Span.current().addEvent("item.read.error"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemWriteListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemWriteListener.java new file mode 100644 index 000000000000..063bb271cbfe --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventItemWriteListener.java @@ -0,0 +1,27 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import io.opentelemetry.api.trace.Span; +import java.util.List; +import javax.batch.api.chunk.listener.ItemWriteListener; + +class CustomEventItemWriteListener implements ItemWriteListener { + @Override + public void beforeWrite(List list) { + Span.current().addEvent("item.write.before"); + } + + @Override + public void afterWrite(List list) { + Span.current().addEvent("item.write.after"); + } + + @Override + public void onWriteError(List list, Exception e) { + Span.current().addEvent("item.write.error"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventJobListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventJobListener.java new file mode 100644 index 000000000000..0999cd12864c --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventJobListener.java @@ -0,0 +1,21 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import io.opentelemetry.api.trace.Span; +import javax.batch.api.listener.JobListener; + +class CustomEventJobListener implements JobListener { + @Override + public void beforeJob() { + Span.current().addEvent("job.before"); + } + + @Override + public void afterJob() { + Span.current().addEvent("job.after"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventStepListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventStepListener.java new file mode 100644 index 000000000000..e14081ada7aa --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/CustomEventStepListener.java @@ -0,0 +1,21 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import io.opentelemetry.api.trace.Span; +import javax.batch.api.listener.StepListener; + +class CustomEventStepListener implements StepListener { + @Override + public void beforeStep() { + Span.current().addEvent("step.before"); + } + + @Override + public void afterStep() { + Span.current().addEvent("step.after"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/SingleItemReader.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/SingleItemReader.java new file mode 100644 index 000000000000..d7f334cd77d9 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/SingleItemReader.java @@ -0,0 +1,34 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import java.io.Serializable; +import java.util.concurrent.atomic.AtomicReference; +import javax.batch.api.chunk.ItemReader; + +class SingleItemReader implements ItemReader { + @Override + public void open(Serializable serializable) {} + + @Override + public void close() {} + + @Override + public Object readItem() { + return item.getAndSet(null); + } + + @Override + public Serializable checkpointInfo() { + return null; + } + + public final AtomicReference getItem() { + return item; + } + + private final AtomicReference item = new AtomicReference("42"); +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestBatchlet.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestBatchlet.java new file mode 100644 index 000000000000..813b0bbcf8d0 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestBatchlet.java @@ -0,0 +1,36 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import javax.batch.api.BatchProperty; +import javax.batch.api.Batchlet; +import javax.inject.Inject; + +class TestBatchlet implements Batchlet { + @Inject + @BatchProperty(name = "fail") + private String fail; + + @Override + public String process() { + if (fail != null && Integer.valueOf(fail) == 1) { + throw new IllegalStateException("fail"); + } + + return "FINISHED"; + } + + @Override + public void stop() {} + + public String getFail() { + return fail; + } + + public void setFail(String fail) { + this.fail = fail; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestDecider.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestDecider.java new file mode 100644 index 000000000000..71ff22671f6b --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestDecider.java @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import javax.batch.api.Decider; +import javax.batch.runtime.StepExecution; + +class TestDecider implements Decider { + @Override + public String decide(StepExecution[] stepExecutions) { + return "LEFT"; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemProcessor.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemProcessor.java new file mode 100644 index 000000000000..3814b823ff02 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemProcessor.java @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import javax.batch.api.chunk.ItemProcessor; +import org.codehaus.groovy.runtime.DefaultGroovyMethods; + +class TestItemProcessor implements ItemProcessor { + @Override + public Object processItem(Object item) { + return Integer.parseInt(DefaultGroovyMethods.asType(item, String.class)); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemReader.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemReader.java new file mode 100644 index 000000000000..41fb17e5e298 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemReader.java @@ -0,0 +1,44 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import java.io.Serializable; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import javax.batch.api.chunk.ItemReader; + +class TestItemReader implements ItemReader { + + private final List items = + IntStream.range(0, 13).mapToObj(String::valueOf).collect(Collectors.toList()); + private Iterator itemsIt; + + @Override + public void open(Serializable serializable) { + itemsIt = items.iterator(); + } + + @Override + public void close() { + itemsIt = null; + } + + @Override + public Object readItem() { + if (itemsIt == null) { + return null; + } + + return itemsIt.hasNext() ? itemsIt.next() : null; + } + + @Override + public Serializable checkpointInfo() { + return null; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemWriter.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemWriter.java new file mode 100644 index 000000000000..c14aeb871924 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestItemWriter.java @@ -0,0 +1,34 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import javax.batch.api.chunk.ItemWriter; +import org.codehaus.groovy.runtime.DefaultGroovyMethods; + +class TestItemWriter implements ItemWriter { + private final List items = new ArrayList<>(); + + @Override + public void open(Serializable checkpoint) {} + + @Override + public void close() {} + + @Override + public void writeItems(List items) { + for (Object item : items) { + this.items.add(DefaultGroovyMethods.asType(item, Integer.class)); + } + } + + @Override + public Serializable checkpointInfo() { + return null; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestPartitionedItemReader.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestPartitionedItemReader.java new file mode 100644 index 000000000000..d3b0820e2f48 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/jsr/TestPartitionedItemReader.java @@ -0,0 +1,80 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.jsr; + +import java.io.Serializable; +import javax.batch.api.BatchProperty; +import javax.batch.api.chunk.ItemReader; +import javax.inject.Inject; + +class TestPartitionedItemReader implements ItemReader { + + @Inject + @BatchProperty(name = "start") + private String startStr; + + @Inject + @BatchProperty(name = "end") + private String endStr; + + private int start; + private int end; + + @Override + public void open(Serializable checkpoint) { + start = Integer.parseInt(startStr); + end = Integer.parseInt(endStr); + } + + @Override + public void close() {} + + @Override + public Object readItem() { + if (start >= end) { + return null; + } + + return String.valueOf(start++); + } + + @Override + public Serializable checkpointInfo() { + return null; + } + + public String getStartStr() { + return startStr; + } + + public void setStartStr(String startStr) { + this.startStr = startStr; + } + + public String getEndStr() { + return endStr; + } + + public void setEndStr(String endStr) { + this.endStr = endStr; + } + + public int getStart() { + return start; + } + + public void setStart(int start) { + this.start = start; + } + + public int getEnd() { + return end; + } + + public void setEnd(int end) { + this.end = end; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/ApplicationConfigRunner.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/ApplicationConfigRunner.java new file mode 100644 index 000000000000..79ecbc0e428f --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/ApplicationConfigRunner.java @@ -0,0 +1,62 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner; + +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Supplier; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParameter; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.context.ConfigurableApplicationContext; + +public class ApplicationConfigRunner implements BeforeEachCallback, AfterEachCallback, JobRunner { + private final Supplier applicationContextFactory; + private final BiConsumer jobPostProcessor; + static JobLauncher jobLauncher; + private ConfigurableApplicationContext applicationContext; + + public ApplicationConfigRunner( + Supplier applicationContextFactory) { + this(applicationContextFactory, (jobName, job) -> {}); + } + + public ApplicationConfigRunner( + Supplier applicationContextFactory, + BiConsumer jobPostProcessor) { + this.applicationContextFactory = applicationContextFactory; + this.jobPostProcessor = jobPostProcessor; + } + + @Override + public void beforeEach(ExtensionContext context) { + applicationContext = applicationContextFactory.get(); + applicationContext.start(); + + jobLauncher = applicationContext.getBean(JobLauncher.class); + } + + @Override + public void afterEach(ExtensionContext context) { + applicationContext.stop(); + applicationContext.close(); + } + + @Override + public void runJob(String jobName, Map params) { + Job job = applicationContext.getBean(jobName, Job.class); + jobPostProcessor.accept(jobName, job); + try { + jobLauncher.run(job, new JobParameters(params)); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/JavaxBatchConfigRunner.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/JavaxBatchConfigRunner.java new file mode 100644 index 000000000000..e92886d996ff --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/JavaxBatchConfigRunner.java @@ -0,0 +1,34 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner; + +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; +import javax.batch.operations.JobOperator; +import javax.batch.runtime.BatchRuntime; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.springframework.batch.core.JobParameter; + +public class JavaxBatchConfigRunner implements BeforeEachCallback, JobRunner { + static JobOperator jobOperator; + static AtomicInteger counter = new AtomicInteger(); + + @Override + public void beforeEach(ExtensionContext context) { + jobOperator = BatchRuntime.getJobOperator(); + } + + @Override + public void runJob(String jobName, Map params) { + Properties jobParams = new Properties(); + params.forEach((k, v) -> jobParams.setProperty(k, v.getValue().toString())); + // each job instance with the same name needs to be unique + jobParams.setProperty("uniqueJobIdCounter", String.valueOf(counter.getAndIncrement())); + jobOperator.start(jobName, jobParams); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/JobRunner.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/JobRunner.java new file mode 100644 index 000000000000..7368a7491083 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/JobRunner.java @@ -0,0 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner; + +import static java.util.Collections.emptyMap; + +import java.util.Map; +import org.springframework.batch.core.JobParameter; + +public interface JobRunner { + void runJob(String jobName, Map params); + + default void runJob(String jobName) { + runJob(jobName, emptyMap()); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/SpringBatchApplication.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/SpringBatchApplication.java new file mode 100644 index 000000000000..a1d790e81b5b --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/runner/SpringBatchApplication.java @@ -0,0 +1,274 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.runner; + +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.CustomEventChunkListener; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.CustomEventItemProcessListener; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.CustomEventItemReadListener; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.CustomEventItemWriteListener; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.CustomEventJobListener; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.CustomEventStepListener; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.SingleItemReader; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestDecider; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestItemProcessor; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestItemReader; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestItemWriter; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestPartitionedItemReader; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestPartitioner; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestSyncItemReader; +import io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch.TestTasklet; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.job.builder.FlowBuilder; +import org.springframework.batch.core.job.flow.Flow; +import org.springframework.batch.core.job.flow.support.SimpleFlow; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.partition.support.Partitioner; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.AsyncTaskExecutor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration +@EnableBatchProcessing +public class SpringBatchApplication { + + @Autowired JobBuilderFactory jobs; + @Autowired StepBuilderFactory steps; + @Autowired JobRepository jobRepository; + + @Bean + AsyncTaskExecutor asyncTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(10); + executor.setMaxPoolSize(10); + return executor; + } + + @Bean + JobLauncher jobLauncher() { + SimpleJobLauncher launcher = new SimpleJobLauncher(); + launcher.setJobRepository(jobRepository); + launcher.setTaskExecutor(asyncTaskExecutor()); + return launcher; + } + + // common + @Bean + ItemReader itemReader() { + return new TestItemReader(); + } + + @Bean + ItemProcessor itemProcessor() { + return new TestItemProcessor(); + } + + @Bean + ItemWriter itemWriter() { + return new TestItemWriter(); + } + + // simple tasklet job + @Bean + Job taskletJob() { + return jobs.get("taskletJob").start(step()).build(); + } + + @Bean + Step step() { + return steps.get("step").tasklet(new TestTasklet()).build(); + } + + // 2-step tasklet + chunked items job + @Bean + Job itemsAndTaskletJob() { + return jobs.get("itemsAndTaskletJob").start(itemStep()).next(taskletStep()).build(); + } + + @Bean + Step taskletStep() { + return steps.get("taskletStep").tasklet(new TestTasklet()).build(); + } + + @Bean + Step itemStep() { + return steps + .get("itemStep") + .chunk(5) + .reader(itemReader()) + .processor(itemProcessor()) + .writer(itemWriter()) + .build(); + } + + // parallel items job + @Bean + Job parallelItemsJob() { + return jobs.get("parallelItemsJob").start(parallelItemsStep()).build(); + } + + @Bean + Step parallelItemsStep() { + return steps + .get("parallelItemsStep") + .chunk(2) + .reader(new TestSyncItemReader(5)) + .processor(itemProcessor()) + .writer(itemWriter()) + .taskExecutor(asyncTaskExecutor()) + .throttleLimit(2) + .build(); + } + + // job using a flow + @Bean + Job flowJob() { + return jobs.get("flowJob").start(flow()).build().build(); + } + + @Bean + Flow flow() { + return new FlowBuilder("flow").start(flowStep1()).on("*").to(flowStep2()).build(); + } + + @Bean + Step flowStep1() { + return steps.get("flowStep1").tasklet(new TestTasklet()).build(); + } + + @Bean + Step flowStep2() { + return steps.get("flowStep2").tasklet(new TestTasklet()).build(); + } + + // split job + @Bean + Job splitJob() { + return jobs.get("splitJob") + .start(splitFlowStep1()) + .split(asyncTaskExecutor()) + .add(splitFlow2()) + .build() + .build(); + } + + @Bean + Step splitFlowStep1() { + return steps.get("splitFlowStep1").tasklet(new TestTasklet()).build(); + } + + @Bean + Flow splitFlow2() { + return new FlowBuilder("splitFlow2").start(splitFlowStep2()).build(); + } + + @Bean + Step splitFlowStep2() { + return steps.get("splitFlowStep2").tasklet(new TestTasklet()).build(); + } + + // job with decisions + @Bean + Job decisionJob() { + return jobs.get("decisionJob") + .start(decisionStepStart()) + .next(new TestDecider()) + .on("LEFT") + .to(decisionStepLeft()) + .on("RIGHT") + .to(decisionStepRight()) + .end() + .build(); + } + + @Bean + Step decisionStepStart() { + return steps.get("decisionStepStart").tasklet(new TestTasklet()).build(); + } + + @Bean + Step decisionStepLeft() { + return steps.get("decisionStepLeft").tasklet(new TestTasklet()).build(); + } + + @Bean + Step decisionStepRight() { + return steps.get("decisionStepRight").tasklet(new TestTasklet()).build(); + } + + // partitioned job + @Bean + Job partitionedJob() { + return jobs.get("partitionedJob").start(partitionManagerStep()).build(); + } + + @Bean + Step partitionManagerStep() { + return steps + .get("partitionManagerStep") + .partitioner("partitionWorkerStep", partitioner()) + .step(partitionWorkerStep()) + .gridSize(2) + .taskExecutor(asyncTaskExecutor()) + .build(); + } + + @Bean + Partitioner partitioner() { + return new TestPartitioner(); + } + + @Bean + Step partitionWorkerStep() { + return steps + .get("partitionWorkerStep") + .chunk(5) + .reader(partitionedItemReader()) + .processor(itemProcessor()) + .writer(itemWriter()) + .build(); + } + + @Bean + ItemReader partitionedItemReader() { + return new TestPartitionedItemReader(); + } + + // custom span events items job + @Bean + Job customSpanEventsItemsJob() { + return jobs.get("customSpanEventsItemsJob") + .start(customSpanEventsItemStep()) + .listener(new CustomEventJobListener()) + .build(); + } + + @Bean + Step customSpanEventsItemStep() { + return steps + .get("customSpanEventsItemStep") + .chunk(5) + .reader(new SingleItemReader()) + .processor(itemProcessor()) + .writer(itemWriter()) + .listener(new CustomEventStepListener()) + .listener(new CustomEventChunkListener()) + .listener(new CustomEventItemReadListener()) + .listener(new CustomEventItemProcessListener()) + .listener(new CustomEventItemWriteListener()) + .build(); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventChunkListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventChunkListener.java new file mode 100644 index 000000000000..1b5dd8a15efa --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventChunkListener.java @@ -0,0 +1,27 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import io.opentelemetry.api.trace.Span; +import org.springframework.batch.core.ChunkListener; +import org.springframework.batch.core.scope.context.ChunkContext; + +public class CustomEventChunkListener implements ChunkListener { + @Override + public void beforeChunk(ChunkContext context) { + Span.current().addEvent("chunk.before"); + } + + @Override + public void afterChunk(ChunkContext context) { + Span.current().addEvent("chunk.after"); + } + + @Override + public void afterChunkError(ChunkContext context) { + Span.current().addEvent("chunk.error"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemProcessListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemProcessListener.java new file mode 100644 index 000000000000..fa37fe866810 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemProcessListener.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import io.opentelemetry.api.trace.Span; +import org.springframework.batch.core.ItemProcessListener; + +public class CustomEventItemProcessListener implements ItemProcessListener { + @Override + public void beforeProcess(String item) { + Span.current().addEvent("item.process.before"); + } + + @Override + public void afterProcess(String item, Integer result) { + Span.current().addEvent("item.process.after"); + } + + @Override + public void onProcessError(String item, Exception e) { + Span.current().addEvent("item.process.error"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemReadListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemReadListener.java new file mode 100644 index 000000000000..9486112372d2 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemReadListener.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import io.opentelemetry.api.trace.Span; +import org.springframework.batch.core.ItemReadListener; + +public class CustomEventItemReadListener implements ItemReadListener { + @Override + public void beforeRead() { + Span.current().addEvent("item.read.before"); + } + + @Override + public void afterRead(String item) { + Span.current().addEvent("item.read.after"); + } + + @Override + public void onReadError(Exception ex) { + Span.current().addEvent("item.read.error"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemWriteListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemWriteListener.java new file mode 100644 index 000000000000..7fee43ce01a8 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventItemWriteListener.java @@ -0,0 +1,27 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import io.opentelemetry.api.trace.Span; +import java.util.List; +import org.springframework.batch.core.ItemWriteListener; + +public class CustomEventItemWriteListener implements ItemWriteListener { + @Override + public void beforeWrite(List items) { + Span.current().addEvent("item.write.before"); + } + + @Override + public void afterWrite(List items) { + Span.current().addEvent("item.write.after"); + } + + @Override + public void onWriteError(Exception exception, List items) { + Span.current().addEvent("item.write.error"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventJobListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventJobListener.java new file mode 100644 index 000000000000..2ccd465b6e6a --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventJobListener.java @@ -0,0 +1,22 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import io.opentelemetry.api.trace.Span; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobExecutionListener; + +public class CustomEventJobListener implements JobExecutionListener { + @Override + public void beforeJob(JobExecution jobExecution) { + Span.current().addEvent("job.before"); + } + + @Override + public void afterJob(JobExecution jobExecution) { + Span.current().addEvent("job.after"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventStepListener.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventStepListener.java new file mode 100644 index 000000000000..5fc343556673 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/CustomEventStepListener.java @@ -0,0 +1,24 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import io.opentelemetry.api.trace.Span; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; + +public class CustomEventStepListener implements StepExecutionListener { + @Override + public void beforeStep(StepExecution stepExecution) { + Span.current().addEvent("step.before"); + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + Span.current().addEvent("step.after"); + return null; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/SingleItemReader.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/SingleItemReader.java new file mode 100644 index 000000000000..6e4cd14b7ff1 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/SingleItemReader.java @@ -0,0 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import java.util.concurrent.atomic.AtomicReference; +import org.springframework.batch.item.ItemReader; + +public class SingleItemReader implements ItemReader { + final AtomicReference item = new AtomicReference<>("42"); + + @Override + public String read() { + return item.getAndSet(null); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestDecider.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestDecider.java new file mode 100644 index 000000000000..4911d3b794e0 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestDecider.java @@ -0,0 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.job.flow.FlowExecutionStatus; +import org.springframework.batch.core.job.flow.JobExecutionDecider; + +public class TestDecider implements JobExecutionDecider { + @Override + public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) { + return new FlowExecutionStatus("LEFT"); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemProcessor.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemProcessor.java new file mode 100644 index 000000000000..c4aa9cbf9b13 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemProcessor.java @@ -0,0 +1,15 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import org.springframework.batch.item.ItemProcessor; + +public class TestItemProcessor implements ItemProcessor { + @Override + public Integer process(String item) { + return Integer.parseInt(item); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemReader.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemReader.java new file mode 100644 index 000000000000..c66a1ff3243c --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemReader.java @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.springframework.batch.item.support.ListItemReader; + +public class TestItemReader extends ListItemReader { + public TestItemReader() { + super(IntStream.range(0, 13).mapToObj(String::valueOf).collect(Collectors.toList())); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemWriter.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemWriter.java new file mode 100644 index 000000000000..d660c8079ea9 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestItemWriter.java @@ -0,0 +1,21 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import static java.util.Collections.synchronizedList; + +import java.util.ArrayList; +import java.util.List; +import org.springframework.batch.item.ItemWriter; + +public class TestItemWriter implements ItemWriter { + final List items = synchronizedList(new ArrayList<>()); + + @Override + public void write(List items) { + this.items.addAll(items); + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestPartitionedItemReader.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestPartitionedItemReader.java new file mode 100644 index 000000000000..18b1181a83e6 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestPartitionedItemReader.java @@ -0,0 +1,37 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemStream; + +public class TestPartitionedItemReader implements ItemReader, ItemStream { + ThreadLocal start = new ThreadLocal<>(); + ThreadLocal end = new ThreadLocal<>(); + + @Override + public String read() { + if (start.get() >= end.get()) { + return null; + } + Integer value = start.get(); + start.set(value + 1); + return String.valueOf(value); + } + + @Override + public void open(ExecutionContext executionContext) { + start.set(executionContext.getInt("start")); + end.set(executionContext.getInt("end")); + } + + @Override + public void update(ExecutionContext executionContext) {} + + @Override + public void close() {} +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestPartitioner.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestPartitioner.java new file mode 100644 index 000000000000..9f46037c9840 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestPartitioner.java @@ -0,0 +1,22 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.Map; +import org.springframework.batch.core.partition.support.Partitioner; +import org.springframework.batch.item.ExecutionContext; + +public class TestPartitioner implements Partitioner { + @Override + public Map partition(int gridSize) { + Map map = new HashMap<>(); + map.put("partition0", new ExecutionContext(ImmutableMap.of("start", 0, "end", 8))); + map.put("partition1", new ExecutionContext(ImmutableMap.of("start", 8, "end", 13))); + return map; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestSyncItemReader.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestSyncItemReader.java new file mode 100644 index 000000000000..62b673ef2ea9 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestSyncItemReader.java @@ -0,0 +1,28 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import java.util.Iterator; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.springframework.batch.item.ItemReader; + +public class TestSyncItemReader implements ItemReader { + private final Iterator items; + + public TestSyncItemReader(int max) { + items = + IntStream.range(0, max).mapToObj(String::valueOf).collect(Collectors.toList()).iterator(); + } + + @Override + public synchronized String read() { + if (items.hasNext()) { + return items.next(); + } + return null; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestTasklet.java b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestTasklet.java new file mode 100644 index 000000000000..e5cbe65f47f6 --- /dev/null +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/batch/v3_0/springbatch/TestTasklet.java @@ -0,0 +1,23 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.spring.batch.v3_0.springbatch; + +import java.util.Objects; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +public class TestTasklet implements Tasklet { + @Override + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) { + if (Objects.equals( + chunkContext.getStepContext().getStepExecution().getJobParameters().getLong("fail"), 1L)) { + throw new IllegalStateException("fail"); + } + return RepeatStatus.FINISHED; + } +} diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/customSpanEventsItemsJob.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/customSpanEventsItemsJob.xml index feeff7e20a6d..0e3df085f3ed 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/customSpanEventsItemsJob.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/customSpanEventsItemsJob.xml @@ -1,20 +1,23 @@ - + - + - - - - - + + + + + - - - + + + diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/decisionJob.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/decisionJob.xml index 3f2a98def83d..d6d972c159a8 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/decisionJob.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/decisionJob.xml @@ -1,16 +1,19 @@ - + - + - + - + - + - \ No newline at end of file + diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/flowJob.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/flowJob.xml index a43b024d91d9..c6d647c9062e 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/flowJob.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/flowJob.xml @@ -1,11 +1,14 @@ - + - + - + diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/itemsAndTaskletJob.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/itemsAndTaskletJob.xml index 7d2a890e8442..d2fc4565a4d4 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/itemsAndTaskletJob.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/itemsAndTaskletJob.xml @@ -1,14 +1,17 @@ - + - - - + + + - + - \ No newline at end of file + diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/partitionedJob.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/partitionedJob.xml index 330f99c2c68e..42b4f709ec04 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/partitionedJob.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/partitionedJob.xml @@ -1,15 +1,18 @@ - + - + - - + + diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/splitJob.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/splitJob.xml index a9f8356f46d5..8a274af8949c 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/splitJob.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/splitJob.xml @@ -1,14 +1,17 @@ - + - + - + diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/taskletJob.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/taskletJob.xml index fe2f62430559..13043c08cdd3 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/taskletJob.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/META-INF/batch-jobs/taskletJob.xml @@ -1,10 +1,13 @@ - + - + - \ No newline at end of file + diff --git a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/spring-batch.xml b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/spring-batch.xml index e6b8ca3c78bb..0e9cf65992d7 100644 --- a/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/spring-batch.xml +++ b/instrumentation/spring/spring-batch-3.0/javaagent/src/test/resources/spring-batch.xml @@ -90,31 +90,31 @@ writer="itemWriter"/> - - + + - + - + - + - + - + - + @@ -122,17 +122,17 @@ - + - - + + - - - - - \ No newline at end of file + + + + +