Repository navigation
HDDS-13679. Add trace and span IDs to log MDC via OpenTelemetry ContextStorageProvider #11607
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
Merged
chihsuan
merged 10 commits into
apache:master
from
rajeshkumarchandolu:observabilityImporvements
Oct 29, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2030f70
Add trace and span IDs to log MDC via OpenTelemetry ContextStoragePro…
rajeshkumarchandolu 8318652
Revert log4j pattern change; trace IDs remain available via %X{trace_…
rajeshkumarchandolu 84c8640
changing the test-cases as per the comments
rajeshkumarchandolu 8479ce8
Install MDC trace context only in Ozone services and CLIs, not in cli…
rajeshkumarchandolu 7192157
Add tests for MDC context storage installed through TracingUtil
rajeshkumarchandolu 23140e5
Install MDC context storage before OM and SCM init and bootstrap
rajeshkumarchandolu 591553e
Move MDC context storage install from TracingReconfigurationCallback …
rajeshkumarchandolu ddb442f
Avoid OTLP export retries in TestMdcContextStorageInstall when no col…
rajeshkumarchandolu d075f11
fix for the test failure
rajeshkumarchandolu 6e58d54
checkstyle fix
rajeshkumarchandolu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...dds/common/src/test/java/org/apache/hadoop/hdds/tracing/TestMdcContextStorageInstall.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * 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.hadoop.hdds.tracing; | ||
|
|
||
| import static org.apache.hadoop.hdds.tracing.TracingUtil.MdcContextStorage.MDC_SPAN_ID; | ||
| import static org.apache.hadoop.hdds.tracing.TracingUtil.MdcContextStorage.MDC_TRACE_ID; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.opentelemetry.api.GlobalOpenTelemetry; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.ContextStorage; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
| import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; | ||
| import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import org.apache.hadoop.hdds.conf.InMemoryConfigurationForTesting; | ||
| import org.apache.hadoop.hdds.conf.MutableConfigurationSource; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.MDC; | ||
|
|
||
| /** | ||
| * Tests {@link TracingUtil#installMdcContextStorage()}. | ||
| * When running together with other test classes, use fork mode per class. | ||
| */ | ||
| public class TestMdcContextStorageInstall { | ||
|
|
||
| @BeforeAll | ||
| public static void install() { | ||
| TracingUtil.installMdcContextStorage(); | ||
| assertInstanceOf(TracingUtil.MdcContextStorage.class, ContextStorage.get(), "use fork mode per class"); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| TracingUtil.shutdownTracing(); | ||
| GlobalOpenTelemetry.resetForTest(); | ||
| MDC.clear(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| TracingUtil.shutdownTracing(); | ||
| GlobalOpenTelemetry.resetForTest(); | ||
| MDC.clear(); | ||
| } | ||
|
|
||
| private static void initTracing(boolean enabled) { | ||
|
rajeshkumarchandolu marked this conversation as resolved.
Resolved
|
||
| SdkTracerProvider provider = SdkTracerProvider.builder() | ||
| .addSpanProcessor(SimpleSpanProcessor.create(SpanExporter.noop())).build(); | ||
| OpenTelemetrySdk sdk = OpenTelemetrySdk.builder().setTracerProvider(provider).build(); | ||
| GlobalOpenTelemetry.set(sdk); | ||
| MutableConfigurationSource conf = new InMemoryConfigurationForTesting(); | ||
| conf.setBoolean("ozone.tracing.enabled", enabled); | ||
| TracingUtil.initTracing("mdc-install", conf); | ||
| } | ||
|
|
||
| private static void assertMdcMatchesCurrentSpan() { | ||
| SpanContext current = Span.current().getSpanContext(); | ||
| assertTrue(current.isValid()); | ||
| assertEquals(current.getTraceId(), MDC.get(MDC_TRACE_ID)); | ||
| assertEquals(current.getSpanId(), MDC.get(MDC_SPAN_ID)); | ||
| } | ||
|
|
||
| private static void assertMdcEmpty() { | ||
| assertNull(MDC.get(MDC_TRACE_ID)); | ||
| assertNull(MDC.get(MDC_SPAN_ID)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testActivatedSpanSetsMdc() { | ||
| initTracing(true); | ||
| try (TracingUtil.TraceCloseable ignored = TracingUtil.createActivatedSpan("outer")) { | ||
| assertMdcMatchesCurrentSpan(); | ||
| String traceId = MDC.get(MDC_TRACE_ID); | ||
| String outerSpanId = MDC.get(MDC_SPAN_ID); | ||
| try (TracingUtil.TraceCloseable ignoredInner = TracingUtil.createActivatedSpan("inner")) { | ||
| assertMdcMatchesCurrentSpan(); | ||
| assertEquals(traceId, MDC.get(MDC_TRACE_ID)); | ||
| assertNotEquals(outerSpanId, MDC.get(MDC_SPAN_ID)); | ||
| } | ||
| assertEquals(traceId, MDC.get(MDC_TRACE_ID)); | ||
| assertEquals(outerSpanId, MDC.get(MDC_SPAN_ID)); | ||
| } | ||
| assertMdcEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testImportedParentSpanSetsMdc() { | ||
| initTracing(true); | ||
| String carrier; | ||
| String parentTraceId; | ||
| try (TracingUtil.TraceCloseable ignored = TracingUtil.createActivatedSpan("client")) { | ||
| carrier = TracingUtil.exportCurrentSpan(); | ||
| parentTraceId = Span.current().getSpanContext().getTraceId(); | ||
| } | ||
| assertMdcEmpty(); | ||
|
|
||
| Span server = TracingUtil.importAndCreateSpan("server", carrier); | ||
| try (Scope ignored = server.makeCurrent()) { | ||
| assertMdcMatchesCurrentSpan(); | ||
| assertEquals(parentTraceId, MDC.get(MDC_TRACE_ID)); | ||
| } finally { | ||
| server.end(); | ||
| } | ||
| assertMdcEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testContextWrapPropagatesMdcToOtherThread() throws Exception { | ||
| initTracing(true); | ||
| ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
| try { | ||
| try (TracingUtil.TraceCloseable ignored = TracingUtil.createActivatedSpan("span")) { | ||
| String[] inTask = executor.submit(Context.current().wrap( | ||
| () -> new String[]{MDC.get(MDC_TRACE_ID), MDC.get(MDC_SPAN_ID)})).get(); | ||
| assertEquals(MDC.get(MDC_TRACE_ID), inTask[0]); | ||
| assertEquals(MDC.get(MDC_SPAN_ID), inTask[1]); | ||
| } | ||
| assertNull(executor.submit(() -> MDC.get(MDC_TRACE_ID)).get()); | ||
| assertNull(executor.submit(() -> MDC.get(MDC_SPAN_ID)).get()); | ||
| } finally { | ||
| executor.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testTracingDisabledLeavesMdcEmpty() { | ||
| initTracing(false); | ||
| try (TracingUtil.TraceCloseable ignored = TracingUtil.createActivatedSpan("span")) { | ||
| assertMdcEmpty(); | ||
| } | ||
| assertMdcEmpty(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.