Skip to content

Commit f3fb74e

Browse files
authored
regex_revalidate: add stats for miss/stale counts (#7950)
1 parent c75c797 commit f3fb74e

File tree

8 files changed

+135
-3
lines changed

8 files changed

+135
-3
lines changed

doc/admin-guide/plugins/regex_revalidate.en.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ regular expression against your origin URLs permits. Thus, individual cache
4040
objects may have rules created for them, or entire path prefixes, or even any
4141
cache objects with a particular file extension.
4242

43+
Revalidate count stats for MISS and STALE are recorded under plugins
44+
4345
Installation
4446
============
4547

plugins/regex_revalidate/regex_revalidate.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,50 @@ static char const *const RESULT_MISS = "MISS";
5151
static char const *const RESULT_STALE = "STALE";
5252
static char const *const RESULT_UNKNOWN = "UNKNOWN";
5353

54+
static int stat_id_stale = TS_ERROR;
55+
static char const *const stat_name_stale = "plugin.regex_revalidate.stale";
56+
static int stat_id_miss = TS_ERROR;
57+
static char const *const stat_name_miss = "plugin.regex_revalidate.miss";
58+
59+
static void
60+
create_stats()
61+
{
62+
if (TS_ERROR == stat_id_stale && TS_ERROR == TSStatFindName(stat_name_stale, &stat_id_stale)) {
63+
stat_id_stale = TSStatCreate(stat_name_stale, TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
64+
if (TS_ERROR != stat_id_stale) {
65+
TSDebug(PLUGIN_NAME, "Created stat '%s'", stat_name_stale);
66+
}
67+
}
68+
69+
if (TS_ERROR == stat_id_miss && TS_ERROR == TSStatFindName(stat_name_miss, &stat_id_miss)) {
70+
stat_id_miss = TSStatCreate(stat_name_miss, TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_COUNT);
71+
if (TS_ERROR != stat_id_miss) {
72+
TSDebug(PLUGIN_NAME, "Created stat '%s'", stat_name_miss);
73+
}
74+
}
75+
}
76+
77+
static void
78+
increment_stat(TSCacheLookupResult const result)
79+
{
80+
switch (result) {
81+
case TS_CACHE_LOOKUP_MISS:
82+
if (TS_ERROR != stat_id_miss) {
83+
TSStatIntIncrement(stat_id_miss, 1);
84+
TSDebug(PLUGIN_NAME, "Incrementing stat '%s'", stat_name_miss);
85+
}
86+
break;
87+
case TS_CACHE_LOOKUP_HIT_STALE:
88+
if (TS_ERROR != stat_id_stale) {
89+
TSStatIntIncrement(stat_id_stale, 1);
90+
TSDebug(PLUGIN_NAME, "Incrementing stat '%s'", stat_name_stale);
91+
}
92+
break;
93+
default:
94+
break;
95+
}
96+
}
97+
5498
static char const *const
5599
strForResult(TSCacheLookupResult const result)
56100
{
@@ -585,6 +629,7 @@ main_handler(TSCont cont, TSEvent event, void *edata)
585629
}
586630
if (pcre_exec(iptr->regex, iptr->regex_extra, url, url_len, 0, 0, NULL, 0) >= 0) {
587631
TSHttpTxnCacheLookupStatusSet(txn, iptr->new_result);
632+
increment_stat(iptr->new_result);
588633
TSDebug(PLUGIN_NAME, "Forced revalidate - %.*s %s", url_len, url, strForResult(iptr->new_result));
589634
iptr = NULL;
590635
}
@@ -700,6 +745,8 @@ TSPluginInit(int argc, const char *argv[])
700745
TSDebug(PLUGIN_NAME, "Plugin registration succeeded");
701746
}
702747

748+
create_stats();
749+
703750
main_cont = TSContCreate(main_handler, NULL);
704751
TSContDataSet(main_cont, (void *)pstate);
705752
TSHttpHookAdd(TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, main_cont);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
plugin.regex_revalidate.stale 3
2+
plugin.regex_revalidate.miss 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
plugin.regex_revalidate.stale 1
2+
plugin.regex_revalidate.miss 2
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
N=60
18+
while (( N > 0 ))
19+
do
20+
rm -f metrics.out
21+
traffic_ctl metric match regex_revalidate > metrics.out
22+
sleep 1
23+
if diff metrics.out ${AUTEST_TEST_DIR}/gold/metrics.gold
24+
then
25+
exit 0
26+
fi
27+
let N=N-1
28+
done
29+
echo TIMEOUT
30+
exit 1
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
N=60
18+
while (( N > 0 ))
19+
do
20+
rm -f metrics.out
21+
traffic_ctl metric match regex_revalidate > metrics.out
22+
sleep 1
23+
if diff metrics.out ${AUTEST_TEST_DIR}/gold/metrics_miss.gold
24+
then
25+
exit 0
26+
fi
27+
let N=N-1
28+
done
29+
echo TIMEOUT
30+
exit 1

tests/gold_tests/pluginTest/regex_revalidate/regex_revalidate.test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
# Define ATS and configure
4545
ts = Test.MakeATSProcess("ts", command="traffic_manager", select_ports=True)
4646

47+
Test.testName = "regex_revalidate"
48+
Test.Setup.Copy("metrics.sh")
49+
4750
# default root
4851
request_header_0 = {"headers":
4952
"GET / HTTP/1.1\r\n" +
@@ -264,3 +267,11 @@
264267
tr.Processes.Default.ReturnCode = 0
265268
tr.Processes.Default.Streams.stdout = "gold/regex_reval-stale.gold"
266269
tr.StillRunningAfter = ts
270+
271+
# 12 Stats check
272+
tr = Test.AddTestRun("Check stats")
273+
tr.DelayStart = 5
274+
tr.Processes.Default.Command = "bash -c ./metrics.sh"
275+
tr.Processes.Default.Env = ts.Env
276+
tr.Processes.Default.ReturnCode = 0
277+
tr.StillRunningAfter = ts

tests/gold_tests/pluginTest/regex_revalidate/regex_revalidate_miss.test.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
# Define ATS and configure
3939
ts = Test.MakeATSProcess("ts", command="traffic_manager")
4040

41-
# **testname is required**
42-
#testName = "regex_reval"
41+
Test.testName = "regex_revalidate_miss"
42+
Test.Setup.Copy("metrics_miss.sh")
4343

4444
# default root
4545
request_header_0 = {"headers":
@@ -220,11 +220,19 @@
220220
ps.TimeOut = 5
221221
tr.TimeOut = 5
222222

223-
# 8 Test - Cache stale
223+
# 10 Test - Cache stale
224224
tr = Test.AddTestRun("Cache stale path1")
225225
ps = tr.Processes.Default
226226
tr.DelayStart = 5
227227
ps.Command = curl_and_args + ' http://ats/path1'
228228
ps.ReturnCode = 0
229229
ps.Streams.stdout.Content = Testers.ContainsExpression("X-Cache: hit-fresh", "expected cache hit response")
230230
tr.StillRunningAfter = ts
231+
232+
# 11 Stats check
233+
tr = Test.AddTestRun("Check stats")
234+
tr.DelayStart = 5
235+
tr.Processes.Default.Command = "bash -c ./metrics_miss.sh"
236+
tr.Processes.Default.Env = ts.Env
237+
tr.Processes.Default.ReturnCode = 0
238+
tr.StillRunningAfter = ts

0 commit comments

Comments
 (0)