bneradt commented on code in PR #13278:
URL: https://github.com/apache/trafficserver/pull/13278#discussion_r3447034624


##########
include/proxy/http/remap/PluginDso.h:
##########
@@ -46,16 +46,72 @@
 namespace fs = swoc::file;
 
 #include "tscore/Ptr.h"
+#include "tsutil/Metrics.h"
 #include "iocore/eventsystem/EventSystem.h"
 
 #include "proxy/Plugin.h"
 
+#include <cctype>
+#include <string>
+#include <string_view>
+
 class PluginThreadContext : public RefCountObjInHeap
 {
 public:
-  virtual void                       acquire() = 0;
-  virtual void                       release() = 0;
-  static constexpr const char *const _tag      = "plugin_context"; /** @brief 
log tag used by this class */
+  virtual void acquire() = 0;
+  virtual void release() = 0;
+
+  /** @brief Register this plugin's proxy.process.plugin.<name>.* metrics. @a 
plugin_name is the DSO
+   *  path; only its basename stem is used as <name>. Defined inline so 
callers in ts::proxy (the
+   *  global-plugin context) take no link dependency on ts::http_remap. */
+  void
+  registerPluginMetrics(std::string_view plugin_name)
+  {
+    std::string prefix = "proxy.process.plugin." + _metric_token(plugin_name) 
+ ".";
+
+    _invocations = ts::Metrics::Counter::createPtr(prefix + "invocations");
+    _bytes       = ts::Metrics::Counter::createPtr(prefix + "bytes");
+    _transfers   = ts::Metrics::Counter::createPtr(prefix + "transfers");
+  }

Review Comment:
   Can we put the implementation of this and these other functions in 
PluginDso.cc?



##########
tests/gold_tests/pluginTest/per_plugin_metrics/per_plugin_metrics.test.py:
##########
@@ -0,0 +1,100 @@
+'''
+Verify the per-plugin workload counters 
proxy.process.plugin.<name>.{invocations,bytes,transfers}.
+'''
+#  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.
+
+Test.Summary = '''
+Verify the per-plugin workload counters 
proxy.process.plugin.<name>.invocations (global and remap
+dispatch), .bytes and .transfers (PluginVC intercept transport).
+'''
+
+Test.ContinueOnFail = True
+
+ts = Test.MakeATSProcess("ts")
+server = Test.MakeOriginServer("server")
+
+request_header = {"headers": "GET / HTTP/1.1\r\nHost: test.example\r\n\r\n", 
"timestamp": "1469733493.993", "body": ""}

Review Comment:
   Can you please ask your clanker to reorganize this as a test class?
   
   ```python
   class TestPerPluginWorkloadCounters:
      def __init__(self, params):
         # Create a TestRun and setup up the needed servers and configure the 
client
   
   TestPerPluginWorkloadCounters(params1)
   TestPerPluginWorkloadCounters(params2)
   ```



##########
include/proxy/http/remap/PluginDso.h:
##########
@@ -46,16 +46,72 @@
 namespace fs = swoc::file;
 
 #include "tscore/Ptr.h"
+#include "tsutil/Metrics.h"
 #include "iocore/eventsystem/EventSystem.h"
 
 #include "proxy/Plugin.h"
 
+#include <cctype>
+#include <string>
+#include <string_view>
+
 class PluginThreadContext : public RefCountObjInHeap
 {
 public:
-  virtual void                       acquire() = 0;
-  virtual void                       release() = 0;
-  static constexpr const char *const _tag      = "plugin_context"; /** @brief 
log tag used by this class */
+  virtual void acquire() = 0;
+  virtual void release() = 0;
+
+  /** @brief Register this plugin's proxy.process.plugin.<name>.* metrics. @a 
plugin_name is the DSO
+   *  path; only its basename stem is used as <name>. Defined inline so 
callers in ts::proxy (the
+   *  global-plugin context) take no link dependency on ts::http_remap. */
+  void
+  registerPluginMetrics(std::string_view plugin_name)
+  {
+    std::string prefix = "proxy.process.plugin." + _metric_token(plugin_name) 
+ ".";
+
+    _invocations = ts::Metrics::Counter::createPtr(prefix + "invocations");
+    _bytes       = ts::Metrics::Counter::createPtr(prefix + "bytes");
+    _transfers   = ts::Metrics::Counter::createPtr(prefix + "transfers");
+  }
+
+  void
+  countInvocation()
+  {
+    if (_invocations != nullptr) {
+      _invocations->increment(1);
+    }
+  }
+
+  ts::Metrics::Counter::AtomicType *_invocations = nullptr;
+  ts::Metrics::Counter::AtomicType *_bytes       = nullptr;
+  ts::Metrics::Counter::AtomicType *_transfers   = nullptr;
+
+  static constexpr const char *const _tag = "plugin_context"; /** @brief log 
tag used by this class */
+
+private:
+  /** Derive a metric-safe token from a plugin path: the basename with the 
extension removed, then any
+   *  character outside [A-Za-z0-9_-] replaced by '_' (e.g. 
"/.../header_rewrite.so" -> "header_rewrite"). */
+  static std::string
+  _metric_token(std::string_view name)
+  {
+    if (auto slash = name.find_last_of('/'); slash != std::string_view::npos) {
+      name.remove_prefix(slash + 1);
+    }
+    if (auto dot = name.find_last_of('.'); dot != std::string_view::npos) {
+      name = name.substr(0, dot);
+    }
+
+    std::string token{name};
+    for (auto &c : token) {
+      if (!(std::isalnum(static_cast<unsigned char>(c)) || c == '_' || c == 
'-')) {
+        c = '_';
+      }
+    }

Review Comment:
   `replace_if` is made for this and by virtue of its name will communicate 
intent better:
   
   ```cpp
   std::string token{name};
   std::replace_if(token.begin(), token.end(),
                   [](unsigned char c) {
                       return !(std::isalnum(c) || c == '_' || c == '-');
                   },
                   '_');
   ```



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