|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 11 | + |
| 12 | +import io.opentelemetry.api.common.AttributeKey; |
| 13 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 14 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 15 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.component.IntervalTask; |
| 16 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.component.OneTimeTask; |
| 17 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.component.TaskWithError; |
| 18 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.component.TriggerTask; |
| 19 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.config.EnhancedClassTaskConfig; |
| 20 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.config.IntervalTaskConfig; |
| 21 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.config.LambdaTaskConfig; |
| 22 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.config.OneTimeTaskConfig; |
| 23 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.config.TaskWithErrorConfig; |
| 24 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.config.TriggerTaskConfig; |
| 25 | +import io.opentelemetry.javaagent.instrumentation.spring.scheduling.v3_1.spring.service.LambdaTaskConfigurer; |
| 26 | +import io.opentelemetry.sdk.trace.data.StatusData; |
| 27 | +import io.opentelemetry.semconv.SemanticAttributes; |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 32 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 33 | + |
| 34 | +class SpringSchedulingTest { |
| 35 | + |
| 36 | + @RegisterExtension |
| 37 | + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 38 | + |
| 39 | + @Test |
| 40 | + void scheduleOneTimeTest() throws InterruptedException { |
| 41 | + try (AnnotationConfigApplicationContext context = |
| 42 | + new AnnotationConfigApplicationContext(OneTimeTaskConfig.class)) { |
| 43 | + OneTimeTask task = context.getBean(OneTimeTask.class); |
| 44 | + task.blockUntilExecute(); |
| 45 | + |
| 46 | + assertThat(task).isNotNull(); |
| 47 | + assertThat(testing.waitForTraces(0)).isEmpty(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void scheduleCronExpressionTest() throws InterruptedException { |
| 53 | + try (AnnotationConfigApplicationContext context = |
| 54 | + new AnnotationConfigApplicationContext(TriggerTaskConfig.class)) { |
| 55 | + TriggerTask task = context.getBean(TriggerTask.class); |
| 56 | + task.blockUntilExecute(); |
| 57 | + |
| 58 | + assertThat(task).isNotNull(); |
| 59 | + testing.waitAndAssertTraces( |
| 60 | + trace -> |
| 61 | + trace.hasSpansSatisfyingExactly( |
| 62 | + span -> |
| 63 | + span.hasName("TriggerTask.run") |
| 64 | + .hasNoParent() |
| 65 | + .hasAttributesSatisfyingExactly( |
| 66 | + equalTo(AttributeKey.stringKey("job.system"), "spring_scheduling"), |
| 67 | + equalTo( |
| 68 | + AttributeKey.stringKey("code.namespace"), |
| 69 | + TriggerTask.class.getName()), |
| 70 | + equalTo(AttributeKey.stringKey("code.function"), "run")))); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void scheduleIntervalTest() throws InterruptedException { |
| 76 | + try (AnnotationConfigApplicationContext context = |
| 77 | + new AnnotationConfigApplicationContext(IntervalTaskConfig.class)) { |
| 78 | + IntervalTask task = context.getBean(IntervalTask.class); |
| 79 | + task.blockUntilExecute(); |
| 80 | + |
| 81 | + assertThat(task).isNotNull(); |
| 82 | + testing.waitAndAssertTraces( |
| 83 | + trace -> |
| 84 | + trace.hasSpansSatisfyingExactly( |
| 85 | + span -> |
| 86 | + span.hasName("IntervalTask.run") |
| 87 | + .hasNoParent() |
| 88 | + .hasAttributesSatisfyingExactly( |
| 89 | + equalTo(AttributeKey.stringKey("job.system"), "spring_scheduling"), |
| 90 | + equalTo( |
| 91 | + AttributeKey.stringKey("code.namespace"), |
| 92 | + IntervalTask.class.getName()), |
| 93 | + equalTo(AttributeKey.stringKey("code.function"), "run")))); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void scheduleLambdaTest() throws InterruptedException { |
| 99 | + try (AnnotationConfigApplicationContext context = |
| 100 | + new AnnotationConfigApplicationContext(LambdaTaskConfig.class)) { |
| 101 | + LambdaTaskConfigurer configurer = context.getBean(LambdaTaskConfigurer.class); |
| 102 | + configurer.singleUseLatch.await(2000, TimeUnit.MILLISECONDS); |
| 103 | + |
| 104 | + assertThat(configurer).isNotNull(); |
| 105 | + testing.waitAndAssertTraces( |
| 106 | + trace -> |
| 107 | + trace.hasSpansSatisfyingExactly( |
| 108 | + span -> |
| 109 | + span.hasName("LambdaTaskConfigurer$$Lambda.run") |
| 110 | + .hasNoParent() |
| 111 | + .hasAttributesSatisfyingExactly( |
| 112 | + equalTo(AttributeKey.stringKey("job.system"), "spring_scheduling"), |
| 113 | + equalTo(AttributeKey.stringKey("code.function"), "run"), |
| 114 | + satisfies( |
| 115 | + AttributeKey.stringKey("code.namespace"), |
| 116 | + codeNamespace -> |
| 117 | + codeNamespace |
| 118 | + .isNotBlank() |
| 119 | + .startsWith( |
| 120 | + LambdaTaskConfigurer.class.getName() |
| 121 | + + "$$Lambda"))))); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void scheduleEnhancedClassTest() throws InterruptedException { |
| 127 | + try (AnnotationConfigApplicationContext context = |
| 128 | + new AnnotationConfigApplicationContext(EnhancedClassTaskConfig.class)) { |
| 129 | + CountDownLatch latch = context.getBean(CountDownLatch.class); |
| 130 | + latch.await(5, TimeUnit.SECONDS); |
| 131 | + |
| 132 | + assertThat(latch).isNotNull(); |
| 133 | + testing.waitAndAssertTraces( |
| 134 | + trace -> |
| 135 | + trace.hasSpansSatisfyingExactly( |
| 136 | + span -> |
| 137 | + span.hasName("EnhancedClassTaskConfig.run") |
| 138 | + .hasNoParent() |
| 139 | + .hasAttributesSatisfyingExactly( |
| 140 | + equalTo(AttributeKey.stringKey("job.system"), "spring_scheduling"), |
| 141 | + equalTo( |
| 142 | + AttributeKey.stringKey("code.namespace"), |
| 143 | + EnhancedClassTaskConfig.class.getName()), |
| 144 | + equalTo(AttributeKey.stringKey("code.function"), "run")))); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void taskWithErrorTest() throws InterruptedException { |
| 150 | + try (AnnotationConfigApplicationContext context = |
| 151 | + new AnnotationConfigApplicationContext(TaskWithErrorConfig.class)) { |
| 152 | + TaskWithError task = context.getBean(TaskWithError.class); |
| 153 | + task.blockUntilExecute(); |
| 154 | + |
| 155 | + assertThat(task).isNotNull(); |
| 156 | + testing.waitAndAssertTraces( |
| 157 | + trace -> |
| 158 | + trace.hasSpansSatisfyingExactly( |
| 159 | + span -> |
| 160 | + span.hasName("TaskWithError.run") |
| 161 | + .hasNoParent() |
| 162 | + .hasStatus(StatusData.error()) |
| 163 | + .hasAttributesSatisfyingExactly( |
| 164 | + equalTo(AttributeKey.stringKey("job.system"), "spring_scheduling"), |
| 165 | + equalTo( |
| 166 | + AttributeKey.stringKey("code.namespace"), |
| 167 | + TaskWithError.class.getName()), |
| 168 | + equalTo(AttributeKey.stringKey("code.function"), "run")) |
| 169 | + .hasEventsSatisfyingExactly( |
| 170 | + event -> |
| 171 | + event |
| 172 | + .hasName(SemanticAttributes.EXCEPTION_EVENT_NAME) |
| 173 | + .hasAttributesSatisfying( |
| 174 | + equalTo( |
| 175 | + SemanticAttributes.EXCEPTION_TYPE, |
| 176 | + IllegalStateException.class.getName()), |
| 177 | + equalTo(SemanticAttributes.EXCEPTION_MESSAGE, "failure"), |
| 178 | + satisfies( |
| 179 | + SemanticAttributes.EXCEPTION_STACKTRACE, |
| 180 | + value -> value.isInstanceOf(String.class)))), |
| 181 | + span -> span.hasName("error-handler").hasParent(trace.getSpan(0)))); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments