Skip to content

Commit

Permalink
Run context customizers before span start instead of after (open-tele…
Browse files Browse the repository at this point in the history
…metry#6634)

...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
  • Loading branch information
trask authored and LironKS committed Oct 23, 2022
1 parent 2f0e4a3 commit 32585bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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);
}

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);
}
}

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

Expand Down

0 comments on commit 32585bd

Please sign in to comment.