Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,21 @@

package io.opentelemetry.instrumentation.api.incubator.semconv.messaging;

import static java.util.Collections.unmodifiableList;

import io.opentelemetry.api.common.AttributeKey;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

final class CapturedMessageHeadersUtil {

private static final ConcurrentMap<String, AttributeKey<List<String>>> attributeKeysCache =
new ConcurrentHashMap<>();

static List<String> lowercase(List<String> names) {
return unmodifiableList(
names.stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(Collectors.toList()));
}

static AttributeKey<List<String>> attributeKey(String headerName) {
return attributeKeysCache.computeIfAbsent(headerName, n -> createKey(n));
}

private static AttributeKey<List<String>> createKey(String headerName) {
// headerName is always lowercase, see MessagingAttributesExtractor
String key = "messaging.header." + headerName.replace('-', '_');
return AttributeKey.stringArrayKey(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.internal.SpanKey;
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -85,7 +86,7 @@ public static <REQUEST, RESPONSE> MessagingAttributesExtractorBuilder<REQUEST, R
List<String> capturedHeaders) {
this.getter = getter;
this.operation = operation;
this.capturedHeaders = CapturedMessageHeadersUtil.lowercase(capturedHeaders);
this.capturedHeaders = new ArrayList<>(capturedHeaders);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public final class MessagingAttributesExtractorBuilder<REQUEST, RESPONSE> {
* Configures the messaging headers that will be captured as span attributes.
*
* <p>The messaging header values will be captured under the {@code messaging.header.<name>}
* attribute key. The {@code <name>} part in the attribute key is the normalized header name:
* lowercase, with dashes replaced by underscores.
* attribute key. The {@code <name>} part in the attribute key is the header name with dashes
* replaced by underscores.
*
* @param capturedHeaders A list of messaging header names.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
tasks {
withType<Test>().configureEach {
systemProperty("otel.instrumentation.aws-sdk.experimental-span-attributes", "true")
systemProperty("otel.instrumentation.messaging.experimental.capture-headers", "test-message-header")
systemProperty("otel.instrumentation.messaging.experimental.capture-headers", "Test-Message-Header")
}

val testReceiveSpansDisabled by registering(Test::class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public AmazonSQSAsyncClientBuilder configureClient(AmazonSQSAsyncClientBuilder c
AwsSdkTelemetry.builder(testing().getOpenTelemetry())
.setCaptureExperimentalSpanAttributes(true)
.setMessagingReceiveInstrumentationEnabled(true)
.setCapturedHeaders(singletonList("test-message-header"))
.setCapturedHeaders(singletonList("Test-Message-Header"))
.build()
.newRequestHandler());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ void testSimpleSqsProducerConsumerServicesCaptureHeaders(boolean testCaptureHead

if (testCaptureHeaders) {
sendMessageRequest.addMessageAttributesEntry(
"test-message-header",
"Test-Message-Header",
new MessageAttributeValue().withDataType("String").withStringValue("test"));
}
sqsClient.sendMessage(sendMessageRequest);

ReceiveMessageRequest receiveMessageRequest =
new ReceiveMessageRequest("http://localhost:" + sqsPort + "/000000000000/testSdkSqs");
if (testCaptureHeaders) {
receiveMessageRequest.withMessageAttributeNames("test-message-header");
receiveMessageRequest.withMessageAttributeNames("Test-Message-Header");
}
ReceiveMessageResult receiveMessageResult = sqsClient.receiveMessage(receiveMessageRequest);

Expand Down Expand Up @@ -182,7 +182,7 @@ void testSimpleSqsProducerConsumerServicesCaptureHeaders(boolean testCaptureHead
if (testCaptureHeaders) {
attributes.add(
satisfies(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
val -> val.isEqualTo(singletonList("test"))));
}

Expand Down Expand Up @@ -222,7 +222,7 @@ void testSimpleSqsProducerConsumerServicesCaptureHeaders(boolean testCaptureHead
if (testCaptureHeaders) {
attributes.add(
satisfies(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
val -> val.isEqualTo(singletonList("test"))));
}

Expand Down Expand Up @@ -261,7 +261,7 @@ void testSimpleSqsProducerConsumerServicesCaptureHeaders(boolean testCaptureHead
if (testCaptureHeaders) {
attributes.add(
satisfies(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
val -> val.isEqualTo(singletonList("test"))));
}
span.hasName("testSdkSqs process")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tasks {
withType<Test>().configureEach {
systemProperty("otel.instrumentation.aws-sdk.experimental-span-attributes", true)
systemProperty("otel.instrumentation.aws-sdk.experimental-record-individual-http-error", true)
systemProperty("otel.instrumentation.messaging.experimental.capture-headers", "test-message-header")
systemProperty("otel.instrumentation.messaging.experimental.capture-headers", "Test-Message-Header")
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void setup() {
AwsSdkTelemetry.builder(getTesting().getOpenTelemetry())
.setCaptureExperimentalSpanAttributes(true)
.setMessagingReceiveInstrumentationEnabled(true)
.setCapturedHeaders(singletonList("test-message-header"));
.setCapturedHeaders(singletonList("Test-Message-Header"));

configure(telemetryBuilder);
telemetry = telemetryBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void assertSqsTraces(boolean withParent, boolean captureHeaders) {
if (captureHeaders) {
attributes.add(
satisfies(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
v -> v.isEqualTo(ImmutableList.of("test"))));
}
span.hasName("testSdkSqs publish")
Expand Down Expand Up @@ -163,7 +163,7 @@ protected void assertSqsTraces(boolean withParent, boolean captureHeaders) {
if (captureHeaders) {
attributes.add(
satisfies(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
v -> v.isEqualTo(ImmutableList.of("test"))));
}

Expand Down Expand Up @@ -201,7 +201,7 @@ protected void assertSqsTraces(boolean withParent, boolean captureHeaders) {
if (captureHeaders) {
attributes.add(
satisfies(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
v -> v.isEqualTo(singletonList("test"))));
}

Expand Down Expand Up @@ -238,13 +238,13 @@ void testCaptureMessageHeaderAsAttributeSpan() throws URISyntaxException {
sendMessageRequest.toBuilder()
.messageAttributes(
Collections.singletonMap(
"test-message-header",
"Test-Message-Header",
MessageAttributeValue.builder().dataType("String").stringValue("test").build()))
.build();
client.sendMessage(newSendMessageRequest);

ReceiveMessageRequest newReceiveMessageRequest =
receiveMessageRequest.toBuilder().messageAttributeNames("test-message-header").build();
receiveMessageRequest.toBuilder().messageAttributeNames("Test-Message-Header").build();
ReceiveMessageResponse response = client.receiveMessage(newReceiveMessageRequest);

assertThat(response.messages().size()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ void shouldCaptureMessageHeaders(
// given
Destination destination = destinationFactory.create(session);
TextMessage sentMessage = session.createTextMessage("a message");
sentMessage.setStringProperty("test_message_header", "test");
sentMessage.setIntProperty("test_message_int_header", 1234);
sentMessage.setStringProperty("Test_Message_Header", "test");
sentMessage.setIntProperty("Test_Message_Int_Header", 1234);

MessageProducer producer = session.createProducer(destination);
cleanup.deferCleanup(producer::close);
Expand Down Expand Up @@ -215,10 +215,10 @@ void shouldCaptureMessageHeaders(
equalTo(MESSAGING_MESSAGE_ID, messageId),
messagingTempDestination(isTemporary),
equalTo(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
singletonList("test")),
equalTo(
stringArrayKey("messaging.header.test_message_int_header"),
stringArrayKey("messaging.header.Test_Message_Int_Header"),
singletonList("1234"))),
span ->
span.hasName(destinationName + " process")
Expand All @@ -231,10 +231,10 @@ void shouldCaptureMessageHeaders(
equalTo(MESSAGING_MESSAGE_ID, messageId),
messagingTempDestination(isTemporary),
equalTo(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
singletonList("test")),
equalTo(
stringArrayKey("messaging.header.test_message_int_header"),
stringArrayKey("messaging.header.Test_Message_Int_Header"),
singletonList("1234"))),
span -> span.hasName("consumer").hasParent(trace.getSpan(2))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ void shouldCaptureMessageHeaders(DestinationFactory destinationFactory, boolean
// given
Destination destination = destinationFactory.create(session);
TextMessage sentMessage = session.createTextMessage("hello there");
sentMessage.setStringProperty("test_message_header", "test");
sentMessage.setIntProperty("test_message_int_header", 1234);
sentMessage.setStringProperty("Test_Message_Header", "test");
sentMessage.setIntProperty("Test_Message_Int_Header", 1234);

MessageProducer producer = session.createProducer(destination);
cleanup.deferCleanup(producer);
Expand Down Expand Up @@ -232,10 +232,10 @@ void shouldCaptureMessageHeaders(DestinationFactory destinationFactory, boolean
equalTo(MESSAGING_MESSAGE_ID, messageId),
messagingTempDestination(isTemporary),
equalTo(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
singletonList("test")),
equalTo(
stringArrayKey("messaging.header.test_message_int_header"),
stringArrayKey("messaging.header.Test_Message_Int_Header"),
singletonList("1234"))),
span ->
span.hasName(actualDestinationName + " process")
Expand All @@ -247,10 +247,10 @@ void shouldCaptureMessageHeaders(DestinationFactory destinationFactory, boolean
equalTo(MESSAGING_OPERATION, "process"),
equalTo(MESSAGING_MESSAGE_ID, messageId),
equalTo(
stringArrayKey("messaging.header.test_message_header"),
stringArrayKey("messaging.header.Test_Message_Header"),
singletonList("test")),
equalTo(
stringArrayKey("messaging.header.test_message_int_header"),
stringArrayKey("messaging.header.Test_Message_Int_Header"),
singletonList("1234"))),
span -> span.hasName("consumer").hasParent(trace.getSpan(2))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void testKafkaProducerAndConsumerSpan(boolean testHeaders) throws Exception {
if (testHeaders) {
producerRecord
.headers()
.add("test-message-header", "test".getBytes(StandardCharsets.UTF_8));
.add("Test-Message-Header", "test".getBytes(StandardCharsets.UTF_8));
}
producer
.send(
Expand Down Expand Up @@ -214,7 +214,7 @@ void testKafkaHeaderNull() throws Exception {
() -> {
ProducerRecord<Integer, String> producerRecord =
new ProducerRecord<>(SHARED_TOPIC, 10, greeting);
producerRecord.headers().add("test-message-header", null);
producerRecord.headers().add("Test-Message-Header", null);
producer
.send(
producerRecord,
Expand All @@ -238,7 +238,7 @@ void testKafkaHeaderNull() throws Exception {
() -> {
assertThat(record.key()).isEqualTo(10);
assertThat(record.value()).isEqualTo(greeting);
assertThat(record.headers().lastHeader("test-message-header").value()).isNull();
assertThat(record.headers().lastHeader("Test-Message-Header").value()).isNull();
});
}
AtomicReference<SpanData> producerSpan = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected static List<AttributeAssertion> sendAttributes(
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}
return assertions;
Expand All @@ -211,7 +211,7 @@ protected static List<AttributeAssertion> receiveAttributes(boolean testHeaders)
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}
return assertions;
Expand Down Expand Up @@ -249,7 +249,7 @@ protected static List<AttributeAssertion> processAttributes(
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class AbstractWrapperTest extends KafkaClientBaseTest {
void testWrappers(boolean testHeaders) throws InterruptedException {
KafkaTelemetryBuilder telemetryBuilder =
KafkaTelemetry.builder(testing.getOpenTelemetry())
.setCapturedHeaders(singletonList("test-message-header"))
.setCapturedHeaders(singletonList("Test-Message-Header"))
// TODO run tests both with and without experimental span attributes
.setCaptureExperimentalSpanAttributes(true);
configure(telemetryBuilder);
Expand All @@ -50,7 +50,7 @@ void testWrappers(boolean testHeaders) throws InterruptedException {
if (testHeaders) {
producerRecord
.headers()
.add("test-message-header", "test".getBytes(StandardCharsets.UTF_8));
.add("Test-Message-Header", "test".getBytes(StandardCharsets.UTF_8));
}
wrappedProducer.send(
producerRecord,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected static List<AttributeAssertion> sendAttributes(boolean testHeaders) {
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}
return assertions;
Expand All @@ -100,7 +100,7 @@ private static List<AttributeAssertion> processAttributes(String greeting, boole
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}
return assertions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected static List<AttributeAssertion> sendAttributes(boolean testHeaders) {
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}
return assertions;
Expand All @@ -123,7 +123,7 @@ private static List<AttributeAssertion> processAttributes(String greeting, boole
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}
return assertions;
Expand All @@ -143,7 +143,7 @@ protected static List<AttributeAssertion> receiveAttributes(boolean testHeaders)
if (testHeaders) {
assertions.add(
equalTo(
AttributeKey.stringArrayKey("messaging.header.test_message_header"),
AttributeKey.stringArrayKey("messaging.header.Test_Message_Header"),
Collections.singletonList("test")));
}
return assertions;
Expand Down
Loading