Skip to content

GH-3786: Remove ProducerRecord duplicated traceparent header #3789

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

Merged
merged 3 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
PR review
  • Loading branch information
sobychacko committed Mar 13, 2025
commit 1a6b2ecab27581a6a64e8058b507a2a1263a9014
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.micrometer.observation.transport.SenderContext;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.Headers;

/**
Expand All @@ -43,8 +44,8 @@ public class KafkaRecordSenderContext extends SenderContext<ProducerRecord<?, ?>
public KafkaRecordSenderContext(ProducerRecord<?, ?> record, String beanName, Supplier<String> clusterId) {
super((carrier, key, value) -> {
Headers headers = record.headers();
// For traceparent context headers, ensure there's only one
if ("traceparent".equals(key)) {
Iterable<Header> existingHeaders = headers.headers(key);
if (existingHeaders.iterator().hasNext()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to do this check?
Why just plain remove() not enough?

headers.remove(key);
}
headers.add(key, value == null ? null : value.getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import io.micrometer.common.KeyValues;
Expand Down Expand Up @@ -456,8 +455,6 @@ void kafkaAdminNotRecreatedIfBootstrapServersSameInProducerAndAdminConfig(
assertThat(template.getKafkaAdmin()).isSameAs(kafkaAdmin);
}

// https://github.com/spring-cloud/spring-cloud-stream/issues/3095#issuecomment-2707075861
// https://github.com/spring-projects/spring-kafka/issues/3786
@Test
void verifyKafkaRecordSenderContextTraceParentHandling() {
String initialTraceParent = "traceparent-from-previous";
Expand All @@ -474,16 +471,15 @@ void verifyKafkaRecordSenderContextTraceParentHandling() {
context.getSetter().set(record, "traceparent", updatedTraceParent);

Iterable<Header> traceparentHeaders = record.headers().headers("traceparent");

List<String> headerValues = StreamSupport.stream(traceparentHeaders.spliterator(), false)
.map(header -> new String(header.value(), StandardCharsets.UTF_8))
.collect(Collectors.toList());
.toList();

// Verify there's only one traceparent header and it contains the updated value
assertThat(headerValues).containsExactly(updatedTraceParent);
}

// https://github.com/spring-cloud/spring-cloud-stream/issues/3095#issuecomment-2707075861
// https://github.com/spring-projects/spring-kafka/issues/3786
@Test
void verifyTraceParentHeader(@Autowired KafkaTemplate<Integer, String> template,
@Autowired SimpleTracer tracer) throws Exception {
Expand Down