Skip to content

Performance critical tracing code has high overhead due to the use of the Stream API #46838

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -19,7 +19,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

import brave.propagation.B3Propagation;
Expand Down Expand Up @@ -76,11 +75,19 @@ public Propagation<String> get() {

@Override
public TraceContext decorate(TraceContext context) {
return Stream.concat(this.injectors.stream(), this.extractors.stream())
.map((factory) -> factory.decorate(context))
.filter((decorated) -> decorated != context)
.findFirst()
.orElse(context);
for (Propagation.Factory factory : this.injectors.factories) {
TraceContext decorated = factory.decorate(context);
if (decorated != context) {
return decorated;
}
}
for (Propagation.Factory factory : this.extractors.factories) {
TraceContext decorated = factory.decorate(context);
if (decorated != context) {
return decorated;
}
}
return context;
}

/**
Expand Down Expand Up @@ -179,11 +186,21 @@ private static class PropagationFactories {
}

boolean requires128BitTraceId() {
return stream().anyMatch(Propagation.Factory::requires128BitTraceId);
for (Propagation.Factory factory : this.factories) {
if (factory.requires128BitTraceId()) {
return true;
}
}
return false;
}

boolean supportsJoin() {
return stream().allMatch(Propagation.Factory::supportsJoin);
for (Propagation.Factory factory : this.factories) {
if (!factory.supportsJoin()) {
return false;
}
}
return true;
}

List<Propagation<String>> get() {
Expand Down Expand Up @@ -224,19 +241,24 @@ public List<String> keys() {

@Override
public <R> TraceContext.Injector<R> injector(Setter<R, String> setter) {
return (traceContext, request) -> this.injectors.stream()
.map((propagation) -> propagation.injector(setter))
.forEach((injector) -> injector.inject(traceContext, request));
return (traceContext, request) -> {
for (Propagation<String> propagation : this.injectors) {
propagation.injector(setter).inject(traceContext, request);
}
};
}

@Override
public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) {
return (request) -> this.extractors.stream()
.map((propagation) -> propagation.extractor(getter))
.map((extractor) -> extractor.extract(request))
.filter(Predicate.not(TraceContextOrSamplingFlags.EMPTY::equals))
.findFirst()
.orElse(TraceContextOrSamplingFlags.EMPTY);
return (request) -> {
for (Propagation<String> propagation : this.extractors) {
TraceContextOrSamplingFlags extracted = propagation.extractor(getter).extract(request);
if (!TraceContextOrSamplingFlags.EMPTY.equals(extracted)) {
return extracted;
}
}
return TraceContextOrSamplingFlags.EMPTY;
};
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter)
if (getter == null) {
return context;
}
Context result = this.extractors.stream()
.map((extractor) -> extractor.extract(context, carrier, getter))
.filter((extracted) -> extracted != context)
.findFirst()
.orElse(context);
Context result = context;
for (TextMapPropagator extractor : this.extractors) {
Context extracted = extractor.extract(context, carrier, getter);
if (extracted != context) {
result = extracted;
break;
}
}
if (this.baggagePropagator != null) {
result = this.baggagePropagator.extract(result, carrier, getter);
}
Expand Down