Skip to content

Commit 384ff65

Browse files
[BugFix] Fix the bug of json parse in get fe metrics (backport #68164) (#68176)
Signed-off-by: trueeyu <lxhhust350@qq.com> Co-authored-by: trueeyu <lxhhust350@qq.com>
1 parent b8d6437 commit 384ff65

4 files changed

Lines changed: 104 additions & 11 deletions

File tree

be/src/exec/schema_scanner/schema_fe_metrics_scanner.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,21 @@ Status SchemaFeMetricsScanner::_get_fe_metrics(RuntimeState* state) {
5757

5858
simdjson::ondemand::parser parser;
5959
simdjson::padded_string json_metrics(metrics);
60-
for (auto json_metric : parser.iterate(json_metrics)) {
61-
auto& info = _infos.emplace_back();
62-
info.id = frontend.id;
63-
info.value = static_cast<int64_t>(double(json_metric["value"]));
64-
auto n = std::string_view(json_metric["tags"]["metric"]);
65-
info.name = std::string(n.begin(), n.end());
66-
std::ostringstream oss;
67-
oss << simdjson::to_json_string(json_metric["tags"]);
68-
info.labels = oss.str();
69-
VLOG(2) << "id: " << info.id << "name: " << info.name << ", labels: " << info.labels
70-
<< ", value: " << info.value;
60+
try {
61+
for (auto json_metric : parser.iterate(json_metrics)) {
62+
auto& info = _infos.emplace_back();
63+
info.id = frontend.id;
64+
info.value = static_cast<int64_t>(double(json_metric["value"]));
65+
auto n = std::string_view(json_metric["tags"]["metric"]);
66+
info.name = std::string(n.begin(), n.end());
67+
std::ostringstream oss;
68+
oss << simdjson::to_json_string(json_metric["tags"]);
69+
info.labels = oss.str();
70+
VLOG(2) << "id: " << info.id << "name: " << info.name << ", labels: " << info.labels
71+
<< ", value: " << info.value;
72+
}
73+
} catch (const simdjson::simdjson_error& e) {
74+
return Status::InternalError("Parse the result of fe metrics failed: " + std::string(e.what()));
7175
}
7276
}
7377

be/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ set(EXEC_FILES
7272
./exec/query_cache/query_cache_test.cpp
7373
./exec/query_cache/transform_operator.cpp
7474
./exec/schema_columns_scanner_test.cpp
75+
./exec/schema_scanner/schema_fe_metrics_scanner_test.cpp
7576
./exec/sink/connector_sink_operator_test.cpp
7677
./exec/sink/sink_io_buffer_test.cpp
7778
./exec/stream/mem_state_table_test.cpp
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2021-present StarRocks, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "exec/schema_scanner/schema_fe_metrics_scanner.h"
16+
17+
#include <gtest/gtest.h>
18+
19+
#include "http/ev_http_server.h"
20+
#include "http/http_channel.h"
21+
#include "http/http_client.h"
22+
#include "http/http_handler.h"
23+
#include "http/http_request.h"
24+
#include "runtime/runtime_state.h"
25+
#include "testutil/assert.h"
26+
27+
namespace starrocks {
28+
29+
class SchemaFeMetricsScannerGetHandler : public HttpHandler {
30+
public:
31+
SchemaFeMetricsScannerGetHandler() = default;
32+
void handle(HttpRequest* req) override {
33+
std::string resp = R"([{"tags":{"metric":"jvm_young_gc","type":"count"},"unit":"nounit","value:741}])";
34+
HttpChannel::send_reply(req, resp);
35+
}
36+
};
37+
38+
class SchemaFeMetricsScannerTest : public testing::Test {
39+
public:
40+
SchemaFeMetricsScannerTest() {}
41+
~SchemaFeMetricsScannerTest() override = default;
42+
43+
void SetUp() override {
44+
_server = std::make_unique<EvHttpServer>(0);
45+
_server->register_handler(GET, "/metrics", &_handler);
46+
ASSERT_OK(_server->start());
47+
_port = _server->get_real_port();
48+
ASSERT_NE(0, _port);
49+
_hostname = "http://127.0.0.1:" + std::to_string(_port);
50+
}
51+
52+
void TearDown() override {
53+
_server->stop();
54+
_server->join();
55+
}
56+
57+
protected:
58+
SchemaFeMetricsScannerGetHandler _handler;
59+
std::unique_ptr<EvHttpServer> _server = nullptr;
60+
int _port = 0;
61+
std::string _hostname = "";
62+
ObjectPool _pool;
63+
};
64+
65+
TEST_F(SchemaFeMetricsScannerTest, test_partial_json) {
66+
RuntimeState state;
67+
TFrontend frontend;
68+
frontend.__set_ip("127.0.0.1");
69+
frontend.__set_id("1");
70+
frontend.__set_http_port(_port);
71+
72+
SchemaScannerParam param;
73+
param.frontends.push_back(std::move(frontend));
74+
75+
SchemaFeMetricsScanner scanner;
76+
ASSERT_OK(scanner.init(&param, &_pool));
77+
auto st = scanner.start(&state);
78+
ASSERT_ERROR(st);
79+
ASSERT_TRUE(st.message().find("A string is opened") != std::string::npos);
80+
}
81+
} // namespace starrocks

be/test/http/http_client_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "http/http_channel.h"
2727
#include "http/http_handler.h"
2828
#include "http/http_request.h"
29+
#include "testutil/assert.h"
2930

3031
namespace starrocks {
3132

@@ -85,7 +86,13 @@ class HttpClientTest : public testing::Test {
8586
s_server->register_handler(GET, "/simple_get", &s_simple_get_handler);
8687
s_server->register_handler(HEAD, "/simple_get", &s_simple_get_handler);
8788
s_server->register_handler(POST, "/simple_post", &s_simple_post_handler);
89+
<<<<<<< HEAD
8890
s_server->start();
91+
=======
92+
s_server->register_handler(GET, "/header_test", &s_header_handler);
93+
s_server->register_handler(GET, "/multi_header_test", &s_multi_header_handler);
94+
ASSERT_OK(s_server->start());
95+
>>>>>>> d44be25275 ([BugFix] Fix the bug of json parse in get fe metrics (#68164))
8996
real_port = s_server->get_real_port();
9097
ASSERT_NE(0, real_port);
9198
hostname = "http://127.0.0.1:" + std::to_string(real_port);

0 commit comments

Comments
 (0)