Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/header_rewrite/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Condition : public Statement
bool
do_eval(const Resources &res)
{
// In do_eval, so AND/OR short-circuited conditions aren't counted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good comment.

if (TS_ERROR != hrw_stat_conditions) {
TSStatIntIncrement(hrw_stat_conditions, 1);
}
bool rt = eval(res);

if (has_modifier(_mods, CondModifiers::NOT)) {
Expand Down
4 changes: 4 additions & 0 deletions plugins/header_rewrite/header_rewrite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ TSPluginInit(int argc, const char *argv[])
return;
}

init_hrw_work_stats();

std::string geoDBpath;
int inboundIpSource = 0;
int timezone = 0;
Expand Down Expand Up @@ -663,6 +665,8 @@ TSRemapInit(TSRemapInterface *api_info, char *errbuf, int errbuf_size)
CHECK_REMAP_API_COMPATIBILITY(api_info, errbuf, errbuf_size);
Dbg(pi_dbg_ctl, "Remap plugin is successfully initialized");

init_hrw_work_stats();

return TS_SUCCESS;
}

Expand Down
3 changes: 3 additions & 0 deletions plugins/header_rewrite/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class Operator : public Statement
unsigned
do_exec(const Resources &res) const
{
if (TS_ERROR != hrw_stat_operators) {
TSStatIntIncrement(hrw_stat_operators, 1);
}
unsigned no_reenable{exec(res) ? 0U : 1U};
if (nullptr != _next) {
no_reenable += static_cast<Operator *>(_next)->do_exec(res);
Expand Down
18 changes: 18 additions & 0 deletions plugins/header_rewrite/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
//
#include "statement.h"

int hrw_stat_operators = TS_ERROR;
int hrw_stat_conditions = TS_ERROR;

void
init_hrw_work_stats()
{
// Plugin init is single-threaded, so the create-once guard is safe. header_rewrite.so can
// be loaded both globally and as a remap plugin; both must share one process-wide stat.
if (TS_ERROR == hrw_stat_operators) {
hrw_stat_operators = TSStatCreate("proxy.process.plugin.header_rewrite.operators", TS_RECORDDATATYPE_INT,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our API documentation says that TsStatCreate is deprecated since ATS 10, and that we should use the Metrics.h APIs instead. I think we should heed that if possible.

TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
}
if (TS_ERROR == hrw_stat_conditions) {
hrw_stat_conditions = TSStatCreate("proxy.process.plugin.header_rewrite.conditions", TS_RECORDDATATYPE_INT,
TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
}
}

void
Statement::append(Statement *stmt)
{
Expand Down
7 changes: 7 additions & 0 deletions plugins/header_rewrite/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
#include "parser.h"
#include "lulu.h"

// Counters for the number of header_rewrite operators executed and conditions evaluated; the
// bare hook invocation count cannot tell a small ruleset from a large one. Created once via
// init_hrw_work_stats(); both are TS_ERROR until then, so increments are guarded.
Comment on lines +35 to +37

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is init_hrw_work_stats not serialized with the hook registration?

extern int hrw_stat_operators;
extern int hrw_stat_conditions;
void init_hrw_work_stats();

namespace header_rewrite_ns
{
constexpr int NUM_STATE_FLAGS = 16;
Expand Down
30 changes: 22 additions & 8 deletions tests/gold_tests/autest-site/ats_replay.test.ext
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,28 @@ def ATSReplayTest(obj, replay_file: str):

for check in metric_checks:
metric_name = check['metric']
expected_value = check['value']
check_tr = obj.AddTestRun(f'Verify {metric_name} == {expected_value}')
check_tr.Processes.Default.Command = f'traffic_ctl metric get {metric_name}'
check_tr.Processes.Default.Env = ts.Env
check_tr.Processes.Default.ReturnCode = 0
check_tr.Processes.Default.Streams.All = Testers.ContainsExpression(
f'^{re.escape(metric_name)}\\s+{expected_value}$', f'{metric_name} should be {expected_value}')
check_tr.StillRunningAfter = ts
if 'value' in check:
expected_value = check['value']
check_tr = obj.AddTestRun(f'Verify {metric_name} == {expected_value}')
check_tr.Processes.Default.Command = f'traffic_ctl metric get {metric_name}'
check_tr.Processes.Default.Env = ts.Env
check_tr.Processes.Default.ReturnCode = 0
check_tr.Processes.Default.Streams.All = Testers.ContainsExpression(
f'^{re.escape(metric_name)}\\s+{expected_value}$', f'{metric_name} should be {expected_value}')
check_tr.StillRunningAfter = ts
elif 'min' in check:
minimum = check['min']
check_tr = obj.AddTestRun(f'Verify {metric_name} >= {minimum}')
# Fail if the metric value (field 2) is below the minimum, or absent.
# $$ escapes the awk field ref from autest's command templating.
check_tr.Processes.Default.Command = (
f"traffic_ctl metric get {metric_name} | "
f"awk 'BEGIN {{ rc = 1 }} {{ if ($$2 + 0 >= {minimum}) rc = 0 }} END {{ exit rc }}'")
Comment thread
moonchen marked this conversation as resolved.
check_tr.Processes.Default.Env = ts.Env
check_tr.Processes.Default.ReturnCode = 0
check_tr.StillRunningAfter = ts
else:
raise ValueError(f"metric_checks entry for '{metric_name}' must specify 'value' or 'min'")

# wait for error log
if log_validation and log_validation.get('error_log'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ autest:
args:
- "rules/rule_session_vars.conf"

metric_checks:
- metric: "proxy.process.plugin.header_rewrite.operators"
min: 1
- metric: "proxy.process.plugin.header_rewrite.conditions"
min: 1

log_validation:
traffic_out:
excludes:
Expand Down