lfrancke commented on code in PR #2586:
URL: https://github.com/apache/phoenix/pull/2586#discussion_r3791359802


##########
phoenix-core-client/src/main/java/org/apache/phoenix/trace/PhoenixTracing.java:
##########
@@ -0,0 +1,385 @@
+/*
+ * 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.phoenix.trace;
+
+import io.opentelemetry.api.GlobalOpenTelemetry;
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.api.trace.SpanKind;
+import io.opentelemetry.api.trace.StatusCode;
+import io.opentelemetry.api.trace.Tracer;
+import io.opentelemetry.context.Context;
+import io.opentelemetry.context.Scope;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+import org.apache.phoenix.call.CallWrapper;
+import org.apache.phoenix.job.JobManager.JobCallable;
+import org.apache.phoenix.monitoring.TaskExecutionMetricsHolder;
+
+import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions;
+
+/**
+ * Central tracing facade for Apache Phoenix using OpenTelemetry. All methods 
are no-ops with zero
+ * overhead when no OpenTelemetry SDK is configured.
+ * <p>
+ * Adapted from Apache HBase's {@code org.apache.hadoop.hbase.trace.TraceUtil} 
(HBASE-22120). The
+ * span helpers, the future and runnable wrappers, and the throwing functional 
interfaces are taken
+ * from it largely verbatim.
+ * @see <a 
href="https://issues.apache.org/jira/browse/PHOENIX-5215";>PHOENIX-5215</a>
+ */
+public final class PhoenixTracing {
+
+  private static final String INSTRUMENTATION_NAME = "org.apache.phoenix";
+
+  private PhoenixTracing() {
+  }
+
+  /**
+   * Returns the global tracer for Phoenix. The tracer is obtained from {@link 
GlobalOpenTelemetry}
+   * on each call (the OTel SDK caches it internally, so there is no 
performance penalty). This
+   * avoids issues with eager initialization when the SDK has not been 
configured yet.
+   */
+  public static Tracer getTracer() {
+    return GlobalOpenTelemetry.getTracer(INSTRUMENTATION_NAME);
+  }
+
+  /**
+   * Create a {@link SpanKind#INTERNAL} span. This is the default for most 
Phoenix operations (query
+   * compilation, mutation processing, index maintenance, etc.).
+   */
+  public static Span createSpan(String name) {
+    return createSpan(name, SpanKind.INTERNAL);
+  }
+
+  /**
+   * Create a span with the given {@code kind}. Note that OpenTelemetry 
expects at most one
+   * {@link SpanKind#CLIENT} span and one {@link SpanKind#SERVER} span per 
traced request, so use
+   * this with caution for kinds other than {@link SpanKind#INTERNAL}.
+   */
+  private static Span createSpan(String name, SpanKind kind) {
+    return getTracer().spanBuilder(name).setSpanKind(kind).startSpan();
+  }

Review Comment:
   The docs were wrong, that's true. But other than that this is a port from 
the HBase version.
   
   Server side this makes no difference as HBase already creates a span, but 
client side it runs _wherever_ and in those cases it does make sense to start 
something.
   
   We've chosen to fix the javadoc instead of changing the behavior.



-- 
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]

Reply via email to