Skip to content

Commit e7e30bb

Browse files
authored
Make sure string metrics are dumped in RecDumpRecords (#13074)
1 parent 9089a5c commit e7e30bb

3 files changed

Lines changed: 96 additions & 2 deletions

File tree

src/records/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ target_link_libraries(
4545

4646
if(BUILD_TESTING)
4747
add_executable(
48-
test_records unit_tests/unit_test_main.cc unit_tests/test_RecHttp.cc unit_tests/test_RecUtils.cc
49-
unit_tests/test_RecRegister.cc unit_tests/test_ConfigReloadTask.cc unit_tests/test_ConfigRegistry.cc
48+
test_records
49+
unit_tests/unit_test_main.cc
50+
unit_tests/test_RecHttp.cc
51+
unit_tests/test_RecUtils.cc
52+
unit_tests/test_RecRegister.cc
53+
unit_tests/test_ConfigReloadTask.cc
54+
unit_tests/test_ConfigRegistry.cc
55+
unit_tests/test_RecDumpRecords.cc
5056
)
5157
target_link_libraries(test_records PRIVATE records configmanager inkevent Catch2::Catch2 ts::tscore libswoc::libswoc)
5258
add_catch2_test(NAME test_records COMMAND test_records)

src/records/RecCore.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "records/RecDefs.h"
2929
#include "swoc/swoc_file.h"
3030

31+
#include "ts/apidefs.h"
3132
#include "tscore/ink_platform.h"
3233
#include "tscore/ink_memory.h"
3334
#include "tscore/ink_string.h"
@@ -940,6 +941,12 @@ RecDumpRecords(RecT rec_type, RecDumpEntryCb callback, void *edata)
940941
callback(RECT_PLUGIN, edata, true, name.data(),
941942
type == Metrics::MetricType::COUNTER ? TS_RECORDDATATYPE_COUNTER : TS_RECORDDATATYPE_INT, &datum);
942943
}
944+
945+
ts::Metrics::StaticString::instance().for_each([&](const std::string &name, const std::string &value) {
946+
datum.rec_string = const_cast<char *>(value.c_str());
947+
948+
callback(RECT_PLUGIN, edata, true, name.data(), TS_RECORDDATATYPE_STRING, &datum);
949+
});
943950
}
944951

945952
void
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/** @file
2+
3+
Catch-based tests for RecDumpRecords
4+
5+
@section license License
6+
7+
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
8+
See the NOTICE file distributed with this work for additional information regarding copyright
9+
ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
"License"); you may not use this file except in compliance with the License. You may obtain a
11+
copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software distributed under the License
16+
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17+
or implied. See the License for the specific language governing permissions and limitations under
18+
the License.
19+
*/
20+
#include <catch2/catch_test_macros.hpp>
21+
#include <string>
22+
#include <vector>
23+
24+
#include "../P_RecCore.h"
25+
#include "tsutil/Metrics.h"
26+
27+
struct DumpEntry {
28+
RecT rec_type;
29+
int registered;
30+
std::string name;
31+
int data_type;
32+
std::string string_value;
33+
RecInt int_value{0};
34+
};
35+
36+
static void
37+
collect_callback(RecT rec_type, void *edata, int registered, const char *name, int data_type, RecData *datum)
38+
{
39+
auto *entries = static_cast<std::vector<DumpEntry> *>(edata);
40+
DumpEntry entry;
41+
42+
entry.rec_type = rec_type;
43+
entry.registered = registered;
44+
entry.name = name;
45+
entry.data_type = data_type;
46+
47+
if (data_type == RECD_STRING && datum->rec_string != nullptr) {
48+
entry.string_value = datum->rec_string;
49+
} else if (data_type == RECD_INT || data_type == RECD_COUNTER) {
50+
entry.int_value = datum->rec_int;
51+
}
52+
53+
entries->push_back(std::move(entry));
54+
}
55+
56+
TEST_CASE("RecDumpRecords - StaticString metrics", "[librecords][RecDump]")
57+
{
58+
const std::string test_name = "proxy.test.dump.string_metric";
59+
const std::string test_value = "test_string_value";
60+
61+
ts::Metrics::StaticString::createString(test_name, test_value);
62+
63+
std::vector<DumpEntry> entries;
64+
65+
RecDumpRecords(RECT_NULL, collect_callback, &entries);
66+
67+
bool found = false;
68+
69+
for (const auto &entry : entries) {
70+
if (entry.name == test_name) {
71+
found = true;
72+
CHECK(entry.rec_type == RECT_PLUGIN);
73+
CHECK(entry.registered == 1);
74+
CHECK(entry.data_type == RECD_STRING);
75+
CHECK(entry.string_value == test_value);
76+
break;
77+
}
78+
}
79+
80+
REQUIRE(found);
81+
}

0 commit comments

Comments
 (0)