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 the issue with DefaultSpanScope restoring wrong span in the Trace… #11316

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Delegating CachingWeightWrapper#count to internal weight object ([#10543](https://github.com/opensearch-project/OpenSearch/pull/10543))
- Fix per request latency last phase not tracked ([#10934](https://github.com/opensearch-project/OpenSearch/pull/10934))
- Fix for stuck update action in a bulk with `retry_on_conflict` property ([#11152](https://github.com/opensearch-project/OpenSearch/issues/11152))
- Fix the issue with DefaultSpanScope restoring wrong span in the TracerContextStorage upon detach ([#11316](https://github.com/opensearch-project/OpenSearch/issues/11316))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class DefaultSpanScope implements SpanScope {
private final Span span;
private final SpanScope previousSpanScope;
private final Span beforeSpan;
private static final ThreadLocal<SpanScope> spanScopeThreadLocal = new ThreadLocal<>();
private final TracerContextStorage<String, Span> tracerContextStorage;

Expand All @@ -29,8 +30,14 @@ class DefaultSpanScope implements SpanScope {
* @param span span
* @param previousSpanScope before attached span scope.
*/
private DefaultSpanScope(Span span, SpanScope previousSpanScope, TracerContextStorage<String, Span> tracerContextStorage) {
private DefaultSpanScope(
Span span,
final Span beforeSpan,
SpanScope previousSpanScope,
TracerContextStorage<String, Span> tracerContextStorage
) {
this.span = Objects.requireNonNull(span);
this.beforeSpan = beforeSpan;
this.previousSpanScope = previousSpanScope;
this.tracerContextStorage = tracerContextStorage;
}
Expand All @@ -43,7 +50,8 @@ private DefaultSpanScope(Span span, SpanScope previousSpanScope, TracerContextSt
*/
public static SpanScope create(Span span, TracerContextStorage<String, Span> tracerContextStorage) {
final SpanScope beforeSpanScope = spanScopeThreadLocal.get();
SpanScope newSpanScope = new DefaultSpanScope(span, beforeSpanScope, tracerContextStorage);
final Span beforeSpan = tracerContextStorage.get(TracerContextStorage.CURRENT_SPAN);
dblock marked this conversation as resolved.
Show resolved Hide resolved
SpanScope newSpanScope = new DefaultSpanScope(span, beforeSpan, beforeSpanScope, tracerContextStorage);
return newSpanScope;
}

Expand All @@ -61,8 +69,8 @@ public SpanScope attach() {

private void detach() {
spanScopeThreadLocal.set(previousSpanScope);
if (previousSpanScope != null) {
tracerContextStorage.put(TracerContextStorage.CURRENT_SPAN, previousSpanScope.getSpan());
if (beforeSpan != null) {
tracerContextStorage.put(TracerContextStorage.CURRENT_SPAN, beforeSpan);
} else {
tracerContextStorage.put(TracerContextStorage.CURRENT_SPAN, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,87 @@ public void run() {
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(nullValue()));
}

public void testNoThreadContextToPreserve() throws InterruptedException, ExecutionException, TimeoutException {
final Runnable r = new Runnable() {
@Override
public void run() {
assertThat(threadContext.getTransient(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(nullValue()));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(nullValue()));

final Span local1 = tracer.startSpan(SpanCreationContext.internal().name("test-local-1"));
try (SpanScope localScope = tracer.withSpanInScope(local1)) {
try (StoredContext ignored = threadContext.stashContext()) {
assertThat(local1.getParentSpan(), is(nullValue()));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(local1));
}
}

final Span local2 = tracer.startSpan(SpanCreationContext.internal().name("test-local-2"));
try (SpanScope localScope = tracer.withSpanInScope(local2)) {
try (StoredContext ignored = threadContext.stashContext()) {
assertThat(local2.getParentSpan(), is(nullValue()));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(local2));
}
}

final Span local3 = tracer.startSpan(SpanCreationContext.internal().name("test-local-3"));
try (SpanScope localScope = tracer.withSpanInScope(local3)) {
try (StoredContext ignored = threadContext.stashContext()) {
assertThat(local3.getParentSpan(), is(nullValue()));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(local3));
}
}
}
};

executorService.submit(threadContext.preserveContext(r)).get(1, TimeUnit.SECONDS);

assertThat(threadContext.getTransient(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(nullValue()));
}

public void testPreservingContextThreadContextMultipleSpans() throws InterruptedException, ExecutionException, TimeoutException {
final Span span = tracer.startSpan(SpanCreationContext.internal().name("test"));

try (SpanScope scope = tracer.withSpanInScope(span)) {
final Runnable r = new Runnable() {
@Override
public void run() {
assertThat(threadContext.getTransient(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(not(nullValue())));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(span));

final Span local1 = tracer.startSpan(SpanCreationContext.internal().name("test-local-1"));
try (SpanScope localScope = tracer.withSpanInScope(local1)) {
try (StoredContext ignored = threadContext.stashContext()) {
assertThat(local1.getParentSpan(), is(span));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(local1));
}
}

final Span local2 = tracer.startSpan(SpanCreationContext.internal().name("test-local-2"));
try (SpanScope localScope = tracer.withSpanInScope(local2)) {
try (StoredContext ignored = threadContext.stashContext()) {
assertThat(local2.getParentSpan(), is(span));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(local2));
}
}

final Span local3 = tracer.startSpan(SpanCreationContext.internal().name("test-local-3"));
try (SpanScope localScope = tracer.withSpanInScope(local3)) {
try (StoredContext ignored = threadContext.stashContext()) {
assertThat(local3.getParentSpan(), is(span));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(local3));
}
}
}
};

executorService.submit(threadContext.preserveContext(r)).get(1, TimeUnit.SECONDS);
}

assertThat(threadContext.getTransient(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(not(nullValue())));
assertThat(threadContextStorage.get(ThreadContextBasedTracerContextStorage.CURRENT_SPAN), is(nullValue()));
}

public void testPreservingContextAndStashingThreadContext() throws InterruptedException, ExecutionException, TimeoutException {
final Span span = tracer.startSpan(SpanCreationContext.internal().name("test"));

Expand Down
Loading