sreejasahithi commented on code in PR #10077:
URL: https://github.com/apache/ozone/pull/10077#discussion_r3084560463


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/tracing/TracingUtil.java:
##########
@@ -253,16 +289,20 @@ static Map<String, LoopSampler> 
parseSpanSamplingConfig(String configStr) {
    * If a parent span exists in the current context, this becomes a child span.
    */
   public static <E extends Exception> void executeInNewSpan(String spanName,
-      CheckedRunnable<E> runnable) throws E {
+                                                            CheckedRunnable<E> 
runnable) throws E {
+    if (shouldByPassSpanCreation()) {
+      runnable.run();
+      return;
+    }
     Span span = buildSpan(spanName);
     executeInSpan(span, runnable);
   }
 
-  /**
-   * Execute {@code supplier} inside an activated new span.
-   */
   public static <R, E extends Exception> R executeInNewSpan(String spanName,
-      CheckedSupplier<R, E> supplier) throws E {
+                                                            CheckedSupplier<R, 
E> supplier) throws E {

Review Comment:
   same here



##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/tracing/TracingUtil.java:
##########
@@ -253,16 +289,20 @@ static Map<String, LoopSampler> 
parseSpanSamplingConfig(String configStr) {
    * If a parent span exists in the current context, this becomes a child span.
    */
   public static <E extends Exception> void executeInNewSpan(String spanName,
-      CheckedRunnable<E> runnable) throws E {
+                                                            CheckedRunnable<E> 
runnable) throws E {

Review Comment:
   please provide the proper spacing



##########
hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/tracing/TestTracingUtilClientApplicationAware.java:
##########
@@ -0,0 +1,207 @@
+/*
+ * 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.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+import io.opentelemetry.api.GlobalOpenTelemetry;
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.api.trace.Tracer;
+import io.opentelemetry.context.Scope;
+import io.opentelemetry.sdk.OpenTelemetrySdk;
+import io.opentelemetry.sdk.common.CompletableResultCode;
+import io.opentelemetry.sdk.trace.SdkTracerProvider;
+import io.opentelemetry.sdk.trace.data.SpanData;
+import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
+import io.opentelemetry.sdk.trace.export.SpanExporter;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+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.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests client application-aware tracing: when Ozone OTLP tracing is off but 
an
+ * active span exists, spans use {@link GlobalOpenTelemetry}; otherwise the
+ * static Ozone {@link TracingUtil} tracer is used, or span creation is 
skipped.
+ */
+public class TestTracingUtilClientApplicationAware {
+
+  private static final String OZONE_CLIENT_TRACER_SCOPE = 
"org.apache.hadoop.ozone.client";
+
+  private final List<SpanData> exportedSpans = new CopyOnWriteArrayList<>();
+  private SdkTracerProvider testSdkTracerProvider;
+
+  @BeforeEach
+  public void setUp() {
+    GlobalOpenTelemetry.resetForTest();
+    TracingUtil.reconfigureTracing("client", clientTracingConfig(false, true));
+  }
+
+  @AfterEach
+  public void tearDown() {
+    if (testSdkTracerProvider != null) {
+      testSdkTracerProvider.shutdown();
+      testSdkTracerProvider = null;
+    }
+    GlobalOpenTelemetry.resetForTest();
+    exportedSpans.clear();
+    TracingUtil.reconfigureTracing("client", clientTracingConfig(false, true));
+  }
+
+  private static TracingConfig clientTracingConfig(boolean tracingEnabled,
+                                                   boolean 
clientApplicationAware) {
+    MutableConfigurationSource conf = new InMemoryConfigurationForTesting();
+    conf.setBoolean("ozone.tracing.enabled", tracingEnabled);
+    conf.setBoolean("ozone.tracing.client.application-aware", 
clientApplicationAware);
+    return conf.getObject(TracingConfig.class);
+  }
+
+  /**
+   * Registers a global SDK that records ended spans into {@link 
#exportedSpans}.
+   */
+  private void registerGlobalTestSdk() {
+    SpanExporter exporter = new SpanExporter() {
+      @Override
+      public CompletableResultCode export(Collection<SpanData> spans) {
+        exportedSpans.addAll(spans);
+        return CompletableResultCode.ofSuccess();
+      }
+
+      @Override
+      public CompletableResultCode flush() {
+        return CompletableResultCode.ofSuccess();
+      }
+
+      @Override
+      public CompletableResultCode shutdown() {
+        return CompletableResultCode.ofSuccess();
+      }
+    };
+    testSdkTracerProvider = SdkTracerProvider.builder()
+        .addSpanProcessor(SimpleSpanProcessor.create(exporter))
+        .build();
+    GlobalOpenTelemetry.set(
+        OpenTelemetrySdk.builder()
+            .setTracerProvider(testSdkTracerProvider)
+            .build());
+  }
+
+  private boolean hasExportedSpanNamed(String name) {
+    return exportedSpans.stream().anyMatch(s -> name.equals(s.getName()));
+  }
+
+  @Test
+  public void testOzoneTracingOnUsesStaticTracerNotGlobalOpenTelemetry() 
throws Exception {
+    registerGlobalTestSdk();
+    TracingUtil.reconfigureTracing("client", clientTracingConfig(true, true));
+
+    TracingUtil.executeInNewSpan("ozone-client-span", () -> {
+      assertThat(Span.current().getSpanContext().isValid()).isTrue();
+    });
+
+    assertThat(hasExportedSpanNamed("ozone-client-span"))
+        .withFailMessage("When Ozone client tracing is on, spans must use the 
Ozone SDK tracer, "
+            + "not GlobalOpenTelemetry, so the test global exporter must not 
see them.")
+        .isFalse();
+  }
+
+  @Test
+  public void testOzoneTracingOffWithAppAwareAndParentUsesGlobalTracer() 
throws Exception {
+    registerGlobalTestSdk();
+    TracingUtil.reconfigureTracing("client", clientTracingConfig(false, true));
+
+    Tracer appTracer = GlobalOpenTelemetry.get().getTracer("test-app");
+    Span parent = appTracer.spanBuilder("parent").startSpan();
+    try (Scope ignored = parent.makeCurrent()) {
+      TracingUtil.executeInNewSpan("child-from-global", () ->
+          assertThat(Span.current().getSpanContext().isValid()).isTrue());
+    } finally {
+      parent.end();
+    }
+    testSdkTracerProvider.forceFlush();
+
+    assertThat(hasExportedSpanNamed("child-from-global"))
+        .withFailMessage("With Ozone tracing off, client application-aware on, 
and a valid parent span, "
+            + "the child must be created with GlobalOpenTelemetry.")
+        .isTrue();
+
+    assertThat(exportedSpans)
+        .filteredOn(s -> "child-from-global".equals(s.getName()))
+        .singleElement()
+        .extracting(s -> s.getInstrumentationScopeInfo().getName())
+        .isEqualTo(OZONE_CLIENT_TRACER_SCOPE);

Review Comment:
   you can also add the parent span ID assertion.
   
   something like the following:
   ```
   assertThat(exportedSpans)
       .filteredOn(s -> "child-from-global".equals(s.getName()))
       .singleElement()
       .extracting(SpanData::getParentSpanId)
       .isEqualTo(parent.getSpanContext().getSpanId());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to