|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.spring.data.v3_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 9 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_CONNECTION_STRING; |
| 10 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_NAME; |
| 11 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_OPERATION; |
| 12 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_SQL_TABLE; |
| 13 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_STATEMENT; |
| 14 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_SYSTEM; |
| 15 | +import static io.opentelemetry.semconv.SemanticAttributes.DB_USER; |
| 16 | +import static io.opentelemetry.semconv.SemanticAttributes.NET_PEER_NAME; |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import io.opentelemetry.api.trace.SpanKind; |
| 20 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 21 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 22 | +import io.opentelemetry.javaagent.instrumentation.spring.data.v3_0.repository.CustomerRepository; |
| 23 | +import io.opentelemetry.javaagent.instrumentation.spring.data.v3_0.repository.PersistenceConfig; |
| 24 | +import io.opentelemetry.semconv.SemanticAttributes; |
| 25 | +import java.time.Duration; |
| 26 | +import org.junit.jupiter.api.AfterAll; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 30 | +import org.springframework.context.ConfigurableApplicationContext; |
| 31 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 32 | + |
| 33 | +@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0 |
| 34 | +class ReactiveSpringDataTest { |
| 35 | + |
| 36 | + @RegisterExtension |
| 37 | + private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 38 | + |
| 39 | + private static ConfigurableApplicationContext applicationContext; |
| 40 | + private static CustomerRepository customerRepository; |
| 41 | + |
| 42 | + @BeforeAll |
| 43 | + static void setUp() { |
| 44 | + applicationContext = new AnnotationConfigApplicationContext(PersistenceConfig.class); |
| 45 | + customerRepository = applicationContext.getBean(CustomerRepository.class); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterAll |
| 49 | + static void cleanUp() { |
| 50 | + applicationContext.close(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void testFindAll() { |
| 55 | + long count = |
| 56 | + testing |
| 57 | + .runWithSpan("parent", () -> customerRepository.findAll()) |
| 58 | + .count() |
| 59 | + .block(Duration.ofSeconds(30)); |
| 60 | + assertThat(count).isEqualTo(1); |
| 61 | + |
| 62 | + testing.waitAndAssertTraces( |
| 63 | + trace -> |
| 64 | + trace.hasSpansSatisfyingExactly( |
| 65 | + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL), |
| 66 | + span -> |
| 67 | + span.hasName("CustomerRepository.findAll") |
| 68 | + .hasKind(SpanKind.INTERNAL) |
| 69 | + .hasAttributesSatisfyingExactly( |
| 70 | + equalTo( |
| 71 | + SemanticAttributes.CODE_NAMESPACE, |
| 72 | + CustomerRepository.class.getName()), |
| 73 | + equalTo(SemanticAttributes.CODE_FUNCTION, "findAll")), |
| 74 | + span -> |
| 75 | + span.hasName("SELECT db.CUSTOMER") |
| 76 | + .hasKind(SpanKind.CLIENT) |
| 77 | + .hasParent(trace.getSpan(1)) |
| 78 | + // assert that this span ends before its parent span |
| 79 | + .satisfies( |
| 80 | + spanData -> |
| 81 | + assertThat(spanData.getEndEpochNanos()) |
| 82 | + .isLessThanOrEqualTo(trace.getSpan(1).getEndEpochNanos())) |
| 83 | + .hasAttributesSatisfyingExactly( |
| 84 | + equalTo(DB_SYSTEM, "h2"), |
| 85 | + equalTo(DB_NAME, "db"), |
| 86 | + equalTo(DB_USER, "sa"), |
| 87 | + equalTo(DB_STATEMENT, "SELECT CUSTOMER.* FROM CUSTOMER"), |
| 88 | + equalTo(DB_OPERATION, "SELECT"), |
| 89 | + equalTo(DB_SQL_TABLE, "CUSTOMER"), |
| 90 | + equalTo(DB_CONNECTION_STRING, "h2:mem://localhost"), |
| 91 | + equalTo(NET_PEER_NAME, "localhost")))); |
| 92 | + } |
| 93 | +} |
0 commit comments