|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.spring.integration.v4_1; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.testing.GlobalTraceUtil.runWithSpan; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 10 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | + |
| 13 | +import io.opentelemetry.api.trace.SpanKind; |
| 14 | +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
| 15 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 16 | +import io.opentelemetry.semconv.NetworkAttributes; |
| 17 | +import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 20 | + |
| 21 | +class SpringIntegrationAndRabbitTest { |
| 22 | + |
| 23 | + @RegisterExtension RabbitExtension rabbit; |
| 24 | + |
| 25 | + @RegisterExtension |
| 26 | + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
| 27 | + |
| 28 | + SpringIntegrationAndRabbitTest() { |
| 29 | + rabbit = new RabbitExtension(null); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void shouldCooperateWithExistingRabbitMqInstrumentation() { |
| 34 | + testing.waitForTraces(13); // from rabbitmq instrumentation of startup |
| 35 | + testing.clearData(); |
| 36 | + |
| 37 | + runWithSpan("parent", () -> rabbit.getBean("producer", Runnable.class).run()); |
| 38 | + |
| 39 | + testing.waitAndAssertTraces( |
| 40 | + trace -> |
| 41 | + trace.hasSpansSatisfyingExactly( |
| 42 | + span -> span.hasName("parent").hasTotalAttributeCount(0), |
| 43 | + span -> |
| 44 | + span.hasName("producer").hasParent(trace.getSpan(0)).hasTotalAttributeCount(0), |
| 45 | + span -> span.hasName("exchange.declare"), |
| 46 | + span -> |
| 47 | + span.hasName("exchange.declare") |
| 48 | + .hasParent(trace.getSpan(1)) |
| 49 | + .hasKind(SpanKind.CLIENT) |
| 50 | + .hasAttributesSatisfyingExactly( |
| 51 | + satisfies( |
| 52 | + NetworkAttributes.NETWORK_PEER_ADDRESS, |
| 53 | + s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), |
| 54 | + satisfies( |
| 55 | + NetworkAttributes.NETWORK_PEER_PORT, |
| 56 | + l -> l.isInstanceOf(Long.class)), |
| 57 | + satisfies( |
| 58 | + NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), |
| 59 | + equalTo(MessagingIncubatingAttributes.MESSAGING_SYSTEM, "rabbitmq")), |
| 60 | + span -> span.hasName("queue.declare"), |
| 61 | + span -> span.hasName("queue.bind"), |
| 62 | + span -> |
| 63 | + span.hasName("testTopic publish") |
| 64 | + .hasParent(trace.getSpan(1)) |
| 65 | + .hasKind(SpanKind.PRODUCER) |
| 66 | + .hasAttributesSatisfyingExactly( |
| 67 | + satisfies( |
| 68 | + NetworkAttributes.NETWORK_PEER_ADDRESS, |
| 69 | + s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), |
| 70 | + satisfies( |
| 71 | + NetworkAttributes.NETWORK_PEER_PORT, |
| 72 | + l -> l.isInstanceOf(Long.class)), |
| 73 | + satisfies( |
| 74 | + NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), |
| 75 | + equalTo(MessagingIncubatingAttributes.MESSAGING_SYSTEM, "rabbitmq"), |
| 76 | + equalTo( |
| 77 | + MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME, |
| 78 | + "testTopic"), |
| 79 | + equalTo(MessagingIncubatingAttributes.MESSAGING_OPERATION, "publish"), |
| 80 | + satisfies( |
| 81 | + MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, |
| 82 | + l -> l.isInstanceOf(Long.class)), |
| 83 | + satisfies( |
| 84 | + MessagingIncubatingAttributes |
| 85 | + .MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY, |
| 86 | + s -> s.isInstanceOf(String.class))), |
| 87 | + // spring-cloud-stream-binder-rabbit listener puts all messages into a BlockingQueue |
| 88 | + // immediately after receiving |
| 89 | + // that's why the rabbitmq CONSUMER span will never have any child span (and |
| 90 | + // propagate context, actually) |
| 91 | + span -> |
| 92 | + span.satisfies( |
| 93 | + spanData -> |
| 94 | + assertThat(spanData.getName()) |
| 95 | + .matches("testTopic.anonymous.[-\\w]+ process")) |
| 96 | + .hasParent(trace.getSpan(6)) |
| 97 | + .hasKind(SpanKind.CONSUMER) |
| 98 | + .hasAttributesSatisfyingExactly( |
| 99 | + satisfies( |
| 100 | + NetworkAttributes.NETWORK_PEER_ADDRESS, |
| 101 | + s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), |
| 102 | + satisfies( |
| 103 | + NetworkAttributes.NETWORK_PEER_PORT, |
| 104 | + l -> l.isInstanceOf(Long.class)), |
| 105 | + satisfies( |
| 106 | + NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), |
| 107 | + equalTo(MessagingIncubatingAttributes.MESSAGING_SYSTEM, "rabbitmq"), |
| 108 | + equalTo( |
| 109 | + MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME, |
| 110 | + "testTopic"), |
| 111 | + equalTo(MessagingIncubatingAttributes.MESSAGING_OPERATION, "process"), |
| 112 | + satisfies( |
| 113 | + MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, |
| 114 | + l -> l.isInstanceOf(Long.class)), |
| 115 | + satisfies( |
| 116 | + MessagingIncubatingAttributes |
| 117 | + .MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY, |
| 118 | + s -> s.isInstanceOf(String.class))), |
| 119 | + // spring-integration will detect that spring-rabbit has already created a consumer |
| 120 | + // span and back off |
| 121 | + span -> |
| 122 | + span.hasName("testTopic process") |
| 123 | + .hasParent(trace.getSpan(6)) |
| 124 | + .hasKind(SpanKind.CONSUMER) |
| 125 | + .hasAttributesSatisfyingExactly( |
| 126 | + equalTo(MessagingIncubatingAttributes.MESSAGING_SYSTEM, "rabbitmq"), |
| 127 | + equalTo( |
| 128 | + MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME, |
| 129 | + "testTopic"), |
| 130 | + equalTo(MessagingIncubatingAttributes.MESSAGING_OPERATION, "process"), |
| 131 | + satisfies( |
| 132 | + MessagingIncubatingAttributes.MESSAGING_MESSAGE_ID, |
| 133 | + s -> s.isInstanceOf(String.class)), |
| 134 | + satisfies( |
| 135 | + MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, |
| 136 | + l -> l.isInstanceOf(Long.class))), |
| 137 | + span -> |
| 138 | + span.hasName("consumer").hasParent(trace.getSpan(8)).hasTotalAttributeCount(0)), |
| 139 | + trace -> |
| 140 | + trace.hasSpansSatisfyingExactly( |
| 141 | + span -> |
| 142 | + span.hasName("basic.ack") |
| 143 | + .hasKind(SpanKind.CLIENT) |
| 144 | + .hasAttributesSatisfyingExactly( |
| 145 | + satisfies( |
| 146 | + NetworkAttributes.NETWORK_PEER_ADDRESS, |
| 147 | + s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), |
| 148 | + satisfies( |
| 149 | + NetworkAttributes.NETWORK_PEER_PORT, |
| 150 | + l -> l.isInstanceOf(Long.class)), |
| 151 | + satisfies( |
| 152 | + NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), |
| 153 | + equalTo(MessagingIncubatingAttributes.MESSAGING_SYSTEM, "rabbitmq")))); |
| 154 | + } |
| 155 | +} |
0 commit comments