Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE in end span #35299

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix NPE in end span
lmolkova committed Jun 5, 2023
commit b3b8d07bf3e640b03c3e4308f79c9a15e0610514
1 change: 1 addition & 0 deletions sdk/servicebus/azure-messaging-servicebus/pom.xml
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@
--add-opens com.azure.messaging.servicebus/com.azure.messaging.servicebus.administration.models=ALL-UNNAMED

--add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
--add-exports com.azure.core/com.azure.core.implementation.instrumentation=ALL-UNNAMED
--add-opens com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
--add-reads com.azure.messaging.servicebus=com.azure.http.netty
--add-reads com.azure.messaging.servicebus=com.azure.core.tracing.opentelemetry
Original file line number Diff line number Diff line change
@@ -127,7 +127,9 @@ public void endSpan(Throwable throwable, Context span, AutoCloseable scope) {
String errorCondition = null;
if (throwable instanceof AmqpException) {
AmqpException exception = (AmqpException) throwable;
errorCondition = exception.getErrorCondition().getErrorCondition();
if (exception.getErrorCondition() != null) {
errorCondition = exception.getErrorCondition().getErrorCondition();
}
}

try {
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.messaging.servicebus.implementation.instrumentation;

import com.azure.core.amqp.exception.AmqpErrorCondition;
import com.azure.core.amqp.exception.AmqpException;
import com.azure.core.util.Context;
import com.azure.core.util.tracing.Tracer;
import com.azure.messaging.servicebus.ServiceBusErrorSource;
import com.azure.messaging.servicebus.implementation.DispositionStatus;
import org.apache.qpid.proton.amqp.transport.DeliveryState;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ServiceBusTracerTests {
@Test
public void testSpanEndNoError() {
Tracer inner = mock(Tracer.class);
when(inner.isEnabled()).thenReturn(true);

ServiceBusTracer tracer = new ServiceBusTracer(inner, "fqdn", "entityPath");
tracer.endSpan(null, Context.NONE, null);

verify(inner, times(1)).end(isNull(), isNull(), same(Context.NONE));
}

@Test
public void testSpanEndNoErrorAndScope() {
Tracer inner = mock(Tracer.class);
when(inner.isEnabled()).thenReturn(true);

ServiceBusTracer tracer = new ServiceBusTracer(inner, "fqdn", "entityPath");
AtomicBoolean closed = new AtomicBoolean();
tracer.endSpan(null, Context.NONE, () -> closed.set(true));

verify(inner, times(1)).end(isNull(), isNull(), same(Context.NONE));
assertTrue(closed.get());
}

@Test
public void testSpanEndException() {
Tracer inner = mock(Tracer.class);
when(inner.isEnabled()).thenReturn(true);

ServiceBusTracer tracer = new ServiceBusTracer(inner, "fqdn", "entityPath");

Exception ex = new RuntimeException("foo");
tracer.endSpan(ex, Context.NONE, null);

verify(inner, times(1)).end(isNull(), eq(ex), same(Context.NONE));
}

@ParameterizedTest
@MethodSource("getAmqpException")
public void testSpanEndAmqpException(AmqpException amqpException, String expectedStatus) {
Tracer inner = mock(Tracer.class);
when(inner.isEnabled()).thenReturn(true);

ServiceBusTracer tracer = new ServiceBusTracer(inner, "fqdn", "entityPath");

tracer.endSpan(amqpException, Context.NONE, null);

verify(inner, times(1)).end(eq(expectedStatus), eq(amqpException), same(Context.NONE));
}

public static Stream<Arguments> getAmqpException() {
return Stream.of(
Arguments.of(new AmqpException(false, "foo", null, null), null),
Arguments.of(new AmqpException(false, AmqpErrorCondition.NOT_FOUND, "foo", null), AmqpErrorCondition.NOT_FOUND.getErrorCondition()),
Arguments.of(new AmqpException(false, AmqpErrorCondition.TIMEOUT_ERROR, "", null), AmqpErrorCondition.TIMEOUT_ERROR.getErrorCondition()),
Arguments.of(new AmqpException(false, AmqpErrorCondition.SERVER_BUSY_ERROR, null, new RuntimeException("foo"), null), AmqpErrorCondition.SERVER_BUSY_ERROR.getErrorCondition()));
}
}