chetanmeh commented on a change in pull request #2282: Distributed tracing 
support #2192
URL: 
https://github.com/apache/incubator-openwhisk/pull/2282#discussion_r190802858
 
 

 ##########
 File path: 
common/scala/src/main/scala/whisk/common/tracing/OpenTracingProvider.scala
 ##########
 @@ -0,0 +1,198 @@
+/*
+ * 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 whisk.common.tracing
+
+import scala.collection.concurrent.TrieMap
+import scala.collection.mutable
+
+import brave.Tracing
+import brave.opentracing.BraveTracer
+import io.opentracing.{ActiveSpan, SpanContext}
+import io.opentracing.util.GlobalTracer
+import io.opentracing.propagation.{Format, TextMapExtractAdapter, 
TextMapInjectAdapter}
+import zipkin.reporter.Sender
+import zipkin.reporter.okhttp3.OkHttpSender
+import zipkin.reporter.AsyncReporter
+import zipkin.reporter.Reporter
+import pureconfig._
+import whisk.common.{LogMarkerToken, TransactionId}
+import whisk.core.ConfigKeys
+
+/**
+ * OpenTracing based implementation for tracing
+ */
+object OpenTracingProvider {
+
+  private val spanMap: mutable.Map[String, mutable.ListBuffer[ActiveSpan]] =
+    TrieMap[String, mutable.ListBuffer[ActiveSpan]]()
+  private val contextMap: mutable.Map[String, SpanContext] = TrieMap[String, 
SpanContext]()
+
+  var enabled = false;
+
+  def apply(serviceName: String): Unit = {
+    configureTracer(serviceName)
+  }
+
+  /**
+   * Start a Trace for given service.
+   *
+   * @param transactionId transactionId to which this Trace belongs.
+   * @return TracedRequest which provides details about current service being 
traced.
+   */
+  def startTrace(logMarker: LogMarkerToken, transactionId: TransactionId): 
Unit = {
+    if (enabled) {
+      var activeSpan: Option[ActiveSpan] = None
+      spanMap.get(transactionId.meta.id) match {
+        case Some(spanList) => {
+          //create a child trace
+          activeSpan = Some(
+            GlobalTracer
+              .get()
+              .buildSpan(logMarker.action)
+              .withTag("transactionId", transactionId.meta.id)
+              .asChildOf(spanList.last)
+              .startActive())
+        }
+        case None => {
+          contextMap.get(transactionId.meta.id) match {
+            case Some(context) => {
+              //create child trace if we have a tracing context
+              activeSpan = 
Some(GlobalTracer.get().buildSpan(logMarker.action).asChildOf(context).startActive())
+            }
+            case None => {
+              activeSpan = 
Some(GlobalTracer.get().buildSpan(logMarker.action).ignoreActiveSpan().startActive())
+            }
+          }
+          //initialize list for this transactionId
+          val list: mutable.ListBuffer[ActiveSpan] = mutable.ListBuffer()
+          spanMap.put(transactionId.meta.id, list)
+        }
+      }
+
+      //add active span to list
+      if (activeSpan.isDefined)
+        spanMap.get(transactionId.meta.id).map(_.+=:(activeSpan.get))
+    }
+  }
+
+  /**
+   * Finish a Trace associated with given transactionId.
+   *
+   * @param transactionId
+   */
+  def finish(logMarker: LogMarkerToken, transactionId: TransactionId): Unit = {
+    if (enabled)
+      clear(transactionId)
+  }
+
+  /**
+   * Register error
+   *
+   * @param transactionId
+   */
+  def error(transactionId: TransactionId): Unit = {
+    if (enabled)
+      clear(transactionId)
+  }
+
+  /**
+   * Get the current TraceContext which can be used for downstream services
+   *
+   * @param transactionId
+   * @return
+   */
+  def getTraceContext(transactionId: TransactionId): Option[Map[String, 
String]] = {
+    var contextMap: Option[Map[String, String]] = None
+    if (enabled) {
+      spanMap.get(transactionId.meta.id) match {
+        case Some(spanList) => {
+          var map: java.util.Map[String, String] = new java.util.HashMap()
+          //inject latest span context in map
+          GlobalTracer.get().inject(spanList.last.context(), 
Format.Builtin.TEXT_MAP, new TextMapInjectAdapter(map))
+          contextMap = 
Some(scala.collection.JavaConverters.mapAsScalaMapConverter(map).asScala.toMap)
+        }
+        case None => None
+      }
+    }
+    contextMap
+  }
+
+  /**
+   * Get the current TraceContext which can be used for downstream services
+   *
+   * @param transactionId
+   * @return
+   */
+  def setTraceContext(transactionId: TransactionId, context: 
Option[Map[String, String]]) = {
+    if (enabled) {
+      context match {
+        case Some(scalaMap) => {
+          var javaMap: java.util.Map[String, String] =
+            
scala.collection.JavaConverters.mapAsJavaMapConverter(scalaMap).asJava
+          var ctx: SpanContext = 
GlobalTracer.get().extract(Format.Builtin.TEXT_MAP, new 
TextMapExtractAdapter(javaMap))
+          contextMap.put(transactionId.meta.id, ctx)
+        }
+        case None =>
+      }
+    }
+  }
+
+  def clear(transactionId: TransactionId): Unit = {
 
 Review comment:
   Mark internal methods as `private`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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

Reply via email to