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

Run context customizers before span start instead of after #6634

Merged
merged 4 commits into from
Oct 6, 2022
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 @@ -18,5 +18,5 @@
public interface ContextCustomizer<REQUEST> {

/** Allows to customize the operation {@link Context}. */
Context onStart(Context context, REQUEST request, Attributes startAttributes);
Context onStart(Context parentContext, REQUEST request, Attributes startAttributes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ Context startAndEnd(
private Context doStart(Context parentContext, REQUEST request, @Nullable Instant startTime) {
SpanKind spanKind = spanKindExtractor.extract(request);
SpanBuilder spanBuilder =
tracer
.spanBuilder(spanNameExtractor.extract(request))
.setSpanKind(spanKind)
.setParent(parentContext);
tracer.spanBuilder(spanNameExtractor.extract(request)).setSpanKind(spanKind);

if (startTime != null) {
spanBuilder.setStartTimestamp(startTime);
Expand All @@ -181,22 +178,28 @@ private Context doStart(Context parentContext, REQUEST request, @Nullable Instan

Context context = parentContext;

spanBuilder.setAllAttributes(attributes);
Span span = spanBuilder.startSpan();
context = context.with(span);

// context customizers run before span start, so that they can have access to the parent span
// context, and so that their additions to the context will be visible to span processors
for (ContextCustomizer<? super REQUEST> contextCustomizer : contextCustomizers) {
context = contextCustomizer.onStart(context, request, attributes);
Comment on lines 183 to 184
Copy link
Member

Choose a reason for hiding this comment

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

You should rename the first param of ContextCustomizer#onStart() to parentContext too

}

boolean localRoot = LocalRootSpan.isLocalRoot(context);

spanBuilder.setAllAttributes(attributes);
Span span = spanBuilder.setParent(context).startSpan();
context = context.with(span);

if (!operationListeners.isEmpty()) {
// operation listeners run after span start, so that they have access to the current span
// for capturing exemplars
long startNanos = getNanos(startTime);
for (OperationListener operationListener : operationListeners) {
context = operationListener.onStart(context, attributes, startNanos);
}
Copy link
Member

Choose a reason for hiding this comment

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

The OperationListener need to get the context with the newly created span, so that they have the proper exemplars

}

if (LocalRootSpan.isLocalRoot(parentContext)) {
if (localRoot) {
context = LocalRootSpan.store(context, span);
}

Expand Down