shalinmangar commented on a change in pull request #685: SOLR-13434: 
OpenTracing support for Solr
URL: https://github.com/apache/lucene-solr/pull/685#discussion_r288055779
 
 

 ##########
 File path: solr/core/src/java/org/apache/solr/util/tracing/GlobalTracer.java
 ##########
 @@ -0,0 +1,82 @@
+/*
+ * 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.solr.util.tracing;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Random;
+
+import io.opentracing.SpanContext;
+import io.opentracing.Tracer;
+import io.opentracing.noop.NoopTracerFactory;
+import io.opentracing.propagation.Format;
+
+public class GlobalTracer {
+  private static final Random RANDOM;
+  static {
+    // We try to make things reproducible in the context of our tests by 
initializing the random instance
+    // based on the current seed
+    String seed = System.getProperty("tests.seed");
+    if (seed == null) {
+      RANDOM = new Random();
+    } else {
+      RANDOM = new Random(seed.hashCode());
+    }
+  }
+
+  private static final Tracer NOOP_TRACER = NoopTracerFactory.create();
+  private static volatile Tracer TRACER = NOOP_TRACER;
+  private static volatile int rate;
+  private final static ThreadLocal<Tracer> threadLocal = new ThreadLocal<>();
+
+  public static void setup(Tracer tracer, double rate) {
+    GlobalTracer.rate = (int) (rate * 10);
+    GlobalTracer.TRACER = tracer;
+  }
+
+  public static boolean tracing() {
+    return threadLocal.get() != null
+        && threadLocal.get() != NOOP_TRACER
+        && threadLocal.get().activeSpan() != null;
+  }
+
+  public static SpanContext extract(HttpServletRequest request) {
+    SpanContext spanContext = TRACER.extract(Format.Builtin.HTTP_HEADERS, new 
HttpServletCarrier(request));
+    if (spanContext != null) {
+      threadLocal.set(TRACER);
+    }
+    return spanContext;
+  }
+
+  public static Tracer get() {
+    Tracer tracer = threadLocal.get();
+    if (tracer != null)
+      return tracer;
+
+    if (traced()) {
 
 Review comment:
   I think the trace id if it exists should be added to MDC anyway.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org
For additional commands, e-mail: dev-h...@lucene.apache.org

Reply via email to