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

 ##########
 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))
+    }
 
 Review comment:
   Avoid using `var` as much as possible. For example here it would be better 
to have `spanMap` use immutable list as values instead of buffer. And then 
above logic can be rewritten as below. Note here we add to head of list. So 
later in clear we need to remove from head
   
   ```scala
     override def startTrace(logMarker: LogMarkerToken, transactionId: 
TransactionId): Unit = {
       val spanList = spanMap.getOrElse(transactionId.meta.id, Nil)
       val activeSpan = spanList match {
         case Nil => getContextSpan(logMarker, transactionId)
         case _ =>
           tracer
             .buildSpan(logMarker.action)
             .withTag("transactionId", transactionId.meta.id)
             .asChildOf(spanList.last)
             .startActive()
       }
   
       spanMap.put(transactionId.meta.id, activeSpan :: spanList)
     }
   
     private def getContextSpan(logMarker: LogMarkerToken, transactionId: 
TransactionId) = {
       val baseSpan = tracer.buildSpan(logMarker.action)
       contextMap
         .get(transactionId.meta.id)
         .map(baseSpan.asChildOf(_).startActive())
         .getOrElse(baseSpan.ignoreActiveSpan().startActive())
     }
   ```

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