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


##########
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:
   Good call on getting these out of the header. One wrinkle: 
`PluginThreadContext` is used by both remap plugins (`PluginDso`, in 
`ts::http_remap`) and global plugins (`GlobalPluginContext`, in `ts::proxy`), 
and the link dependency only runs one way — `ts::http_remap` → `ts::proxy`. If 
the implementations went into `PluginDso.cc` they would land in 
`ts::http_remap`, which would force `ts::proxy` to link *back* against 
`ts::http_remap` to resolve them for the global-plugin path — that is what 
broke `test_proxy` earlier and why they got inlined.
   
   So I have pulled `PluginThreadContext` into its own file in `ts::proxy` 
(`PluginThreadContext.{h,cc}`) with the implementations in the `.cc`. 
`ts::http_remap` already links `ts::proxy`, so both plugin paths resolve the 
symbols and the header stays thin. Does that address your concern?



##########
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:
   Done in becf6595e9 — switched to `std::replace_if`.



##########
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:
   Done in 40426530d0 — reorganized as a `TestPerPluginWorkloadCounters` class.



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