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 opentracing header name issue #5840

Merged
merged 4 commits into from
Oct 31, 2023
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 @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -108,14 +109,17 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
if (carrier != null) {
BaggageBuilder baggageBuilder = Baggage.builder();
for (String key : getter.keys(carrier)) {
if (!key.startsWith(PREFIX_BAGGAGE_HEADER)) {
String lowercaseKey = key.toLowerCase(Locale.ROOT);
if (!lowercaseKey.startsWith(PREFIX_BAGGAGE_HEADER)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively could use

if (!key.regionMatches(true, 0, PREFIX_BAGGAGE_HEADER, 0, PREFIX_BAGGAGE_HEADER.length())) {
  continue;
}

to avoid converting the whole key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we decided to keep the case of header, I will also apply this change.

continue;
}
String value = getter.get(carrier, key);
if (value == null) {
continue;
}
baggageBuilder.put(key.replace(OtTracePropagator.PREFIX_BAGGAGE_HEADER, ""), value);
String baggageKey =
lowercaseKey.substring(OtTracePropagator.PREFIX_BAGGAGE_HEADER.length());
baggageBuilder.put(baggageKey, value);
}
Baggage baggage = baggageBuilder.build();
if (!baggage.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,6 +56,18 @@ private static Context withSpanContext(SpanContext spanContext, Context context)
return context.with(Span.wrap(spanContext));
}

private static String capitalizeFirstLetter(String input, String delimiter) {
String[] words = input.split(delimiter);

for (int i = 0; i < words.length; i++) {
String firstLetter = words[i].substring(0, 1).toUpperCase(Locale.ROOT);
String restOfWord = words[i].substring(1).toLowerCase(Locale.ROOT);
words[i] = firstLetter + restOfWord;
}

return String.join(delimiter, words);
}

@Test
void inject_invalidContext() {
Map<String, String> carrier = new LinkedHashMap<>();
Expand Down Expand Up @@ -313,6 +326,22 @@ void extract_Baggage() {
assertThat(Baggage.fromContext(context)).isEqualTo(expectedBaggage);
}

@Test
void extract_Baggage_CapitalizedHeaders() {
String capitalizedBaggageHeader =
capitalizeFirstLetter(OtTracePropagator.PREFIX_BAGGAGE_HEADER + "some-key", "-");
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(OtTracePropagator.TRACE_ID_HEADER, TRACE_ID);
carrier.put(OtTracePropagator.SPAN_ID_HEADER, SPAN_ID);
carrier.put(OtTracePropagator.SAMPLED_HEADER, Common.TRUE_INT);
carrier.put(capitalizedBaggageHeader, "value");

Context context = propagator.extract(Context.current(), carrier, getter);

Baggage expectedBaggage = Baggage.builder().put("some-key", "value").build();
assertThat(Baggage.fromContext(context)).isEqualTo(expectedBaggage);
}

@Test
void extract_Baggage_InvalidContext() {
Map<String, String> carrier = new LinkedHashMap<>();
Expand Down