TS-2316: header_rewrite: added optional counter to each rule

Now you can count each rule's tripping by providing one or more "counter"
statements e.g::

    cond %{SEND_RESPONSE_HDR_HOOK} [AND]
    add-header X-DC "DC1"
    counter plugins.header_rewrite.POP
    counter plugins.header_rewrite.ALL

Submitted by: Alexey Ivanov <aiva...@linkedin.com>
Sponsored by: LinkedIn


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/b79c9074
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/b79c9074
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/b79c9074

Branch: refs/heads/5.0.x
Commit: b79c9074455e4634d2a809a71e6cc6122567f610
Parents: 8e2cc15
Author: Alexey Ivanov <aiva...@linkedin.com>
Authored: Tue Nov 5 11:14:06 2013 -0800
Committer: Alexey Ivanov <aiva...@linkedin.com>
Committed: Sun Nov 10 18:07:53 2013 -0800

----------------------------------------------------------------------
 plugins/header_rewrite/factory.cc   |  2 ++
 plugins/header_rewrite/operators.cc | 37 ++++++++++++++++++++++++++++++++
 plugins/header_rewrite/operators.h  | 19 ++++++++++++++++
 3 files changed, 58 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b79c9074/plugins/header_rewrite/factory.cc
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/factory.cc 
b/plugins/header_rewrite/factory.cc
index 7810c8c..9af8f25 100644
--- a/plugins/header_rewrite/factory.cc
+++ b/plugins/header_rewrite/factory.cc
@@ -53,6 +53,8 @@ operator_factory(const std::string& op)
     o = new OperatorSetTimeoutOut();
   } else if (op == "no-op") {
     o = new OperatorNoOp();
+  } else if (op == "counter") {
+    o = new OperatorCounter();
   } else {
     TSError("%s: unknown operator: %s", PLUGIN_NAME, op.c_str());
     return NULL;

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b79c9074/plugins/header_rewrite/operators.cc
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/operators.cc 
b/plugins/header_rewrite/operators.cc
index 881e058..f06b1d0 100644
--- a/plugins/header_rewrite/operators.cc
+++ b/plugins/header_rewrite/operators.cc
@@ -466,3 +466,40 @@ OperatorSetHeader::exec(const Resources& res) const
     }
   }
 }
+
+// OperatorCounter
+void
+OperatorCounter::initialize(Parser& p) {
+  Operator::initialize(p);
+
+  _counter_name = p.get_arg();
+
+  // Sanity
+  if (_counter_name.length() == 0) {
+    TSError("%s: counter name is empty", PLUGIN_NAME);
+    return;
+  }
+
+  // Check if counter already created by another rule
+  if (TSStatFindName(_counter_name.c_str(), &_counter) == TS_ERROR) {
+    _counter = TSStatCreate(_counter_name.c_str(), TS_RECORDDATATYPE_INT, 
TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
+    if (_counter == TS_ERROR) {
+      TSError("%s: TSStatCreate() failed. Can't create counter: %s", 
PLUGIN_NAME, _counter_name.c_str());
+      return;
+    }
+    TSDebug(PLUGIN_NAME, "OperatorCounter::initialize(%s) created counter with 
id: %d", _counter_name.c_str(), _counter);
+  } else {
+    TSDebug(PLUGIN_NAME, "OperatorCounter::initialize(%s) reusing id: %d", 
_counter_name.c_str(), _counter);
+  }
+}
+
+void
+OperatorCounter::exec(const Resources& res) const
+{
+  // Sanity
+  if (_counter == TS_ERROR)
+    return;
+
+  TSDebug(PLUGIN_NAME, "OperatorCounter::exec() invoked on counter %s", 
_counter_name.c_str());
+  TSStatIntIncrement(_counter, 1);
+}

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/b79c9074/plugins/header_rewrite/operators.h
----------------------------------------------------------------------
diff --git a/plugins/header_rewrite/operators.h 
b/plugins/header_rewrite/operators.h
index 65bbcb4..6ab67ff 100644
--- a/plugins/header_rewrite/operators.h
+++ b/plugins/header_rewrite/operators.h
@@ -240,5 +240,24 @@ private:
   Value _value;
 };
 
+class OperatorCounter : public Operator
+{
+public:
+  OperatorCounter()
+    : _counter_name(""), _counter(TS_ERROR)
+  {
+    TSDebug(PLUGIN_NAME_DBG, "Calling CTOR for OperatorCounter");
+  }
+  void initialize(Parser& p);
+
+protected:
+  void exec(const Resources& res) const;
+
+private:
+  DISALLOW_COPY_AND_ASSIGN(OperatorCounter);
+
+  std::string _counter_name;
+  int _counter;
+};
 
 #endif // __OPERATORS_H

Reply via email to