This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new f296667  Open Census support refactoring.
f296667 is described below

commit f29666728879d384a9c527c09aa68f52dbf14342
Author: unknown <[email protected]>
AuthorDate: Fri Jun 11 17:57:01 2021 -0700

    Open Census support refactoring.
---
 .../common/opencensus/NCOpenCensusStats.scala      | 74 ++++++++++++++++++++++
 .../opencensus/stats/NCOpenCensusModelStats.scala  | 54 ++--------------
 .../opencensus/NCOpenCensusServerStats.scala       | 55 ++--------------
 3 files changed, 82 insertions(+), 101 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/opencensus/NCOpenCensusStats.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/opencensus/NCOpenCensusStats.scala
new file mode 100644
index 0000000..af3d484
--- /dev/null
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/opencensus/NCOpenCensusStats.scala
@@ -0,0 +1,74 @@
+/*
+ * 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
+ *
+ *      https://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.nlpcraft.common.opencensus
+
+import io.opencensus.stats.Aggregation.{Count, Distribution}
+import io.opencensus.stats.Measure
+import io.opencensus.stats.Measure._
+import io.opencensus.stats.View.Name
+import io.opencensus.stats._
+
+import java.util
+import java.util.Collections
+
+/**
+ * Base trait for Open Census stats implementation.
+ */
+trait NCOpenCensusStats {
+    private val LAT_DIST = 
Distribution.create(BucketBoundaries.create(util.Arrays.asList(
+        0.0, 25.0, 100.0, 200.0, 400.0, 800.0, 10000.0
+    )))
+
+    /**
+     * Records OpenCensus metrics.
+     *
+     * @param pairs Pairs of Open Census measure and its value. Values must be 
`Long` or `Double` only.
+     */
+    def recordStats(pairs: (Measure, AnyVal)*): Unit = {
+        val map = Stats.getStatsRecorder.newMeasureMap()
+
+        for ((m, v) <- pairs) {
+            m match {
+                case d: MeasureDouble => map.put(d, v.asInstanceOf[Double])
+                case l: MeasureLong => map.put(l, v.asInstanceOf[Long])
+                case _ => throw new AssertionError()
+            }
+        }
+
+        map.record()
+    }
+
+    def mkViews(m: Measure, call: String): List[View] = {
+        List(
+            View.create(
+                Name.create(s"$call/latdist"),
+                s"The distribution of the '$call' call latencies",
+                m,
+                LAT_DIST,
+                Collections.emptyList()
+            ),
+            View.create(
+                Name.create(s"$call/count"),
+                s"The number of the '$call' invocations",
+                m,
+                Count.create,
+                Collections.emptyList()
+            )
+        )
+    }
+}
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/opencensus/stats/NCOpenCensusModelStats.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/opencensus/stats/NCOpenCensusModelStats.scala
index 56b0ece..8114f74 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/opencensus/stats/NCOpenCensusModelStats.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/opencensus/stats/NCOpenCensusModelStats.scala
@@ -17,71 +17,25 @@
 
 package org.apache.nlpcraft.model.opencensus.stats
 
-import java.util
-import java.util.Collections
-
-import io.opencensus.stats.Aggregation.{Count, Distribution}
-import io.opencensus.stats.Measure.{MeasureDouble, MeasureLong}
-import io.opencensus.stats.View.Name
+import io.opencensus.stats.Measure._
 import io.opencensus.stats._
+import org.apache.nlpcraft.common.opencensus.NCOpenCensusStats
 
 /**
   * OpenCensus stats instrumentation.
   */
-trait NCOpenCensusModelStats {
+trait NCOpenCensusModelStats extends NCOpenCensusStats {
     val M_SYS_LATENCY_MS: MeasureLong =
         MeasureLong.create("sys_latency", "The latency of system tasks", "ms")
     val M_USER_LATENCY_MS: MeasureLong =
         MeasureLong.create("user_latency", "The latency of user tasks", "ms")
-    
-    /**
-      * Records OpenCensus metrics.
-      *
-      * @param pairs Pairs of OC measure and its value. Values must be `Long` 
or `Double` only.
-      */
-    def recordStats(pairs: (Measure, AnyVal)*): Unit = {
-        val map = Stats.getStatsRecorder.newMeasureMap()
-        
-        for ((m, v) <- pairs) {
-            m match {
-                case d: MeasureDouble => map.put(d, v.asInstanceOf[Double])
-                case l: MeasureLong => map.put(l, v.asInstanceOf[Long])
-                case _ => throw new AssertionError()
-            }
-        }
-        
-        map.record()
-    }
-    
+
     init()
     
     /**
       *
       */
     private def init(): Unit = {
-        val restLatDist = 
Distribution.create(BucketBoundaries.create(util.Arrays.asList(
-            0.0, 25.0, 100.0, 200.0, 400.0, 800.0, 10000.0
-        )))
-        
-        def mkViews(m: Measure, name: String): List[View] = {
-            List(
-                View.create(
-                    Name.create(s"$name/latdist"),
-                    s"The distribution of the '$name' latency",
-                    m,
-                    restLatDist,
-                    Collections.emptyList()
-                ),
-                View.create(
-                    Name.create(s"$name/count"),
-                    s"The number of the '$name' invocations",
-                    m,
-                    Count.create,
-                    Collections.emptyList()
-                )
-            )
-        }
-        
         val views = List(
             mkViews(M_SYS_LATENCY_MS, "sys_task"),
             mkViews(M_USER_LATENCY_MS, "user_task")
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala
index 6528811..2667254 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/opencensus/NCOpenCensusServerStats.scala
@@ -17,18 +17,16 @@
 
 package org.apache.nlpcraft.server.opencensus
 
-import java.util
 import java.util.Collections
-
-import io.opencensus.stats.Aggregation.Distribution
-import io.opencensus.stats.Measure.{MeasureDouble, MeasureLong}
+import io.opencensus.stats.Measure._
 import io.opencensus.stats.View.Name
 import io.opencensus.stats._
+import org.apache.nlpcraft.common.opencensus.NCOpenCensusStats
 
 /**
   * OpenCensus stats instrumentation.
   */
-trait NCOpenCensusServerStats {
+trait NCOpenCensusServerStats extends NCOpenCensusStats {
     val M_HEALTH_MS: MeasureLong = MeasureLong.create("health_latency", "The 
latency of '/health' REST call", "ms")
     val M_ASK_LATENCY_MS: MeasureLong = MeasureLong.create("ask_latency", "The 
latency of '/ask' REST call", "ms")
     val M_CHECK_LATENCY_MS: MeasureLong = MeasureLong.create("check_latency", 
"The latency of '/check' REST call", "ms")
@@ -57,25 +55,6 @@ trait NCOpenCensusServerStats {
     val M_PROBE_ALL_LATENCY_MS: MeasureLong = 
MeasureLong.create("probe_all_latency", "The latency of '/probe/all' REST 
call", "ms")
     
     val M_ROUND_TRIP_LATENCY_MS: MeasureLong = 
MeasureLong.create("round_trip_latency", "The latency of a full server<->probe 
round trip", "ms")
-    
-    /**
-      * Records OpenCensus metrics.
-      *
-      * @param pairs Pairs of OC measure and its value. Values must be `Long` 
or `Double` only.
-      */
-    def recordStats(pairs: (Measure, AnyVal)*): Unit = {
-        val map = Stats.getStatsRecorder.newMeasureMap()
-        
-        for ((m, v) <- pairs) {
-            m match {
-                case d: MeasureDouble => map.put(d, v.asInstanceOf[Double])
-                case l: MeasureLong => map.put(l, v.asInstanceOf[Long])
-                case _ => throw new AssertionError()
-            }
-        }
-        
-        map.record()
-    }
 
     init()
     
@@ -83,22 +62,6 @@ trait NCOpenCensusServerStats {
       *
       */
     private def init(): Unit = {
-        val restLatDist = 
Distribution.create(BucketBoundaries.create(util.Arrays.asList(
-                0.0, 25.0, 100.0, 200.0, 400.0, 800.0, 10000.0
-        )))
-        
-        def mkViews(m: Measure, rest: String): List[View] = {
-            List(
-                View.create(
-                    Name.create(s"$rest/latdist"),
-                    s"The distribution of the '$rest' REST call latencies",
-                    m,
-                    restLatDist,
-                    Collections.emptyList()
-                )
-            )
-        }
-        
         val views = List(
             mkViews(M_HEALTH_MS, "health"),
             mkViews(M_ASK_LATENCY_MS, "ask"),
@@ -127,17 +90,7 @@ trait NCOpenCensusServerStats {
             mkViews(M_FEEDBACK_GET_LATENCY_MS, "feedback/get"),
             mkViews(M_MODEL_SUGSYN_LATENCY_MS, "model/sugsyn"),
             mkViews(M_PROBE_ALL_LATENCY_MS, "probe/all"),
-            
-            // Special views for round trip metrics. 
-            List(
-                View.create(
-                    Name.create("roundTrip/latdist"),
-                    s"The distribution of the full server-probe-server round 
trip latencies",
-                    M_ROUND_TRIP_LATENCY_MS,
-                    restLatDist,
-                    Collections.emptyList()
-                )
-            )
+            mkViews(M_ROUND_TRIP_LATENCY_MS, "roundTrip/latdist"),
         ).flatten
         
         val viewMgr = Stats.getViewManager

Reply via email to