Skip to content
Open
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 @@ -21,6 +21,7 @@
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationHandler;
import io.micrometer.tracing.Tracer;
import org.slf4j.MDC;

public class DubboClientTracingObservationHandler<T extends DubboClientContext> implements ObservationHandler<T> {
private final Tracer tracer;
Expand All @@ -30,7 +31,27 @@ public DubboClientTracingObservationHandler(Tracer tracer) {
}

@Override
public void onScopeOpened(T context) {}
public void onScopeOpened(T context) {
io.micrometer.tracing.TraceContext traceContext =
tracer.currentTraceContext().context();
if (traceContext == null) {
return;
}
try {
MDC.put("traceId", traceContext.traceId());
MDC.put("spanId", traceContext.spanId());
} catch (Throwable ignored) {
}
Comment on lines +43 to +44
}

@Override
public void onScopeClosed(T context) {
try {
MDC.remove("traceId");
MDC.remove("spanId");
} catch (Throwable ignored) {
}
Comment on lines +48 to +53
}

@Override
public boolean supportsContext(Observation.Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.micrometer.observation.ObservationHandler;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.Tracer;
import org.slf4j.MDC;

public class DubboServerTracingObservationHandler<T extends DubboServerContext> implements ObservationHandler<T> {

Expand All @@ -41,6 +42,20 @@ public void onScopeOpened(T context) {
return;
}
RpcContext.getServerContext().setAttachment(DEFAULT_TRACE_ID_KEY, traceContext.traceId());
try {
MDC.put(DEFAULT_TRACE_ID_KEY, traceContext.traceId());
MDC.put("spanId", traceContext.spanId());
} catch (Throwable ignored) {
}
Comment on lines +48 to +49
}

@Override
public void onScopeClosed(T context) {
try {
MDC.remove(DEFAULT_TRACE_ID_KEY);
MDC.remove("spanId");
} catch (Throwable ignored) {
}
Comment on lines +53 to +58
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.tracing.handler;

import org.apache.dubbo.tracing.context.DubboClientContext;

import io.micrometer.tracing.CurrentTraceContext;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.Tracer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.MDC;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when;

class DubboClientTracingObservationHandlerTest {

private Tracer mockTracer;
private CurrentTraceContext mockCurrentTraceContext;
private TraceContext mockTraceContext;
private DubboClientContext mockContext;
private DubboClientTracingObservationHandler<DubboClientContext> handler;

@BeforeEach
void setUp() {
// Clear MDC before each test to ensure a clean state
MDC.clear();

mockTracer = Mockito.mock(Tracer.class);
mockCurrentTraceContext = Mockito.mock(CurrentTraceContext.class);
mockTraceContext = Mockito.mock(TraceContext.class);
mockContext = Mockito.mock(DubboClientContext.class);

when(mockTracer.currentTraceContext()).thenReturn(mockCurrentTraceContext);
when(mockCurrentTraceContext.context()).thenReturn(mockTraceContext);
when(mockTraceContext.traceId()).thenReturn("mock-trace-id-123");
when(mockTraceContext.spanId()).thenReturn("mock-span-id-456");

handler = new DubboClientTracingObservationHandler<>(mockTracer);
}

@AfterEach
void tearDown() {
MDC.clear();
}

@Test
void proveWorking_WhenScopeOpens_MdcShouldHaveIds() {
// 1. Verify MDC is currently empty
assertNull(MDC.get("traceId"));
assertNull(MDC.get("spanId"));

handler.onScopeOpened(mockContext);

assertEquals("mock-trace-id-123", MDC.get("traceId"), "Trace ID should be mapped to MDC!");
assertEquals("mock-span-id-456", MDC.get("spanId"), "Span ID should be mapped to MDC!");
}

@Test
void proveFailurePrevention_WhenScopeCloses_MdcShouldBeCleaned() {
handler.onScopeOpened(mockContext);
assertEquals("mock-trace-id-123", MDC.get("traceId"));

handler.onScopeClosed(mockContext);

// PROOF THE LEAK IS PREVENTED: MDC must be completely empty now.
assertNull(MDC.get("traceId"), "Trace ID leaked! It was not removed when scope closed.");
assertNull(MDC.get("spanId"), "Span ID leaked! It was not removed when scope closed.");
}
Comment on lines +77 to +87
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.tracing.handler;

import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.tracing.context.DubboServerContext;

import io.micrometer.tracing.CurrentTraceContext;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.Tracer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.MDC;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when;

class DubboServerTracingObservationHandlerTest {

private Tracer mockTracer;
private CurrentTraceContext mockCurrentTraceContext;
private TraceContext mockTraceContext;
private DubboServerContext mockContext;
private DubboServerTracingObservationHandler<DubboServerContext> handler;

@BeforeEach
void setUp() {
// Clear MDC before each test to ensure a clean state
MDC.clear();

mockTracer = Mockito.mock(Tracer.class);
mockCurrentTraceContext = Mockito.mock(CurrentTraceContext.class);
mockTraceContext = Mockito.mock(TraceContext.class);
mockContext = Mockito.mock(DubboServerContext.class);

when(mockTracer.currentTraceContext()).thenReturn(mockCurrentTraceContext);
when(mockCurrentTraceContext.context()).thenReturn(mockTraceContext);
when(mockTraceContext.traceId()).thenReturn("mock-trace-id-123");
when(mockTraceContext.spanId()).thenReturn("mock-span-id-456");

handler = new DubboServerTracingObservationHandler<>(mockTracer);
}

@AfterEach
void tearDown() {
MDC.clear();
RpcContext.removeServerContext();
}

@Test
void proveWorking_WhenScopeOpens_MdcShouldHaveIds() {
// 1. Verify MDC is currently empty
assertNull(MDC.get("traceId"));
assertNull(MDC.get("spanId"));

handler.onScopeOpened(mockContext);

assertEquals("mock-trace-id-123", MDC.get("traceId"), "Trace ID should be mapped to MDC!");
assertEquals("mock-span-id-456", MDC.get("spanId"), "Span ID should be mapped to MDC!");
}

@Test
void proveFailurePrevention_WhenScopeCloses_MdcShouldBeCleaned() {
handler.onScopeOpened(mockContext);
assertEquals("mock-trace-id-123", MDC.get("traceId"));

handler.onScopeClosed(mockContext);

// PROOF THE LEAK IS PREVENTED: MDC must be completely empty now.
assertNull(MDC.get("traceId"), "Trace ID leaked! It was not removed when scope closed.");
assertNull(MDC.get("spanId"), "Span ID leaked! It was not removed when scope closed.");
}
Comment on lines +79 to +89
}
Loading