Implement forEach support for aws sqs tracing list (#10062)

This commit is contained in:
Lauri Tulmin 2023-12-13 18:32:55 +02:00 committed by GitHub
parent b8126c8da7
commit c7fd4d4b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
class TracingList extends SdkInternalList<Message> {
private static final long serialVersionUID = 1L;
@ -57,6 +58,13 @@ class TracingList extends SdkInternalList<Message> {
return it;
}
@Override
public void forEach(Consumer<? super Message> action) {
for (Message message : this) {
action.accept(message);
}
}
private static boolean inAwsClient() {
for (Class<?> caller : CallerClass.INSTANCE.getClassContext()) {
if (AmazonSQSClient.class == caller) {

View File

@ -68,8 +68,15 @@ abstract class AbstractSqsTracingTest extends InstrumentationSpecification {
receiveMessageRequest.withMessageAttributeNames("test-message-header")
}
def receiveMessageResult = client.receiveMessage(receiveMessageRequest)
receiveMessageResult.messages.each {message ->
runWithSpan("process child") {}
// test different ways of iterating the messages list
if (testCaptureHeaders) {
receiveMessageResult.messages.each { message ->
runWithSpan("process child") {}
}
} else {
receiveMessageResult.messages.forEach { message ->
runWithSpan("process child") {}
}
}
then:

View File

@ -10,6 +10,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.services.sqs.model.Message;
@ -59,4 +60,11 @@ class TracingList extends ArrayList<Message> {
return it;
}
@Override
public void forEach(Consumer<? super Message> action) {
for (Message message : this) {
action.accept(message);
}
}
}

View File

@ -300,7 +300,8 @@ abstract class AbstractAws2SqsTracingTest extends InstrumentationSpecification {
then:
resp.messages.size() == 1
resp.messages.each {message -> runWithSpan("process child") {}}
// using forEach instead of each here to test different ways of iterating messages list
resp.messages.forEach {message -> runWithSpan("process child") {}}
assertSqsTraces(false, true)
}