|
| 1 | +/* |
| 2 | + * Copyright 2013-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.sleuth.brave.bridge; |
| 18 | + |
| 19 | +import brave.context.slf4j.MDCScopeDecorator; |
| 20 | +import brave.propagation.ThreadLocalCurrentTraceContext; |
| 21 | +import brave.propagation.TraceContext; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.slf4j.MDC; |
| 24 | + |
| 25 | +import org.springframework.cloud.sleuth.CurrentTraceContext; |
| 26 | + |
| 27 | +import static org.assertj.core.api.BDDAssertions.then; |
| 28 | + |
| 29 | +class BraveCurrentTraceContextTests { |
| 30 | + |
| 31 | + ThreadLocalCurrentTraceContext currentTraceContext = ThreadLocalCurrentTraceContext.newBuilder() |
| 32 | + .addScopeDecorator(MDCScopeDecorator.newBuilder().build()).build(); |
| 33 | + |
| 34 | + @Test |
| 35 | + void should_clear_any_thread_locals_and_scopes_when_null_context_passed_to_new_scope() { |
| 36 | + BraveCurrentTraceContext braveCurrentTraceContext = new BraveCurrentTraceContext(currentTraceContext); |
| 37 | + brave.propagation.CurrentTraceContext.Scope newScope = currentTraceContext |
| 38 | + .newScope(TraceContext.newBuilder().traceId(12345678).spanId(12345678).build()); |
| 39 | + then(currentTraceContext.get()).isNotNull(); |
| 40 | + |
| 41 | + CurrentTraceContext.Scope scope = braveCurrentTraceContext.newScope(null); |
| 42 | + |
| 43 | + thenThreadLocalsGotCleared(braveCurrentTraceContext, scope); |
| 44 | + newScope.close(); |
| 45 | + thenThreadLocalsGotCleared(braveCurrentTraceContext, scope); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void should_clear_any_thread_locals_and_scopes_when_null_context_passed_to_new_scope_with_nested_scopes() { |
| 50 | + BraveCurrentTraceContext braveCurrentTraceContext = new BraveCurrentTraceContext(currentTraceContext); |
| 51 | + |
| 52 | + try (CurrentTraceContext.Scope scope1 = braveCurrentTraceContext.newScope( |
| 53 | + BraveTraceContext.fromBrave(TraceContext.newBuilder().traceId(12345678).spanId(12345670).build()))) { |
| 54 | + then(currentTraceContext.get()).isNotNull(); |
| 55 | + thenMdcEntriesArePresent(); |
| 56 | + try (CurrentTraceContext.Scope scope2 = braveCurrentTraceContext.newScope(BraveTraceContext |
| 57 | + .fromBrave(TraceContext.newBuilder().traceId(12345678).spanId(12345671).build()))) { |
| 58 | + then(currentTraceContext.get()).isNotNull(); |
| 59 | + thenMdcEntriesArePresent(); |
| 60 | + try (CurrentTraceContext.Scope scope3 = braveCurrentTraceContext.newScope(BraveTraceContext |
| 61 | + .fromBrave(TraceContext.newBuilder().traceId(12345678).spanId(12345672).build()))) { |
| 62 | + then(currentTraceContext.get()).isNotNull(); |
| 63 | + thenMdcEntriesArePresent(); |
| 64 | + try (CurrentTraceContext.Scope nullScope = braveCurrentTraceContext.newScope(null)) { |
| 65 | + // This closes all scopes and MDC entries |
| 66 | + then(currentTraceContext.get()).isNull(); |
| 67 | + } |
| 68 | + // We have nothing to revert to since the nullScope is ignoring |
| 69 | + // everything there was before |
| 70 | + then(currentTraceContext.get()).isNull(); |
| 71 | + thenMdcEntriesAreMissing(); |
| 72 | + } |
| 73 | + then(currentTraceContext.get()).isNull(); |
| 74 | + thenMdcEntriesAreMissing(); |
| 75 | + } |
| 76 | + then(currentTraceContext.get()).isNull(); |
| 77 | + thenMdcEntriesAreMissing(); |
| 78 | + } |
| 79 | + |
| 80 | + then(currentTraceContext.get()).isNull(); |
| 81 | + then(braveCurrentTraceContext.scopes.get()).isNull(); |
| 82 | + then(MDC.getCopyOfContextMap()).isEmpty(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void should_clear_any_thread_locals_and_scopes_when_null_context_passed_to_maybe_scope_with_nested_scopes() { |
| 87 | + BraveCurrentTraceContext braveCurrentTraceContext = new BraveCurrentTraceContext(currentTraceContext); |
| 88 | + |
| 89 | + try (CurrentTraceContext.Scope scope1 = braveCurrentTraceContext.maybeScope( |
| 90 | + BraveTraceContext.fromBrave(TraceContext.newBuilder().traceId(12345678).spanId(12345670).build()))) { |
| 91 | + then(currentTraceContext.get()).isNotNull(); |
| 92 | + thenMdcEntriesArePresent(); |
| 93 | + try (CurrentTraceContext.Scope scope2 = braveCurrentTraceContext.maybeScope(BraveTraceContext |
| 94 | + .fromBrave(TraceContext.newBuilder().traceId(12345678).spanId(12345671).build()))) { |
| 95 | + then(currentTraceContext.get()).isNotNull(); |
| 96 | + thenMdcEntriesArePresent(); |
| 97 | + try (CurrentTraceContext.Scope scope3 = braveCurrentTraceContext.maybeScope(BraveTraceContext |
| 98 | + .fromBrave(TraceContext.newBuilder().traceId(12345678).spanId(12345672).build()))) { |
| 99 | + then(currentTraceContext.get()).isNotNull(); |
| 100 | + thenMdcEntriesArePresent(); |
| 101 | + try (CurrentTraceContext.Scope nullScope = braveCurrentTraceContext.maybeScope(null)) { |
| 102 | + // This closes all scopes and MDC entries |
| 103 | + then(currentTraceContext.get()).isNull(); |
| 104 | + } |
| 105 | + // We have nothing to revert to since the nullScope is ignoring |
| 106 | + // everything there was before |
| 107 | + then(currentTraceContext.get()).isNull(); |
| 108 | + thenMdcEntriesAreMissing(); |
| 109 | + } |
| 110 | + then(currentTraceContext.get()).isNull(); |
| 111 | + thenMdcEntriesAreMissing(); |
| 112 | + } |
| 113 | + then(currentTraceContext.get()).isNull(); |
| 114 | + thenMdcEntriesAreMissing(); |
| 115 | + } |
| 116 | + |
| 117 | + then(currentTraceContext.get()).isNull(); |
| 118 | + then(braveCurrentTraceContext.scopes.get()).isNull(); |
| 119 | + then(MDC.getCopyOfContextMap()).isEmpty(); |
| 120 | + } |
| 121 | + |
| 122 | + private static void thenMdcEntriesArePresent() { |
| 123 | + then(MDC.get("traceId")).isEqualTo("0000000000bc614e"); |
| 124 | + then(MDC.get("spanId")).isNotEmpty(); |
| 125 | + } |
| 126 | + |
| 127 | + private static void thenMdcEntriesAreMissing() { |
| 128 | + then(MDC.getCopyOfContextMap()).isEmpty(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void should_clear_any_thread_locals_and_scopes_when_null_context_passed_to_maybe_scope() { |
| 133 | + BraveCurrentTraceContext braveCurrentTraceContext = new BraveCurrentTraceContext(currentTraceContext); |
| 134 | + brave.propagation.CurrentTraceContext.Scope maybeScope = currentTraceContext |
| 135 | + .newScope(TraceContext.newBuilder().traceId(12345678).spanId(12345678).build()); |
| 136 | + then(currentTraceContext.get()).isNotNull(); |
| 137 | + |
| 138 | + CurrentTraceContext.Scope scope = braveCurrentTraceContext.maybeScope(null); |
| 139 | + |
| 140 | + thenThreadLocalsGotCleared(braveCurrentTraceContext, scope); |
| 141 | + maybeScope.close(); |
| 142 | + thenThreadLocalsGotCleared(braveCurrentTraceContext, scope); |
| 143 | + } |
| 144 | + |
| 145 | + private void thenThreadLocalsGotCleared(BraveCurrentTraceContext braveCurrentTraceContext, |
| 146 | + CurrentTraceContext.Scope scope) { |
| 147 | + then(scope).isSameAs(CurrentTraceContext.Scope.NOOP); |
| 148 | + then(currentTraceContext.get()).isNull(); |
| 149 | + then(braveCurrentTraceContext.scopes.get()).isNull(); |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments