-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| package org.springframework.kafka.support.micrometer; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.Deque; | ||
| import java.util.List; | ||
|
|
@@ -26,6 +27,8 @@ | |
| 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; | ||
| import io.micrometer.core.instrument.MeterRegistry; | ||
|
|
@@ -56,6 +59,7 @@ | |
| import org.apache.kafka.common.errors.InvalidTopicException; | ||
| import org.apache.kafka.common.header.Header; | ||
| import org.apache.kafka.common.header.Headers; | ||
| import org.apache.kafka.common.header.internals.RecordHeader; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.core.publisher.Mono; | ||
|
|
@@ -78,6 +82,7 @@ | |
| import org.springframework.kafka.core.ProducerFactory; | ||
| import org.springframework.kafka.listener.MessageListenerContainer; | ||
| import org.springframework.kafka.listener.RecordInterceptor; | ||
| import org.springframework.kafka.support.ProducerListener; | ||
| import org.springframework.kafka.support.micrometer.KafkaListenerObservation.DefaultKafkaListenerObservationConvention; | ||
| import org.springframework.kafka.support.micrometer.KafkaTemplateObservation.DefaultKafkaTemplateObservationConvention; | ||
| import org.springframework.kafka.test.EmbeddedKafkaBroker; | ||
|
|
@@ -104,7 +109,7 @@ | |
| @SpringJUnitConfig | ||
| @EmbeddedKafka(topics = { ObservationTests.OBSERVATION_TEST_1, ObservationTests.OBSERVATION_TEST_2, | ||
| ObservationTests.OBSERVATION_TEST_3, ObservationTests.OBSERVATION_RUNTIME_EXCEPTION, | ||
| ObservationTests.OBSERVATION_ERROR }, partitions = 1) | ||
| ObservationTests.OBSERVATION_ERROR, ObservationTests.OBSERVATION_TRACEPARENT_DUPLICATE }, partitions = 1) | ||
| @DirtiesContext | ||
| public class ObservationTests { | ||
|
|
||
|
|
@@ -122,6 +127,8 @@ public class ObservationTests { | |
|
|
||
| public final static String OBSERVATION_ERROR_MONO = "observation.error.mono"; | ||
|
|
||
| public final static String OBSERVATION_TRACEPARENT_DUPLICATE = "observation.traceparent.duplicate"; | ||
|
|
||
| @Test | ||
| void endToEnd(@Autowired Listener listener, @Autowired KafkaTemplate<Integer, String> template, | ||
| @Autowired SimpleTracer tracer, @Autowired KafkaListenerEndpointRegistry rler, | ||
|
|
@@ -449,6 +456,65 @@ 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"; | ||
| String updatedTraceParent = "traceparent-current"; | ||
| ProducerRecord<Integer, String> record = new ProducerRecord<>("test-topic", "test-value"); | ||
| record.headers().add("traceparent", initialTraceParent.getBytes(StandardCharsets.UTF_8)); | ||
|
|
||
| // Create the context and update the traceparent | ||
| KafkaRecordSenderContext context = new KafkaRecordSenderContext( | ||
| record, | ||
| "test-bean", | ||
| () -> "test-cluster" | ||
| ); | ||
| 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()); | ||
|
||
|
|
||
| // 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 { | ||
| CompletableFuture<ProducerRecord<Integer, String>> producerRecordFuture = new CompletableFuture<>(); | ||
| template.setProducerListener(new ProducerListener<>() { | ||
| @Override | ||
| public void onSuccess(ProducerRecord<Integer, String> producerRecord, RecordMetadata recordMetadata) { | ||
| producerRecordFuture.complete(producerRecord); | ||
| } | ||
| }); | ||
| String initialTraceParent = "traceparent-from-previous"; | ||
| Header header = new RecordHeader("traceparent", initialTraceParent.getBytes(StandardCharsets.UTF_8)); | ||
| ProducerRecord<Integer, String> producerRecord = new ProducerRecord<>( | ||
| OBSERVATION_TRACEPARENT_DUPLICATE, | ||
| null, null, null, | ||
| "test-value", | ||
| List.of(header) | ||
| ); | ||
|
|
||
| template.send(producerRecord).get(10, TimeUnit.SECONDS); | ||
| ProducerRecord<Integer, String> recordResult = producerRecordFuture.get(10, TimeUnit.SECONDS); | ||
|
|
||
| Iterable<Header> traceparentHeaders = recordResult.headers().headers("traceparent"); | ||
| assertThat(traceparentHeaders).hasSize(1); | ||
|
|
||
| String traceparentValue = new String(traceparentHeaders.iterator().next().value(), StandardCharsets.UTF_8); | ||
| assertThat(traceparentValue).isEqualTo("traceparent-from-propagator"); | ||
|
|
||
| tracer.getSpans().clear(); | ||
| } | ||
|
|
||
| @Configuration | ||
| @EnableKafka | ||
| public static class Config { | ||
|
|
@@ -598,6 +664,9 @@ public List<String> fields() { | |
| public <C> void inject(TraceContext context, @Nullable C carrier, Setter<C> setter) { | ||
| setter.set(carrier, "foo", "some foo value"); | ||
| setter.set(carrier, "bar", "some bar value"); | ||
|
|
||
| // Add a traceparent header to simulate W3C trace context | ||
| setter.set(carrier, "traceparent", "traceparent-from-propagator"); | ||
| } | ||
|
|
||
| // This is called on the consumer side when the message is consumed | ||
|
|
@@ -606,7 +675,9 @@ public <C> void inject(TraceContext context, @Nullable C carrier, Setter<C> sett | |
| public <C> Span.Builder extract(C carrier, Getter<C> getter) { | ||
| String foo = getter.get(carrier, "foo"); | ||
| String bar = getter.get(carrier, "bar"); | ||
| return tracer.spanBuilder().tag("foo", foo).tag("bar", bar); | ||
| return tracer.spanBuilder() | ||
| .tag("foo", foo) | ||
| .tag("bar", bar); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct.
Different vendors provides different header name.
I have so far know here: https://github.com/spring-cloud/spring-cloud-stream-binder-aws-kinesis/blob/main/spring-cloud-stream-binder-kinesis/src/main/java/org/springframework/cloud/stream/binder/kinesis/KinesisMessageChannelBinder.java#L575-L577.
But that does not mean that anything else custom could be provided from the context supplier.
I think it is totally safe to replace whatever was asked from the supplier.
The point is to not mislead with extra entry from what might came from the previous observation.
We have to provide here what can be set from keys supplier.